Docsity

Interpreting Language Programs: Low-Level and High-Level Interpretation - Prof. Jingke Li, Study notes of Computer Science

Toggle sidebar
bg1
Language Interpreters
Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 Language Interpreters 1 / 18
Language Interpreters
An interpreter reads in a source program and executes it immediately.
โ€ขAdvantages โ€” Easier to write than compilers, and more portable
โ€ขDrawbacks โ€” Slower than running compiled code
Levels of Interpretation:
โ€ขLow-Level Language (Including IR Code) Interpretation:
โ€“ program is just a simple sequence of instructions
โ€“ single name-space, although function calls have local data
โ€ขHigh-Level Language Interpretation:
โ€“ program structure can be complex: declarations, data types,
modules, exceptions, etc.
โ€“ multiple name-spaces, with variables
Jingke Li (Portland State University) CS322 Language Interpreters 2 / 18
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Interpreting Language Programs: Low-Level and High-Level Interpretation - Prof. Jingke Li and more Study notes Computer Science in PDF only on Docsity!

Language Interpreters

Jingke Li

Portland State University

Jingke Li (Portland State University) CS322 Language Interpreters 1 / 18

Language Interpreters

An interpreter reads in a source program and executes it immediately.

  • Advantages โ€” Easier to write than compilers, and more portable
  • Drawbacks โ€” Slower than running compiled code

Levels of Interpretation:

  • Low-Level Language (Including IR Code) Interpretation:
    • program is just a simple sequence of instructions
    • single name-space, although function calls have local data
  • (^) High-Level Language Interpretation:
    • program structure can be complex: declarations, data types,

modules, exceptions, etc.

  • multiple name-spaces, with variables

Interpreting Low-Level Languages

The interpreter needs to manage a simple memory model and executes a

fetch-execute cycle:

while (there are still instructions) { fetch the next instruction; execute this instruction; }

Example: Interpreting an assembly language for a machine with only two

registers (and no functions).

Opcode Instruction Meaning 0 LOAD d ACC โ† word at address d 1 STORE d word at address d โ† ACC 2 MOVE d ACC โ† d 3 ADD d ACC โ† ACC+ word at address d 4 SUB d ACC โ† ACCโˆ’ word at address d 5 JUMP d PC โ† d 6 JUMPZ d PC โ† d, if ACC = 0 7 HALT stop execution

Jingke Li (Portland State University) CS322 Language Interpreters 3 / 18

The Data Structures

public class Instruction { public static final byte LOAD=0, STORE=1, MOVE=2, ADD=3, SUB=4, JUMP=5, JUMPZ=6, HALT=7; public byte op; public short d; }

public class State { public static final short CODESIZE=4096, DATASIZE=4096; public static final byte RUNNING=0, HALT=1, FAILED=2; public Instruction[] code = new Instruction[CODESIZE]; public short[] data = new short[DATASIZE]; public short PC, ACC; public byte status; }

Handling Function Calls (cont.)

void interpFunc(FUNC f) { sp = sp - f.varCnt - f.argCnt - 1; // allocate an AR on the stack do { // the main interpretation loop Instruction inst = code[PC++]; short d = inst.d; switch (inst.op) { ... case CALL: for (int i=0; i<args.size(); i++) mem[sp+i+1] = ; mem[sp] = fp; fp = sp; interpFunc(g); sp = fp; fp = mem[sp]; ... } } while (status == RUNNING); sp = sp + f.varCnt + f.argCnt + 1; // de-allocate the AR }

Jingke Li (Portland State University) CS322 Language Interpreters 7 / 18

Interpreting High-Level Languages

New Issues: declarations and scopes

The interpreter needs to manage a symbol table to keep information

around, and the interpretation actions need to take place in an

environment.

Env Program Text ฯƒ 0 --- ฯƒ 1 --- function f(a:int, b:int, c:int) = { print int(a+c); ฯƒ 2 --- { var j := a+b; ฯƒ 3 --- var a := "hello"; print int(a); print int(b); } ฯƒ 1 --- print int(c); } ฯƒ 0 ---

ฯƒ 0 = base environment (empty) ฯƒ 2 = ฯƒ 1 + {j 7 โ†’ int} ฯƒ 1 = ฯƒ 0 + {a 7 โ†’ int, b 7 โ†’ int, c 7 โ†’ int} ฯƒ 3 = ฯƒ 2 + {a 7 โ†’ string}

Environments

For imperative languages, there is only one active environment at any time

during a programโ€™s execution. This active environment corresponds to the

set of visible bindings of the nested active scopes.

For other languages, several environments can be active at the same time.

Example: Simultaneous active environments in an OOP program.

Env Program Text package M; class E ฯƒ 1 --- { static int a=5; } class N ฯƒ 3 --- { static int b=10; static int a=E.a+b; } class D ฯƒ 5 --- { static int d=E.a + N.a; }

ฯƒ 1 = {a 7 โ†’ int} ฯƒ 2 = {E 7 โ†’ ฯƒ 1 } ฯƒ 3 = {b 7 โ†’ int, a 7 โ†’ int} ฯƒ 4 = {N 7 โ†’ ฯƒ 3 } ฯƒ 5 = {d 7 โ†’ int} ฯƒ 6 = {D 7 โ†’ ฯƒ 5 } ฯƒ 7 = ฯƒ 2 + ฯƒ 4 + ฯƒ 6

Jingke Li (Portland State University) CS322 Language Interpreters 9 / 18

A Simple Expression Language

prog -> exp exp -> NUM exp -> VAR exp -> exp โ€™+โ€™ exp exp -> LET VAR โ€™=โ€™ exp IN exp END

A program in this language:

let a = 2 in 14 + let b = a + 3 in b + 7 end end

which should evaluate to 26.

A Second Language

Adding functions.

prog -> exp exp -> NUM exp -> VAR exp -> exp โ€™+โ€™ exp exp -> LET VAR โ€™=โ€™ exp IN exp END exp -> FN VAR "=>" exp exp -> exp exp

A program in this language:

let add2 = fn x "=>" x + 2 in 14 + let b = add2 3 in b + 7 end end

which should evaluate to 26.

Jingke Li (Portland State University) CS322 Language Interpreters 13 / 18

Interpreting the Second Language

Production Attributes prog -> exp exp.env := empty; prog.val := exp.val; exp -> NUM exp.val := NUM.num; exp -> VAR exp.val := lookup(exp.env, VAR.id); exp -> exp1 โ€™+โ€™ exp2 exp1.env := exp.env; exp2.env := exp.env; exp.val := exp1.val + exp2.val; exp -> LET VAR โ€™=โ€™ exp1 exp1.env := exp.env; IN exp2 END exp2.env := extend(exp.env, VAR.id, exp1.val); exp.val := exp2.val; exp -> FN VAR "=>" exp1 exp.val := closure(VAR.id, exp1, exp.env); exp -> exp1 exp2 exp1.env := exp.env; exp2.env := exp.env; exp.val := eval(exp1.val.body, extend(exp1.val.env, exp1.val.formal, exp2.val));

A function closure consists of formal, body, and env.

Interpreting the Second Language (cont.)

prog -> {env := empty;} exp {prog.val := exp.val;} exp -> NUM {exp.val := NUM.num;} exp -> VAR {exp.val := lookup(env, VAR.id);} exp -> exp1 โ€™+โ€™ exp2 {exp.val := exp1.val + exp2.val;} exp -> LET VAR โ€™=โ€™ exp1 {env := extend(env, VAR.id, exp1.val);} IN exp2 END {env := retract(env, VAR.id); exp.val := exp2.val;} exp -> FN VAR "=>" exp1 {exp.val := closure(VAR.id, exp1, env);} exp -> exp1 {c := exp1.val;} exp2 {actual := exp2.val; push-stack(env; env := extend(c.env, c.formal, actual); exp.val := c.body.val; env := pop-stack();}

Jingke Li (Portland State University) CS322 Language Interpreters 15 / 18

An Implementation in C

typedef char* id; typedef struct ExpS *Exp; struct ExpS { enum {Num,Var,Plus,Print,Let,Assign} kind; union { struct {int n;} num; struct {id v;} var; struct {Exp e1,e2;} plus; struct {Exp e;} print; struct {id v; Exp e1,e2;} let; struct {int v; Exp e;} assign; } u; }