progs/while-arrays/compile_bfc.sc
changeset 956 ae9782e62bdd
parent 949 61797848eede
child 958 fddf099a82f8
equal deleted inserted replaced
955:47acfd7f9096 956:ae9782e62bdd
    20 //
    20 //
    21 // Scala-cli is another REPL for scala. Unfortunately
    21 // Scala-cli is another REPL for scala. Unfortunately
    22 // fastparse used in this file is not yet supported
    22 // fastparse used in this file is not yet supported
    23 // under ammonite.
    23 // under ammonite.
    24 
    24 
    25 
       
    26 //> using toolkit latest
       
    27 //> using file compile_arrays2.sc
    25 //> using file compile_arrays2.sc
    28 import compile_arrays2._
    26 import compile_arrays2._
    29 
    27 
    30 def time_needed[T](i: Int, code: => T) = {
    28 def time_needed[T](i: Int, code: => T) = {
    31   val start = System.nanoTime()
    29   val start = System.nanoTime()
   118   P(  P(Fa ~ "*" ~ Te).map{ case (l, r) => Aop("*", l, r)} 
   116   P(  P(Fa ~ "*" ~ Te).map{ case (l, r) => Aop("*", l, r)} 
   119     | Fa )   
   117     | Fa )   
   120 def Fa[$ : P]: P[AExp] = 
   118 def Fa[$ : P]: P[AExp] = 
   121   P( "(" ~ AExp ~ ")" 
   119   P( "(" ~ AExp ~ ")" 
   122      | P (Ident ~ "[" ~ AExp ~ "]").map{Ref(_, _)}
   120      | P (Ident ~ "[" ~ AExp ~ "]").map{Ref(_, _)}
   123      | P(Number).map{Num} 
   121      | P(Number).map{Num(_)} 
   124      | P(Ident).map{Var} )
   122      | P(Ident).map{Var(_)} )
   125 
   123 
   126 // boolean expressions
   124 // boolean expressions
   127 def BExp[$ : P]: P[BExp] = 
   125 def BExp[$ : P]: P[BExp] = 
   128   P(  P(AExp ~ "=" ~ AExp).map{ case (x, z) => Bop("=", x, z)} 
   126   P(  P(AExp ~ "=" ~ AExp).map{ case (x, z) => Bop("=", x, z)} 
   129     | P(AExp ~ "!=" ~ AExp).map{ case (x, z) => Bop("!=", x, z)}  
   127     | P(AExp ~ "!=" ~ AExp).map{ case (x, z) => Bop("!=", x, z)}  
   139     | P(Ident ~ ":=" ~ AExp).map{Assign(_, _)} 
   137     | P(Ident ~ ":=" ~ AExp).map{Assign(_, _)} 
   140     | P(Ident ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp).map{AssignA(_, _, _)} 
   138     | P(Ident ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp).map{AssignA(_, _, _)} 
   141     | P("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block).map{If(_, _, _)} 
   139     | P("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block).map{If(_, _, _)} 
   142     | P("while" ~ BExp ~ "do" ~ Block).map{While(_, _)} 
   140     | P("while" ~ BExp ~ "do" ~ Block).map{While(_, _)} 
   143     | P("new(" ~ Ident ~ "[" ~ Number ~ "])").map{ArrayDef(_, _)} 
   141     | P("new(" ~ Ident ~ "[" ~ Number ~ "])").map{ArrayDef(_, _)} 
   144     | P("write(" ~ Ident ~ ")").map{Write} ) 
   142     | P("write(" ~ Ident ~ ")").map{Write(_)} ) 
   145 
   143 
   146 def Stmts[$ : P]: P[Block] =
   144 def Stmts[$ : P]: P[Block] =
   147   P(  P(Stmt ~ ";" ~ Stmts).map{ case (x, z) => x :: z } 
   145   P(  P(Stmt ~ ";" ~ Stmts).map{ case (x, z) => x :: z } 
   148     | P(Stmt).map{s => List(s)} ) 
   146     | P(Stmt).map{s => List(s)} ) 
   149 
   147 
   190   case '>' => "ptr := ptr + 1;"
   188   case '>' => "ptr := ptr + 1;"
   191   case '<' => "ptr := ptr - 1;"
   189   case '<' => "ptr := ptr - 1;"
   192   case '+' => "mem[ptr] := mem[ptr] + 1;"
   190   case '+' => "mem[ptr] := mem[ptr] + 1;"
   193   case '-' => "mem[ptr] := mem[ptr] - 1;"
   191   case '-' => "mem[ptr] := mem[ptr] - 1;"
   194   case '.' => "x := mem[ptr]; write x;"
   192   case '.' => "x := mem[ptr]; write x;"
   195   //case ',' => "XXX" // "ptr = getchar();\n"
       
   196   case '['  => "while (mem[ptr] != 0) do {"
   193   case '['  => "while (mem[ptr] != 0) do {"
   197   case ']'  => "skip};"
   194   case ']'  => "skip};"
   198   case _ => ""
   195   case _ => ""
   199 }
   196 }
   200 
   197 
   320 //
   317 //
   321 //@doc(" All benchmarks.")
   318 //@doc(" All benchmarks.")
   322 //@main
   319 //@main
   323 def all() = { bfc0(); bfc1(); bfc2(); bfc3(); bfc4() } 
   320 def all() = { bfc0(); bfc1(); bfc2(); bfc3(); bfc4() } 
   324 
   321 
   325 all()
   322 //all()
   326 
   323 bfc4()
   327 
   324 
   328 
   325 
   329 
   326 
   330 
   327 
   331 // old way to run it with Ammonite
   328 // old way to run it with Ammonite