What is the power of eval()? [closed] - eval

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Lately I came across the eval() function in a couple of places (some D and JavaScript code). This experience make me realize that I didn't understand the difference between a language with or without eval().
In fact, I have been able to survive without using it (although sometimes is tempting :)).
Do you know from any fundamental difference between a language implementing eval() and a language that does not implement it (even if it is at compile time)? If there is no fundamental difference is there an expressiveness difference?
Do you know any paper or reference explaining what is the added computational power/expressiveness provided by eval()? This is, What can we implement with eval() that we can not implement without it (or at a higher cost)?

There is no difference in capability between a language with eval and a language without. You can always implement eval yourself in a language that doesn't have it, although it is easier in some languages (Lisp) and harder in other languages (C). This is why we know it doesn't add any new capabilities to a language.
Eval is generally considered "powerful but dangerous and unnecessary in production code". If you ever get your source code reviewed, and it uses eval, you will have to get used to people telling you not to use eval. Usually, if you use eval, there is a more straightforward and safer way to accomplish the same task.
Security vulnerabilities
The eval function often leads to security vulnerabilities, such as in web applications. For example, in Python:
i = input('Enter a number> ')
The input function in Python prior to 3.x evaluates the user input, leading to security problems:
Enter a number> __import__('os').system('rm -rf $HOME') # DO NOT TRY THIS
This can be avoided in fully sandboxed runtimes, such as JavaScript, Lua, CLI, etc. However, in code that is not sandboxed, it is just too difficult to use eval safely.
REPL
The one glorious use of eval is the REPL, which is a useful tool for software development. In Google Chrome, you can bring a REPL up right now by pressing Ctrl+Shift+I -- just start typing JavaScript in the box that comes up. Firefox and Safari both have REPLs too.
Why not include eval?
The eval function is usually incredibly complex. Including it in the standard library means that the library or runtime has to include a complete compiler or interpreter for the language, which is a hefty chunk of code. That's why you usually see eval in languages that are typically interpreted or JITed -- Lisp, Python, and JavaScript runtimes typically already include a complete compiler, so eval is no extra baggage.
Implementations of eval for C are real beasts, so they're optional and shoved into libraries.

Related

What programming languages, without a runtime, besides C, are good for writing programming languages? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm looking into a hobby of writing a toy programming language, partly from minor annoyances with other languages, partly so that I can understand what it's like, but mostly just to fool around.
On the off chance that it gets really useful, I don't want it to depend on the run-time of another programming language for programs written in it to run. That is, I want the interpreter / compiler to itself be a program compiled natively into the target OS (language itself may be interpreted / provide a run-time).
Is there any alternative to doing this besides C? What are some advantages / disadvantages or using each?
Clarification 1: I am not intending to go low-level enough to write kernels, filesystems, device drivers, boot loaders. However I would like to be able to manage my own memory.
Clarification 2: Due to a terminology error / misunderstanding, and since I was so used to the C runtime running on various OS's, I said that C does not have a runtime / and or I am not interested in a runtime. A better way to say what I really want is that my programs compile natively into the target (desktop) OS without needing to install additional software from the bootstrapping language.
2.1: if I write the compiler/interpreter in python, I don't want the emitted executables to depend on the python program.
2.2: if I use a compiling step, for instance, to compile the programs using perl, I don't want the emitted executables to depend on a libperl.dll/so.
2.3: the exception is with runtimes is C since the C runtime is usually installed on almost all desktop OS's as many core OS tools depend on it.
You could use any language that has an existing compiler that emits native code without dependencies. C and C++ are pretty good bets because their runtimes are available pretty much everywhere (even more so in C).
One approach in your language build-up that could be well worth trying is this: make your compiler output C (or C++). Then you can use all the existing ecosystem around those languages and their runtimes (linkers, object dumpers, debuggers, etc...), even plan integration with existing code.
Those tools would be useful both for the users of your language, and obviously for yourself while you're experimenting on that toy language.
Once you get to the point where your language is "self-hosted" (i.e. your compiler is written in your own language), you'll be able to start thinking about doing away with the whole C part and write a native code compiler, with its runtime.
Good luck :-)
Also make sure you go look at LLVM. It's a "compiler infrastructure". That is probably the best place to start with these days to implement a new language. The documentation is pretty good, and the tutorials include building a toy language.
C has a runtime... C++ has a slightly bigger minimal run-time than C. Some implementations of Ada have pragmas allowing to check that some features mandating the use of a run-time aren't used (I wonder if they weren't standardized later, I've stopped to follow Ada standardization in the late 90's), making it perhaps having a minimal run-time of the same compexity as C.
PyPy usses RPython to implement python language. Will this work for you?
Haxe is written in OCaml, and I think this is a really good language for writing other languages.
http://haxe.org/
http://caml.inria.fr/
If your going to write bootloaders and kernels then C is your language, otherwise it doesn't matter what language you use to develop your language. Just because your host language has a runtime doesn't automatically mean your target language must have one.
But ofcourse toy languages need a runtime, for example a JVM/LLVM/.NET CLR. Or interpreters. If you don't go for these choices you need to generate machine code that conforms to a ABI, and that is very painfull.
I suggest you look target llvm and generate machine code from there, dalvik might also fit your needs (since it is extremely lightweight).
I think Python is best for you. Python is a programming language that you can work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs.
Python, recently, has been ported to the Java and .NET virtual machines.
The most important, Python is free.

Experiences in learning Eiffel [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Does anyone have any experiences in learning the language Eiffel.
Is the Eiffel Studio like Visual Studio?
Is it a good route to take to learn OOP?
etc.
I learned this language about 15 years ago and never regret it.
Eiffel is a bit different from mainstream languages and features some mechanisms not readily available in other environments. In some sense it's a bit more academic than mainstream.
EiffelStudio environment takes some approaches that are not present in average IDEs as well. (For example, it supports Pick&Drop facility which is similar to Drag&Drop, but does not require to keep mouse button pressed as you drag the selected item.) It provides pretty advanced browsing facilities, so in this sense it's quite close to Visual Studio. It also has several built-ins like Diagram and Metrics tools, recently added AutoTest, etc. You can have a look at EiffelStudio documentation to get an idea about its features.
Since the language lacks hacks to introduce basic types, supports multiple inheritance and generic types and integrates Design by Contract right from the beginning, it is extremely good as a learning tool for OOP even if you do not plan to use it on a day-by-day basis. For that purpose I'd recommend "Object-Oriented Software Construction", 2nd Ed. by Bertrand Meyer. It does not include the most recent additions to the language, but it does provide a good route to OOP as a whole.
I think it's the best route to take for OOP, since it does object orientation very well.
I haven't seen an oop language that is as clean and consistent with regard to object orientation. therefore it's beneficial for a student to see how it should be done right.
As a language, if you contrast it with the scripting languages such as python, rebol etc. it's more heavy-weight. Best for large projects where you want to maintain quality. It focuses entirely on proper object orientation. (so you will not see all concepts of programming, but OO is a major paradigm)
Its speed is very good, as it compiles down to C, so I only meant heavy-weight about the structure.
It promotes iterative development, and thought-out design, so that's also a good technique to have.
You can read the Eiffel Tutorial to get a thorough overview, but take it gently, as it's a hundred pages "tutorial".
I actually think students who are being taught OOP should learn on the easiest language possible so they struggle with the concepts more so than the language. I learned on both Java and C++. The former was OK, but the latter was a nightmare. Python would be my suggestion
With regards to Eiffel, I tried doing my PhD project in Eiffel and I ran into serious problems. I would definitely not recommend using the GUI as there is little to no documentation on it. Also the compiler is very buggy. I was constantly in touch with their support regarding completely bizarre behavior. For example putting a print statement would solve a compile time error!
Eiffel provides some of the best multiple inheritance options, but even that can turn into a nightmare sometimes. I remember inheriting from multiple classes which all inherited from one main class, then having to SELECT one redefined function out of them, or to have to RENAME a function.
The design by contract was a good addition, and the variant helped avoid infinite loops, but at other times you just looked like an idiot when you had checks like these (my Eiffel is a little rusty)
foo (something)
do
x = x + 1
ensure
updated: x == old x + 1
There are other minor problems too. Often times the Eiffel studio would get corrupted and you had to erase certain back up files to get it to work.

What is the best way to design GUI Applications with C? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I always find good tutorials on the subject, but it almost always is C++. Is it hard or just doesn't make sense to make GUI applications with C?
A good book, guide or a tutorial is fine.
Platform: Linux
Use GTK+. It's written in C and exposes a well-designed C API. Their tutorial is quite excellent.
GTK+ is the toolkit for GNOME, so your applications will fit right in with many Linux desktops.
If you are programming in C on Windows - Petzold's programming windows used to be the bible for C based ui work.
EDIT:
Since I answered my question the post has been updated to be on Linux. I will leave the post here for anybody looking, but it really does not apply to Linux.
Many high quality GUI were written in C with, for example, Windows API. There's no particular reason why not, but object oriented programming was very successful in modeling interactive graphics. GUI elements somehow map naturally into C++ objects that can encapsulate complex behavior behind a simple interface.
It's hard enough (or, mostly, verbose enough) that most people figure it just doesn't make sense. The GUI part of an application (mostly) reacts to user input, so in this area speed is rarely critical -- a difference of a few microseconds (or even milliseconds) rarely makes much difference. As long as responses appear within 100 ms (or so), they're pretty much perceived as "instant".
Dynamic typing also tends to work quite nicely for a lot of GUI programming. C uses only static typing.
Don't.
Use python and then bind the computationally expensive calls written in C.
What is the best way to design GUI Applications with C?
don't, if you don't have to :-)
Is it hard or just doesn't make sense to make GUI applications with C?
It is not necessarily that hard, but a pretty redundant and verbose task (like writing high level programs in assembly language), with lots of repeated boilerplate code.
Personally, I find it more rewarding to simply embed a scripting interpreter (e.g. Lua, Nasal) with bindings for a GUI library and then code the UI in a high level scripting language and code only the core of the application itself in C. Python was previously mentioned, but I think that a dedicated extension language like Lua would be a better fit, because it doesn't bloat your source code and because it does not create any additional requirements (like library dependencies or architectural).
In other words, embedding something like Python, Perl or Ruby may be relatively straight forward (because of good documentation and community momentum), but often these languages are more complex than the host application itself.
This is also the approach taken by the AlgoScore software, which uses an embedded Nasal interpreter with GTK bindings.
If you're on a *nix system, you can use Xlib. But you're probably better off programming in C++ and calling out to your C code.

Web based development for the C language [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am a C programmer and new to web development. Which web-C-technology (open source) suits me to learn quickly for web-development?
Update: my question is based on both client and server technologies.
I know this is going to sound arrogant, but instead of saying that you are ‘a C programmer’ you should say that you are ‘a programmer’ and learn some language better suited to web programming. It will be fun, you will become a better programmer and the applications are very likely to be better, too.
I know that in theory you can write a good web application in any language, but in practice there are lots of aspects that make writing in a more popular (in the ‘webapp sense’) language easier. Lots of examples, if nothing else.
You could try KORE
Kore is an easy to use web application framework for writing scalable web APIs in C.
Kore exposes an easy to use API to build your applications. Below is an example of how simple it is to get going with writing web applications in C. The code will respond to all requests with an "Hello world" response
#include <kore/kore.h>
#include <kore/http.h>
int page(struct http_request *);
int
page(struct http_request *req)
{
http_response(req, 200, "Hello world", 11);
return (KORE_RESULT_OK);
}
Kore makes it easy to get started without having to fiddle with build frameworks such as make.
Using the builtin commands you can create, compile and run Kore applications.
However if you prefer building Makefiles and linking the libraries together yourself, go for it
$ kore create myapp
$ cd myapp
$ kore run
compiling myapp.c
myapp built succesfully!
[parent]: running on https://127.0.0.1:8888
[parent]: kore is starting up
[wrk 0]: worker 0 started (cpu#0)
Source
I'm not aware of any present-day heavy use of C (not C++) for web development, much less one that could be "learned quickly". It's not the right language for the task at large, though some specific modules in a web application could certainly be written in C for performance - and even then C++ would be most likely.
On the client side, you have little choice. You need to know your way around HTML/CSS/Javascript.
On the server-side, you have a ton of options.
I was a C programmer before I got into web development, and when PHP came along, I was attracted to it because it has syntactical similarities with C, and (even better), a lot standard C library functions are there too ( sprintf, fread, etc).
PHP is also a decent place to start as it performs just fine as procedural language, and if you've never done anything remotely OO, you might find that it provides a nice way to get your feet wet.
This is not to say that PHP is the most wonderful language on the planet. There are plenty of issues. But as a "first web-oriented language" for someone coming from a heavy C background, I'm hard-pressed to think of anything significantly better.
I'm not aware of any present-day heavy use of C (not C++) for web development
While not open-source, the G-WAN Web server is the only example I know of Web development in C.
It lets you write C scripts that are run on-the-fly:
http://g-wan.com
Given your experience in C, that may help.
On the client side, you can do nothing in C. On the server side one could argue that it's still possible to create a set of cgi's in C and these cgi's together with some cleverly crafted client code (html+css+javascript) could result in a state of the art web application.
This is, however, based on my experience, a very hard road, and one that's virtually impossible to follow without having a good insight in the very special environment that a web application is. Plus, the end-result will be a Frankenstein-like beast at best. Trust me, been there done that.
My suggestion to you would be to start studying web development as such. That is, client side html+css+javascript and a server side scripting solution. You'll probably benefit from choosing a scripting solution like php, because of its syntactical proximity to C, but IMHO that's the easy part of the job. Understanding how the client side (html+css+javascript) works together to create beautiful, reliable and user friendly web applications is where you need to focus on. Because of your background in C programming, the scripting side will be easier to get started in.
Good luck!
A lot of "web programming" nowadays happens client-side. Unless you investigate (or invent) a cross-compiler from C to Javascript, you're not going to be running a lot of C code in people's browsers.
Server-side, of course, you're free to write stuff in C if you feel like it, and some people do.
Before someone comes to suggest that you use language substitute-fashionable-one, I'd like to tell you that what you need to do the web development with c is to know how to do development in c and understand the nature of protocols, markup, etc. Library needs are minimal here. And no one will tell you which one suits your needs and your perception of beauty.

Artificial Intelligence Project - What language should I go for? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am a computer science student and I am going to work on an artificial intelligence project which will compose a musical tune according to the genre and mood inputs. Are the algorithms to be used for this project likely to be very resource-consuming? Would it make any difference (in terms of speed) if I choose to go with Java rather than C++? (Note : I know only these two languages and I am more comfortable with Java than C++.)
NB : Sorry for my poor English. If someone can, please clean up this post wherever necessary. Thanks.
Go with Java since you are more comfortable with it. That will allow you to concentrate on solving the problem, not the programming. Maybe C++ would end with a faster program, maybe not, but getting there will be slower and you don't categorically state that the program must be blazingly fast.
The resource consumption is way more influenced by the algorithmic approach than the language chosen. If you are comfortable with Java, program your application in that language - even though a C++ implementation might be 10% faster.
That being said, you might be interested with Artificial Intelligence API's for Java.
In my mind, the language mostly associated with AI is Lisp.
See the answers to Why is Lisp used for AI? - top voted mentions this was the case in the 60s and 70s, but these days dynamic languages are used (ruby, python and such).
It looks to me like you're at the proof-of-concept stage of your project. I'd use whatever language your most comfortable with. Well written Java code will run a lot faster then poorly written C.
I would use Common Lisp for a project like this. If you don't know Lisp, I would learn it for this type of project. It would be a great learning experience and since you are a CS student, it will only help you. Lisp is a language that can be a real eye opener.
I did a similar AI project a couple of years ago. I don't know what solution you will be implementing, but AI programs can generally be both resource consuming and may take a long time to run, but on the other hand, you'll need a language you're familiar with to get it done in time.
Therefore, my advice is that if you feel you know C++ (or C), go with one of them. If you don't know them, then consider carefully the time you will need to invest in learning a new language before choosing.
If you're starting from scratch, use whatever you know best. If you want to use established libraries to speed up development, you might want to investigate that first - but Java is certain to have some.
In your shoes, I'd pick Java for sure.
I'd go with Clojure for the following reasons:
It's a Lisp, and Lisps are great languages for AI development (partly historical, but also for some real concrete reasons - see this thread and this thread)
Clojure runs on the JVM and has great Java interop, so you can exploit all the great Java AI libraries (e.g. Weka) plus you already have some experience of the Java environment
JVMs have excellent optimizing JIT compilers nowadays, for all practical purposes you will get performance as fast as C/C++ for this kind of application.
My advice is design everything you need first, every ADT, every algorithm class, hierarchy, everything. This kind of project/programming could be really hard to design in C/C++ family of languages, maybe you could choose other language with less string typed philosophy. So i encourage you with using a language designed for this kind of problem, better suited to your application, functional paradigm ex: LISP, logical paradigm ex: PROLOG or something like that.
My 3rd year dissertation project was an implementation of heuristics for cellular network radio frequency allocation. I chose Java over C++ because it allowed me to visualize the results much easier than if I'd used C++. I don't believe the performance would have been significantly different in C++ - the complexity factor of your algos is going to be the biggest factor probably.

Resources