diff -r e66bd5c563eb -r b5b5583a3a08 progs/bf/bfc1.sc --- a/progs/bf/bfc1.sc Mon Jul 27 11:02:48 2020 +0100 +++ b/progs/bf/bfc1.sc Thu Jul 30 13:50:54 2020 +0100 @@ -2,8 +2,8 @@ //========================================= // // This version "optimises" the code by replacing -// for example +++ by (*ptr) += 3, instead of -// (*ptr)++, (*ptr)++, (*ptr)++ +// for example +++ by (*ptr) += 3, instead of three +// separate (*ptr)++, (*ptr)++, (*ptr)++ // // Call with // @@ -11,7 +11,7 @@ // -// generating "compound" c-instructions +// generate "compound" c-instructions def instr2(c: Char, n: Int) : String = c match { case '>' => s"ptr += $n ;" case '<' => s"ptr -= $n ;" @@ -40,7 +40,7 @@ def instrs2(prog: String) : String = splice(prog.toList, Nil).reverse.mkString -// adding the boilerplate +// adding boilerplate def compile(prog: String) : String = s"""#include #include @@ -51,16 +51,16 @@ ${instrs2(prog)} return 0;}""" -def compile_file(name: String, prog: String) = +def compile_to_file(name: String, prog: String) = os.write.over(os.pwd / name, compile(prog)) // running the c-compiler over the transpiled // BF program and running the resulting binary -def compile_run(prog: String) = { +def compile_and_run(prog: String) = { val tn = "tmp" - compile_file(s"${tn}.c", prog) + compile_to_file(s"${tn}.c", prog) os.proc("gcc", "-O0", "-o", tn, s"${tn}.c").call() // call gcc os.proc("./tmp").call(stdout = os.Inherit) // run binary } @@ -79,6 +79,6 @@ @main def main(fname: String) = { val bf_str = os.read(os.pwd / fname) - println(s"${time_needed(1, compile_run(bf_str))} secs") + println(s"${time_needed(1, compile_and_run(bf_str))} secs") }