master
billsun 1 year ago
parent 5ec7101cc1
commit 441add2c71

@ -262,7 +262,7 @@ class StackInterpreter extends Interpreter with BugReporter {
case Prim(op, lop, rop) => case Prim(op, lop, rop) =>
eval(lop, sp)(env) eval(lop, sp)(env)
eval(rop, sp + 1)(env) eval(rop, sp + 1)(env)
evalBin(sp, sp + 1) evalBin(op)(sp, sp + 1)
case Let(x, a, b) => case Let(x, a, b) =>
eval(a, sp)(env) eval(a, sp)(env)
eval(b, sp + 1)(env.withVal(x, sp)) eval(b, sp + 1)(env.withVal(x, sp))

@ -128,7 +128,7 @@ class Scanner(in: Reader[Char]) extends Reader[Tokens.Token] with Reporter {
* TODO: implement the method * TODO: implement the method
*/ */
def getNum() = { def getNum() = {
val buf = new java.lang.StringBuilder val buf = new StringBuilder
while (in.hasNext(isDigit)) { while (in.hasNext(isDigit)) {
buf += in.next() buf += in.next()
} }
@ -596,7 +596,7 @@ class VariableParser(in: Scanner) extends BranchParser(in) {
// TODO: remove ??? and complete the implementation. // TODO: remove ??? and complete the implementation.
override def parseSimpleExpression = (in.peek, in.peek1) match { override def parseSimpleExpression = (in.peek, in.peek1) match {
case (Ident(x), Delim('=')) => case (Ident(x), Delim('=')) =>
val pos = in.pos val pos:Position = in.peek.pos
VarAssign(x, parseSimpleExpression).withPos(pos) VarAssign(x, parseSimpleExpression).withPos(pos)
case _ => super.parseExpression case _ => super.parseExpression
} }

@ -122,8 +122,8 @@ class SemanticAnalyzer(parser: Parser) extends Reporter {
case Prim(op, lop, rop) => // Done case Prim(op, lop, rop) => // Done
if(!(isOperator(op) || isBOperator(op))) if(!(isOperator(op) || isBOperator(op)))
error("undefined primitive operator") error("undefined primitive operator")
analyze(lop) analyze(lop)(env)
analyze(rop) analyze(rop)(env)
case Let(x, a, b) => // val x = a; b case Let(x, a, b) => // val x = a; b
// Done: check variable reuse // Done: check variable reuse
if(!env.vars.contains(x)) if(!env.vars.contains(x))
@ -137,7 +137,6 @@ 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)(env)
analyze(l)(env) analyze(l)(env)
analyze(r)(env) analyze(r)(env)
// Done // Done
@ -152,7 +151,7 @@ class SemanticAnalyzer(parser: Parser) extends Reporter {
analyze(body)(env) analyze(body)(env)
// TODO // TODO
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)(env) analyze(rhs)(env)
// TODO // TODO

Loading…
Cancel
Save