JavaScript LISP Interpreter

by Joe Ganley

Samples


[Introduction] | [Language Specifications] | [Copy Permissions]

This version of the JavaScript LISP Interpreter permits you to examine several DEFINE [**] examples that describe functions, and to enter test expressions to experiment with the meaning of these functions.

Procedure:

  1. Click on the button beneath the description of a function in the table below;
  2. the DEFINE expression will be shown in the upper INPUT window;
  3. click on EVALUATE to store the definition; the equivalent LAMBDA definition will be shown in the OUTPUT window;
  4. clear the INPUT window and enter the sample shown in the table; click on EVALUATE
  5. check the result in the OUTPUT window with your understanding of the function definition and then enter some strings of your own.
To examine the general limitations of this implementation click here.
(last s)
Returns the last element in the list s.

Try: (last '(1 2 3 4));
the result should be 4.
What is the result of
(last '((1 2) (3 4 5)))?

(remove e s)
Returns s with all occurences of element e removed.

Try: (remove 'a '(a b c));
the result should be (b c).
What is the result of
(remove 'b '(b b b d))?

(factorial n)
Returns product of the integers 1 through n.

Try: (factorial 3);
the result should be 6.
What is the result of (factorial 7)?
(Don't try values greater than 15 - the resulting values are generally too large!)

(remainder a b)
Returns remainder of the integer division of a by b.

Try: (remainder 13 4);
the result should be 1.
What is the result of (remainder 40 4)?
(Javascript has severe limitations on the degree of recursion that can be supported. Thus when the limit is passed you may get "Javascript error")

INPUT

OUTPUT


Joe Ganley's Introduction

I wanted to get a feel for just how powerful JavaScript is as a programming language (as opposed to just a scripting language), so I sat down one evening and wrote this. It turns out that it was quite good for this application, since it is so flexible - dynamically sized arrays are used throughout, and associative arrays make the symbol table quite easy.

A fairly complete set of Lisp instructions are implemented (see here). The choice of instructions and some of the implementations of the composite functions were taken from Roger Rohrbach's Lisp in Awk interpreter. The syntax is standard, and only integer arithmetic is available. This is Lisp, not Scheme - scoping is dynamic.

Since this is pretty much a toy, it doesn't do much in the way of error handling. However, it should work on correct Lisp code; if it breaks on code you believe to be correct, please send me mail at joe@ganley.org.

Permission

The source of this interpreter is copied by permission from Joe Ganley per his copying restrictions.


NOTE: In this version it is not necessary to include the function name and the arguments in a list as in "standard" LISP.


Portions copyright © 1997 Joe Ganley, modified by J.A.N. Lee, 2001.