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>