





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Portland State University
Jingke Li (Portland State University) CS322 Language Interpreters 1 / 18
while (there are still instructions) { fetch the next instruction; execute this instruction; }
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
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; }
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] =
Jingke Li (Portland State University) CS322 Language Interpreters 7 / 18
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}
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
prog -> exp exp -> NUM exp -> VAR exp -> exp โ+โ exp exp -> LET VAR โ=โ exp IN exp END
let a = 2 in 14 + let b = a + 3 in b + 7 end end
prog -> exp exp -> NUM exp -> VAR exp -> exp โ+โ exp exp -> LET VAR โ=โ exp IN exp END exp -> FN VAR "=>" exp exp -> exp exp
let add2 = fn x "=>" x + 2 in 14 + let b = add2 3 in b + 7 end end
Jingke Li (Portland State University) CS322 Language Interpreters 13 / 18
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.
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
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; }