|
|
|
@ -88,11 +88,11 @@ class Scanner(in: Reader[Char]) extends Reader[Tokens.Token] with Reporter {
|
|
|
|
|
|
|
|
|
|
// List of delimiters
|
|
|
|
|
// TODO: Update this as delimiters are added to our language
|
|
|
|
|
val isDelim = Set('(',')')
|
|
|
|
|
val isDelim = Set('(', ')', ';', '{', '}')
|
|
|
|
|
|
|
|
|
|
// List of keywords
|
|
|
|
|
// TODO: Update this as keywords are added to our language
|
|
|
|
|
val isKeyword = Set[String]("val", "var")
|
|
|
|
|
val isKeyword = Set[String]("val", "var", "if", "else", "while")
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Extract a name from the stream
|
|
|
|
@ -429,9 +429,12 @@ class ArithParser(in: Scanner) extends Parser(in) {
|
|
|
|
|
def parseExpression(min: Int): Exp = {
|
|
|
|
|
var res = parseUAtom
|
|
|
|
|
// TODO: complete DONE?
|
|
|
|
|
if (in.hasNext(isOperator)) {
|
|
|
|
|
while (
|
|
|
|
|
in.hasNext(isOperator) &&
|
|
|
|
|
isInfixOp(min)(in.peek)
|
|
|
|
|
) {
|
|
|
|
|
val (op, pos) = getOperator
|
|
|
|
|
Prim(op, res, parseUAtom).withPos(pos)
|
|
|
|
|
res = Prim(op, res, parseExpression(prec(op))).withPos(pos)
|
|
|
|
|
}
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
@ -538,7 +541,9 @@ class BranchParser(in: Scanner) extends LetParser(in) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: remove ??? and complete the implementation.
|
|
|
|
|
override def parseSimpleExpression: Exp = in.peek match {
|
|
|
|
|
override def parseSimpleExpression: Exp = {
|
|
|
|
|
print(in.peek)
|
|
|
|
|
in.peek match {
|
|
|
|
|
case Keyword("if") =>
|
|
|
|
|
val pos = in.next().pos
|
|
|
|
|
accept('(')
|
|
|
|
@ -546,6 +551,7 @@ class BranchParser(in: Scanner) extends LetParser(in) {
|
|
|
|
|
accept(')')
|
|
|
|
|
val if_stmt = parseSimpleExpression
|
|
|
|
|
if (in.hasNext(_ == Keyword("else"))) {
|
|
|
|
|
in.next()
|
|
|
|
|
val else_stmt = parseSimpleExpression
|
|
|
|
|
If(cond, if_stmt, else_stmt).withPos(pos)
|
|
|
|
|
}
|
|
|
|
@ -553,6 +559,7 @@ class BranchParser(in: Scanner) extends LetParser(in) {
|
|
|
|
|
case _ => super.parseSimpleExpression
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We can now introduce mutable variables using the 'var' keyword.
|
|
|
|
@ -596,9 +603,10 @@ class VariableParser(in: Scanner) extends BranchParser(in) {
|
|
|
|
|
// TODO: remove ??? and complete the implementation.
|
|
|
|
|
override def parseSimpleExpression = (in.peek, in.peek1) match {
|
|
|
|
|
case (Ident(x), Delim('=')) =>
|
|
|
|
|
val pos:Position = in.peek.pos
|
|
|
|
|
val pos:Position = in.next().pos
|
|
|
|
|
accept('=')
|
|
|
|
|
VarAssign(x, parseSimpleExpression).withPos(pos)
|
|
|
|
|
case _ => super.parseExpression
|
|
|
|
|
case _ => super.parseSimpleExpression
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -635,6 +643,7 @@ class LoopParser(in: Scanner) extends VariableParser(in) {
|
|
|
|
|
val cond = parseCondition
|
|
|
|
|
accept(')')
|
|
|
|
|
val simp = parseSimpleExpression
|
|
|
|
|
accept(';')
|
|
|
|
|
val body = parseExpression
|
|
|
|
|
While(cond, simp, body).withPos(pos)
|
|
|
|
|
case _ =>
|
|
|
|
|