How do you create a computer or scripting language for an application? - programming-languages

Duplicate of:
Learning to write a compiler
Documentation on creating a programming language
Learning Resources on Parsers, Interpreters, and Compilers
Suggestions for writing a programming language?
Compiler-Programming: What are the most fundamental ingredients?
Are there some online resources about compiler principle?
and others I'm too lazy to find right now.
I'm not asking how to make an incredibly complex language. I just wanted to understand the basics. I would use c# as the underlying language. I know it's vague. I was hoping for something very basic to direct me.
I think I'm mostly interested in creating scripting languages. For example, I see people that write programs but then they have a scripting language for their application. I do not want to rewrite a windows scripting language. Say I had a text file reader and for some reason wanted a scripting language to automate something. I'm not sure how to ask.
Thank you.
EDIT - Thank you for the answers. I was looking at it more for the learning not the doing at the moment. I would probably use LUA, but I am trying to learn more about the concept in general.

You could take a look at LUA - I've used it to great success each time I asked myself the question "How would I automate insert task here in insert one of my apps here?"
Edit: Here are some examples (taken from the links page, admittedly, unwieldy Lua Wiki) on how you could embed Lua in your app:
Embedding Lua in C: Using Lua from inside C
Embedding a scripting language inside your C/C++ code
Embeddable scripting with Lua

You can use an existing language like Python or Javascript. For example, for Javascript, there is http://www.mozilla.org/rhino/ for Java apps. So typically you don't need to actually invent a new language, you would just provide a custom API for a language that already exists.

first you need a lexical parser like lex, then a syntax parser like bison.
then you can work with the syntax parser to create an interpreter to 'execute' the syntax results.
that's how the most scripting languages do.
p.s: another way is to practice by writing shells - shell scripts (bash, csh, or sh) are highly simplified scripting languages.

Some terminology is in order. You may be talking about a domain-specific language.
The two basic ways to transform a text file into an "executable": a compiler or an interpreter. An interpreter fits the scripting concept better, as it is easier to build and executes lines one at a time. Note that beyond a very simple language both writing a decent parser or a decent interpreter are non-trivial. The classic work on interpreters is SICP, but this is quite a hard book for beginners.

Scott Hanselman mentioned in his latest hanselminutes podcast that integrating IronPython to allow scripting of an existing application was very easy to do.
If you're interested in the end target of having your application be scriptable, then you should definitely consider using an existing language rather than attempting to write your own.
If you are more interested in the educational experience of writing your own scripting language, then you should go for it!

There's no need to create a new scripting language there are several eg. Rhino which is a widely used embeddable javascript (http://www.mozilla.org/rhino/) or Jscript from MS, that you can use directly in your product.
I've gone the way that you are asking - I once created my own scheme interpreter. This worked really well, but we re-invented a lot of technology and didn't really get a lot of additional benefit. We would have been far better off just using one of the scheme's that were available. I would not make that decision again even though it was fun and successful.

Related

Looking for a new language that supports both interpreted and native compilation modes

I currently program in Perl, Python, C#, C, C++, Java, and a few other languages, and I'm looking for a new language to use as a primary when doing personal projects.
My current criteria are:
can be run as an interpreted language (i.e., run without having to wait to compile it);
can be compiled to native code;
are strongly typed (even if optionally);
support macros/templating/code morphing/wtf you want to call it;
has a decent number of libraries for it, or easily accessible to it;
Ideas? Suggestions?
I would suggest that Haskell suits your criteria.
Can be run as an interpreted language? Yes, via GHCI.
Can be compiled to native code? Yes.
Is strongly typed? Very much so. Perhaps even the most strongly typed language today, with the exception of some theorem provers like Agda.
Support macros/templating/morphing? If you use template haskell. This is an optional extension of the language however, so most libraries don't use macros. I haven't used template haskell myself so i can't comment on if it's any good.
Has decent library support? The standard library is not bad. There is also Hackage, an open repository of Haskell libraries a bit in the style of CPAN.
Additionally, it sounds like you already know a lot of imperative/object oriented languages. IMHO if you learn another one of those langs. it will probably be a slightly different permutation of features you've already seen somewhere else. Adding another programming paradigm like functional programming to your toolbox will probably be a better learning experience. Though I guess whether that's an advantage or not depends on if you want to learn new things or be productive quickly.
Common Lisp fits: there is an optional typing, efficient native compilation is available, powerful REPL makes it a perfect choice for scripting, and there is a powerful macro metaprogramming.
OCaml fits as well, with CamlP4 for metaprogramming.
Scala? It does run scripts, although they are compiled (transparently) first. I'm not sure what you mean by code morphing etc, but it's pretty good for DSLs. It meets all your other requirements - compiled as much as Java is, strongly typed, and has a reasonable number of its own libraries as well as all of Java's. I'm still a beginner with it, but I like it so far.

Advice on languages and places to learn them?

My final for my last java class will be Monday. What would be a good language with many free sources (poor college student here) to work off of? I've heard good things about Ruby and Erlang but both seem alien to me.
Perhaps there is something I should try in Java first before moving to another language like some kind of framework or libraries that would advice me more than adopting a new language so quickly?
As a Student, i hear those words a lot from many of my professors:
"Its not about the language, its about algorithms!!"
Which after almost 4 years of trying different languages, i find this to be true.
Because if you learn how to do something in one language... the rest is just applying your knowledge using different syntax.
My advice is to stick with what you started (Java), and after you master the basics, then deal with:
Inheritance
Polymorpism
Data Structures
Because you will find the above in almost every language.
Algorithm Animations helped me visualize and understand a lot.
Also read this post.
I would definitely recommend Ruby - as a fun language to learn more about programming. Java is statically typed and strictly object oriented, so it is natural that Ruby feels alien to you.
Ruby is strong in a number of paradigms - especially object oriented, functional and metaprogramming. Learning lots of paradigms and how to combine them will make you a better programmer no matter what future languages you use.
Having learnt Java, JRuby would be good implementation of Ruby to use - it is written in Java and runs on the JVM. Another advantage of JRuby is that you can use Java libraries from your Ruby code. Just install jruby from your package manager and you should be ready to go.
To learn ruby check out the following resources:
Ruby from Java
Ruby in 20 minutes
Programming Ruby
Since you already know java, get a taste of other styles like Python, Perl and Ruby. PHP, Java, and C++ are closely related style wise.
I would recommend looking at Scala, F#/SML/Ocaml and Haskell -- but Scala in particular:
The reason for this is Scala shows that a "fun" statically/strongly-typed high-level (in a real sense) 'OO' language can exist. There are many useful programming constructs that are not possible in Java due to design and implementation limitations. (Scala is by no means a perfect language.)
Even learning just C#3/4 (which generally has a less powerful type system than Scala, although not all areas overlap) will open eyes with constructs that are not easy to do in Java -- simply passing around "functions" as objects or being able to uniformly and easily run queries and manipulations over lists are something out of reach with Java. You can fake closures/lambdas in Java (look at anon-inner classes or the Functional Java library), but it is hardly pretty: Scala and C# add language support for these constructs so they are "natural".
I won't speak ill of Ruby or Python (or many other languages, excluding PHP ;-) -- but one thing that they lack is static typing. Some people find this a blessing, but I generally find it a curse (good static type systems can prove the absence of a number of type problems) -- the problem is that, if Java/C/C++ is/are the only statically typed language(s) you have dealt with, you will think that static typing has to be painful (and implies "too much type annotation" and "too brittle/closed types") -- this is simply not true in more advanced statically typed languages with type inference, view-bounds, typeclasses, etc.
Learning Haskell will really open your eyes up to a different approach -- but it really is a different beast and requires forgetting how language X works. You can write "Java in Scala", even if not advisable/idiomatic, but you really can't write "Java in Haskell".
If you are looking for web development, I would strongly suggest learning PHP (it is a really fun and useful language). If you are looking to develop for the desktop, C++ and if you want to try iPhone / iPad development, try Objective-C.
I took a Java class and I found that it gave me a good foundation for moving to Objective-c, you just need to be careful because the language does not manage your memory usage (no garbage collector). All of the above languages have tons of support freely available on the internet. If you are looking for a technique to learn a language, I find that looking at example code is the best way to go.
Hope this helps!
I Totally agree with what Chris said, Make sure that when you start learning, look for everything in Google, there are thousands of resources available for how-to's about the language you chose to go with.

How to go about making your own programming language? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Learning to write a compiler
I looked around trying to find out more about programming language development, but couldn't find a whole lot online. I have found some tutorial videos, but not much for text guides, FAQs, advice etc. I am really curious about how to build my own programming language. It brings me to SO to ask:
How can you go about making your own programming language?
I would like to build a very basic language. I don't plan on having a very good language, nor do I think it will be used by anyone. I simply want to make my own language to learn more about operating systems, programming, and become better at everything.
Where does one start? Building the syntax? Building a compiler? What skills are needed? A lot of assembly and understanding of the operating system? What languages are most compilers and languages built in? I assume C.
I'd say that before you begin you might want to take a look at the Dragon Book and/or Programming Language Pragmatics. That will ground you in the theory of programming languages. The books cover compilation, and interpretation, and will enable you to build all the tools that would be needed to make a basic programming language.
I don't know how much assembly language you know, but unless you're rather comfortable with some dialect of assembly language programming I'd advise you against trying to write a compiler that compiles down to assembly code, as it's quite a bit of a challenge. You mentioned earlier that you're familiar wtih both C and C++, so perhaps you can write a compiler that compiles down to C or C++ and then use gcc/g++ or any other C/C++ compiler to convert the code to a native executable. This is what the Vala programming language does (it converts Vala syntax to C code that uses the GObject library).
As for what you can use to write the compiler, you have a lot of options. You could write it by hand in C or C++, or in order to simplify development you could use a higher level language so that you can focus on the writing of the compiler more than the memory allocations and the such that are needed for working with strings in C.
You could simply generate the grammars and have Flex and Bison generate the parser and lexical analyser. This is really useful as it allows you to do iterative development to quickly work on getting a working compiler.
Another option you have is to use ANTLR to generate your parser, the advantage to this is that you get lots of target languages that ANTLR can compile to. I've never used this but I've heard a lot about it.
Furthermore if you'd like a better grounding on the models that are used so frequently in programming language compiler/scanner/parser construction you should get a book on the Models of Computation. I'd recommend Introduction to the Theory of Computation.
You also seem to show an interest in gaining an understanding of operating systems. This I would say is something that is separate from Programming Language Design, and should be pursued separately. The book Principles of Modern Operating Systems is a pretty good starting place for learning about that. You could start with small projects like creating a shell, or writing a programme that emulates the ls command, and then go into more low level things, depending on how through you are with the system calls in C.
I hope that helps you.
EDIT: I've learnt a lot since I write this answer. I was taking the online course on programming languages that Brown University was offering when I saw this answer featured there. The professor very rightly points out that this answer talks a lot about parsers but is light on just about everything else. I'd really suggest going through the course videos and exercises if you'd like to get a better idea on how to create a programming language.
It entirely depends on what your programming language is going to be like.
Do you definitely want it to be compiled? There are interpreted languages as well... or you could implement compilation at execution time
What do you want the target platform to be? Some options:
Native code (which architectures and operating systems?)
JVM
Regular .NET
.NET using the Dynamic Language Runtime (like IronRuby/IronPython)
Parrot
Personally I would strongly consider targeting the JVM or .NET, just because then you get a lot of "safety" for free, as well as a huge set of libraries your language can use. (Obviously with native code there are plenty of libraries too, but I suspect that getting the interoperability between them right may be trickier.)
I see no reason why you'd particularly want to write a compiler (or other part of the system) in C, especially if it's only for educational purposes (so you don't need a 100-million-lines-a-second compiler). What language are you personally most productive in?
Take a look at ANTLR. It is an awesome compiler-compiler the stuff you use to build a parser for a language.
Building a language is basically about defining a grammar and adding production rules to this grammar. Doing that by hand is not trivial, but a good compiler-compiler will help you a lot.
You might also want to have a look at the classic "Dragon Book" (a book about compilers that features a knight slaying a dragon on the front page). (Google it).
Building domain specific languages is a useful skill to master. Domain specific languages is typically not full featured programming language, but typically business rules formulated in a custom made language tailor made for the project. Have a look at that topic too.
There are various tutorials online such as Write Yourself a Scheme in 48 hrs.
One place to start tho' might be with an "embedded domain specific language" (EDSL). This is a language that actually runs within the environment of another, but you have created keywords, operators, etc particularly suited to the subject (domain) that you want to work in.

Lua, what is Lua? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I read something about Lua today, and I was wondering what it was.
I did a Google and Wikipedia search, I understood it until they began talking about C and API. I still don't understand it.
What is Lua and are there any tutorials for beginners?
Lua is a lightweight interpreted programming language developed in Brazil with a focus on embedding.
It is written in Pure ANSI C which means high portability, even as C++ code.
Here is an example:
print("Hello World!")
Wikipedia Summary
Official Site
I'm surprised everyone is getting this one wrong.
Lua is the Hawaiian word for "toilet".
Lua is a scripting language for C and C++. It allows to use the simpler syntax of Lua and execute these scripts in your C/C++ application. Therefore you don't have to compile the program on each change, but simply deliver a new script version.
For tutorials just use google, you'll find enough to keep you busy the next days.
Lua is a simple lightweight highly portable dynamically typed embeddable and extendable multi-paradigm scripting language. The "vanilla" (some would say official) implementation of it is made purely on ANSI C and has an awesome (simple yet powerful) C API that you can use to both embed Lua on your app or extend the behavior of the language itself. It is developed at the Informatics Department of the Pontifical Catholic University of Rio de Janeiro (PUC-Rio).
Thought it was not primarily designed for that, Lua found a big niche in game scripting, with big names such as "Grim Fandango" and "World of Warcraft". Nonetheless, because of its speed, simplicity and portability, it is also heavily used in embedded systems (see, for example, eLua project) and graphic computing.
Its philosophy is to be minimalistic, i.e its core libraries are very small with only minimum functionality (quite like C's standard libraries), though through the C API it is very simple to add features that wouldn't be possible through the pure core library, such as sockets, GUIs etc. In fact, Lua is so minimalistic that its main -and only- structured data type are 'tables', that could be described as associative arrays on 'roids.
Lua is procedural in its essence, but also supports multiple paradigms such as functional programming and object orientation.
Though Lua is not the fastest scripting language around (probably javascript's V8 project wins the prize) it is very fast (faster than vanilla Python or Ruby, for instance) and also features a non-official just-in-time implementation called LuaJIT.
In the end, Lua is actually no more than a fun language to play with, which I recommend!.
=)
About tutorials, I'd recommend the article about that on the lua-users wiki.
I hope I helped! =)
PS: I couldn't post all the links because I'm new on stack overflow, but it shouldn't be hard to find everything on Google. Sorry. =(
Lua is a scripting language. Link is to lua.org. It is heavily used in game development, most notably (to me) World of Warcraft.
Lua is a lightweight, embeddable scripting language. It's garnered a lot of popularity partly due to it's use in many popular games. A good example of this is World of Warcraft which uses an embedded version of lua to drive the behavior of the UI elements in the game.
A good intro to the language can be found here: http://computer-programming-languages.suite101.com/article.cfm/a_brief_introduction_to_lua_scripting
And the official online reference for the language can be found here: http://www.lua.org/manual/5.1/
It's a scripting language that is designed to work with C (and C++). It is designed to be embedded into C/C++ programs. Which means unlike a lot of other scripting languages, it makes no use of global variables and such, this means you can easily thread lua scripting engines.
It also makes claims about being the fastest dynamic scripting language.
I've made use of it in PC based C++ application for creating a plugin scripting interface, and also used it as a embedded scripting language. Its quite versitile, nice and small.
as a general purpose scripting language? Its not quite in the same league as your ruby/python/perl type stuff. It doesn't have as many libraries and the user community is pretty small.
But for extending C++/C apps? its awesome.
The C API, which looks to be the part that confused you, is designed to make it very easy for you to take existing C code (or new C code) and control it with a Lua script. This is what is meant by embedding. Via embedding, you can get a lightweight, programmable, interactive, text-based interface to any C code, for very little effort. Even if you don't embed things yourself, Lua is a very nice little scripting language.
My favorite example is that a nice person put a great many POSIX system calls into Lua. When I want something that is like a shell script but is more sophisticated, I can just use this library. Likewise with the MD5 library and with many others.
When learning Lua you can ignore the C API completely—just benefit from other people's work with the API—and if you ever need to embed your own code, you can come back to it later.
Lua is a SCRIPTING language written in C and is commonly used in game development because of its power and flexibility. Lua is also cross-platform so it can be used anywhere on any platform. Lua can also be used as a programming language with a handy program I use called AutoPlay Media Studio which allows you to create fully fledged applications for the Windows platform. I hope this clears things up for you.
Website: http://www.lua.org.
AutoPlay Media Studio: http://www.indigorose.com.
Lua is a powerful, fast, lightweight, embeddable scripting language.
Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.
for more you can read here

Programming language with native code support, No framework (I write the framework)

I'm looking for a programming language. It should be an easy language to learn, and should have a Garbage Collector. It should be a basic language with features like basic types (integer, boolean), arrays and etc, and I should write the framework.
It is for a game editor I want to write. The editor's designer will write the code of the UI in this programming language. The framework will be a 2D graphics and audio framework, and in the future it'll be 3D too.
I thought about the new Go language, but it doesn't have much support and theres no binding to OpenGL and etc.
Any ideas?
Thanks.
The obvious two are [C](http://en.wikipedia.org/wiki/C_(programming_language)) or C++. However, [D](http://en.wikipedia.org/wiki/D_(programming_language)) is closer to Java and C# given that it has a garbage collector in the standard, as well as an alternative standard library that is fairly closer to Java than the C++ standard library. The downside with D is that they tools are not as mature as C++ or C and the community isn't as large.
The obvious solution though it to look down the list of compiled languages on wikipedia and see which you like the look of.
Well, that's a fairly broad question and without more specific requirements it is difficult to give a focused answer, but it sounds like C (or C++) would fit the bill for you. The languages you described all owe their syntax to C. C will compile to native code. C is basic language in that there is not much to learn beyond the basic syntax and it has all the basic primitives that you require.
Now that you've added the requirement of a garbage collected language, I suppose that you could try Go, but that language is not mature and there's always a risk there.
If you don't want to manage memory all by yourself like C or C++, you can try the new Go language. It compiles to native code (albeit for Linux and MacOSX only for now) and comes with a basic framework that can be easily replaced with your own framework.
It has a very active user base, so IMO it is possible to mature quickly.
You may want to look at Lua.
Lua is a relatively tiny language which manages to be capable and universal with just a few concepts. The BNF specification for the whole language fits easily on one page. It has numbers, booleans, tables and functions, and surprisingly that's all the datatypes it needs. It can even work in an object-oriented fashion.
There's a compiler, Luac, that compiles Lua to bytecode.
Lua is already being used as a UI programming language for games. Addons for World of Warcraft and a few other games are programmed in Lua. I believe Lua is a very good fit for this kind of task.
You want OpenGL? OK... http://luagl.wikidot.com/ is an OpenGL library for Lua.
Since we don't know what you want to do, I don't know what are the chances we success. Therefor, what about a language where you have to set the probability of your statement to fail :
Meet GOTO++.
Don't say "thanks you", it's on me.
Enjoy a challenge?
Try go.
Here's a tech talk by rob pike, and here is a discussion group: http://groups.google.com/group/golang-nuts/topics
.
C++ is Great, it's not scripting lang, so you don't even need a scripting host.

Resources