|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
package project2
|
|
|
|
|
|
|
|
|
|
import java.awt.RenderingHints.Key
|
|
|
|
|
|
|
|
|
|
// Class used to carry position information within the source code
|
|
|
|
|
case class Position(gapLine: Int, gapCol: Int, startLine: Int, startCol: Int, endLine: Int, endCol: Int)
|
|
|
|
|
|
|
|
|
@ -426,7 +428,11 @@ class ArithParser(in: Scanner) extends Parser(in) {
|
|
|
|
|
def parseExpression: Exp = parseExpression(0)
|
|
|
|
|
def parseExpression(min: Int): Exp = {
|
|
|
|
|
var res = parseUAtom
|
|
|
|
|
// TODO: complete
|
|
|
|
|
// TODO: complete DONE?
|
|
|
|
|
if (in.hasNext(isOperator)) {
|
|
|
|
|
val (op, pos) = getOperator
|
|
|
|
|
Prim(op, res, parseUAtom).withPos(pos)
|
|
|
|
|
}
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -469,7 +475,7 @@ class LetParser(in: Scanner) extends ArithParser(in) {
|
|
|
|
|
case Ident(x) =>
|
|
|
|
|
val (_, pos) = getName
|
|
|
|
|
Ref(x).withPos(pos)
|
|
|
|
|
// TODO: remove ??? and add the implementation for this case
|
|
|
|
|
// TODO: remove ??? and add the implementation for this case DONE
|
|
|
|
|
case _ => abort(s"Illegal start of simple expression")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -532,7 +538,20 @@ class BranchParser(in: Scanner) extends LetParser(in) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: remove ??? and complete the implementation.
|
|
|
|
|
override def parseSimpleExpression = ???
|
|
|
|
|
override def parseSimpleExpression: Exp = in.peek match {
|
|
|
|
|
case Keyword("if") =>
|
|
|
|
|
val pos = in.next().pos
|
|
|
|
|
accept('(')
|
|
|
|
|
val cond = parseCondition
|
|
|
|
|
accept(')')
|
|
|
|
|
val if_stmt = parseSimpleExpression
|
|
|
|
|
if (in.hasNext(_ == Keyword("else"))) {
|
|
|
|
|
val else_stmt = parseSimpleExpression
|
|
|
|
|
If(cond, if_stmt, else_stmt).withPos(pos)
|
|
|
|
|
}
|
|
|
|
|
else expected("missing else statement")
|
|
|
|
|
case _ => super.parseSimpleExpression
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@ -562,12 +581,24 @@ class VariableParser(in: Scanner) extends BranchParser(in) {
|
|
|
|
|
|
|
|
|
|
// TODO: remove ??? and complete the implementation.
|
|
|
|
|
override def parseExpression = in.peek match {
|
|
|
|
|
case _ => ???
|
|
|
|
|
case Keyword("val") => super.parseExpression
|
|
|
|
|
case Keyword("var") =>
|
|
|
|
|
in.next()
|
|
|
|
|
val (name, pos) = getName
|
|
|
|
|
accept('=')
|
|
|
|
|
val rhs = parseSimpleExpression
|
|
|
|
|
accept(';')
|
|
|
|
|
val body = parseExpression
|
|
|
|
|
Var(name, rhs, body).withPos(pos)
|
|
|
|
|
case _ => parseSimpleExpression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: remove ??? and complete the implementation.
|
|
|
|
|
override def parseSimpleExpression = (in.peek, in.peek1) match {
|
|
|
|
|
case _ => ???
|
|
|
|
|
case (Ident(x), Delim('=')) =>
|
|
|
|
|
val pos = in.pos
|
|
|
|
|
VarAssign(x, parseSimpleExpression).withPos(pos)
|
|
|
|
|
case _ => super.parseExpression
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -597,5 +628,16 @@ class LoopParser(in: Scanner) extends VariableParser(in) {
|
|
|
|
|
import Tokens._
|
|
|
|
|
|
|
|
|
|
// TODO: remove ??? and complete the implementation.
|
|
|
|
|
override def parseExpression = ???
|
|
|
|
|
override def parseExpression = in.peek match {
|
|
|
|
|
case Keyword("while") =>
|
|
|
|
|
val pos = in.next().pos
|
|
|
|
|
accept('(')
|
|
|
|
|
val cond = parseCondition
|
|
|
|
|
accept(')')
|
|
|
|
|
val simp = parseSimpleExpression
|
|
|
|
|
val body = parseExpression
|
|
|
|
|
While(cond, simp, body).withPos(pos)
|
|
|
|
|
case _ =>
|
|
|
|
|
super.parseExpression
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|