--- a/while1.scala Fri Nov 23 21:21:27 2012 +0000
+++ b/while1.scala Sat Nov 24 07:08:51 2012 +0000
@@ -8,7 +8,7 @@
val DIGIT = RANGE("0123456789")
val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT)))
val NUM = PLUS(DIGIT)
-val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "true", "false", "print")
+val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "true", "false", "write")
val SEMI: Rexp = ";"
val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!", "<", ">")
val WHITESPACE = PLUS(RANGE(" \n"))
@@ -57,7 +57,7 @@
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
case class While(b: BExp, bl: Block) extends Stmt
case class Assign(s: String, a: AExp) extends Stmt
-case class Print(s: String) extends Stmt
+case class Write(s: String) extends Stmt
case class Var(s: String) extends AExp
case class Num(i: Int) extends AExp
@@ -104,12 +104,13 @@
// boolean expressions
lazy val BExp: Parser[List[Token], BExp] =
+ (T_KWD("true") ==> ((_) => True: BExp)) ||
+ (T_KWD("false") ==> ((_) => False: BExp)) ||
+ (T_LPAREN ~> BExp <~ T_RPAREN) ||
(AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } ||
(AExp ~ T_OP("!=") ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } ||
(AExp ~ T_OP("<") ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } ||
- (AExp ~ T_OP(">") ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z): BExp } ||
- (T_KWD("true") ==> ((_) => True)) ||
- (T_KWD("false") ==> ((_) => False: BExp))
+ (AExp ~ T_OP(">") ~ AExp) ==> { case ((x, y), z) => Bop("<", z, x): BExp }
lazy val Stmt: Parser[List[Token], Stmt] =
(T_KWD("skip") ==> ((_) => Skip: Stmt)) ||
@@ -117,7 +118,7 @@
(T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==>
{ case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } ||
(T_KWD("while") ~ BExp ~ T_KWD("do") ~ Block) ==> { case (((x, y), z), w) => While(y, w) } ||
- (T_KWD("print") ~ IdParser) ==> { case (x, y) => Print(y) }
+ (T_KWD("write") ~ IdParser) ==> { case (x, y) => Write(y) }
lazy val Stmts: Parser[List[Token], Block] =
(Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
@@ -135,8 +136,9 @@
case False => false
case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
- case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
+ case Bop("&&", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
+ case Bop("||", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
}
def eval_aexp(a: AExp, env : Env) : Int = a match {
@@ -154,7 +156,7 @@
case While(b, bl) =>
if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
else env
- case Print(x) => { println(env(x)); env }
+ case Write(x) => { println(env(x)); env }
}
def eval_bl(bl: Block, env: Env) : Env = bl match {
@@ -164,11 +166,21 @@
def eval_prog(name: String) : Env = {
val tks = Tok.fromFile(name)
- val ast = Stmts.parse_all(tks).head
+ val ast = Stmts.parse_single(tks)
eval_bl(ast, Map.empty)
}
//examples
-eval_prog("fib.while")
+eval_prog("loops.while")
+//eval_prog("fib.while")
+
+
+
+
+
+
+
+
+