|
| 1 | +import java.util.*; |
| 2 | +import fun.Absyn.*; |
| 3 | +import fun.PrettyPrinter; |
| 4 | + |
| 5 | +public class Interpreter { |
| 6 | + |
| 7 | + final Strategy strategy; |
| 8 | + |
| 9 | + final Map<String,Exp> sig = new TreeMap<String,Exp>(); |
| 10 | + |
| 11 | + public Interpreter(Strategy strategy) { |
| 12 | + this.strategy = strategy; |
| 13 | + } |
| 14 | + |
| 15 | + public void interpret(Program p) { |
| 16 | + switch(p) { |
| 17 | + case Prog prg -> { |
| 18 | + DMain main = (DMain)prg.main_; |
| 19 | + // Make sig from listdef_ |
| 20 | + for (var d : prg.listdef_) { |
| 21 | + DDef dd = (DDef)d; |
| 22 | + Exp e = dd.exp_; |
| 23 | + // [x1,x2] e --> new EAbs(x1, new EAbs(x2, e)) |
| 24 | + var xs = dd.listident_; |
| 25 | + Collections.reverse(xs); |
| 26 | + for (String x : xs) { |
| 27 | + e = new EAbs(x, e); |
| 28 | + } |
| 29 | + sig.put(dd.ident_, e); |
| 30 | + } |
| 31 | + |
| 32 | + // Evaluate main_ |
| 33 | + System.out.println(eval(new Env(), main.exp_).intValue()); |
| 34 | + } |
| 35 | + default -> { throw new RuntimeException("Interpreter not implemented yet"); } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Evaluate expressions |
| 40 | + |
| 41 | + Value eval(Env env, Exp e) { |
| 42 | + switch(e) { |
| 43 | + case EInt p -> { return new VInt(p.integer_); } |
| 44 | + case EAbs p -> { return new VFun(p.ident_, p.exp_, env); } |
| 45 | + case EApp p -> { |
| 46 | + Value v1 = eval(env, p.exp_1); |
| 47 | + Value v2 = eval(env, p.exp_2); |
| 48 | + return v1.apply(v2); |
| 49 | + } |
| 50 | + case EVar p -> { |
| 51 | + String x = p.ident_; |
| 52 | + Value v = env.lookup(x); |
| 53 | + if (v == null) { |
| 54 | + Exp f = sig.get(x); |
| 55 | + if (f == null) throw new RuntimeException("unbound identifier " + x); |
| 56 | + return eval(new Env(), f); |
| 57 | + } else return v; |
| 58 | + } |
| 59 | + default -> { throw new RuntimeException("not yet implemented" + PrettyPrinter.print(e)); } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Values |
| 64 | + |
| 65 | + abstract class Value { |
| 66 | + abstract public Integer intValue(); |
| 67 | + abstract public Value apply(Value arg); |
| 68 | + } |
| 69 | + |
| 70 | + class VInt extends Value { |
| 71 | + final Integer i; |
| 72 | + VInt(Integer i) { this.i = i; } |
| 73 | + public Integer intValue() { return i; } |
| 74 | + public Value apply(Value arg) { throw new RuntimeException("cannot apply number"); } |
| 75 | + } |
| 76 | + |
| 77 | + class VFun extends Value { |
| 78 | + final String x; |
| 79 | + final Exp body; |
| 80 | + final Env env; |
| 81 | + VFun (String x, Exp body, Env env) { this.x = x; this.body = body; this.env = env; } |
| 82 | + public Integer intValue() { throw new RuntimeException("function does not have intValue()"); } |
| 83 | + public Value apply (Value arg) { return eval(new Extend(x, arg, env), body); } |
| 84 | + } |
| 85 | + |
| 86 | + // Environments |
| 87 | + |
| 88 | + class Env { |
| 89 | + public Value lookup(String x) { return null; } |
| 90 | + } |
| 91 | + |
| 92 | + class Extend extends Env { |
| 93 | + final String x; |
| 94 | + final Value v; |
| 95 | + final Env env; |
| 96 | + Extend(String x, Value v, Env env) { this.x = x; this.v = v; this.env = env; } |
| 97 | + public Value lookup(String x) { |
| 98 | + if (x.equals(this.x)) return v; |
| 99 | + else return env.lookup(x); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments