progs/bf/bfc0.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 24 Jul 2020 12:58:19 +0100
changeset 738 084e2843f478
parent 736 d3e477fe6c66
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
738
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     1
// A Transpiler for the Brainf*** language to C
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     2
//===============================================
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     3
//
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     4
// Call with
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     5
//
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     6
//  amm bfc0.sc <<bf_program.bf>>
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     7
//
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
738
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     9
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
import scala.util._
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
// loding a bf-file 
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
def load_bff(name: String) : String = 
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("")
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// simple instructions
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def instr(c: Char) : String = c match {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  case '>' => "ptr++;"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  case '<' => "ptr--;"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  case '+' => "(*ptr)++;"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  case '-' => "(*ptr)--;"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  case '.' => "putchar(*ptr);"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  case ',' => "*ptr = getchar();\n"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case '['  => "while(*ptr){"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case ']'  => "}"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  case _ => ""
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
def instrs(prog: String) : String =
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  prog.toList.map(instr(_)).mkString
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
def compile_str(prog: String) : String = {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  "#include <string.h>\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  "#include <stdio.h>\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  "char field[30000];\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  "char *ptr = &field[15000];" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  "int main()\n{\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  "memset(field, '\\0', 30000);\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  instrs(prog) ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  "\n return 0;\n}"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
def compile(name: String, prog: String) = {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  val fw = new java.io.FileWriter(name + ".c") 
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  val is = compile_str(prog)
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  //println(is)
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  fw.write(is) 
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  fw.close()
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 634
diff changeset
    54
// running the c-compiler over the transpiled
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 634
diff changeset
    55
// BF program and running the result
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
import sys.process._
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
def compile_run(prog: String) = {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  compile("tmp", prog)
638
0367aa7c764b updated
Christian Urban <urbanc@in.tum.de>
parents: 637
diff changeset
    60
  "gcc -O3 -o tmp tmp.c".!
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  "./tmp".!
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  ()
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
def time_needed[T](n: Int, code: => T) = {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  val start = System.nanoTime()
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  for (i <- 0 until n) code
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  val end = System.nanoTime()
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  (end - start)/(n * 1.0e9)
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
738
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    72
// Running Testcases
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    73
//===================
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
738
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    75
@doc(" the argument should be a BF program ")
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    76
@main
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    77
def main(fname: String) = {
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    78
  val bf_str = os.read(os.pwd / fname)
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    79
  println(s"${time_needed(1, run(bf_str))} secs")
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    80
}  
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83