while.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 21 Nov 2012 07:28:28 +0000
changeset 69 cc3f7908b942
parent 68 a09ecb1e7384
child 70 e6868bd2942b
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
//:load matcher.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
// some regular expressions
68
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 66
diff changeset
     5
val SYM = RANGE("""ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_""")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 66
diff changeset
     6
val DIGIT = RANGE("0123456789")
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     7
val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     8
val NUM = PLUS(DIGIT)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     9
val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "begin", "end", "true", "false") 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    10
val SEMI: Rexp = ";"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    11
val OP: Rexp = ALTS(":=", "=", "+", "-", "*")
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
val WHITESPACE = PLUS(RANGE(" \n"))
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    13
val RPAREN: Rexp = ")"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    14
val LPAREN: Rexp = "("
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    15
val BEGIN: Rexp = "{"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    16
val END: Rexp = "}"
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
// for classifying the strings that have been recognised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
abstract class Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
case object T_WHITESPACE extends Token
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    21
case object T_SEMI extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    22
case object T_LPAREN extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    23
case object T_RPAREN extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    24
case object T_BEGIN extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    25
case object T_END extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    26
case class T_ID(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    27
case class T_OP(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    28
case class T_NUM(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    29
case class T_KWD(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    30
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
val lexing_rules: List[Rule[Token]] = 
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    33
  List((KEYWORD, (s) => T_KWD(s.mkString)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    34
       (ID, (s) => T_ID(s.mkString)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    35
       (OP, (s) => T_OP(s.mkString)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    36
       (NUM, (s) => T_NUM(s.mkString)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    37
       (SEMI, (s) => T_SEMI),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    38
       (LPAREN, (s) => T_LPAREN),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    39
       (RPAREN, (s) => T_RPAREN),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    40
       (BEGIN, (s) => T_BEGIN),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    41
       (END, (s) => T_END),
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
       (WHITESPACE, (s) => T_WHITESPACE))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
// the tokenizer
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    45
val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    46
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    47
// the abstract syntax trees
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    48
abstract class Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    49
abstract class AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    50
abstract class BExp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    51
type Block = List[Stmt]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    52
case object Skip extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    53
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    54
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    55
case class Assign(s: String, a: AExp) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    56
case class Var(s: String) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    57
case class Num(i: Int) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    58
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    59
case object True extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    60
case object False extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    61
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    62
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    64
case class TokParser(tok: Token) extends Parser[List[Token], Token] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    65
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    66
    case t::ts if (t == tok) => Set((t, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    67
    case _ => Set ()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    68
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    69
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    70
implicit def token2tparser(t: Token) = TokParser(t)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    71
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    72
case object NumParser extends Parser[List[Token], Int] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    73
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    74
    case T_NUM(s)::ts => Set((s.toInt, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    75
    case _ => Set ()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    76
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    77
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    78
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    79
case object IdParser extends Parser[List[Token], String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    80
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    81
    case T_ID(s)::ts => Set((s, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    82
    case _ => Set ()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    83
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    84
}
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    87
lazy val AExp: Parser[List[Token], AExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    88
  (T ~ T_OP("+") ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    89
  (T ~ T_OP("-") ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || T  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    90
lazy val T: Parser[List[Token], AExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    91
  (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || F
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    92
lazy val F: Parser[List[Token], AExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    93
  (T_LPAREN ~> AExp <~ T_RPAREN) || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    94
  IdParser ==> ((s) => Var(s)) || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    95
  NumParser ==> ((i) => Num(i))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    96
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    97
lazy val BExp: Parser[List[Token], BExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    98
  (AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    99
  (T_KWD("true") ==> ((_) => True: BExp)) || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   100
  (T_KWD("false") ==> ((_) => False: BExp))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   101
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   102
lazy val Stmt: Parser[List[Token], Stmt] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   103
  (T_KWD("skip") ==> ((_) => Skip: Stmt)) ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   104
  (IdParser ~ T_OP(":=") ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   105
  (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==>
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   106
    { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   108
lazy val Stmts: Parser[List[Token], Block] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   109
  (Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   110
  (Stmt ==> ((s) => List(s) : Block))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   111
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   112
lazy val Block: Parser[List[Token], Block] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   113
  (T_BEGIN ~> Stmts <~ T_END) || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   114
  (Stmt ==> ((s) => List(s)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   115
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   116
val p1 = "x := 5"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   117
val p1_toks = Tok.fromString(p1) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   118
val p1_ast = Block.parse_all(p1_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   119
println(p1_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   120
println(p1_ast)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   121
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   122
val p2 = "5 = 6"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   123
val p2_toks = Tok.fromString(p2) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   124
val p2_ast = BExp.parse_all(p2_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   125
println(p2_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   126
println(p2_ast)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   127
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   128
val p2a = "true"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   129
val p2a_toks = Tok.fromString(p2a) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   130
val p2a_ast = BExp.parse_all(p2a_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   131
println(p2a_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   132
println(p2a_ast)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   133
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   134
val p3 = "if true then skip else skip"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   135
val p3_toks = Tok.fromString(p3) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   136
val p3_ast = Stmt.parse_all(p3_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   137
println(p3_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   138
println(p3_ast)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   139
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   140
val p3a = "if true then x := 5 else x := 10"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   141
val p3a_toks = Tok.fromString(p3a) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   142
val p3a_ast = Stmt.parse_all(p3a_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   143
println(p3a_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   144
println(p3a_ast)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   145
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   146
val p3b = "if false then x := 5 else x := 10"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   147
val p3b_toks = Tok.fromString(p3b) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   148
val p3b_ast = Stmt.parse_all(p3b_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   149
println(p3b_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   150
println(p3b_ast)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   151
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   152
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   153
type Env = Map[String, Int]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   154
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   155
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   156
  case True => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   157
  case False => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   158
  case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
}
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   160
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   161
def eval_aexp(a: AExp, env : Env) : Int = a match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   162
  case Num(i) => i
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   163
  case Var(s) => env(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   164
  case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   165
  case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   166
  case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   167
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   168
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   169
def eval_stmt(s: Stmt, env: Env) : Env = s match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   170
  case Skip => env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   171
  case Assign(x, a) => env + (x -> eval_aexp(a, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   172
  case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   173
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   174
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   175
def eval_bl(bl: Block, env: Env) : Env = bl match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   176
  case Nil => env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   177
  case s::bl => eval_bl(bl, eval_stmt(s, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   178
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   179
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   180
//println(eval_stmt(p3a_ast.head, Nil.toMap))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   181
//println(eval_stmt(p3b_ast.head, Nil.toMap))