progs/while-arrays/compile_bfc.sc
changeset 974 0cb4bf2469d1
parent 958 fddf099a82f8
child 975 ae5c03560d4d
equal deleted inserted replaced
973:f25d338d16c9 974:0cb4bf2469d1
    14 //       * the resulting Java program takes about 20 secs 
    14 //       * the resulting Java program takes about 20 secs 
    15 //
    15 //
    16 //
    16 //
    17 // Call with scala-cli:
    17 // Call with scala-cli:
    18 //
    18 //
    19 //  scala-cli --dep com.lihaoyi::fastparse:3.0.2  compile_bfc.sc 
    19 //  scala-cli --dep com.lihaoyi::fastparse:3.1.1  compile_bfc.sc 
    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 // 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_arrays2.sc
    28 //> using file compile_arrays.sc
    29 import compile_arrays2._
    29 import compile_arrays.*
    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
    66   val instructions = compile_block(bl, Map())._1
    66   val instructions = compile_block(bl, Map())._1
    67   (beginning ++ instructions ++ ending).replace("XXX", class_name)
    67   (beginning ++ instructions ++ ending).replace("XXX", class_name)
    68 }
    68 }
    69 
    69 
    70 // post 2.5.0 ammonite
    70 // post 2.5.0 ammonite
    71 import os._
    71 import os.*
    72 
    72 
    73 
    73 
    74 def compile_to_file(bl: Block, class_name: String) : Unit = 
    74 def compile_to_file(bl: Block, class_name: String) : Unit = 
    75   os.write.over(os.pwd / s"$class_name.j", compile(bl, class_name))  
    75   os.write.over(os.pwd / s"$class_name.j", compile(bl, class_name))  
    76 
    76 
    90 
    90 
    91 //=====================================
    91 //=====================================
    92 // Grammar Rules for WHILE with arrays
    92 // Grammar Rules for WHILE with arrays
    93 //=====================================
    93 //=====================================
    94 
    94 
    95 //> using dep com.lihaoyi::fastparse:3.0.2
    95 //> using dep com.lihaoyi::fastparse:3.1.1
    96 
    96 
    97 import fastparse._
    97 import fastparse.*
    98 import MultiLineWhitespace._
    98 import MultiLineWhitespace.*
    99 
    99 
   100 def lowercase [$ : P] = P( CharIn("a-z") )
   100 def lowercase [$ : P] = P( CharIn("a-z") )
   101 def uppercase[$ : P]  = P( CharIn("A-Z") )
   101 def uppercase[$ : P]  = P( CharIn("A-Z") )
   102 def letter[$ : P]     = P( lowercase | uppercase )
   102 def letter[$ : P]     = P( lowercase | uppercase )
   103 def digit [$ : P]     = P( CharIn("0-9") )
   103 def digit [$ : P]     = P( CharIn("0-9") )
   247 def bf_run(prog: String, name: String) = {
   247 def bf_run(prog: String, name: String) = {
   248   println(s"BF pre-processing of $name")
   248   println(s"BF pre-processing of $name")
   249   val bf_string = bf_str(prog)
   249   val bf_string = bf_str(prog)
   250   println(s"BF parsing (program length ${bf_string.length} characters)")
   250   println(s"BF parsing (program length ${bf_string.length} characters)")
   251   val (time, bf_prog) = 
   251   val (time, bf_prog) = 
   252     time_needed(1, fastparse.parse(bf_string, Stmts(_)).get.value)
   252     time_needed(1, fastparse.parse(bf_string, implicit p  => Stmts).get.value)
   253   println(s"BF generated WHILE program (needed $time secs for parsing)")
   253   println(s"BF generated WHILE program (needed $time secs for parsing)")
   254   compile_and_run(bf_prog, name)
   254   compile_and_run(bf_prog, name)
   255 }
   255 }
   256 
   256 
   257 // a benchmark program (counts down from 'Z' to 'A')
   257 // a benchmark program (counts down from 'Z' to 'A')