progs/while/compile.sc
changeset 811 86406d70d6f0
parent 810 ba25eaf6043c
child 812 2f9a0dcf61ae
equal deleted inserted replaced
810:ba25eaf6043c 811:86406d70d6f0
   148      l"$loop_end", env1)
   148      l"$loop_end", env1)
   149   }
   149   }
   150   case Write(x) => 
   150   case Write(x) => 
   151     (i"iload ${env(x)} \t\t; $x" ++ 
   151     (i"iload ${env(x)} \t\t; $x" ++ 
   152      i"invokestatic XXX/XXX/write(I)V", env)
   152      i"invokestatic XXX/XXX/write(I)V", env)
   153   case Read(x) => {
   153   //case Read(x) => {
   154     val index = env.getOrElse(x, env.keys.size) 
   154   //  val index = env.getOrElse(x, env.keys.size) 
   155     (i"invokestatic XXX/XXX/read()I" ++ 
   155   //  (i"invokestatic XXX/XXX/read()I" ++ 
   156      i"istore $index \t\t; $x", env + (x -> index))
   156   //   i"istore $index \t\t; $x", env + (x -> index))
   157   }
   157   //}
   158 }
   158 }
   159 
   159 
   160 // compilation of a block (i.e. list of instructions)
   160 // compilation of a block (i.e. list of instructions)
   161 def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
   161 def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
   162   case Nil => ("", env)
   162   case Nil => ("", env)
   177 
   177 
   178 
   178 
   179 
   179 
   180 // Fibonacci numbers as a bare-bone test-case
   180 // Fibonacci numbers as a bare-bone test-case
   181 val fib_test = 
   181 val fib_test = 
   182   List(Assign("n", Num(9)),            //  n := 10;                     
   182   List(Assign("n", Num(9)),            //  n := 9;                     
   183        Assign("minus1",Num(0)),         //  minus1 := 0;
   183        Assign("minus1",Num(0)),         //  minus1 := 0;
   184        Assign("minus2",Num(1)),         //  minus2 := 1;
   184        Assign("minus2",Num(1)),         //  minus2 := 1;
   185        Assign("temp",Num(0)),           //  temp := 0;
   185        Assign("temp",Num(0)),           //  temp := 0;
   186        While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
   186        While(Bop("<",Num(0),Var("n")),  //  while 0 < n do  {
   187           List(Assign("temp",Var("minus2")), //  temp := minus2;
   187           List(Assign("temp",Var("minus2")), //  temp := minus2;
   188                Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
   188                Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
   189                                         //  minus2 := minus1 + minus2;
   189                                         //  minus2 := minus1 + minus2;
   190                Assign("minus1",Var("temp")), //  minus1 := temp;
   190                Assign("minus1",Var("temp")), //  minus1 := temp;
   191                Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
   191                Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };