From 5ec7101cc17b36f777eb832408aa35f644ad8e2c Mon Sep 17 00:00:00 2001 From: billsun Date: Sat, 2 Sep 2023 00:59:23 +0000 Subject: [PATCH] first version that runs --- src/main/scala/project2/Interpreter.scala | 6 +++--- src/main/scala/project2/Parser.scala | 6 +++--- src/main/scala/project2/SemanticAnalyzer.scala | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/scala/project2/Interpreter.scala b/src/main/scala/project2/Interpreter.scala index 9d5761a..88338c6 100644 --- a/src/main/scala/project2/Interpreter.scala +++ b/src/main/scala/project2/Interpreter.scala @@ -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)) diff --git a/src/main/scala/project2/Parser.scala b/src/main/scala/project2/Parser.scala index 7cf64b1..e189f23 100644 --- a/src/main/scala/project2/Parser.scala +++ b/src/main/scala/project2/Parser.scala @@ -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 } diff --git a/src/main/scala/project2/SemanticAnalyzer.scala b/src/main/scala/project2/SemanticAnalyzer.scala index 112cad0..1241697 100644 --- a/src/main/scala/project2/SemanticAnalyzer.scala +++ b/src/main/scala/project2/SemanticAnalyzer.scala @@ -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") }