Skip to content

Commit

Permalink
Merge pull request #22 from hawkw/switch
Browse files Browse the repository at this point in the history
Switch
  • Loading branch information
hawkw committed Oct 10, 2014
2 parents a88fc93 + 51b3f68 commit 2296679
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/main/scala/decaf/frontend/DecafAST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ trait DecafAST {
}
}

case class SwitchStmt(variable: Expr, cases: List[CaseStmt], default: DefaultCase) extends Stmt(None) {
variable.parent = this
case class SwitchStmt(variable: Option[Expr], cases: List[CaseStmt], default: Option[DefaultCase]) extends Stmt(None) {
if (variable.isDefined) { variable.get.parent = this }
cases.foreach{c => c.parent = this}
default.parent = this
if (default.isDefined) { default.get.parent = this }
def stringifyChildren(indentLevel: Int): String = {
variable.stringify(indentLevel + 1) +
(if (variable.isDefined) { variable.get.stringify(indentLevel + 1) } else {""}) +
cases.foldLeft[String](""){
(acc, c) => acc + c.stringify(indentLevel + 1)
} +
default.stringify(indentLevel + 1)
(if (default.isDefined) { default.get.stringify(indentLevel + 1) } else {""})
}
}
case class CaseStmt(value: Expr, body: List[Stmt]) extends Stmt(None) {
Expand Down
50 changes: 24 additions & 26 deletions src/main/scala/decaf/frontend/DecafSyntactical.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
}
}

lazy val program: PackratParser[Program] = (
decl.+ ^^ {
lazy val program: PackratParser[Program] = decl.+ ^^ {
case decls => new Program(decls)
})
}

lazy val decl: PackratParser[Decl] = (
( variableDecl <~ Delimiter(";") )
Expand Down Expand Up @@ -151,7 +150,7 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
}

lazy val switchStmt: P[Stmt] =
Keyword("switch") ~> Delimiter("(") ~> expr ~ Delimiter(")") ~ Delimiter("{") ~ rep(caseStmt) ~ defaultCase <~ Delimiter("}") ^^{
Keyword("switch") ~> Delimiter("(") ~> expr.? ~ Delimiter(")") ~ Delimiter("{") ~ rep(caseStmt) ~ defaultCase.? <~ Delimiter("}") ^^{
case value ~ Delimiter(")") ~ Delimiter("{") ~ cases ~ default => SwitchStmt(value, cases, default)
}
lazy val caseStmt: P[CaseStmt] = Keyword("case") ~> expr ~ Delimiter(":") ~ stmt.* ^^{
Expand All @@ -161,7 +160,7 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
case body => DefaultCase(body)
}

lazy val expr: P[Expr] = ( indirect ||| logical )
lazy val expr: P[Expr] = indirect ||| logical
lazy val indirect: P[Expr] = (
assign
||| arrayAccess
Expand All @@ -175,7 +174,9 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
}

lazy val call: P[Expr] = (
(rexpr ||| fieldAccess ||| indirect) ~ Delimiter(".") ~ ident ~ fnargs ^^ { case base ~ _ ~ field ~ args => new Call(base.getPos, base, field, args) }
(rexpr ||| fieldAccess ||| indirect) ~ Delimiter(".") ~ ident ~ fnargs ^^ {
case base ~ _ ~ field ~ args => new Call(base.getPos, base, field, args)
}
||| ident ~ fnargs ^^ { case field ~ args => new Call(field.getPos, field, args) }

||| Keyword("ReadLine") ~ Delimiter("(") ~ Delimiter(")") ^^{
Expand All @@ -186,15 +187,13 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
}
)

lazy val arrayAccess: P[Expr] = (
(fieldAccess ||| indirect) ~ Delimiter("[") ~ expr ~ Delimiter("]") ^^ {case first ~ _ ~ last ~ _ => ArrayAccess(first.getPos, first, last)}
)
lazy val arrayAccess: P[Expr] = (fieldAccess ||| indirect) ~ Delimiter("[") ~ expr ~ Delimiter("]") ^^ {
case first ~ _ ~ last ~ _ => ArrayAccess(first.getPos, first, last)
}

lazy val assign: P[Expr] = (
assignable ~ Operator("=") ~ expr ^^{
lazy val assign: P[Expr] = assignable ~ Operator("=") ~ expr ^^{
case left ~ _ ~ right => AssignExpr(left.getPos, left, right)
}
)
lazy val assignable: P[Expr] = arrayAccess ||| fieldAccess

lazy val fieldAccess: P[Expr] = (
Expand All @@ -212,8 +211,8 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
lazy val exprNew: P[Expr] = (
Keyword("new") ~ ident ^^
{ case Keyword("new") ~ i => NewExpr(i.getPos, NamedType(i)) }
| Keyword("NewArray") ~ Delimiter("(") ~ expr ~ Delimiter(",") ~ typ ~ Delimiter(")") ^^
{ case Keyword("NewArray") ~ Delimiter("(") ~ e ~ Delimiter(",") ~ t ~ Delimiter(")") => NewArrayExpr(e.getPos,e,t) }
| ( Keyword("NewArray") ~ Delimiter("(") ) ~> expr ~ Delimiter(",") ~ typ <~ Delimiter(")") ^^
{ case e ~ Delimiter(",") ~ t => NewArrayExpr(e.getPos,e,t) }
)
/*
lazy val tlmath: P[Expr] = (
Expand Down Expand Up @@ -259,8 +258,9 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
)


lazy val unary: P[Expr] = (
( Operator("!") ~ unaryRHS ^^{ case op ~ e => LogicalExpr(op.getPos, None, ASTOperator(op.getPos, "!"), e)}
lazy val unary: P[Expr] = ( Operator("!") ~ unaryRHS ^^{
case op ~ e => LogicalExpr(op.getPos, None, ASTOperator(op.getPos, "!"), e)
}
| Operator("-") ~ unaryRHS ^^{
case op ~ e => ArithmeticExpr(op.getPos, ASTIntConstant(op.getPos, 0), ASTOperator(op.getPos, "-"), e)}
/*
Expand All @@ -271,16 +271,14 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
* unary minus and we can just handle it as any other subtraction operation.
*/
)||| unaryRHS
)

lazy val unaryRHS: P[Expr] = (
rexpr ||| (const
lazy val unaryRHS: P[Expr] = rexpr ||| (const
| exprThis
| exprNew
| indirect)
| indirect
)

lazy val rexpr: P[Expr] = ( Delimiter("(") ~> expr <~ Delimiter(")") )
lazy val rexpr: P[Expr] = Delimiter("(") ~> expr <~ Delimiter(")")
lazy val const: PackratParser[Expr] = (
elem("intConst", _.isInstanceOf[IntConstant])
| elem("doubleConst", _.isInstanceOf[DoubleConstant])
Expand All @@ -299,9 +297,9 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
case t ~ e => VarDecl(e, t)
}

lazy val ident: PackratParser[ASTIdentifier] = (
_ident ^^{ case i: Identifier => ASTIdentifier(Some(i.getPos), i.value)}
)
lazy val ident: PackratParser[ASTIdentifier] = _ident ^^{
case i: Identifier => ASTIdentifier(Some(i.getPos), i.value)
}

lazy val _ident: PackratParser[Elem] = acceptIf(_.isInstanceOf[Identifier])("Identifier token expected but " + _ + " found")

Expand All @@ -324,8 +322,8 @@ class DecafSyntactical extends Parsers with DecafAST with DecafTokens with Packr
)

lazy val classDecl: P[Decl] =
Keyword("class") ~> ident ~ opt(extendPart) ~ opt(implementsPart) ~ Delimiter("{") ~ rep(field) ~ Delimiter("}") ^^{
case name ~ ext ~ imp ~ Delimiter("{") ~ fields ~ Delimiter("}") => ClassDecl(name, ext, imp.getOrElse(Nil), fields)
Keyword("class") ~> ident ~ opt(extendPart) ~ opt(implementsPart) ~ Delimiter("{") ~ rep(field) <~ Delimiter("}") ^^{
case name ~ ext ~ imp ~ Delimiter("{") ~ fields => ClassDecl(name, ext, imp.getOrElse(Nil), fields)
}
lazy val extendPart: P[NamedType] = Keyword("extends") ~> className
lazy val implementsPart: P[List[NamedType]] = Keyword("implements") ~> repsep(className, Delimiter(","))
Expand Down

0 comments on commit 2296679

Please sign in to comment.