progs/while-arrays/compile_bfc.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 22 Nov 2024 12:42:07 +0000
changeset 974 0cb4bf2469d1
parent 958 fddf099a82f8
child 975 ae5c03560d4d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Some more interesting testcases for the 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// WHILE compiler with arrays, it includes: 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
//  - a small parser for WHILE programs
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
//  - transpiles BF programs into WHILE programs
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
//    and then compiles and runs them
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
//  - the power-example is the Mandelbrot set:
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
//       
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
//       * 65k for the transpiled WHILE program 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
//       * parsing uses around 30 secs using fastparse
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
//       * the jasmin assembly file is 236k
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
//       * the resulting Java program takes about 20 secs 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
//
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    16
//
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    17
// Call with scala-cli:
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
//
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    19
//  scala-cli --dep com.lihaoyi::fastparse:3.1.1  compile_bfc.sc 
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    20
//
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    21
// Scala-cli is another REPL for scala. Unfortunately
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    22
// fastparse used in this file is not yet supported
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    23
// under ammonite.
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    25
// compile_arrays.sc (no peephole optimisations)
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    26
// compile_arrays2.sc (peephole optimisations applied)
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    27
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    28
//> using file compile_arrays.sc
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    29
import compile_arrays.*
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
def time_needed[T](i: Int, code: => T) = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
  val start = System.nanoTime()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  for (j <- 2 to i) code
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  val result = code
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
  val end = System.nanoTime()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
  ((end - start) / (i * 1.0e9), result)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
// for BF we have to change the write library-function,
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
// because in BF integers are written out as characters
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
val beginning = """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
.class public XXX.XXX
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
.super java/lang/Object
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
.method public static write(I)V 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
    .limit locals 1 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
    .limit stack 2 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
    getstatic java/lang/System/out Ljava/io/PrintStream; 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
    iload 0
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
    i2c                                           ; Int => Char
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
    invokevirtual java/io/PrintStream/print(C)V   ; println(I)V => print(C)V    
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
    return 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
.end method
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
.method public static main([Ljava/lang/String;)V
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
   .limit locals 200
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
   .limit stack 200
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
; COMPILED CODE STARTS   
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
// modified main compilation function for blocks
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
def compile(bl: Block, class_name: String) : String = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  val instructions = compile_block(bl, Map())._1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
  (beginning ++ instructions ++ ending).replace("XXX", class_name)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
    70
// post 2.5.0 ammonite
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    71
import os.*
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
    72
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
def compile_to_file(bl: Block, class_name: String) : Unit = 
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    75
  os.write.over(os.pwd / s"$class_name.j", compile(bl, class_name))  
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
def compile_and_run(bl: Block, class_name: String) : Unit = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
  println(s"Start of compilation")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  compile_to_file(bl, class_name)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  println(s"generated $class_name.j file")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  val (jasmin_time, _) = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
    time_needed(1, os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call())
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  println(s"generated $class_name.class file (in $jasmin_time secs).")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  val (running_time, output) = 
815
f56d18757526 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
    85
    time_needed(1, os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit, stdin = os.Inherit).out.text())
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
  println(output)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
  println(s"done (in $running_time secs).")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
//=====================================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
// Grammar Rules for WHILE with arrays
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
//=====================================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    95
//> using dep com.lihaoyi::fastparse:3.1.1
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    96
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    97
import fastparse.*
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    98
import MultiLineWhitespace.*
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   100
def lowercase [$ : P] = P( CharIn("a-z") )
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   101
def uppercase[$ : P]  = P( CharIn("A-Z") )
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   102
def letter[$ : P]     = P( lowercase | uppercase )
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   103
def digit [$ : P]     = P( CharIn("0-9") )
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   104
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   105
def Number[$ : P]: P[Int] =  P( digit.rep(1) ).!.map(_.toInt)
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   106
def Ident[$ : P]: P[String] = P( letter ~ (letter | digit | "_").rep ).!
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
// arithmetic expressions
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   109
def AExp[$ : P]: P[AExp] = 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  P(  P(Te ~ "+" ~ AExp).map{ case (l, r) => Aop("+", l, r)} 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
    | P(Te ~ "-" ~ AExp).map{ case (l, r) => Aop("-", l, r)}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
    | Te )
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   113
def Te[$ : P]: P[AExp] = 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
  P(  P(Fa ~ "*" ~ Te).map{ case (l, r) => Aop("*", l, r)} 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
    | Fa )   
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   116
def Fa[$ : P]: P[AExp] = 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
  P( "(" ~ AExp ~ ")" 
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   118
     | P (Ident ~ "[" ~ AExp ~ "]").map{Ref(_, _)}
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 949
diff changeset
   119
     | P(Number).map{Num(_)} 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 949
diff changeset
   120
     | P(Ident).map{Var(_)} )
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
// boolean expressions
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   123
def BExp[$ : P]: P[BExp] = 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  P(  P(AExp ~ "=" ~ AExp).map{ case (x, z) => Bop("=", x, z)} 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
    | P(AExp ~ "!=" ~ AExp).map{ case (x, z) => Bop("!=", x, z)}  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
    | P(AExp ~ "<" ~ AExp).map{ case (x, z) => Bop("<", x, z)}  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
    | P(AExp ~ ">" ~ AExp).map{ case (x, z) => Bop("<", z, x)}  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
    | P("true").map{ _ => True} 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
    | P("false").map{ _ => False} 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
    | "(" ~ BExp ~ ")" )
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
// statements and blocks
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   133
def Stmt[$ : P]: P[Stmt] =
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
  P(  P("skip").map( _ => Skip) 
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   135
    | P(Ident ~ ":=" ~ AExp).map{Assign(_, _)} 
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   136
    | P(Ident ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp).map{AssignA(_, _, _)} 
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   137
    | P("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block).map{If(_, _, _)} 
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   138
    | P("while" ~ BExp ~ "do" ~ Block).map{While(_, _)} 
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   139
    | P("new(" ~ Ident ~ "[" ~ Number ~ "])").map{ArrayDef(_, _)} 
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 949
diff changeset
   140
    | P("write(" ~ Ident ~ ")").map{Write(_)} ) 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   142
def Stmts[$ : P]: P[Block] =
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
  P(  P(Stmt ~ ";" ~ Stmts).map{ case (x, z) => x :: z } 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
    | P(Stmt).map{s => List(s)} ) 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   146
def Block[$ : P]: P[Block] =
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
  P(  "{" ~ Stmts ~ "}" 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
    | P(Stmt).map(s => List(s)) )
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
// some test cases for the parser
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
//println(fastparse.parse("(1 + (2 + 5)) + 4", AExp(_)).get)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
//println(fastparse.parse("1 + 2 + 3 + 45", AExp(_)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
//println(fastparse.parse("1 + 2 * 3", AExp(_)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
//println(fastparse.parse("x + 2 * 3", AExp(_)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
//println(fastparse.parse("x2 := 5 + a", Stmts(_)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
//println(fastparse.parse("x2 := 5 + a[3+a]", Stmts(_)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
//println(fastparse.parse("a[x2+3] := 5 + a[3+a]", Stmts(_)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
//println(fastparse.parse("{x := 5; y := 8}", Block(_)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
//println(fastparse.parse("if (false) then {x := 5} else {x := 10}", Block(_)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
val fib = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
 """n := 10;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
    minus1 := 0;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
    minus2 := 1;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
    temp:=0;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
    while (n > 0) do {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
      temp := minus2; 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
      minus2 := minus1 + minus2;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
      minus1 := temp;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
      n := n - 1};
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
    result := minus2;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
    write(result)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
   """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
//println(fastparse.parse(fib, Stmts(_)).get.value)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
//======================================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
// BF transpiler into WHILE with arrays
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
//======================================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
// simple BF instructions translation
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
def instr(c: Char) : String = c match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
  case '>' => "ptr := ptr + 1;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
  case '<' => "ptr := ptr - 1;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
  case '+' => "mem[ptr] := mem[ptr] + 1;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
  case '-' => "mem[ptr] := mem[ptr] - 1;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
  case '.' => "x := mem[ptr]; write x;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
  case '['  => "while (mem[ptr] != 0) do {"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
  case ']'  => "skip};"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
  case _ => ""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
def instrs(prog: String) : String =
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
  prog.toList.map(instr).mkString
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
// Note: Unfortunately, the transpiled mandelbrot.bf program 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
// is so large that it does not fit inside the limitations of 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
// what the JVM imposes on methods (only 64K of instructions).
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
// Therefore some optimisations are first applied to 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
// BF programs before WHILE programs are created. The
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
// optimisations are 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
//  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
//  - replacing BF-loops of the form [-] with a new 0-instruction 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
//  - combining single increment/decrement instructions
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
// The size of the resulting .j-file is 270K.
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
  case (Nil, acc) => acc
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
  case (c :: cs, Nil) => splice(cs, List((c, 1)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
  case (c :: cs, (d, n) :: acc) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
    if (c == d) splice(cs, (c, n + 1) :: acc)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
    else splice(cs, (c, 1) :: (d, n) :: acc)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
def spl(s: String) = splice(s.toList, Nil).reverse
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
def instr2(c: Char, n: Int) : String = c match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
  case '>' => s"ptr := ptr + $n;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
  case '<' => s"ptr := ptr - $n;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
  case '0' => s"mem[ptr] := 0;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
  case '+' => s"mem[ptr] := mem[ptr] + $n;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
  case '-' => s"mem[ptr] := mem[ptr] - $n;"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
  case '.' => s"x := mem[ptr]; write(x);" 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
  case '['  => "while (mem[ptr] != 0) do {" * n 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
  case ']'  => "skip};" * n
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
  case _ => ""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
def instrs2(prog: String) : String =
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
  spl(prog.replaceAll("""\[-\]""", "0")).map{ case (c, n) => instr2(c, n) }.mkString
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
// adding the "header" to the BF program
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
def bf_str(prog: String) : String = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
  "new(mem[30000]);" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
  "ptr := 15000;" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
  instrs2(prog) ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
  "skip"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
def bf_run(prog: String, name: String) = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
  println(s"BF pre-processing of $name")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
  val bf_string = bf_str(prog)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
  println(s"BF parsing (program length ${bf_string.length} characters)")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
  val (time, bf_prog) = 
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   252
    time_needed(1, fastparse.parse(bf_string, implicit p  => Stmts).get.value)
730
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
   253
  println(s"BF generated WHILE program (needed $time secs for parsing)")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
  compile_and_run(bf_prog, name)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
// a benchmark program (counts down from 'Z' to 'A')
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   258
//@doc(" Benchmark 'Z' to 'A'.")
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   259
//@main
745
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   260
def bfc0() = bf_run(read(pwd / "benchmark.bf"), "bench")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   263
//@doc(" Sierpinski triangle.")
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   264
//@main
745
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   265
def bfc1() = bf_run(read(pwd / "sierpinski.bf"), "sier")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
// Hello World
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
val bf2 = """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
      >>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   271
//@doc(" Hello world.")
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   272
//@main
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   273
def bfc2() = bf_run(bf2, "hello")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   274
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   275
// Fibonacci 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   276
val bf3 = """+++++++++++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   277
      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   278
      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   279
      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   280
      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   281
      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   282
      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   283
      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   284
      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   285
      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   286
      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   287
      [-]++++++++++."""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   288
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   289
//@doc(" Fibonacci numbers.")
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   290
//@main
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   291
def bfc3() = bf_run(bf3, "fibs")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   292
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   293
// Mandelbrot Set
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   294
//----------------
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   295
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   296
// Note: Parsing of the generated WHILE program (around 60K in size)
745
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   297
// takes approximately 10 minutes to parse with our parser combinators,
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   298
// and approximately 30 seconds with Ammonite's fastparse.
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   299
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   300
//@doc(" Mandelbrot set.")
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   301
//@main
757
ea0be0662be0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 747
diff changeset
   302
def bfc4() = bf_run(read(pwd / "mandelbrot.bf"), "mandelbrot")
745
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   303
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   304
747
077fc2e5412c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
   305
// this unfortunately hits the capacity of the JVM, even with optimisations
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   306
//@doc(" Collatz series up to 30.")
745
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   307
//@main
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   308
//def bfc5() = bf_run(read(pwd / "collatz.bf"), "coll")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   309
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   310
// this unfortunately hits the capacity of the JVM, even with optimisations
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   311
//@doc(" Towers of Hanoi.")
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   312
//@main
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   313
//def bfc6() = bf_run(read(pwd / "hanoi.bf"), "hanoi")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   314
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   315
//
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   316
//@doc(" All benchmarks.")
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   317
//@main
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 730
diff changeset
   318
def all() = { bfc0(); bfc1(); bfc2(); bfc3(); bfc4() } 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 730
diff changeset
   319
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 949
diff changeset
   320
//all()
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 949
diff changeset
   321
bfc4()
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 730
diff changeset
   322
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 730
diff changeset
   323
829
dc3c35673e94 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   324
dc3c35673e94 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   325
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   326
// old way to run it with Ammonite
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   327
//
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   328
// Call with (X being 0,1,..,4)
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   329
//
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   330
//  amm compile_bfc.sc all
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   331
//  amm compile_bfc.sc bfcX
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   332
//
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   333
//
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   334
// load the compiler
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   335
//import $file.compile_arrays2
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   336
//import compile_arrays2._ 
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   337
//
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   338
// load fastparse
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 949
diff changeset
   339
//import $ivy.`com.lihaoyi::fastparse:3.0.2`