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)
evalUn(op)(sp)
case Prim(op, lop, rop) =>
eval(a, sp)(env)
eval(b, sp + 1)(env)
eval(lop, sp)(env)
eval(rop, sp + 1)(env)
evalBin(sp, sp + 1)
case Let(x, a, b) =>
eval(a, sp)(env)
@ -275,7 +275,7 @@ class StackInterpreter extends Interpreter with BugReporter {
evalCond(op)(sp, sp + 1)
case If(cond, tBranch, eBranch) =>
eval(cond, sp)(env)
eval(if(flag)tBranch else eBranch, sp)
eval(if(flag)tBranch else eBranch, sp)(env)
case VarDec(x, rhs, body) =>
eval(rhs, sp)(env)
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() = {
val buf = new java.lang.StringBuilder
while (in.hasNext(isNum)) {
buf += in.next
while (in.hasNext(isDigit)) {
buf += in.next()
}
Number(buf.toString().toInt)
}
@ -589,7 +589,7 @@ class VariableParser(in: Scanner) extends BranchParser(in) {
val rhs = parseSimpleExpression
accept(';')
val body = parseExpression
Var(name, rhs, body).withPos(pos)
VarDec(name, rhs, body).withPos(pos)
case _ => parseSimpleExpression
}

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

Loading…
Cancel
Save