Let us discuss the basic concept behind the lisp interpreter using javascript. Those who are new to javascript can go throw some basics and the complete code of our interpreter program given here. Let us go through each functions one by one.
Function Env:
This function defines the environment for a variable used in execution. The function find(variable) in line number 4 will find the correct environment of execution for the variable. If the variable is defined in the existing environment, the current environment is considered, else the function is called recursively to the outer environments untill the correct environment is found. This property is known as lexical scoping.
Function addGlobal:
This functions defines the global environment. Basic math functions such as '+', '*' etc are defined in the global environment. Other than these 21 basic functions, some other math functions such as sin,cos,log etc are also included in the global environment as shown below.
var functions = ['abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log',
'max', 'min', 'pow', 'random', 'round', 'sin', 'sqrt', 'tan'];
Function eval:
Evaluation function defines the steps of evaluation processes for all types of lines of codes. It represents the execution part of the interpreter. 9 cases are defined in the function. For example, if the line of code to executed is a 'define' function, line no 70 is executed.
Functions for parsing
Parsing is the first stage of the interpreter. It converts the input code into a set of tokens and reads according to the semantic meanings of the language. We use a number of functions for these actions which are listed below.
Function repl:
repl() represents the read evaluation print loop. The function is used to form an interactive Lisp interpreter.
Function Env:
This function defines the environment for a variable used in execution. The function find(variable) in line number 4 will find the correct environment of execution for the variable. If the variable is defined in the existing environment, the current environment is considered, else the function is called recursively to the outer environments untill the correct environment is found. This property is known as lexical scoping.
Function addGlobal:
This functions defines the global environment. Basic math functions such as '+', '*' etc are defined in the global environment. Other than these 21 basic functions, some other math functions such as sin,cos,log etc are also included in the global environment as shown below.
var functions = ['abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log',
'max', 'min', 'pow', 'random', 'round', 'sin', 'sqrt', 'tan'];
for (var i = 0; i < functions.length; i += 1) {
env[functions[i]] = Math[functions[i]];
}Function eval:
Evaluation function defines the steps of evaluation processes for all types of lines of codes. It represents the execution part of the interpreter. 9 cases are defined in the function. For example, if the line of code to executed is a 'define' function, line no 70 is executed.
Functions for parsing
Parsing is the first stage of the interpreter. It converts the input code into a set of tokens and reads according to the semantic meanings of the language. We use a number of functions for these actions which are listed below.
- parser() : Returnes the output of readFrom function.
- tokenize() : Converts the input code into a set of tokens. A set of built in functions are used here. s.replace() is used to replace the parenthesis with a space before and after it. s.trim() removes the spaces at the beginning and end of the expression. s.split() is applied to split the code after every space into a set of tokens.
- readFrom() : The functions takes the output of tokenize() function as input and forms a list avoiding all parenthesis and resembles the internal ordering of the expression.
- atom() : Converts integers in string format to numbers.
Function repl:
repl() represents the read evaluation print loop. The function is used to form an interactive Lisp interpreter.
Comments
Post a Comment