progs/comb2.scala
changeset 685 75d9f9e5906f
parent 684 1ee523c4f098
child 686 05cfce0fdef7
equal deleted inserted replaced
684:1ee523c4f098 685:75d9f9e5906f
   101     case Some(s) => Set(sb.splitAt(s.length))
   101     case Some(s) => Set(sb.splitAt(s.length))
   102   }
   102   }
   103 }
   103 }
   104 
   104 
   105 lazy val AExp: Parser[String, AExp] = 
   105 lazy val AExp: Parser[String, AExp] = 
   106   (Te ~ "+" ~ AExp) ==> { case x ~ y ~ z => Aop("+", x, z): AExp } ||
   106   (Te ~ "+" ~ AExp) ==> { case x ~ _ ~ z => Aop("+", x, z): AExp } ||
   107   (Te ~ "-" ~ AExp) ==> { case x ~ y ~ z => Aop("-", x, z): AExp } || Te 
   107   (Te ~ "-" ~ AExp) ==> { case x ~ _ ~ z => Aop("-", x, z): AExp } || Te 
   108 lazy val Te: Parser[String, AExp] = 
   108 lazy val Te: Parser[String, AExp] = 
   109   (Fa ~ "*" ~ Te) ==> { case x ~ y ~ z => Aop("*", x, z): AExp } || 
   109   (Fa ~ "*" ~ Te) ==> { case x ~ _ ~ z => Aop("*", x, z): AExp } || 
   110   (Fa ~ "/" ~ Te) ==> { case x ~ y ~ z => Aop("/", x, z): AExp } || Fa  
   110   (Fa ~ "/" ~ Te) ==> { case x ~ _ ~ z => Aop("/", x, z): AExp } || Fa  
   111 lazy val Fa: Parser[String, AExp] = 
   111 lazy val Fa: Parser[String, AExp] = 
   112    ("(" ~ AExp ~ ")") ==> { case x ~ y ~ z => y } || 
   112    ("(" ~ AExp ~ ")") ==> { case _ ~ y ~ _ => y } || 
   113    IdParser ==> Var || 
   113    IdParser ==> Var || 
   114    NumParser ==> Num
   114    NumParser ==> Num
   115 
   115 
   116 // boolean expressions with some simple nesting
   116 // boolean expressions with some simple nesting
   117 lazy val BExp: Parser[String, BExp] = 
   117 lazy val BExp: Parser[String, BExp] = 
   118    (AExp ~ "==" ~ AExp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || 
   118    (AExp ~ "==" ~ AExp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || 
   119    (AExp ~ "!=" ~ AExp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
   119    (AExp ~ "!=" ~ AExp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
   120    (AExp ~ "<" ~ AExp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || 
   120    (AExp ~ "<" ~ AExp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || 
   121    (AExp ~ ">" ~ AExp) ==> { case x ~ _ ~ z => Bop(">", x, z): BExp } ||
   121    (AExp ~ ">" ~ AExp) ==> { case x ~ _ ~ z => Bop(">", x, z): BExp } ||
   122    ("(" ~ BExp ~ ")" ~ "&&" ~ BExp) ==> { case x ~ y ~ z ~ u ~ v => And(y, v): BExp } ||
   122    ("(" ~ BExp ~ ")" ~ "&&" ~ BExp) ==> { case _ ~ y ~ _ ~ _ ~ v => And(y, v): BExp } ||
   123    ("(" ~ BExp ~ ")" ~ "||" ~ BExp) ==> { case x ~ y ~ z ~ u ~ v => Or(y, v): BExp } ||
   123    ("(" ~ BExp ~ ")" ~ "||" ~ BExp) ==> { case _ ~ y ~ _ ~ _ ~ v => Or(y, v): BExp } ||
   124    ("true" ==> (_ => True: BExp )) || 
   124    ("true" ==> (_ => True: BExp )) || 
   125    ("false" ==> (_ => False: BExp )) ||
   125    ("false" ==> (_ => False: BExp )) ||
   126    ("(" ~ BExp ~ ")") ==> { case _ ~ x ~ _ => x }
   126    ("(" ~ BExp ~ ")") ==> { case _ ~ x ~ _ => x }
   127 
   127 
   128 // statement / statements
   128 // statement / statements
   129 lazy val Stmt: Parser[String, Stmt] =
   129 lazy val Stmt: Parser[String, Stmt] =
   130   (("skip" ==> (_ => Skip: Stmt)) ||
   130   (("skip" ==> (_ => Skip: Stmt)) ||
   131    (IdParser ~ ":=" ~ AExp) ==> { case x ~ y ~ z => Assign(x, z): Stmt } ||
   131    (IdParser ~ ":=" ~ AExp) ==> { case x ~ _ ~ z => Assign(x, z): Stmt } ||
   132    ("write(" ~ IdParser ~ ")") ==> { case x ~y ~ z => Write(y): Stmt } ||
   132    ("write(" ~ IdParser ~ ")") ==> { case _ ~ y ~ _ => Write(y): Stmt } ||
   133    ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
   133    ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
   134     { case x ~ y ~ z ~ u ~ v ~ w => If(y, u, w): Stmt } ||
   134     { case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w): Stmt } ||
   135    ("while" ~ BExp ~ "do" ~ Block) ==> { case x ~ y ~ z ~ w => While(y, w) }) 
   135    ("while" ~ BExp ~ "do" ~ Block) ==> { case _ ~ y ~ _ ~ w => While(y, w) }) 
   136  
   136  
   137 lazy val Stmts: Parser[String, Block] =
   137 lazy val Stmts: Parser[String, Block] =
   138   (Stmt ~ ";" ~ Stmts) ==> { case x ~ y ~ z => x :: z : Block } ||
   138   (Stmt ~ ";" ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } ||
   139   (Stmt ==> ( s => List(s) : Block))
   139   (Stmt ==> ( s => List(s) : Block))
   140 
   140 
   141 // blocks (enclosed in curly braces)
   141 // blocks (enclosed in curly braces)
   142 lazy val Block: Parser[String, Block] =
   142 lazy val Block: Parser[String, Block] =
   143   (("{" ~ Stmts ~ "}") ==> { case x ~ y ~ z => y} || 
   143   (("{" ~ Stmts ~ "}") ==> { case x ~ y ~ z => y} ||