diff -r fdf87cccbf17 -r 5cadcc5aee02 progs/while-arrays/compile_bfc.sc --- a/progs/while-arrays/compile_bfc.sc Wed Nov 19 14:51:06 2025 +0000 +++ b/progs/while-arrays/compile_bfc.sc Wed Nov 19 16:37:32 2025 +0000 @@ -14,19 +14,17 @@ // * the resulting Java program takes about 20 secs // // -// Call with scala-cli: -// -// scala-cli --dep com.lihaoyi::fastparse:3.1.1 compile_bfc.sc +// Call with: // -// Scala-cli is another REPL for scala. Unfortunately -// fastparse used in this file is not yet supported -// under ammonite. +// amm compile_bfc.sc +// + // compile_arrays.sc (no peephole optimisations) // compile_arrays2.sc (peephole optimisations applied) -//> using file compile_arrays2.sc -import compile_arrays2.* +import $file.compile_arrays2, compile_arrays2.* + def time_needed[T](i: Int, code: => T) = { val start = System.nanoTime() @@ -161,7 +159,7 @@ //println(fastparse.parse("{x := 5; y := 8}", Block(_))) //println(fastparse.parse("if (false) then {x := 5} else {x := 10}", Block(_))) -val fib = +val fib_prog = """n := 10; minus1 := 0; minus2 := 1; @@ -175,7 +173,7 @@ write(result) """ -//println(fastparse.parse(fib, Stmts(_)).get.value) +//println(fastparse.parse(fib_prog, Stmts(_)).get.value) @@ -260,21 +258,21 @@ // a benchmark program (counts down from 'Z' to 'A') //@doc(" Benchmark 'Z' to 'A'.") -//@main -def bfc0() = bf_run(read(pwd / "benchmark.bf"), "bench") +@main +def bench() = bf_run(read(pwd / "benchmark.bf"), "bench") //@doc(" Sierpinski triangle.") -//@main -def bfc1() = bf_run(read(pwd / "sierpinski.bf"), "sier") +@main +def sierpinski() = bf_run(read(pwd / "sierpinski.bf"), "sier") // Hello World val bf2 = """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-] >>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.""" //@doc(" Hello world.") -//@main -def bfc2() = bf_run(bf2, "hello") +@main +def helloworld() = bf_run(bf2, "hello") // Fibonacci val bf3 = """+++++++++++ @@ -291,8 +289,8 @@ [-]++++++++++.""" //@doc(" Fibonacci numbers.") -//@main -def bfc3() = bf_run(bf3, "fibs") +@main +def fib() = bf_run(bf3, "fibs") // Mandelbrot Set //---------------- @@ -302,42 +300,25 @@ // and approximately 30 seconds with Ammonite's fastparse. //@doc(" Mandelbrot set.") -//@main -def bfc4() = bf_run(read(pwd / "mandelbrot.bf"), "mandelbrot") +@main +def mandelbrot() = bf_run(read(pwd / "mandelbrot.bf"), "mandelbrot") // this unfortunately hits the capacity of the JVM, even with optimisations //@doc(" Collatz series up to 30.") -//@main -//def bfc5() = bf_run(read(pwd / "collatz.bf"), "coll") +@main +def collatz() = bf_run(read(pwd / "collatz.bf"), "coll") // this unfortunately hits the capacity of the JVM, even with optimisations //@doc(" Towers of Hanoi.") -//@main -//def bfc6() = bf_run(read(pwd / "hanoi.bf"), "hanoi") +@main +def hanoi() = bf_run(read(pwd / "hanoi.bf"), "hanoi") // //@doc(" All benchmarks.") -//@main -def all() = { bfc0(); bfc1(); bfc2(); bfc3(); bfc4() } - -//all() -//bfc4() - -def donut() = bf_run(read(pwd / "donut.bf"), "donut") -donut() +@main +def all() = { bench(); sierpinski(); helloworld(); fib(); mandelbrot() } -// old way to run it with Ammonite -// -// Call with (X being 0,1,..,4) -// -// amm compile_bfc.sc all -// amm compile_bfc.sc bfcX -// -// -// load the compiler -//import $file.compile_arrays2 -//import compile_arrays2._ -// -// load fastparse -//import $ivy.`com.lihaoyi::fastparse:3.0.2` + + +