progs/while-arrays/compile_bfc.sc
changeset 949 61797848eede
parent 943 5365ef60707e
child 956 ae9782e62bdd
--- a/progs/while-arrays/compile_bfc.sc	Sun Oct 29 13:05:09 2023 +0000
+++ b/progs/while-arrays/compile_bfc.sc	Mon Oct 30 12:58:45 2023 +0000
@@ -13,15 +13,19 @@
 //       * the jasmin assembly file is 236k
 //       * the resulting Java program takes about 20 secs 
 //
-// Call with (X being 0,1,..,4)
+//
+// Call with scala-cli:
 //
-//  amm compile_bfc.sc all
-//  amm compile_bfc.sc bfcX
+//  scala-cli --dep com.lihaoyi::fastparse:3.0.2  compile_bfc.sc 
+//
+// Scala-cli is another REPL for scala. Unfortunately
+// fastparse used in this file is not yet supported
+// under ammonite.
 
 
-// load the compiler
-import $file.compile_arrays2
-import compile_arrays2._ 
+//> using toolkit latest
+//> using file compile_arrays2.sc
+import compile_arrays2._
 
 def time_needed[T](i: Int, code: => T) = {
   val start = System.nanoTime()
@@ -72,7 +76,7 @@
 
 
 def compile_to_file(bl: Block, class_name: String) : Unit = 
-  write.over(pwd / s"$class_name.j", compile(bl, class_name))  
+  os.write.over(os.pwd / s"$class_name.j", compile(bl, class_name))  
 
 def compile_and_run(bl: Block, class_name: String) : Unit = {
   println(s"Start of compilation")
@@ -92,13 +96,11 @@
 // Grammar Rules for WHILE with arrays
 //=====================================
 
-import $ivy.`com.lihaoyi::fastparse:3.0.2`
+//> using dep com.lihaoyi::fastparse:3.0.2
+
 import fastparse._
 import MultiLineWhitespace._
 
-def string[A: P]: P[String]   = P(CharIn("a-zA-Z0-9").rep(1).!)
-
-/*
 def lowercase [$ : P] = P( CharIn("a-z") )
 def uppercase[$ : P]  = P( CharIn("A-Z") )
 def letter[$ : P]     = P( lowercase | uppercase )
@@ -117,7 +119,7 @@
     | Fa )   
 def Fa[$ : P]: P[AExp] = 
   P( "(" ~ AExp ~ ")" 
-     | P (Ident ~ "[" ~ AExp ~ "]").map{Ref.tupled}
+     | P (Ident ~ "[" ~ AExp ~ "]").map{Ref(_, _)}
      | P(Number).map{Num} 
      | P(Ident).map{Var} )
 
@@ -134,11 +136,11 @@
 // statements and blocks
 def Stmt[$ : P]: P[Stmt] =
   P(  P("skip").map( _ => Skip) 
-    | P(Ident ~ ":=" ~ AExp).map{Assign.tupled} 
-    | P(Ident ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp).map{AssignA.tupled} 
-    | P("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block).map{If.tupled} 
-    | P("while" ~ BExp ~ "do" ~ Block).map{While.tupled} 
-    | P("new(" ~ Ident ~ "[" ~ Number ~ "])").map{ArrayDef.tupled} 
+    | P(Ident ~ ":=" ~ AExp).map{Assign(_, _)} 
+    | P(Ident ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp).map{AssignA(_, _, _)} 
+    | P("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block).map{If(_, _, _)} 
+    | P("while" ~ BExp ~ "do" ~ Block).map{While(_, _)} 
+    | P("new(" ~ Ident ~ "[" ~ Number ~ "])").map{ArrayDef(_, _)} 
     | P("write(" ~ Ident ~ ")").map{Write} ) 
 
 def Stmts[$ : P]: P[Block] =
@@ -259,12 +261,12 @@
 
 // a benchmark program (counts down from 'Z' to 'A')
 //@doc(" Benchmark 'Z' to 'A'.")
-@main
+//@main
 def bfc0() = bf_run(read(pwd / "benchmark.bf"), "bench")
 
 
 //@doc(" Sierpinski triangle.")
-@main
+//@main
 def bfc1() = bf_run(read(pwd / "sierpinski.bf"), "sier")
 
 // Hello World
@@ -272,7 +274,7 @@
       >>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."""
 
 //@doc(" Hello world.")
-@main
+//@main
 def bfc2() = bf_run(bf2, "hello")
 
 // Fibonacci 
@@ -290,7 +292,7 @@
       [-]++++++++++."""
 
 //@doc(" Fibonacci numbers.")
-@main
+//@main
 def bfc3() = bf_run(bf3, "fibs")
 
 // Mandelbrot Set
@@ -301,12 +303,12 @@
 // and approximately 30 seconds with Ammonite's fastparse.
 
 //@doc(" Mandelbrot set.")
-@main
+//@main
 def bfc4() = bf_run(read(pwd / "mandelbrot.bf"), "mandelbrot")
 
 
 // this unfortunately hits the capacity of the JVM, even with optimisations
-//@doc(" Coolatz serries up to 30.")
+//@doc(" Collatz series up to 30.")
 //@main
 //def bfc5() = bf_run(read(pwd / "collatz.bf"), "coll")
 
@@ -317,11 +319,26 @@
 
 //
 //@doc(" All benchmarks.")
-@main
+//@main
 def all() = { bfc0(); bfc1(); bfc2(); bfc3(); bfc4() } 
 
+all()
+
 
 
 
 
-*/
+// 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`
\ No newline at end of file