Lisp Interpreter in JavaScript

Nidhin P
1 min readAug 11, 2019

An interpreter has two parts.

  • Parsing
  • Execution

Parsing: Parsing component takes input program as a sequence of characters, verifies it according to the syntactical rules of the language and convert that into an internal representation. An array is used as an internal representation.
Execution: The internal representation from parsing phase is then processed according to the semantics rules of the language, thereby carrying out the computation.

The Parser

The parser first reads the program as a string. And produce an array of tokens.
program = “(begin (define r 3) (* r r))”
tokenize(program);
Output: [‘(‘, ‘begin’, ‘(‘, ‘define’, ‘r’, 3, ‘*’, ‘r’, ‘r’, ‘)’, ‘)’]

This array of tokens are then passed to read_from function that try to read an expression from a sequence of tokens.

Execution

In execution phase nine forms(variable reference, constant literals, quotation, conditional, assignment, definition, procedure, sequencing, procedure call) are evaluated.

Environment

Environment is used to map values to the corresponding variables.

Here is the complete code.

--

--