while.scala
changeset 73 27469183da75
parent 72 d65525aeca08
child 74 8f85d1f61663
equal deleted inserted replaced
72:d65525aeca08 73:27469183da75
   171             }"""
   171             }"""
   172 val p4_toks = Tok.fromString(p4) 
   172 val p4_toks = Tok.fromString(p4) 
   173 val p4_ast = Block.parse_all(p4_toks)
   173 val p4_ast = Block.parse_all(p4_toks)
   174 println(p4_ast)
   174 println(p4_ast)
   175 
   175 
   176 // fibonacci numbers
   176 val p5 = """
   177 val p5 = 
   177  n := 9;
   178 """{ n := 9;
   178  minus1 := 0;
   179      minus1 := 0;
   179  minus2 := 1;
   180      minus2 := 1;
   180  temp := 0;
   181      temp := 0;
   181  while n > 0 do  {
   182      while n > 0 do  {
   182    temp := minus2;
   183        temp := minus2;
   183    minus2 := minus1 + minus2;
   184        minus2 := minus1 + minus2;
   184    minus1 := temp;
   185        minus1 := temp;
   185    n := n - 1
   186        n := n - 1
   186  };
   187      };
   187  fib_res := minus2
   188      fib_res := minus2
       
   189   }
       
   190 """
   188 """
   191 val p5_toks = Tok.fromString(p5) 
   189 val p5_toks = Tok.fromString(p5) 
   192 val p5_ast = Block.parse_all(p5_toks)
   190 val p5_ast = Stmts.parse_all(p5_toks)
   193 
   191 
   194 // interpreter
   192 // interpreter
   195 type Env = Map[String, Int]
   193 type Env = Map[String, Int]
   196 
   194 
   197 def eval_bexp(b: BExp, env: Env) : Boolean = b match {
   195 def eval_bexp(b: BExp, env: Env) : Boolean = b match {