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