

How to Write a Simple Interpreter in JavaScript - jim_lawless
http://www.codeproject.com/Articles/345888/How-to-write-a-simple-interpreter-in-JavaScript

======
gouranga
Nice article - covers pretty much everything in a reasonable amount of detail.

As for the author, it should be noted that he's a 17 year old who has been
programming for 2 years. Good job - we need more people like that!

------
nene
I'm a bit disappointed, that the author didn't built a turing-complete
interpreter. Frankly I've seen several of such articles which tell you how to
build this kind of deterministic evaluator, but they all seem to stop before
loops and branching.

And then there are articles about building compilers which do support loops
etc, but the trick is that these constructs are implemented by some other
already existing machine.

So I feel like there's some gap in my education. Pointers, anybody?

~~~
yossi_peti
I'm not sure I'd be so quick to say it's not turing complete--the language
includes building functions (not first-class functions though), but it might
be possible that they can be used to emulate conditionals and branching.

Anyway, I'm pretty sure it wouldn't be that hard to extend the language to add
some looping and branching constructs to the language, there's nothing really
special there to add, it's just another simple type of construct to evaluate.

In my understanding, loops are implemented at the lowest level with jump
instructions, but I've never really done much low-lever programming so maybe
someone else can explain that better.

~~~
btilly
There is no conditional, and without a conditional you can't get a
conditional.

However modifying it to support conditionals is easy. Just add a built-in
function if(condition, expr1, expr2). Now you can build if's. Since the
language already properly supports recursive function calls, now it is
possible to implement loops through recursion, and you're off and running.

~~~
tmhedberg
If you have function application, then you don't truly need a primitive
conditional. You can represent Boolean values and conditional functions with
the Church encoding [1]. This is how conditionals are represented in, for
instance, the lambda calculus.

[1] <http://en.wikipedia.org/wiki/Church_encoding#Church_booleans>

~~~
btilly
Good point. But you need to have first class functions for that. This toy
language does not. And converting from "this native number equals that native
number" to your encoding of "true" is going to be an interesting trick.

------
heapify
This is interesting, but I feel as though the author shouldn't be instructing
one on writing a parser by hand, because this is highly impractical and there
are parser generators out there that target JavaScript. One's time is better
spent studying the abstract math behind how parsing works. Context free
grammars are complex, and it's far too easy to do something wrong.

That being said, writing an interpreter is fun and anyone with even a mild
interest in parsing or compilers should do it.

~~~
stinky613
This article made me want to try making a compiler or interpreter when I get a
block of free time. I actually stopped reading because I want to try to figure
out as much as I can on my own.

~~~
heapify
It's a very fun thing to do! Especially if you construct your own virtual
machine--function calls aren't as simple as they appear. Also fun is targeting
the Java Virtual Machine using a bytecode library such as ASM.

I personally enjoy using ANTLR for parsing, so that might give you a place to
start.

------
dugmartin
If you want to see an interesting way to parse using Javascript check out
Douglas Crockfords Top Down Operator Precedence paper here:

<http://javascript.crockford.com/tdop/tdop.html>

~~~
yossi_peti
I think the parsing method in this article was borrowed from Crockford's TDOP
article, except it parses a simpler language than Javascript.

