progs/while-arrays/compile_bfc.sc
changeset 1005 0ffb6e4de10a
parent 975 ae5c03560d4d
equal deleted inserted replaced
1004:99e89ad35d76 1005:0ffb6e4de10a
    23 // under ammonite.
    23 // under ammonite.
    24 
    24 
    25 // compile_arrays.sc (no peephole optimisations)
    25 // compile_arrays.sc (no peephole optimisations)
    26 // compile_arrays2.sc (peephole optimisations applied)
    26 // compile_arrays2.sc (peephole optimisations applied)
    27 
    27 
    28 //> using file compile_arrays.sc
    28 //> using file compile_arrays2.sc
    29 import compile_arrays.*
    29 import compile_arrays2.*
    30 
    30 
    31 def time_needed[T](i: Int, code: => T) = {
    31 def time_needed[T](i: Int, code: => T) = {
    32   val start = System.nanoTime()
    32   val start = System.nanoTime()
    33   for (j <- 2 to i) code
    33   for (j <- 2 to i) code
    34   val result = code
    34   val result = code
   190   case '+' => "mem[ptr] := mem[ptr] + 1;"
   190   case '+' => "mem[ptr] := mem[ptr] + 1;"
   191   case '-' => "mem[ptr] := mem[ptr] - 1;"
   191   case '-' => "mem[ptr] := mem[ptr] - 1;"
   192   case '.' => "x := mem[ptr]; write x;"
   192   case '.' => "x := mem[ptr]; write x;"
   193   case '['  => "while (mem[ptr] != 0) do {"
   193   case '['  => "while (mem[ptr] != 0) do {"
   194   case ']'  => "skip};"
   194   case ']'  => "skip};"
       
   195   case '0' => "mem[ptr] := 0;"
   195   case _ => ""
   196   case _ => ""
   196 }
   197 }
   197 
   198 
   198 def instrs(prog: String) : String =
   199 def instrs(prog: String) : String =
   199   prog.toList.map(instr).mkString
   200   prog.toList.map(instr).mkString
   247 
   248 
   248 
   249 
   249 def bf_run(prog: String, name: String) = {
   250 def bf_run(prog: String, name: String) = {
   250   println(s"BF pre-processing of $name")
   251   println(s"BF pre-processing of $name")
   251   val bf_string = bf_str(prog)
   252   val bf_string = bf_str(prog)
       
   253   os.write.over(os.pwd / s"$name.while", bf_string)
   252   println(s"BF parsing (program length ${bf_string.length} characters)")
   254   println(s"BF parsing (program length ${bf_string.length} characters)")
   253   val (time, bf_prog) = 
   255   val (time, bf_prog) = 
   254     time_needed(1, fastparse.parse(bf_string, implicit p  => Stmts).get.value)
   256     time_needed(1, fastparse.parse(bf_string, implicit p  => Stmts).get.value)
   255   println(s"BF generated WHILE program (needed $time secs for parsing)")
   257   println(s"BF generated WHILE program (needed $time secs for parsing)")
   256   compile_and_run(bf_prog, name)
   258   compile_and_run(bf_prog, name)
   318 //@doc(" All benchmarks.")
   320 //@doc(" All benchmarks.")
   319 //@main
   321 //@main
   320 def all() = { bfc0(); bfc1(); bfc2(); bfc3(); bfc4() } 
   322 def all() = { bfc0(); bfc1(); bfc2(); bfc3(); bfc4() } 
   321 
   323 
   322 //all()
   324 //all()
   323 bfc4()
   325 //bfc4()
   324 
   326 
   325 
   327 def donut() = bf_run(read(pwd / "donut.bf"), "donut")
   326 
   328 donut()
   327 
   329 
   328 // old way to run it with Ammonite
   330 // old way to run it with Ammonite
   329 //
   331 //
   330 // Call with (X being 0,1,..,4)
   332 // Call with (X being 0,1,..,4)
   331 //
   333 //