--- a/progs/bf/bfc0.sc Mon Jul 27 11:02:48 2020 +0100
+++ b/progs/bf/bfc0.sc Thu Jul 30 13:50:54 2020 +0100
@@ -7,7 +7,9 @@
//
//
// Note: An interesting exercise is to call
-// gcc with -O3 instead of -O0
+// gcc with -O3 instead of -O0 (see invocation
+// below).
+
// simple instructions
def instr(c: Char) : String = c match {
@@ -25,7 +27,7 @@
def instrs(prog: String) : String =
prog.toList.map(instr(_)).mkString
-// adding the boilerplate
+// adding boilerplate
def compile(prog: String) : String =
s"""#include <string.h>
#include <stdio.h>
@@ -36,16 +38,17 @@
${instrs(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
}
@@ -64,7 +67,7 @@
@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")
}