progs/bf/bfc1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 20 Jul 2020 16:46:30 +0100
changeset 736 d3e477fe6c66
parent 648 progs/bfc1.scala@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
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 633
diff changeset
    12
// "splicing" a BF program counting occurrences
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  case (Nil, acc) => acc
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  case (c :: cs, Nil) => splice(cs, List((c, 1)))
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case (c :: cs, (d, n) :: acc) => 
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
    if (c == d) splice(cs, (c, n + 1) :: acc)
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
    else splice(cs, (c, 1) :: (d, n) :: acc)
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def spl(s: String) = splice(s.toList, Nil).reverse
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 633
diff changeset
    23
// generating "compound" c-instructions 
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def instr2(c: Char, n: Int) : String = c match {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  case '>' => "ptr += " + n.toString + ";"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case '<' => "ptr -= " + n.toString + ";"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case '+' => "(*ptr) += " + n.toString + ";"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  case '-' => "(*ptr) -= " + n.toString + ";"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  case '.' => "putchar(*ptr);" * n
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  case ',' => "*ptr = getchar();\n" * n
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  case '['  => "while(*ptr){" * n
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  case ']'  => "}" * n
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  case _ => ""
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
def instrs2(prog: String) : String =
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  spl(prog).map{ case (c, n) => instr2(c, n) }.mkString
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_str(prog: String) : String = {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  "#include <string.h>\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  "#include <stdio.h>\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  "char field[30000];\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  "char *ptr = &field[15000];" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  "int main()\n{\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  "memset(field, '\\0', 30000);\n" ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  instrs2(prog) ++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  "\n return 0;\n}"
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
def compile(name: String, prog: String) = {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  val fw = new java.io.FileWriter(name + ".c") 
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  val is = compile_str(prog)
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  //println(is)
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  fw.write(is) 
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  fw.close()
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
import sys.process._
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
def compile_run(prog: String) = {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  compile("tmp", prog)
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 633
diff changeset
    64
  "gcc -O0 -o tmp tmp.c".!
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  "./tmp".!
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  ()
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
def time_needed[T](n: Int, code: => T) = {
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  val start = System.nanoTime()
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  for (i <- 0 until n) code
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  val end = System.nanoTime()
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  (end - start) / (n * 1.0e9)
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
}
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
637
27f71d2755f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 636
diff changeset
    76
// the mandelbrot program
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 633
diff changeset
    77
val b0 = load_bff("mandelbrot.bf")
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 633
diff changeset
    79
println(s"${time_needed(1, compile_run(b0))} secs")
633
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
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
// a benchmark program (counts down from 'Z' to 'A')
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
            [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
            ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
            +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
e4889da2fe29 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
println(s"${time_needed(1, compile_run(b1))} secs")
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 637
diff changeset
    90
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 637
diff changeset
    91