Interpreters
an interpreter does 3 things:
Reads input from the user in a specific programming language, translates the input to be computer readable and
Evaluates the result
Prints the result for the user
REPL - (Read -> Evaluate -> Print) Loop
2 languages involved:
Implemented language:
the 1 the user types in
Implementation language:
the 1 the interpreter is implemented in
Reading Input
Lexical analysis (LEXER)
turning the input into a collection of tokens
a token:
single input of the input string:
literals, names, keywords, delimiters
Syntactic analysis (PARSER)
turning tokens into a representation of the expression in the implementing language
Evaluating expressions
eval takes in 1 argument besides the expression itself - the current environment - consisting of the current frame and all its ancestors, until the Global frame
rules depend on the type
self-evaluating - booleans and numbers - use Python booleans and numbers
symbols - use Python strings
call expressions
Evaluating combinations
(<operator> <operand 1> <operand 2>)
if the operator is a symbol and is found in the dictionary of special forms, the combination is a special form
each special form has special rules for evaluation
otherwise the combination is a call expression
Evaluate the operator to get a procedure
Evaluate all of the operands from left to right
Apply the procedure to the values of the operands
Types of procedures
BUILT-IN: eg.: +, modulo, list, ... - predefined in the Scheme interpreter
User defined: defined with a lambda or a define expression
a list of formal parameters
a body (which is a Scheme list)
a parent frame
The evaluator
consists of 2 mutually recursive components
Last updated