12 // * parsing uses around 30 secs using fastparse |
12 // * parsing uses around 30 secs using fastparse |
13 // * the jasmin assembly file is 236k |
13 // * the jasmin assembly file is 236k |
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: |
18 // |
18 // |
19 // scala-cli --dep com.lihaoyi::fastparse:3.1.1 compile_bfc.sc |
19 // amm compile_bfc.sc |
20 // |
20 // |
21 // Scala-cli is another REPL for scala. Unfortunately |
21 |
22 // fastparse used in this file is not yet supported |
|
23 // under ammonite. |
|
24 |
22 |
25 // compile_arrays.sc (no peephole optimisations) |
23 // compile_arrays.sc (no peephole optimisations) |
26 // compile_arrays2.sc (peephole optimisations applied) |
24 // compile_arrays2.sc (peephole optimisations applied) |
27 |
25 |
28 //> using file compile_arrays2.sc |
26 import $file.compile_arrays2, compile_arrays2.* |
29 import compile_arrays2.* |
27 |
30 |
28 |
31 def time_needed[T](i: Int, code: => T) = { |
29 def time_needed[T](i: Int, code: => T) = { |
32 val start = System.nanoTime() |
30 val start = System.nanoTime() |
33 for (j <- 2 to i) code |
31 for (j <- 2 to i) code |
34 val result = code |
32 val result = code |
159 //println(fastparse.parse("x2 := 5 + a[3+a]", Stmts(_))) |
157 //println(fastparse.parse("x2 := 5 + a[3+a]", Stmts(_))) |
160 //println(fastparse.parse("a[x2+3] := 5 + a[3+a]", Stmts(_))) |
158 //println(fastparse.parse("a[x2+3] := 5 + a[3+a]", Stmts(_))) |
161 //println(fastparse.parse("{x := 5; y := 8}", Block(_))) |
159 //println(fastparse.parse("{x := 5; y := 8}", Block(_))) |
162 //println(fastparse.parse("if (false) then {x := 5} else {x := 10}", Block(_))) |
160 //println(fastparse.parse("if (false) then {x := 5} else {x := 10}", Block(_))) |
163 |
161 |
164 val fib = |
162 val fib_prog = |
165 """n := 10; |
163 """n := 10; |
166 minus1 := 0; |
164 minus1 := 0; |
167 minus2 := 1; |
165 minus2 := 1; |
168 temp:=0; |
166 temp:=0; |
169 while (n > 0) do { |
167 while (n > 0) do { |
258 compile_and_run(bf_prog, name) |
256 compile_and_run(bf_prog, name) |
259 } |
257 } |
260 |
258 |
261 // a benchmark program (counts down from 'Z' to 'A') |
259 // a benchmark program (counts down from 'Z' to 'A') |
262 //@doc(" Benchmark 'Z' to 'A'.") |
260 //@doc(" Benchmark 'Z' to 'A'.") |
263 //@main |
261 @main |
264 def bfc0() = bf_run(read(pwd / "benchmark.bf"), "bench") |
262 def bench() = bf_run(read(pwd / "benchmark.bf"), "bench") |
265 |
263 |
266 |
264 |
267 //@doc(" Sierpinski triangle.") |
265 //@doc(" Sierpinski triangle.") |
268 //@main |
266 @main |
269 def bfc1() = bf_run(read(pwd / "sierpinski.bf"), "sier") |
267 def sierpinski() = bf_run(read(pwd / "sierpinski.bf"), "sier") |
270 |
268 |
271 // Hello World |
269 // Hello World |
272 val bf2 = """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-] |
270 val bf2 = """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-] |
273 >>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.""" |
271 >>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.""" |
274 |
272 |
275 //@doc(" Hello world.") |
273 //@doc(" Hello world.") |
276 //@main |
274 @main |
277 def bfc2() = bf_run(bf2, "hello") |
275 def helloworld() = bf_run(bf2, "hello") |
278 |
276 |
279 // Fibonacci |
277 // Fibonacci |
280 val bf3 = """+++++++++++ |
278 val bf3 = """+++++++++++ |
281 >+>>>>++++++++++++++++++++++++++++++++++++++++++++ |
279 >+>>>>++++++++++++++++++++++++++++++++++++++++++++ |
282 >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> |
280 >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> |
289 <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< |
287 <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< |
290 [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-] |
288 [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-] |
291 [-]++++++++++.""" |
289 [-]++++++++++.""" |
292 |
290 |
293 //@doc(" Fibonacci numbers.") |
291 //@doc(" Fibonacci numbers.") |
294 //@main |
292 @main |
295 def bfc3() = bf_run(bf3, "fibs") |
293 def fib() = bf_run(bf3, "fibs") |
296 |
294 |
297 // Mandelbrot Set |
295 // Mandelbrot Set |
298 //---------------- |
296 //---------------- |
299 // |
297 // |
300 // Note: Parsing of the generated WHILE program (around 60K in size) |
298 // Note: Parsing of the generated WHILE program (around 60K in size) |
301 // takes approximately 10 minutes to parse with our parser combinators, |
299 // takes approximately 10 minutes to parse with our parser combinators, |
302 // and approximately 30 seconds with Ammonite's fastparse. |
300 // and approximately 30 seconds with Ammonite's fastparse. |
303 |
301 |
304 //@doc(" Mandelbrot set.") |
302 //@doc(" Mandelbrot set.") |
305 //@main |
303 @main |
306 def bfc4() = bf_run(read(pwd / "mandelbrot.bf"), "mandelbrot") |
304 def mandelbrot() = bf_run(read(pwd / "mandelbrot.bf"), "mandelbrot") |
307 |
305 |
308 |
306 |
309 // this unfortunately hits the capacity of the JVM, even with optimisations |
307 // this unfortunately hits the capacity of the JVM, even with optimisations |
310 //@doc(" Collatz series up to 30.") |
308 //@doc(" Collatz series up to 30.") |
311 //@main |
309 @main |
312 //def bfc5() = bf_run(read(pwd / "collatz.bf"), "coll") |
310 def collatz() = bf_run(read(pwd / "collatz.bf"), "coll") |
313 |
311 |
314 // this unfortunately hits the capacity of the JVM, even with optimisations |
312 // this unfortunately hits the capacity of the JVM, even with optimisations |
315 //@doc(" Towers of Hanoi.") |
313 //@doc(" Towers of Hanoi.") |
316 //@main |
314 @main |
317 //def bfc6() = bf_run(read(pwd / "hanoi.bf"), "hanoi") |
315 def hanoi() = bf_run(read(pwd / "hanoi.bf"), "hanoi") |
318 |
316 |
319 // |
317 // |
320 //@doc(" All benchmarks.") |
318 //@doc(" All benchmarks.") |
321 //@main |
319 @main |
322 def all() = { bfc0(); bfc1(); bfc2(); bfc3(); bfc4() } |
320 def all() = { bench(); sierpinski(); helloworld(); fib(); mandelbrot() } |
323 |
321 |
324 //all() |
322 |
325 //bfc4() |
323 |
326 |
324 |
327 def donut() = bf_run(read(pwd / "donut.bf"), "donut") |
|
328 donut() |
|
329 |
|
330 // old way to run it with Ammonite |
|
331 // |
|
332 // Call with (X being 0,1,..,4) |
|
333 // |
|
334 // amm compile_bfc.sc all |
|
335 // amm compile_bfc.sc bfcX |
|
336 // |
|
337 // |
|
338 // load the compiler |
|
339 //import $file.compile_arrays2 |
|
340 //import compile_arrays2._ |
|
341 // |
|
342 // load fastparse |
|
343 //import $ivy.`com.lihaoyi::fastparse:3.0.2` |
|