first version that runs

master
billsun 1 year ago
parent 4a2fbdaa22
commit 5ec7101cc1

@ -260,8 +260,8 @@ class StackInterpreter extends Interpreter with BugReporter {
eval(v, sp)(env) eval(v, sp)(env)
evalUn(op)(sp) evalUn(op)(sp)
case Prim(op, lop, rop) => case Prim(op, lop, rop) =>
eval(a, sp)(env) eval(lop, sp)(env)
eval(b, sp + 1)(env) eval(rop, sp + 1)(env)
evalBin(sp, sp + 1) evalBin(sp, sp + 1)
case Let(x, a, b) => case Let(x, a, b) =>
eval(a, sp)(env) eval(a, sp)(env)
@ -275,7 +275,7 @@ class StackInterpreter extends Interpreter with BugReporter {
evalCond(op)(sp, sp + 1) evalCond(op)(sp, sp + 1)
case If(cond, tBranch, eBranch) => case If(cond, tBranch, eBranch) =>
eval(cond, sp)(env) eval(cond, sp)(env)
eval(if(flag)tBranch else eBranch, sp) eval(if(flag)tBranch else eBranch, sp)(env)
case VarDec(x, rhs, body) => case VarDec(x, rhs, body) =>
eval(rhs, sp)(env) eval(rhs, sp)(env)
eval(body, sp + 1)(env.withVal(x, sp)) eval(body, sp + 1)(env.withVal(x, sp))

@ -129,8 +129,8 @@ class Scanner(in: Reader[Char]) extends Reader[Tokens.Token] with Reporter {
*/ */
def getNum() = { def getNum() = {
val buf = new java.lang.StringBuilder val buf = new java.lang.StringBuilder
while (in.hasNext(isNum)) { while (in.hasNext(isDigit)) {
buf += in.next buf += in.next()
} }
Number(buf.toString().toInt) Number(buf.toString().toInt)
} }
@ -589,7 +589,7 @@ class VariableParser(in: Scanner) extends BranchParser(in) {
val rhs = parseSimpleExpression val rhs = parseSimpleExpression
accept(';') accept(';')
val body = parseExpression val body = parseExpression
Var(name, rhs, body).withPos(pos) VarDec(name, rhs, body).withPos(pos)
case _ => parseSimpleExpression case _ => parseSimpleExpression
} }

@ -137,9 +137,9 @@ class SemanticAnalyzer(parser: Parser) extends Reporter {
case Cond(op, l, r) => case Cond(op, l, r) =>
if (!isBOperator(op)) if (!isBOperator(op))
error(s"$op is not a boolean operator") error(s"$op is not a boolean operator")
analyze(op) analyze(op)(env)
analyze(l) analyze(l)(env)
analyze(r) analyze(r)(env)
// Done // Done
case If(cond, tBranch, eBranch) => case If(cond, tBranch, eBranch) =>
analyze(cond)(env) analyze(cond)(env)
@ -154,12 +154,12 @@ class SemanticAnalyzer(parser: Parser) extends Reporter {
case VarAssign(x, rhs) => // x = rhs case VarAssign(x, rhs) => // x = rhs
if (!(env.vars.contains(x) && env.vars[x])) if (!(env.vars.contains(x) && env.vars[x]))
error(s"cannot assign $x: ${if(env.vars.contains(x))"immutable"else"undefined"}") error(s"cannot assign $x: ${if(env.vars.contains(x))"immutable"else"undefined"}")
analyze(rhs) analyze(rhs)(env)
// TODO // TODO
case While(cond, lBody, body) => case While(cond, lBody, body) =>
analyze(cond) analyze(cond)(env)
analyze(lBody) analyze(lBody)(env)
analyze(body) analyze(body)(env)
// TODO // TODO
case _ => abort(s"unknown AST node $exp") case _ => abort(s"unknown AST node $exp")
} }

Loading…
Cancel
Save