progs/compile_arr2.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 24 Sep 2021 23:56:31 +0100
changeset 839 7af70f9b30ab
parent 712 e71eb9ce2373
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// A Small Compiler for the WHILE Language
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     2
//
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
     3
//  - includes arrays and a small parser for
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     4
//    WHILE programs
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     5
//
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     6
//  - transpiles BF programs into WHILE programs
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     7
//    and compiles and runs them
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     8
//
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     9
// Call with
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    10
//
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
    11
// scala compile_arr.scala
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    12
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
    13
// Mandelbrot
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
    14
// mand.j size       232266
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
    15
// mand.class size   21787
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
    16
// running time      16 secs
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// the abstract syntax trees
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
abstract class Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
abstract class AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
abstract class BExp 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
type Block = List[Stmt]
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// statements
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case object Skip extends Stmt
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    26
case class ArrayDef(s: String, n: Int) extends Stmt
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
case class While(b: BExp, bl: Block) extends Stmt
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    29
case class Assign(s: String, a: AExp) extends Stmt             // var := exp
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    30
case class AssignA(s: String, a1: AExp, a2: AExp) extends Stmt // arr[exp1] := exp2
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    31
case class Write(s: String) extends Stmt
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    32
case class Read(s: String) extends Stmt
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// arithmetic expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
case class Var(s: String) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
case class Num(i: Int) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    38
case class Ref(s: String, a: AExp) extends AExp
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
// boolean expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
case object True extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
case object False extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// compiler headers needed for the JVM
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// (contains an init method, as well as methods for read and write)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
val beginning = """
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
.class public XXX.XXX
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
.super java/lang/Object
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
.method public static write(I)V 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
    .limit locals 1 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
    .limit stack 2 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
    getstatic java/lang/System/out Ljava/io/PrintStream; 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
    iload 0
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    57
    i2c       ; Int => Char
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    58
    invokevirtual java/io/PrintStream/print(C)V   ; println(I)V => print(C)V    
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    return 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
.method public static main([Ljava/lang/String;)V
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
   .limit locals 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
   .limit stack 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
    66
; COMPILED CODE STARTS   
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
    67
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
val ending = """
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
    71
; COMPILED CODE ENDS
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
   return
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    77
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
// for generating new labels
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
var counter = -1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
def Fresh(x: String) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  counter += 1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  x ++ "_" ++ counter.toString()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
// environments and instructions
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    89
type Env = Map[String, Int]
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    90
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    91
// convenient string interpolations 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    92
// for instructions and labels
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    93
import scala.language.implicitConversions
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    94
import scala.language.reflectiveCalls
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    95
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    96
implicit def sring_inters(sc: StringContext) = new {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    97
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    98
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    99
}
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   100
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   101
def compile_op(op: String) = op match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   102
  case "+" => i"iadd"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   103
  case "-" => i"isub"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   104
  case "*" => i"imul"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   105
}
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   107
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   108
def compile_num(i: Int) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   109
  if (0 <= i && i <= 5) i"iconst_$i" else 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   110
  if (-128 <= i && i <= 127) i"bipush $i" else i"ldc $i"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   111
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   112
def compile_aload(i: Int) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   113
  if (0 <= i && i <= 3) i"aload_$i" else i"aload $i"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   114
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   115
def compile_iload(i: Int) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   116
  if (0 <= i && i <= 3) i"iload_$i" else i"iload $i"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   117
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   118
def compile_istore(i: Int) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   119
  if (0 <= i && i <= 3) i"istore_$i" else i"istore $i"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   120
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
// arithmetic expression compilation
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   122
def compile_aexp(a: AExp, env : Env) : String = a match {
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   123
  case Num(i) => compile_num(i)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   124
  case Var(s) => compile_iload(env(s))
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   125
  case Aop(op, a1, a2) => 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   126
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   127
  case Ref(s, a) => 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   128
    compile_aload(env(s)) ++ compile_aexp(a, env) ++  i"iaload"
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   131
def compile_bop(op: String, jmp: String) = op match {
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   132
  case "==" => i"if_icmpne $jmp"
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   133
  case "!=" => i"if_icmpeq $jmp"
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   134
  case "<"  => i"if_icmpge $jmp"
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   135
}
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   136
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   137
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
// boolean expression compilation
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   139
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   140
  case True => ""
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   141
  case False => i"goto $jmp"
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   142
  case Bop(op, a1, a2) => 
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   143
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_bop(op, jmp)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
// statement compilation
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   147
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   148
  case Skip => ("", env)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
  case Assign(x, a) => {
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   150
     val index = env.getOrElse(x, env.keys.size) //
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   151
    (compile_aexp(a, env) ++ compile_istore(index), env + (x -> index)) 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
  } 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
  case If(b, bl1, bl2) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
    val if_else = Fresh("If_else")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
    val if_end = Fresh("If_end")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
    val (instrs1, env1) = compile_block(bl1, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
    val (instrs2, env2) = compile_block(bl2, env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
    (compile_bexp(b, env, if_else) ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
     instrs1 ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   160
     i"goto $if_end" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   161
     l"$if_else" ++
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
     instrs2 ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   163
     l"$if_end", env2)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
  case While(b, bl) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
    val loop_begin = Fresh("Loop_begin")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
    val loop_end = Fresh("Loop_end")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
    val (instrs1, env1) = compile_block(bl, env)
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   169
    (l"$loop_begin" ++
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
     compile_bexp(b, env, loop_end) ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
     instrs1 ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   172
     i"goto $loop_begin" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   173
     l"$loop_end", env1)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
  case Write(x) => 
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   176
    (compile_iload(env(x)) ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   177
     i"invokestatic XXX/XXX/write(I)V", env)
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   178
  case Read(x) => {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   179
    val index = env.getOrElse(x, env.keys.size) 
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   180
    (i"invokestatic XXX/XXX/read()I" ++     
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   181
     compile_istore(index), env + (x -> index))
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   182
  }
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   183
  case ArrayDef(s: String, n: Int) => {
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   184
    val index = if (env.isDefinedAt(s)) throw new Exception("array def error") else 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   185
                    env.keys.size
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   186
    (i"ldc $n" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   187
     i"newarray int" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   188
     i"astore $index", env + (s -> index))
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   189
  }
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   190
  case AssignA(s, a1, a2) => {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   191
    val index = if (env.isDefinedAt(s)) env(s) else 
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   192
                    throw new Exception("array not defined")
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   193
    (compile_aload(env(s)) ++
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   194
     compile_aexp(a1, env) ++
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   195
     compile_aexp(a2, env) ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   196
     i"iastore", env)
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   197
  } 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   200
// compilation of a block (i.e. list of statements)
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   201
def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   202
  case Nil => ("", env)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
  case s::bl => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
    val (instrs1, env1) = compile_stmt(s, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
    val (instrs2, env2) = compile_block(bl, env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
    (instrs1 ++ instrs2, env2)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   210
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
// main compilation function for blocks
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
def compile(bl: Block, class_name: String) : String = {
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   213
  val instructions = compile_block(bl, Map())._1
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   214
  (beginning ++ instructions ++ ending).replaceAllLiterally("XXX", class_name)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   218
// Fibonacci numbers as a test-case
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   219
val fib_test = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   220
  List(Read("n"),                       //  read n;                     
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   221
       Assign("minus1",Num(0)),         //  minus1 := 0;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   222
       Assign("minus2",Num(1)),         //  minus2 := 1;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   223
       Assign("temp",Num(0)),           //  temp := 0;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   224
       While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   225
          List(Assign("temp",Var("minus2")),    //  temp := minus2;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   226
               Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   227
                                        //  minus2 := minus1 + minus2;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   228
               Assign("minus1",Var("temp")), //  minus1 := temp;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   229
               Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   230
       Write("minus1"))                 //  write minus1
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   231
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   232
// prints out the JVM-assembly instructions for fib
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   233
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   234
// println(compile(fib_test, "fib"))
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   235
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   236
// can be assembled by hand with 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   237
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   238
//    java -jar jvm/jasmin-2.4/jasmin.jar fib.j
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   239
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   240
// and started with
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   241
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   242
//    java fib/fib
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   243
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
import scala.util._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
import scala.sys.process._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
def time_needed[T](i: Int, code: => T) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
  val start = System.nanoTime()
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   249
  for (j <- 2 to i) code
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   250
  val result = code
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
  val end = System.nanoTime()
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   252
  ((end - start) / (i * 1.0e9), result)
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   253
}
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   254
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   255
def compile_to_file(bl: Block, class_name: String) : Unit = 
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   256
  Using(new java.io.FileWriter(class_name + ".j")) {
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   257
    _.write(compile(bl, class_name))
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   258
  }
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   259
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   260
def compile_and_run(bl: Block, class_name: String) : Unit = {
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   261
  println(s"Start compilation of $class_name")
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   262
  compile_to_file(bl, class_name)
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   263
  println("generated .j file")
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   264
  (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!!
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   265
  println("generated .class file ")
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   266
  println("Time: " + time_needed(3, (s"java ${class_name}/${class_name}").!)._1)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   270
val arr_test = 
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   271
  List(ArrayDef("a", 10),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   272
       ArrayDef("b", 2),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   273
       AssignA("a", Num(0), Num(10)),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   274
       Assign("x", Ref("a", Num(0))),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   275
       Write("x"),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   276
       AssignA("b", Num(1), Num(5)),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   277
       Assign("x", Ref("b", Num(1))),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   278
       Write("x"))       
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   279
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   280
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   281
//compile_and_run(arr_test, "a")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   282
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   283
//====================
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   284
// Parser Combinators
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   285
//====================
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   286
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   287
import scala.language.implicitConversions
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   288
import scala.language.reflectiveCalls
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   289
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   290
// more convenience for the semantic actions later on
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   291
case class ~[+A, +B](_1: A, _2: B)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   292
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   293
type IsSeq[A] = A => Seq[_]
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   294
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   295
abstract class Parser[I : IsSeq, T] {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   296
  def parse(ts: I): Set[(T, I)]
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   297
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   298
  def parse_all(ts: I) : Set[T] =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   299
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   300
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   301
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   302
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   303
  def parse(sb: I) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   304
    for ((head1, tail1) <- p.parse(sb); 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   305
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   306
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   307
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   308
class AltParser[I : IsSeq, T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   309
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   310
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   311
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   312
class FunParser[I : IsSeq, T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   313
  def parse(sb: I) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   314
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   315
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   316
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   317
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   318
import scala.util.matching.Regex
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   319
case class RegexParser(reg: Regex) extends Parser[String, String] {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   320
  def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   321
    case None => Set()
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   322
    case Some(m) => Set((m.matched, m.after.toString))  
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   323
  }
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   324
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   325
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   326
def StringParser(s: String) = RegexParser(Regex.quote(s).r)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   327
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   328
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   329
implicit def string2parser(s : String) = StringParser(s)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   330
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   331
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   332
  def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   333
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   334
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   335
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   336
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   337
implicit def StringOps(s: String) = new {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   338
  def | (q : => Parser[String, String]) = new AltParser[String, String](s, q)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   339
  def | (r: String) = new AltParser[String, String](s, r)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   340
  def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   341
  def ~[S](q : => Parser[String, S]) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   342
    new SeqParser[String, String, S](s, q)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   343
  def ~ (r: String) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   344
    new SeqParser[String, String, String](s, r)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   345
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   346
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   347
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   348
val NumParser = RegexParser("[0-9]+".r) ==> (s => s.toInt : Int)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   349
val IdParser = RegexParser("[a-z][a-z,0-9]*".r)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   350
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   351
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   352
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   353
// Grammar Rules for WHILE with arrays
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   354
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   355
lazy val AExp: Parser[String, AExp] = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   356
  (Te ~ "+" ~ AExp) ==> { case x ~ _ ~ z => Aop("+", x, z):AExp } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   357
  (Te ~ "-" ~ AExp) ==> { case x ~ _ ~ z => Aop("-", x, z):AExp } | Te
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   358
lazy val Te: Parser[String, AExp] = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   359
  (Fa ~ "*" ~ Te) ==> { case x ~ _ ~ z => Aop("*", x, z):AExp } | Fa
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   360
lazy val Fa: Parser[String, AExp] = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   361
   ("(" ~ AExp ~ ")") ==> { case _ ~ y ~ _ => y } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   362
   (IdParser ~ "[" ~ AExp ~ "]") ==> { case x ~ _ ~ y ~ _ => Ref(x, y) } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   363
   IdParser ==> Var | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   364
   NumParser ==> Num
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   365
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   366
// boolean expressions
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   367
lazy val BExp: Parser[String, BExp] = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   368
   (AExp ~ "=" ~ AExp) ==> { case x ~ y ~ z => Bop("=", x, z):BExp } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   369
   (AExp ~ "!=" ~ AExp) ==> { case x ~ y ~ z => Bop("!=", x, z):BExp } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   370
   (AExp ~ "<" ~ AExp) ==> { case x ~ y ~ z => Bop("<", x, z):BExp } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   371
   (AExp ~ ">" ~ AExp) ==> { case x ~ y ~ z => Bop("<", z, x):BExp } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   372
   ("true" ==> ((_) => True:BExp )) | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   373
   ("false" ==> ((_) => False:BExp )) |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   374
   ("(" ~ BExp ~ ")") ==> { case x ~ y ~ z => y}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   375
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   376
lazy val Stmt: Parser[String, Stmt] =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   377
   ("skip" ==> (_ => Skip: Stmt)) |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   378
   (IdParser ~ ":=" ~ AExp) ==> { case x ~ y ~z => Assign(x, z): Stmt } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   379
   (IdParser ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp) ==> { 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   380
     case x ~ _ ~ z ~ _ ~ _ ~ u => AssignA(x, z, u): Stmt } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   381
   ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   382
    { case _ ~ y ~ _ ~ u ~ _ ~w => If(y, u, w): Stmt } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   383
   ("while" ~ BExp ~ "do" ~ Block) ==> { case _ ~ y ~ _ ~ w => While(y, w) } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   384
   ("new(" ~ IdParser ~ "[" ~ NumParser ~ "])") ==> { 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   385
    case _ ~ y ~ _ ~ u ~ _ => ArrayDef(y, u) } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   386
   ("write" ~ IdParser) ==> { case _ ~ y => Write(y) } 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   387
 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   388
lazy val Stmts: Parser[String, Block] =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   389
  (Stmt ~ ";" ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   390
  (Stmt ==> ((s) => List(s) : Block)) 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   391
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   392
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   393
lazy val Block: Parser[String, Block] =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   394
   ("{" ~ Stmts ~ "}") ==> { case _ ~ y ~ _ => y} | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   395
   (Stmt ==> (s => List(s)))
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   396
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   397
//Stmts.parse_all("x2:=5+a")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   398
//Stmts.parse_all("x2:=5+a[3+a]")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   399
//Stmts.parse_all("a[x2+3]:=5+a[3+a]")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   400
//Block.parse_all("{x:=5;y:=8}")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   401
//Block.parse_all("if(false)then{x:=5}else{x:=10}")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   402
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   403
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   404
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   405
val fib = """
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   406
   n := 10;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   407
   minus1 := 0;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   408
   minus2 := 1;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   409
   temp:=0;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   410
   while (n > 0) do {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   411
     temp := minus2; 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   412
     minus2 := minus1 + minus2;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   413
     minus1 := temp;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   414
     n := n - 1};
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   415
   result := minus2;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   416
   write result
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   417
   """.replaceAll("\\s+", "")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   418
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   419
val fib_prog = Stmts.parse_all(fib).toList
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   420
//compile_and_run(fib_prog.head, "fib")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   421
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   422
//======================================
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   423
// BF transpiler into WHILE with arrays
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   424
//======================================
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   425
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   426
// simple BF instructions translation
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   427
def instr(c: Char) : String = c match {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   428
  case '>' => "ptr := ptr + 1;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   429
  case '<' => "ptr := ptr - 1;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   430
  case '+' => "mem[ptr] := mem[ptr] + 1;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   431
  case '-' => "mem[ptr] := mem[ptr] - 1;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   432
  case '.' => "x := mem[ptr]; write x;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   433
  //case ',' => "XXX" // "ptr = getchar();\n"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   434
  case '['  => "while (mem[ptr] != 0) do {"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   435
  case ']'  => "skip};"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   436
  case _ => ""
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   437
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   438
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   439
def instrs(prog: String) : String =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   440
  prog.toList.map(instr).mkString
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   441
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   442
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   443
// Note: the mandelbrot.bf program is so large that
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   444
// it does not fit inside the limitations of what the
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   445
// JVM imposes on methods (only 64K of instructions).
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   446
// Therefore some optimisations are first applied to 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   447
// BF programs before WHILE programs are created. The
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   448
// optimisations are 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   449
//  
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   450
//  - replacing BF-loops of the form [-] 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   451
//  - combining single increment/decrement instructions
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   452
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   453
// The size of the resulting .j-file is 270K.
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   454
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   455
def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   456
  case (Nil, acc) => acc
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   457
  case (c :: cs, Nil) => splice(cs, List((c, 1)))
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   458
  case (c :: cs, (d, n) :: acc) => 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   459
    if (c == d) splice(cs, (c, n + 1) :: acc)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   460
    else splice(cs, (c, 1) :: (d, n) :: acc)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   461
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   462
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   463
def spl(s: String) = splice(s.toList, Nil).reverse
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   464
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   465
def instr2(c: Char, n: Int) : String = c match {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   466
  case '>' => s"ptr := ptr + $n;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   467
  case '<' => s"ptr := ptr - $n;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   468
  case '0' => s"mem[ptr] := 0;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   469
  case '+' => s"mem[ptr] := mem[ptr] + $n;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   470
  case '-' => s"mem[ptr] := mem[ptr] - $n;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   471
  case '.' => s"x := mem[ptr]; write x;" 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   472
  case '['  => "while (mem[ptr] != 0) do {" * n 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   473
  case ']'  => "skip};" * n
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   474
  case _ => ""
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   475
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   476
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   477
def instrs2(prog: String) : String =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   478
  spl(prog.replaceAll("""\[-\]""", "0")).map{ case (c, n) => instr2(c, n) }.mkString
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   479
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   480
def bf_str(prog: String) : String = {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   481
  "new(mem[30000]);" ++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   482
  "ptr := 15000;" ++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   483
  instrs2(prog) ++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   484
  "skip"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   485
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   486
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   487
def bf_run(prog: String, name: String) = {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   488
  println(s"BF pre-processing of $name")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   489
  val bf_string = bf_str(prog).replaceAll("\\s", "")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   490
  println(s"BF parsing (program length ${bf_string.length} characters)")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   491
  val (time, bf_prog) = time_needed(1, Stmts.parse_all(bf_string).toList.head)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   492
  println(s"BF generated WHILE program (needed $time for parsing)")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   493
  compile_and_run(bf_prog, name)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   494
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   495
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   496
// a benchmark program (counts down from 'Z' to 'A')
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   497
val bf0 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   498
            [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   499
            ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   500
            +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   501
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   502
bf_run(bf0, "bench")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   503
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   504
// Sierpinski triangle
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   505
val bf1 = """++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   506
      ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   507
      ]>.>+[>>]>+]"""
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   508
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   509
bf_run(bf1, "sier")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   510
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   511
// Helo World!
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   512
bf_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   513
       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", "hello")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   514
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   515
// Fibonacci 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   516
val bf2 = """+++++++++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   517
      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   518
      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   519
      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   520
      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   521
      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   522
      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   523
      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   524
      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   525
      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
712
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   526
      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   527
      [-]++++++++++."""
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   528
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   529
bf_run(bf2, "fibs")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   530
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   531
// Mandelbrot Set
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   532
//----------------
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   533
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   534
// Note: Parsing of the generated WHILE program (around 60K in size)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   535
// takes approximately 10 minutes
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   536
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   537
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   538
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   539
bf_run("""A mandelbrot set fractal viewer in brainf*** written by Erik Bosman
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   540
+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   541
>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   542
<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   543
>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+[>>>>>>[>>>>>>>[-]>>]<<<<<<<<<[<<<<<<<<<]>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   544
>>>>>[-]+<<<<<<++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<+++++++[-[->>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   545
>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[[-]>>>>>>[>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   546
>>[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   547
[>>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<+<<]>>>>>>>>]<<<<<<<<<[<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   548
<<]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<<<]>>>>>>>>>+++++++++++++++[[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   549
>>>>>>>>>]+>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   550
>+>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>[-<<<<+>>>>]<<<<[->>>>+<<<<<[->>[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   551
-<<+>>]<<[->>+>>+<<<<]+>>>>>>>>>]<<<<<<<<[<<<<<<<<<]]>>>>>>>>>[>>>>>>>>>]<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   552
<<[>[->>>>>>>>>+<<<<<<<<<]<<<<<<<<<<]>[->>>>>>>>>+<<<<<<<<<]<+>>>>>>>>]<<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   553
[>[-]<->>>>[-<<<<+>[<->-<<<<<<+>>>>>>]<[->+<]>>>>]<<<[->>>+<<<]<+<<<<<<<<<]>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   554
>>>>[>+>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>>[-<<<<<+>>>>>]<<<<<[->>>>>+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   555
<<<<<<[->>>[-<<<+>>>]<<<[->>>+>+<<<<]+>>>>>>>>>]<<<<<<<<[<<<<<<<<<]]>>>>>>>>>[>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   556
>>>>>>>]<<<<<<<<<[>>[->>>>>>>>>+<<<<<<<<<]<<<<<<<<<<<]>>[->>>>>>>>>+<<<<<<<<<]<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   557
+>>>>>>>>]<<<<<<<<<[>[-]<->>>>[-<<<<+>[<->-<<<<<<+>>>>>>]<[->+<]>>>>]<<<[->>>+<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   558
<]<+<<<<<<<<<]>>>>>>>>>[>>>>[-<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   559
>>>>>>>>>>>>>>>>>>>>>>>]>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>+++++++++++++++[[>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   560
>>>>>]<<<<<<<<<-<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+>>>>>>>>>>>>>>>>>>>>>+<<<[<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   561
<<<]>>>>>>>>>[>>>[-<<<->>>]+<<<[->>>->[-<<<<+>>>>]<<<<[->>>>+<<<<<<<<<<<<<[<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   562
<<<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>[-<<<<->>>>]+<<<<[->>>>-<[-<<<+>>>]<<<[->
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   563
>>+<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   564
<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]<<<<<<<[->+>>>-<<<<]>>>>>>>>>+++++++++++++++++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   565
+++++++>>[-<<<<+>>>>]<<<<[->>>>+<<[-]<<]>>[<<<<<<<+<[-<+>>>>+<<[-]]>[-<<[->+>>>-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   566
<<<<]>>>]>>>>>>>>>>>>>[>>[-]>[-]>[-]>>>>>]<<<<<<<<<[<<<<<<<<<]>>>[-]>>>>>>[>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   567
[-<<<<+>>>>]<<<<[->>>>+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>>[-<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   568
<+>>>>>>>>>]>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>+++++++++++++++[[>>>>>>>>>]+>[-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   569
]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+[>+>>>>>>>>]<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   570
<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>>[-<<<<<+>>>>>]<<<<<[->>>>>+<<<<<<[->>[-<<+>>]<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   571
<[->>+>+<<<]+>>>>>>>>>]<<<<<<<<[<<<<<<<<<]]>>>>>>>>>[>>>>>>>>>]<<<<<<<<<[>[->>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   572
>>>>>+<<<<<<<<<]<<<<<<<<<<]>[->>>>>>>>>+<<<<<<<<<]<+>>>>>>>>]<<<<<<<<<[>[-]<->>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   573
[-<<<+>[<->-<<<<<<<+>>>>>>>]<[->+<]>>>]<<[->>+<<]<+<<<<<<<<<]>>>>>>>>>[>>>>>>[-<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   574
<<<<+>>>>>]<<<<<[->>>>>+<<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>+>>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   575
]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>>[-<<<<<+>>>>>]<<<<<[->>>>>+<<<<<<[->>[-<<+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   576
>>]<<[->>+>>+<<<<]+>>>>>>>>>]<<<<<<<<[<<<<<<<<<]]>>>>>>>>>[>>>>>>>>>]<<<<<<<<<[>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   577
[->>>>>>>>>+<<<<<<<<<]<<<<<<<<<<]>[->>>>>>>>>+<<<<<<<<<]<+>>>>>>>>]<<<<<<<<<[>[-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   578
]<->>>>[-<<<<+>[<->-<<<<<<+>>>>>>]<[->+<]>>>>]<<<[->>>+<<<]<+<<<<<<<<<]>>>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   579
[>>>>[-<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   580
]>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>[-<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   581
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>++++++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   582
+++++++[[>>>>>>>>>]<<<<<<<<<-<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-<<<<<<<+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   583
>>>>>>>]<<<<<<<[->>>>>>>+<<<<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>>>>[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   584
-]>>>]<<<<<<<<<[<<<<<<<<<]>>>>+>[-<-<<<<+>>>>>]>[-<<<<<<[->>>>>+<++<<<<]>>>>>[-<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   585
<<<<+>>>>>]<->+>]<[->+<]<<<<<[->>>>>+<<<<<]>>>>>>[-]<<<<<<+>>>>[-<<<<->>>>]+<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   586
[->>>>->>>>>[>>[-<<->>]+<<[->>->[-<<<+>>>]<<<[->>>+<<<<<<<<<<<<[<<<<<<<<<]>>>[-]
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   587
+>>>>>>[>>>>>>>>>]>+<]]+>>>[-<<<->>>]+<<<[->>>-<[-<<+>>]<<[->>+<<<<<<<<<<<[<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   588
<<<<]>>>>[-]+>>>>>[>>>>>>>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   589
[<<<<<<<<<]>>>>[-<<<<+>>>>]<<<<[->>>>+>>>>>[>+>>[-<<->>]<<[->>+<<]>>>>>>>>]<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   590
<<<+<[>[->>>>>+<<<<[->>>>-<<<<<<<<<<<<<<+>>>>>>>>>>>[->>>+<<<]<]>[->>>-<<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   591
<<<<<+>>>>>>>>>>>]<<]>[->>>>+<<<[->>>-<<<<<<<<<<<<<<+>>>>>>>>>>>]<]>[->>>+<<<]<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   592
<<<<<<<<<<]>>>>[-]<<<<]>>>[-<<<+>>>]<<<[->>>+>>>>>>[>+>[-<->]<[->+<]>>>>>>>>]<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   593
<<<<<+<[>[->>>>>+<<<[->>>-<<<<<<<<<<<<<<+>>>>>>>>>>[->>>>+<<<<]>]<[->>>>-<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   594
<<<<<<<+>>>>>>>>>>]<]>>[->>>+<<<<[->>>>-<<<<<<<<<<<<<<+>>>>>>>>>>]>]<[->>>>+<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   595
]<<<<<<<<<<<]>>>>>>+<<<<<<]]>>>>[-<<<<+>>>>]<<<<[->>>>+>>>>>[>>>>>>>>>]<<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   596
[>[->>>>>+<<<<[->>>>-<<<<<<<<<<<<<<+>>>>>>>>>>>[->>>+<<<]<]>[->>>-<<<<<<<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   597
+>>>>>>>>>>>]<<]>[->>>>+<<<[->>>-<<<<<<<<<<<<<<+>>>>>>>>>>>]<]>[->>>+<<<]<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   598
<<<<<]]>[-]>>[-]>[-]>>>>>[>>[-]>[-]>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>>>[-<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   599
<<<+>>>>]<<<<[->>>>+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>+++++++++++++++[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   600
[>>>>>>>>>]+>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   601
[>+>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>[-<<<<+>>>>]<<<<[->>>>+<<<<<[->>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   602
[-<<+>>]<<[->>+>+<<<]+>>>>>>>>>]<<<<<<<<[<<<<<<<<<]]>>>>>>>>>[>>>>>>>>>]<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   603
<[>[->>>>>>>>>+<<<<<<<<<]<<<<<<<<<<]>[->>>>>>>>>+<<<<<<<<<]<+>>>>>>>>]<<<<<<<<<[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   604
>[-]<->>>[-<<<+>[<->-<<<<<<<+>>>>>>>]<[->+<]>>>]<<[->>+<<]<+<<<<<<<<<]>>>>>>>>>[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   605
>>>[-<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   606
>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>[-]>>>>+++++++++++++++[[>>>>>>>>>]<<<<<<<<<-<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   607
<<<<[<<<<<<<<<]>>>>>>>>>-]+[>>>[-<<<->>>]+<<<[->>>->[-<<<<+>>>>]<<<<[->>>>+<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   608
<<<<<<<<[<<<<<<<<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>[-<<<<->>>>]+<<<<[->>>>-<[-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   609
<<<+>>>]<<<[->>>+<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>>>>]>[-]+<]]+>[-<[>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   610
>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>[-<<<+>>>]<<<[->>>+>>>>>>[>+>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   611
[-<<<->>>]<<<[->>>+<<<]>>>>>>>>]<<<<<<<<+<[>[->+>[-<-<<<<<<<<<<+>>>>>>>>>>>>[-<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   612
+>>]<]>[-<<-<<<<<<<<<<+>>>>>>>>>>>>]<<<]>>[-<+>>[-<<-<<<<<<<<<<+>>>>>>>>>>>>]<]>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   613
[-<<+>>]<<<<<<<<<<<<<]]>>>>[-<<<<+>>>>]<<<<[->>>>+>>>>>[>+>>[-<<->>]<<[->>+<<]>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   614
>>>>>>]<<<<<<<<+<[>[->+>>[-<<-<<<<<<<<<<+>>>>>>>>>>>[-<+>]>]<[-<-<<<<<<<<<<+>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   615
>>>>>>>]<<]>>>[-<<+>[-<-<<<<<<<<<<+>>>>>>>>>>>]>]<[-<+>]<<<<<<<<<<<<]>>>>>+<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   616
]>>>>>>>>>[>>>[-]>[-]>[-]>>>>]<<<<<<<<<[<<<<<<<<<]>>>[-]>[-]>>>>>[>>>>>>>[-<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   617
<+>>>>>>]<<<<<<[->>>>>>+<<<<+<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>+>[-<-<<<<+>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   618
>]>>[-<<<<<<<[->>>>>+<++<<<<]>>>>>[-<<<<<+>>>>>]<->+>>]<<[->>+<<]<<<<<[->>>>>+<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   619
<<<]+>>>>[-<<<<->>>>]+<<<<[->>>>->>>>>[>>>[-<<<->>>]+<<<[->>>-<[-<<+>>]<<[->>+<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   620
<<<<<<<<<[<<<<<<<<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>[-<<->>]+<<[->>->[-<<<+>>>]<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   621
<<[->>>+<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>>>>]>[-]+<]]+>[-<[>>>>>>>>>]<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   622
<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>[-<<<+>>>]<<<[->>>+>>>>>>[>+>[-<->]<[->+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   623
<]>>>>>>>>]<<<<<<<<+<[>[->>>>+<<[->>-<<<<<<<<<<<<<+>>>>>>>>>>[->>>+<<<]>]<[->>>-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   624
<<<<<<<<<<<<<+>>>>>>>>>>]<]>>[->>+<<<[->>>-<<<<<<<<<<<<<+>>>>>>>>>>]>]<[->>>+<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   625
]<<<<<<<<<<<]>>>>>[-]>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<<<]]>>>>[-<<<<+>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   626
>>>]<<<<[->>>>+>>>>>[>+>>[-<<->>]<<[->>+<<]>>>>>>>>]<<<<<<<<+<[>[->>>>+<<<[->>>-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   627
<<<<<<<<<<<<<+>>>>>>>>>>>[->>+<<]<]>[->>-<<<<<<<<<<<<<+>>>>>>>>>>>]<<]>[->>>+<<[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   628
->>-<<<<<<<<<<<<<+>>>>>>>>>>>]<]>[->>+<<]<<<<<<<<<<<<]]>>>>[-]<<<<]>>>>[-<<<<+>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   629
>>]<<<<[->>>>+>[-]>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<<<]>>>>>>>>>[>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   630
>>>]<<<<<<<<<[>[->>>>+<<<[->>>-<<<<<<<<<<<<<+>>>>>>>>>>>[->>+<<]<]>[->>-<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   631
<<<<<+>>>>>>>>>>>]<<]>[->>>+<<[->>-<<<<<<<<<<<<<+>>>>>>>>>>>]<]>[->>+<<]<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   632
<<<<]]>>>>>>>>>[>>[-]>[-]>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>[-]>[-]>>>>>[>>>>>[-<<<<+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   633
>>>>]<<<<[->>>>+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>>>>[-<<<<<+>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   634
]<<<<<[->>>>>+<<<+<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>+++++++++++++++[[>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   635
>>>>>]+>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+[>+>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   636
>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>[-<<<<+>>>>]<<<<[->>>>+<<<<<[->>[-<<+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   637
>>]<<[->>+>>+<<<<]+>>>>>>>>>]<<<<<<<<[<<<<<<<<<]]>>>>>>>>>[>>>>>>>>>]<<<<<<<<<[>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   638
[->>>>>>>>>+<<<<<<<<<]<<<<<<<<<<]>[->>>>>>>>>+<<<<<<<<<]<+>>>>>>>>]<<<<<<<<<[>[-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   639
]<->>>>[-<<<<+>[<->-<<<<<<+>>>>>>]<[->+<]>>>>]<<<[->>>+<<<]<+<<<<<<<<<]>>>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   640
[>+>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>>[-<<<<<+>>>>>]<<<<<[->>>>>+<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   641
<<[->>>[-<<<+>>>]<<<[->>>+>+<<<<]+>>>>>>>>>]<<<<<<<<[<<<<<<<<<]]>>>>>>>>>[>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   642
>>>]<<<<<<<<<[>>[->>>>>>>>>+<<<<<<<<<]<<<<<<<<<<<]>>[->>>>>>>>>+<<<<<<<<<]<<+>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   643
>>>>>]<<<<<<<<<[>[-]<->>>>[-<<<<+>[<->-<<<<<<+>>>>>>]<[->+<]>>>>]<<<[->>>+<<<]<+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   644
<<<<<<<<<]>>>>>>>>>[>>>>[-<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   645
>>>>>>>>>>>>>>>>>>>]>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>+++++++++++++++[[>>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   646
>]<<<<<<<<<-<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+>>>>>>>>>>>>>>>>>>>>>+<<<[<<<<<<<<<]
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   647
>>>>>>>>>[>>>[-<<<->>>]+<<<[->>>->[-<<<<+>>>>]<<<<[->>>>+<<<<<<<<<<<<<[<<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   648
]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>[-<<<<->>>>]+<<<<[->>>>-<[-<<<+>>>]<<<[->>>+<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   649
<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   650
>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>->>[-<<<<+>>>>]<<<<[->>>>+<<[-]<<]>>]<<+>>>>[-<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   651
->>>>]+<<<<[->>>>-<<<<<<.>>]>>>>[-<<<<<<<.>>>>>>>]<<<[-]>[-]>[-]>[-]>[-]>[-]>>>[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   652
>[-]>[-]>[-]>[-]>[-]>[-]>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>>>[-]>>>>]<<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   653
[<<<<<<<<<]>+++++++++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>+>>>>>>>>>+<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   654
<<<<<<[<<<<<<<<<]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+[-]>>[>>>>>>>>>]<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   655
<<<<[>>>>>>>[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<[<<<<<<<<<]>>>>>>>[-]+>>>]<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   656
<<<<<<]]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+>>[>+>>>>[-<<<<->>>>]<<<<[->>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   657
>+<<<<]>>>>>>>>]<<+<<<<<<<[>>>>>[->>+<<]<<<<<<<<<<<<<<]>>>>>>>>>[>>>>>>>>>]<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   658
<<<<[>[-]<->>>>>>>[-<<<<<<<+>[<->-<<<+>>>]<[->+<]>>>>>>>]<<<<<<[->>>>>>+<<<<<<]<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   659
+<<<<<<<<<]>>>>>>>-<<<<[-]+<<<]+>>>>>>>[-<<<<<<<->>>>>>>]+<<<<<<<[->>>>>>>->>[>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   660
>>>[->>+<<]>>>>]<<<<<<<<<[>[-]<->>>>>>>[-<<<<<<<+>[<->-<<<+>>>]<[->+<]>>>>>>>]<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   661
<<<<[->>>>>>+<<<<<<]<+<<<<<<<<<]>+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>+<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   662
<<[<<<<<<<<<]>>>>>>>>>[>>>>>[-<<<<<->>>>>]+<<<<<[->>>>>->>[-<<<<<<<+>>>>>>>]<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   663
<<<[->>>>>>>+<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>[-<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   664
<<<<<<->>>>>>>]+<<<<<<<[->>>>>>>-<<[-<<<<<+>>>>>]<<<<<[->>>>>+<<<<<<<<<<<<<<[<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   665
<<<<<<]>>>[-]+>>>>>>[>>>>>>>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   666
<<[<<<<<<<<<]>>>>[-]<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>-<<<<<[<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   667
<<]]>>>]<<<<.>>>>>>>>>>[>>>>>>[-]>>>]<<<<<<<<<[<<<<<<<<<]>++++++++++[-[->>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   668
>+<<<<<<<<<]>>>>>>>>>]>>>>>+>>>>>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   669
<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+[-]>[>>>>>>>>>]<<<<<<<<<[>>>>>>>>[-<<<<<<<+>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   670
>]<<<<<<<[->>>>>>>+<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+>>]<<<<<<<<<<]]>>>>>>>>[-<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   671
<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+>[>+>>>>>[-<<<<<->>>>>]<<<<<[->>>>>+<<<<<]>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   672
>>]<+<<<<<<<<[>>>>>>[->>+<<]<<<<<<<<<<<<<<<]>>>>>>>>>[>>>>>>>>>]<<<<<<<<<[>[-]<-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   673
>>>>>>>>[-<<<<<<<<+>[<->-<<+>>]<[->+<]>>>>>>>>]<<<<<<<[->>>>>>>+<<<<<<<]<+<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   674
<<<]>>>>>>>>-<<<<<[-]+<<<]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>->[>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   675
>>>[->>+<<]>>>]<<<<<<<<<[>[-]<->>>>>>>>[-<<<<<<<<+>[<->-<<+>>]<[->+<]>>>>>>>>]<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   676
<<<<<[->>>>>>>+<<<<<<<]<+<<<<<<<<<]>+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   677
+>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>>>>[-<<<<<<->>>>>>]+<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   678
<<<<<[->>>>>>->>[-<<<<<<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   679
<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   680
-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   681
>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   682
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   683
<<<<<]]>>>]""", "mand")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   684
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   685