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