DV1565 / DV1511:

Compiler and Interpreter Technology

Thursday, January 26th, 2017

Interpreters

Table of Contents

1. Architectures for program execution

Two general models of system architecture (a.k.a computer organisation).

Harvard Architecture (late '30s)Von Neumann Archtecture (late '40s)
Separated program and data (memories)Single unified memory
The machine we pretend we useThe machine that we use

2. Simulation

3. Simulation: instructions or trees

4. Simulation: interpreter

5. Simulation: interpreter structure

6. Interpreter Control

Evaluate expressions.Walk sub-tree containing expression parse.
Assign values to variables.Evaluate expression, store in memory.
Evaluate conditions.Walk (boolean) expression sub-tree
Perform control-flow.Change the walk over the tree.
Handle builtins.Some node evaluations call into the runtime.

7. Environments (virtual memory)

8. Environments : Data model

9. Environments: Data model

10. Evaluation of expressions

int evalIntConstant(Tree *node) { return std::stoi(node->text); } // Always demonstrate the easy case.
int evalIntVariable(Environment *scope, Tree *node) { return scope->read(node->text); } // Hide any complexity somewhere else.

11. Evaluation of expressions

class AddNode : public Node { int evalInt(Environment *scope) { return this->leftChild.evalInt(scope) + this->rightChild.evalInt(scope); } }; // I am assuming that evaluation cannot update the scope.

12. Execution of statements

class Node { ... virtual Environment *execute(Environment *)=0; ... }; // Return type differs between two interfaces.

13. Execution: assignment statements

class AssignNode : public Node { Environment *execute(Environment *e) { e->update( this->leftChild, this->rightChild.evaluate(e) ); return e; }
%3 identx ident : x identy ident : y assign assign assign->identx add add assign->add add->identy const const : 3 add->const

Break (15mins)





Intermission

15. Executing sequences

foreach child in stmt node E = execute(child,E)
%3 assign assign stmt1 stmt1 assign->stmt1 stmt2 stmt2 assign->stmt2 stmt3 stmt3 assign->stmt3

16. Executing sequences

17. Executing if-then-else

v = eval(cond,E) if v is True: E = execute(child1,E) else: E = execute(child2,E)

18. Executing while loops

while eval(cond,E) is True: E = execute(child,E)

19. Other looping constructs

X; while(Y) { W; Z; }

20. Executing calls

21. Executing calls

function sqrt(float val) { ... }

22. Executing calls.

23. Runtimes

24. Summary