I've written multiple parsers/interpreters in both JS and Python -- Python being my favorite language. From that experience, I've come around to the fact that they're both the wrong language for writing languages -- lexers, parsers, interpreter loops, compilers.<p>I have a good analogy to explain this. Take this OCaml program.<p><pre><code> let sum_file filename =
let file = In_channel.create filename in
let numbers = List.map ~f:Int.of_string (In_channel.input_lines file) in
let sum = List.fold ~init:0 ~f:(+) numbers in
In_channel.close file;
sum
;;
</code></pre>
This is roughly equivalent to:<p><pre><code> def sum_file(filename):
with open(filename) as f:
return sum([int(x) for x in f])
</code></pre>
Now think of the degree to which OCaml is awkward here. That's exactly how awkward Python and JS are for writing languages :)<p>OCaml is based around ML-style typed records, which are exactly what you need for manipulating languages.<p>I am not a type safety guy, but when you write languages, there are tons of nested conditionals. In Python in JS or C, you end up with bugs in those corners. It is quite easy to crash any non-trivial parser, like the CPython parser, or Clang's parsers, etc. The type safety that ML offers helps a lot with this.<p>The code for writing parsers and interpreters is just SHORTER in OCaml than in Python. I used to think Python was the most concise language. But no, it depends on the problem domain. Try it and you'll see.<p>Someone saying the same thing here:<p><a href="http://flint.cs.yale.edu/cs421/case-for-ml.html" rel="nofollow">http://flint.cs.yale.edu/cs421/case-for-ml.html</a>
Another really good course to learn the basic about programming language design:<p><a href="http://nathansuniversity.com/" rel="nofollow">http://nathansuniversity.com/</a><p>And if you want to actually build a "real" programming language, I advise you to try llvm - it really takes away the pain of generating bytecode, and gives you everything you need to deal with the actual design of your language.
Interesting read.<p>Possibly more digestable, I recommend the walkthrough of building "Egg" in the fantastic (and free!) Eloquent JavaScript by Marijn Haverbeke: <a href="http://eloquentjavascript.net/11_language.html" rel="nofollow">http://eloquentjavascript.net/11_language.html</a>
Given the domain name I was hoping they'd first implement a Lisp in 12 lines of Javascript, and then implement the new language in that lisp, that would've been an interesting tutorial.<p>I get that it's a beginners tutorial and they have some constraints, but it starts off with "let's dream up a language" and then presents a super standard language, it's basically just Javascript with obligatory semicolons..
I'm glad they didn't write a tutorial that made another Lisp - I'd your audience is javacript developers, a lisp isn't all that appealing usually.<p>Anyways, this is such a detailed series of tutorials I may be distracted for days bringing my half finished programming language back from the dead.
I remember reading this the first time as a JS rookie. Every time the author mentions "this is same in JS" a light went on in my head and all the JS quirks found earlier became just natural. It still deserves its place in the Christmas tree bookmark section.