Saturday 24 November 2012

LISP INTERPRETER IN JAVASCRIPT






For the construction of an interpreter we are considering two main parts parsing and execution. Parsing is implemented using the function parse and execution is done using the function eval

First we define the function Env():

1.This function is defined to find the right environment for a variable.
2.This process of looking variables first in inner environments and then in outer ones is called 
   Lexical Scoping

Consider this scheme example:
(define make-account(lambda (balance)(lambda (amt)(begin (set! balance (+ balance amt)) balance))))

Their environment structure will be: 

State 1:  (define make-account 
State 2:                      (lambda (balance) 
State 3:                                         (lambda (amt) 
State 4:                                                    (begin (set! balance (+ balance amt)) balance))))

In State 4, the variables amt and balance are used. The variable defined in the inner environment(Stage 3) is amt. And the variable balance is defined in the outer environment(Stage 2).
But '+' sign in the Stage 4 is not defined in both environment. So we need to define a global environment .

So we define the function add_globals() :

1. This function is to define the global environment.
2. Many functions for mathematical operators are  defined.
3. Many built-in procedures are accessed by importing modules.

The methods in the list Method are built-in functions and can be used by importing the module Math.

Method = ['abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log', 'max', 'min', 'pow', 'random', 'round', 'sin', 'sqrt', 'tan']

Where as other methods like addition, subtraction,multiplication, division, modulus and comparison operators and defined and added to the module.

Then we define the function eval():

1. Function for execution part of an Interpreter.
2. Here we evaluates the expression in an environment.
3. Special forms or syntactic constructor are defined.

For eg :-  In case of scheme (if test conseq alt), if the expression test is true, then returns 
                conseq,  else returns alt.

Now we define the function parse():

1. We defines the four other functions tokenize(), read_from(), read() and atom()

2. Inputs are scanned and made a list of strings in expressions.

tokenize() :- This function takes the input expression and replaces '(' by ' ( ' and ')' by ' ) ' and 

                    then  removes the spaces at the beginning and end of the expression using 
                    built-in function trim() and splits the input based on the single space ' ' using 
                    built-in function split(' ').

read_from() :- This function takes the list returned by the tokenize() function and it removes '(' and ')'.

                       Thus we gets the list of expression elements in its order without parenthesis.

read()  :-      This function returns the read_from(tokenize(input))

   
atom() :-      This function converts string of integers to numbers and returns.

These all are the main functions used in my javascript code to make Lisp Interpreter.


Download my Javascript Lisp Interpreter code here :                



[Expecting Your Valuable Comments]
Thank You

No comments:

Post a Comment