Reads a character and decides what action to take:
Of course, this is a simple representation and we haven't taken other considerations, such as escape characters (e.g. \), line continuation characters (e.g. \ or 1, 2 etc.), complex numbers etc. into account. Again, a lot of these depend on the language features that we are designing.
As you can see, the parser recursively descends into each stage. It also checks types and syntax rules as it goes.
Why design your own?
Scanner?? We don't need no steenkin' scanner for our language. We'll just pass it straight to the lexer to sort it out.
If the first item is 'quote', simply return the list unevaluated.
(quote 1 2 ax)
If the first item is 'define', then it is of the form:
Pop the var and expression from the array. Recursively evaluate expression and then set var in the symbol table to the result of the expression.
(define var expression)
If the first item is 'set!', then it is of the form:
Similar to handling define, except that it doesn't create a new variable in the symbol table, only replaces an existing symbol.
(set! var expression)
If the first item is 'if', then it is of the form:
Pop the condition, true_exp and false_exp from the array. Then, recursively evaluate the value of condition and depending on the return value, evaluate true_exp or false_exp.
(if condition true_exp false_exp)
If the first item is 'begin', then it is of the form:
Simply read the rest of the array items in a for loop and evaluate each one, then return the value of the last one.
(begin exp exp ...)
If the first item is 'lambda', then it is of the form:
Return an anonymous subroutine that creates a new symbol table based on the args and evaluates the expression.
(lambda (args) exp)
If the first item is none of the above, then it is of the form:
Evaluate this recursively (which will fetch it out of the symbol table) and pass it the arguments.
(proc_name arg1 arg2 ...)
This is implemented as a linked list of objects, each of which has a couple of member variables and methods.
The find() method works like this: