diff -r 1f4e81950ab4 -r 9476086849ad progs/comb2.scala --- a/progs/comb2.scala Mon Nov 14 15:50:42 2016 +0000 +++ b/progs/comb2.scala Sat Jan 07 14:52:26 2017 +0000 @@ -1,3 +1,6 @@ +// A parser and evaluator for the while language +// + import scala.language.implicitConversions import scala.language.reflectiveCalls @@ -125,12 +128,12 @@ // boolean expressions lazy val BExp: Parser[String, BExp] = - ((AExp ~ "=" ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } || - (AExp ~ "!=" ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || - (AExp ~ "<" ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || - (AExp ~ ">" ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z): BExp } || - ("true" ==> ((_) => True: BExp)) || - ("false" ==> ((_) => False: BExp)) || + ((AExp ~ "=" ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z):BExp } || + (AExp ~ "!=" ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z):BExp } || + (AExp ~ "<" ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z):BExp } || + (AExp ~ ">" ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z):BExp } || + ("true" ==> ((_) => True:BExp )) || + ("false" ==> ((_) => False:BExp )) || ("(" ~ BExp ~ ")") ==> { case ((x, y), z) => y}) lazy val Stmt: Parser[String, Stmt] = @@ -149,7 +152,7 @@ (Stmt ==> ((s) => List(s)))) -Block.parse_all("x2:=5") +Stmt.parse_all("x2:=5") Block.parse_all("{x:=5;y:=8}") Block.parse_all("if(false)then{x:=5}else{x:=10}") @@ -193,4 +196,4 @@ def eval(bl: Block) : Env = eval_bl(bl, Map()) -eval(Block.parse_all(fib).head)("result") +println(eval(Block.parse_all(fib).head)("result"))