13 ms·
How to Implement a Programming Language in JavaScript
- arcatek 12y agoAnother really good course to learn the basic about programming language design: http://nathansuniversity.com/ http://nathansuniversity.com/ 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.
- thomasfoster96 12y agoI'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. 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.
- moron4hire 12y agoI do a lot of JavaScript and Lisp is extremely appealing to me. JavaScript is a functional language, but not a very good one. Lisps are much better at doing functional programming than JavaScript. So a Lisp that translates to JavaScript is right up my alley.
- samatman 12y agoYou might enjoy wisp: https://github.com/Gozala/wisp https://github.com/Gozala/wisp
- tinco 12y agoGiven 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. 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..
- mishoo 12y ago... and then it gets continuations, and that's where it becomes interesting. Any case, the intent was purely didactic, but I did implement a Scheme dialect based on half of that code. To be released some day...
- chubot 12y agoI'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. I have a good analogy to explain this. Take this OCaml program. 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 ;; This is roughly equivalent to: def sum_file(filename): with open(filename) as f: return sum([int(x) for x in f]) Now think of the degree to which OCaml is awkward here. That's exactly how awkward Python and JS are for writing languages :) OCaml is based around ML-style typed records, which are exactly what you need for manipulating languages. 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. 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. Someone saying the same thing here: http://flint.cs.yale.edu/cs421/case-for-ml.html http://flint.cs.yale.edu/cs421/case-for-ml.html
- kirse 12y agoI had just stumbled on this C# 1.1 parser written in F# yesterday. I'm not sure if a syntax-tree and parser in 350 lines is decent, but I can at least see what you're talking about in regards to ML/OCaml being well-suited for the task. http://www.fssnip.net/lf http://www.fssnip.net/lf
- munificent 12y agoI agree with you in principle. After all, "ML" got its name "metalanguage" from the fact that it was explicitly designed to be a domain-specific language for implementing programming languages. It's hard to be a DSL at its job. At the same time, that has to be balanced with the ecosystem value of implementing your language in a language lots of others knows. It's a worthwhile investment to implement your language with 4x the code in Python/Java/JS/etc. if you get more than 4x contributions because of it.
- deleted 12y ago[deleted]
- fredkelly 12y agoInteresting read. Possibly more digestable, I recommend the walkthrough of building "Egg" in the fantastic (and free!) Eloquent JavaScript by Marijn Haverbeke: http://eloquentjavascript.net/11_language.html http://eloquentjavascript.net/11_language.html
- noiv 12y agoI 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.