Building Interpreters
1 Language design
1.1 Lexical elements
- Reserved keywords: if,while,return, etc.
- Identifier for variables and functions
- Literals for numbers and strings
- Operators: +, -, *, /, …
- Comments
1.2 Expressions
- Arithmetic: a * (b + c)
- Logical: x && y || (!z)
- Comparison: a <= b
- Function calls: f(x, y)
1.3 Code
- Assignment: x = 10
- Flow control:
- conditions: if-else,switch-case, …
- loops: while,for
 
- conditions: 
- Function definitions
- Blocks of multiple statements
- Scope handling: local vs global scope
More on function definitions later.
1.4 Types
- Primitive types
- Arrays and records
- User defined types
- Type annotations
Note
funcDeclare
  : 'fun'
    IDENTIFIER
    '('
    parameter*
    ')' '{'
      block
    '}'
  ;
  
parameter
  : IDENTIFIER ':' type
  ;1.5 A complete program
A complete program is a sentence in the sentence of \(L(\mathrm{program})\).
A complete program
// funcDeclare
fun count(c:Char, message:[] Char) {
  n = 0;
  for(m in message) {
    if(m == c) {
      n = n + 1;
    }
  }
  n;
}
name = "Parsing and Compilation";
println(count('i', name));2 Runtime Environment
The runtime environment is where the program will be evaluated.
By executing the statements in the program, the interpreter will access the runtime environment in two ways:
- Modify the runtime environment:
- create or delete scopes
- create or modify symnbol bindings in scopes
 
- Modify persistent resources:
- modify memory content
- modify disk content
 
- Data transfer
- read data from input devices (e.g. keyboard, microphone, networks)
- write data to output devices (e.g. monitor, speaker, networks)
 
3 Interpreter
3.1 Interpreter Backend
- An interpreter backend has direct read/write access to the runtime environment.
- The backend can construct expressions, evaluate functions, and access I/O such as keyboard and monitor.
Key responsibilities of an interpreter backend include:
- Expression Evaluation: Computes arithmetic, logical, and function call expressions.
- Function Execution: Resolves function definitions, manages local scopes, and handles returns.
- Variable Binding: Reads and writes variables in the runtime environment.
- Control Flow Handling: Executes conditionals, loops, and recursion.
- I/O Operations: Reads user input, writes output, and interacts with external devices.
Warning
Unlike compilers, which translate code into machine instructions, an interpreter backend directly processes and executes the program without generating a separate binary.
3.2 Tree Traversal
The evaluation phase in an interpreter involves traversing the Abstract Syntax Tree (AST).
The interpreter performs the following using a technique known as syntax-directed definitions (SDD).
- computations,
- variable assignments,
- function calls,
- and control flow operations.