progs/compile_arr3.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 21 Oct 2020 09:24:32 +0100
changeset 785 faa4489267d5
parent 713 0ea14d84efe3
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
//
713
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
     9
//  - makes array access safe by explicit bound checks
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
    10
//    and perform defaults when bounds are exceeded
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
    11
//
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
    12
//  - uses goto_w for jumps in while-loops
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
    13
//
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    14
// Call with
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    15
//
713
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
    16
// scala compile_arr3.scala
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    17
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
    18
// Mandelbrot
713
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
    19
// mand.j size       605196
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
    20
// mand.class size   56812
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
    21
// running time      26 secs
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// the abstract syntax trees
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
abstract class Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
abstract class AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
abstract class BExp 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
type Block = List[Stmt]
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
// statements
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
case object Skip extends Stmt
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    31
case class ArrayDef(s: String, n: Int) extends Stmt
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
case class While(b: BExp, bl: Block) extends Stmt
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    34
case class Assign(s: String, a: AExp) extends Stmt             // var := exp
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    35
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
    36
case class Write(s: String) extends Stmt
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    37
case class Read(s: String) extends Stmt
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// arithmetic expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
case class Var(s: String) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
case class Num(i: Int) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    43
case class Ref(s: String, a: AExp) extends AExp
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// boolean expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
case object True extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
case object False extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
// compiler headers needed for the JVM
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
// (contains an init method, as well as methods for read and write)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
val beginning = """
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
.class public XXX.XXX
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
.super java/lang/Object
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
.method public static write(I)V 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
    .limit locals 1 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    .limit stack 2 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
    getstatic java/lang/System/out Ljava/io/PrintStream; 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
    iload 0
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    62
    i2c       ; Int => Char
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    63
    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
    64
    return 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
.method public static main([Ljava/lang/String;)V
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
   .limit locals 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
   .limit stack 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
    71
; COMPILED CODE STARTS   
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
    72
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
val ending = """
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
    76
; COMPILED CODE ENDS
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
   return
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    82
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
// for generating new labels
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
var counter = -1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
def Fresh(x: String) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  counter += 1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  x ++ "_" ++ counter.toString()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
// environments and instructions
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    94
type Env = Map[String, Int]
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
// convenient string interpolations 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    97
// for instructions and labels
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    98
import scala.language.implicitConversions
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    99
import scala.language.reflectiveCalls
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
implicit def sring_inters(sc: StringContext) = new {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   102
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   103
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   104
}
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   105
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   106
def compile_op(op: String) = op match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   107
  case "+" => i"iadd"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   108
  case "-" => i"isub"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   109
  case "*" => i"imul"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   110
}
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   112
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   113
def compile_num(i: Int) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   114
  if (0 <= i && i <= 5) i"iconst_$i" else 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   115
  if (-128 <= i && i <= 127) i"bipush $i" else i"ldc $i"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   116
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   117
def compile_aload(i: Int) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   118
  if (0 <= i && i <= 3) i"aload_$i" else i"aload $i"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   119
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   120
def compile_iload(i: Int) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   121
  if (0 <= i && i <= 3) i"iload_$i" else i"iload $i"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   122
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   123
def compile_istore(i: Int) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   124
  if (0 <= i && i <= 3) i"istore_$i" else i"istore $i"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   125
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
// arithmetic expression compilation
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   127
def compile_aexp(a: AExp, env : Env) : String = a match {
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   128
  case Num(i) => compile_num(i)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   129
  case Var(s) => compile_iload(env(s))
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   130
  case Aop(op, a1, a2) => 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   131
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
712
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   132
  case Ref(s, a) => {
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   133
    val arr_safe = Fresh("Arr_safe")
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   134
    val arr_end = Fresh("Arr_end_")
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   135
    compile_aexp(a, env) ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   136
    compile_aload(env(s)) ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   137
    i"dup2" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   138
    i"arraylength" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   139
    i"if_icmple $arr_safe" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   140
    i"pop2" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   141
    i"iconst_0" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   142
    i"goto $arr_end" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   143
    l"$arr_safe" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   144
    i"swap" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   145
    i"iaload" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   146
    l"$arr_end"
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   147
  }
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   150
def compile_bop(op: String, jmp: String) = op match {
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   151
  case "==" => i"if_icmpne $jmp"
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   152
  case "!=" => i"if_icmpeq $jmp"
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   153
  case "<"  => i"if_icmpge $jmp"
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   154
}
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   155
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   156
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
// boolean expression compilation
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   158
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
   159
  case True => ""
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   160
  case False => i"goto $jmp"
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   161
  case Bop(op, a1, a2) => 
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   162
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_bop(op, jmp)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
// statement compilation
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   166
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   167
  case Skip => ("", env)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
  case Assign(x, a) => {
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   169
     val index = env.getOrElse(x, env.keys.size) //
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   170
    (compile_aexp(a, env) ++ compile_istore(index), env + (x -> index)) 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
  } 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
  case If(b, bl1, bl2) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
    val if_else = Fresh("If_else")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
    val if_end = Fresh("If_end")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
    val (instrs1, env1) = compile_block(bl1, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
    val (instrs2, env2) = compile_block(bl2, env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
    (compile_bexp(b, env, if_else) ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
     instrs1 ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   179
     i"goto $if_end" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   180
     l"$if_else" ++
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
     instrs2 ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   182
     l"$if_end", env2)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  case While(b, bl) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
    val loop_begin = Fresh("Loop_begin")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
    val loop_end = Fresh("Loop_end")
713
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
   187
    val loop_false = Fresh("Loop_false")
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
   188
    val loop_true = Fresh("Loop_true")
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
    val (instrs1, env1) = compile_block(bl, env)
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   190
    (l"$loop_begin" ++
713
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
   191
     compile_bexp(b, env, loop_false) ++
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
   192
     i"goto $loop_true" ++
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
   193
     l"$loop_false" ++
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
   194
     i"goto_w $loop_end" ++ 
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
   195
     l"$loop_true" ++
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
     instrs1 ++
713
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
   197
     i"goto_w $loop_begin" ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   198
     l"$loop_end", env1)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
  case Write(x) => 
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   201
    (compile_iload(env(x)) ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   202
     i"invokestatic XXX/XXX/write(I)V", env)
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   203
  case Read(x) => {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   204
    val index = env.getOrElse(x, env.keys.size) 
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   205
    (i"invokestatic XXX/XXX/read()I" ++     
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   206
     compile_istore(index), env + (x -> index))
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   207
  }
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   208
  case ArrayDef(s: String, n: Int) => {
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   209
    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
   210
                    env.keys.size
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   211
    (i"ldc $n" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   212
     i"newarray int" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   213
     i"astore $index", env + (s -> index))
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   214
  }
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   215
  case AssignA(s, a1, a2) => {
712
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   216
    val arr_safe = Fresh("Arr_safe")
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   217
    val arr_end = Fresh("Arr_end_")
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   218
    val index = if (env.isDefinedAt(s)) env(s) else 
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   219
                    throw new Exception("array not defined")
712
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   220
    (compile_aexp(a1, env) ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   221
     compile_aload(env(s)) ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   222
     i"dup2" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   223
     i"arraylength" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   224
     i"if_icmple $arr_safe" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   225
     i"pop2" ++
713
0ea14d84efe3 updated
Christian Urban <urbanc@in.tum.de>
parents: 712
diff changeset
   226
     i"goto_w $arr_end" ++
712
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   227
     l"$arr_safe" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   228
     i"swap" ++
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   229
     compile_aexp(a2, env) ++
712
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   230
     i"iastore" ++
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   231
     l"$arr_end", env)
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   232
  } 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   235
// compilation of a block (i.e. list of statements)
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   236
def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   237
  case Nil => ("", env)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
  case s::bl => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
    val (instrs1, env1) = compile_stmt(s, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
    val (instrs2, env2) = compile_block(bl, env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
    (instrs1 ++ instrs2, env2)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   245
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
// main compilation function for blocks
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
def compile(bl: Block, class_name: String) : String = {
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   248
  val instructions = compile_block(bl, Map())._1
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   249
  (beginning ++ instructions ++ ending).replaceAllLiterally("XXX", class_name)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   253
// Fibonacci numbers as a test-case
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   254
val fib_test = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   255
  List(Read("n"),                       //  read n;                     
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   256
       Assign("minus1",Num(0)),         //  minus1 := 0;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   257
       Assign("minus2",Num(1)),         //  minus2 := 1;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   258
       Assign("temp",Num(0)),           //  temp := 0;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   259
       While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   260
          List(Assign("temp",Var("minus2")),    //  temp := minus2;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   261
               Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   262
                                        //  minus2 := minus1 + minus2;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   263
               Assign("minus1",Var("temp")), //  minus1 := temp;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   264
               Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   265
       Write("minus1"))                 //  write minus1
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   266
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   267
// prints out the JVM-assembly instructions for fib
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   268
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   269
// println(compile(fib_test, "fib"))
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   270
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   271
// can be assembled by hand with 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   272
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   273
//    java -jar jvm/jasmin-2.4/jasmin.jar fib.j
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   274
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   275
// and started with
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   276
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   277
//    java fib/fib
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   278
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
import scala.util._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
import scala.sys.process._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
def time_needed[T](i: Int, code: => T) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
  val start = System.nanoTime()
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   284
  for (j <- 2 to i) code
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   285
  val result = code
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
  val end = System.nanoTime()
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   287
  ((end - start) / (i * 1.0e9), result)
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   288
}
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   289
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   290
def compile_to_file(bl: Block, class_name: String) : Unit = 
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   291
  Using(new java.io.FileWriter(class_name + ".j")) {
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   292
    _.write(compile(bl, class_name))
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   293
  }
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   294
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   295
def compile_and_run(bl: Block, class_name: String) : Unit = {
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   296
  println(s"Start compilation of $class_name")
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   297
  compile_to_file(bl, class_name)
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   298
  println("generated .j file")
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   299
  (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!!
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   300
  println("generated .class file ")
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   301
  println("Time: " + time_needed(3, (s"java ${class_name}/${class_name}").!)._1)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   305
val arr_test = 
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   306
  List(ArrayDef("a", 10),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   307
       ArrayDef("b", 2),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   308
       AssignA("a", Num(0), Num(10)),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   309
       Assign("x", Ref("a", Num(0))),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   310
       Write("x"),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   311
       AssignA("b", Num(1), Num(5)),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   312
       Assign("x", Ref("b", Num(1))),
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   313
       Write("x"))       
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   314
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
//compile_and_run(arr_test, "a")
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
//====================
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   319
// Parser Combinators
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   320
//====================
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   321
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   322
import scala.language.implicitConversions
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   323
import scala.language.reflectiveCalls
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
// more convenience for the semantic actions later on
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   326
case class ~[+A, +B](_1: A, _2: B)
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
type IsSeq[A] = A => Seq[_]
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   329
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   330
abstract class Parser[I : IsSeq, T] {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   331
  def parse(ts: I): Set[(T, I)]
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   332
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   333
  def parse_all(ts: I) : Set[T] =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   334
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
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
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
   338
  def parse(sb: I) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   339
    for ((head1, tail1) <- p.parse(sb); 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   340
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   341
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   342
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   343
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
   344
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
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
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
   348
  def parse(sb: I) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   349
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
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
import scala.util.matching.Regex
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   354
case class RegexParser(reg: Regex) extends Parser[String, String] {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   355
  def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   356
    case None => Set()
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   357
    case Some(m) => Set((m.matched, m.after.toString))  
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   358
  }
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   359
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   360
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   361
def StringParser(s: String) = RegexParser(Regex.quote(s).r)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   362
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   363
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   364
implicit def string2parser(s : String) = StringParser(s)
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
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   367
  def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   368
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   369
  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
   370
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   371
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   372
implicit def StringOps(s: String) = new {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   373
  def | (q : => Parser[String, String]) = new AltParser[String, String](s, q)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   374
  def | (r: String) = new AltParser[String, String](s, r)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   375
  def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   376
  def ~[S](q : => Parser[String, S]) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   377
    new SeqParser[String, String, S](s, q)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   378
  def ~ (r: String) = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   379
    new SeqParser[String, String, String](s, r)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   380
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   381
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   382
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   383
val NumParser = RegexParser("[0-9]+".r) ==> (s => s.toInt : Int)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   384
val IdParser = RegexParser("[a-z][a-z,0-9]*".r)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   385
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   386
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
// Grammar Rules for WHILE with arrays
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   389
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   390
lazy val AExp: Parser[String, AExp] = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   391
  (Te ~ "+" ~ AExp) ==> { case x ~ _ ~ z => Aop("+", x, z):AExp } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   392
  (Te ~ "-" ~ AExp) ==> { case x ~ _ ~ z => Aop("-", x, z):AExp } | Te
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   393
lazy val Te: Parser[String, AExp] = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   394
  (Fa ~ "*" ~ Te) ==> { case x ~ _ ~ z => Aop("*", x, z):AExp } | Fa
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   395
lazy val Fa: Parser[String, AExp] = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   396
   ("(" ~ AExp ~ ")") ==> { case _ ~ y ~ _ => y } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   397
   (IdParser ~ "[" ~ AExp ~ "]") ==> { case x ~ _ ~ y ~ _ => Ref(x, y) } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   398
   IdParser ==> Var | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   399
   NumParser ==> Num
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   400
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   401
// boolean expressions
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   402
lazy val BExp: Parser[String, BExp] = 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   403
   (AExp ~ "=" ~ AExp) ==> { case x ~ y ~ z => Bop("=", x, z):BExp } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   404
   (AExp ~ "!=" ~ AExp) ==> { case x ~ y ~ z => Bop("!=", x, z):BExp } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   405
   (AExp ~ "<" ~ AExp) ==> { case x ~ y ~ z => Bop("<", x, z):BExp } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   406
   (AExp ~ ">" ~ AExp) ==> { case x ~ y ~ z => Bop("<", z, x):BExp } | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   407
   ("true" ==> ((_) => True:BExp )) | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   408
   ("false" ==> ((_) => False:BExp )) |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   409
   ("(" ~ BExp ~ ")") ==> { case x ~ y ~ z => y}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   410
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   411
lazy val Stmt: Parser[String, Stmt] =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   412
   ("skip" ==> (_ => Skip: Stmt)) |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   413
   (IdParser ~ ":=" ~ AExp) ==> { case x ~ y ~z => Assign(x, z): Stmt } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   414
   (IdParser ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp) ==> { 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   415
     case x ~ _ ~ z ~ _ ~ _ ~ u => AssignA(x, z, u): Stmt } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   416
   ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   417
    { case _ ~ y ~ _ ~ u ~ _ ~w => If(y, u, w): Stmt } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   418
   ("while" ~ BExp ~ "do" ~ Block) ==> { case _ ~ y ~ _ ~ w => While(y, w) } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   419
   ("new(" ~ IdParser ~ "[" ~ NumParser ~ "])") ==> { 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   420
    case _ ~ y ~ _ ~ u ~ _ => ArrayDef(y, u) } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   421
   ("write" ~ IdParser) ==> { case _ ~ y => Write(y) } 
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
lazy val Stmts: Parser[String, Block] =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   424
  (Stmt ~ ";" ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } |
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   425
  (Stmt ==> ((s) => List(s) : Block)) 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   426
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   427
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   428
lazy val Block: Parser[String, Block] =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   429
   ("{" ~ Stmts ~ "}") ==> { case _ ~ y ~ _ => y} | 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   430
   (Stmt ==> (s => List(s)))
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   431
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   432
//Stmts.parse_all("x2:=5+a")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   433
//Stmts.parse_all("x2:=5+a[3+a]")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   434
//Stmts.parse_all("a[x2+3]:=5+a[3+a]")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   435
//Block.parse_all("{x:=5;y:=8}")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   436
//Block.parse_all("if(false)then{x:=5}else{x:=10}")
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
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   440
val fib = """
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   441
   n := 10;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   442
   minus1 := 0;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   443
   minus2 := 1;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   444
   temp:=0;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   445
   while (n > 0) do {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   446
     temp := minus2; 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   447
     minus2 := minus1 + minus2;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   448
     minus1 := temp;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   449
     n := n - 1};
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   450
   result := minus2;
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   451
   write result
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   452
   """.replaceAll("\\s+", "")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   453
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   454
val fib_prog = Stmts.parse_all(fib).toList
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   455
//compile_and_run(fib_prog.head, "fib")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   456
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   457
//======================================
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   458
// BF transpiler into WHILE with arrays
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   459
//======================================
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   460
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   461
// simple BF instructions translation
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   462
def instr(c: Char) : String = c match {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   463
  case '>' => "ptr := ptr + 1;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   464
  case '<' => "ptr := ptr - 1;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   465
  case '+' => "mem[ptr] := mem[ptr] + 1;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   466
  case '-' => "mem[ptr] := mem[ptr] - 1;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   467
  case '.' => "x := mem[ptr]; write x;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   468
  //case ',' => "XXX" // "ptr = getchar();\n"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   469
  case '['  => "while (mem[ptr] != 0) do {"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   470
  case ']'  => "skip};"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   471
  case _ => ""
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   472
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   473
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   474
def instrs(prog: String) : String =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   475
  prog.toList.map(instr).mkString
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   476
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   477
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   478
// Note: the mandelbrot.bf program is so large that
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   479
// it does not fit inside the limitations of what the
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   480
// JVM imposes on methods (only 64K of instructions).
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   481
// Therefore some optimisations are first applied to 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   482
// BF programs before WHILE programs are created. The
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   483
// optimisations are 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   484
//  
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   485
//  - replacing BF-loops of the form [-] 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   486
//  - combining single increment/decrement instructions
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   487
//
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   488
// The size of the resulting .j-file is 270K.
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   489
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   490
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
   491
  case (Nil, acc) => acc
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   492
  case (c :: cs, Nil) => splice(cs, List((c, 1)))
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   493
  case (c :: cs, (d, n) :: acc) => 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   494
    if (c == d) splice(cs, (c, n + 1) :: acc)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   495
    else splice(cs, (c, 1) :: (d, n) :: acc)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   496
}
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   497
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   498
def spl(s: String) = splice(s.toList, Nil).reverse
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
def instr2(c: Char, n: Int) : String = c match {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   501
  case '>' => s"ptr := ptr + $n;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   502
  case '<' => s"ptr := ptr - $n;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   503
  case '0' => s"mem[ptr] := 0;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   504
  case '+' => s"mem[ptr] := mem[ptr] + $n;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   505
  case '-' => s"mem[ptr] := mem[ptr] - $n;"
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   506
  case '.' => s"x := mem[ptr]; write x;" 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   507
  case '['  => "while (mem[ptr] != 0) do {" * n 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   508
  case ']'  => "skip};" * n
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   509
  case _ => ""
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
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   512
def instrs2(prog: String) : String =
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   513
  spl(prog.replaceAll("""\[-\]""", "0")).map{ case (c, n) => instr2(c, n) }.mkString
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
def bf_str(prog: String) : String = {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   516
  "new(mem[30000]);" ++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   517
  "ptr := 15000;" ++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   518
  instrs2(prog) ++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   519
  "skip"
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
def bf_run(prog: String, name: String) = {
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   523
  println(s"BF pre-processing of $name")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   524
  val bf_string = bf_str(prog).replaceAll("\\s", "")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   525
  println(s"BF parsing (program length ${bf_string.length} characters)")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   526
  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
   527
  println(s"BF generated WHILE program (needed $time for parsing)")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   528
  compile_and_run(bf_prog, name)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   529
}
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
// a benchmark program (counts down from 'Z' to 'A')
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   532
val bf0 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
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
            ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   535
            +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
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
bf_run(bf0, "bench")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   538
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   539
// Sierpinski triangle
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   540
val bf1 = """++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
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
bf_run(bf1, "sier")
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
// Helo World!
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   547
bf_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   548
       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", "hello")
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
// Fibonacci 
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   551
val bf2 = """+++++++++++
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
      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
712
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   561
      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]
e71eb9ce2373 updated
Christian Urban <urbanc@in.tum.de>
parents: 710
diff changeset
   562
      [-]++++++++++."""
710
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
bf_run(bf2, "fibs")
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
// Mandelbrot Set
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
// Note: Parsing of the generated WHILE program (around 60K in size)
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   570
// takes approximately 10 minutes
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
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   573
710
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   574
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
   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
]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>[-<<<<->>>>]+<<<<[->>>>-<[-<<<+>>>]<<<[->>>+<
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
>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>->>[-<<<<+>>>>]<<<<[->>>>+<<[-]<<]>>]<<+>>>>[-<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   686
->>>>]+<<<<[->>>>-<<<<<<.>>]>>>>[-<<<<<<<.>>>>>>>]<<<[-]>[-]>[-]>[-]>[-]>[-]>>>[
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   687
>[-]>[-]>[-]>[-]>[-]>[-]>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>>>[-]>>>>]<<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   688
[<<<<<<<<<]>+++++++++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>+>>>>>>>>>+<<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   689
<<<<<<[<<<<<<<<<]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+[-]>>[>>>>>>>>>]<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   690
<<<<[>>>>>>>[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<[<<<<<<<<<]>>>>>>>[-]+>>>]<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   691
<<<<<<]]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+>>[>+>>>>[-<<<<->>>>]<<<<[->>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   692
>+<<<<]>>>>>>>>]<<+<<<<<<<[>>>>>[->>+<<]<<<<<<<<<<<<<<]>>>>>>>>>[>>>>>>>>>]<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   693
<<<<[>[-]<->>>>>>>[-<<<<<<<+>[<->-<<<+>>>]<[->+<]>>>>>>>]<<<<<<[->>>>>>+<<<<<<]<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   694
+<<<<<<<<<]>>>>>>>-<<<<[-]+<<<]+>>>>>>>[-<<<<<<<->>>>>>>]+<<<<<<<[->>>>>>>->>[>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   695
>>>[->>+<<]>>>>]<<<<<<<<<[>[-]<->>>>>>>[-<<<<<<<+>[<->-<<<+>>>]<[->+<]>>>>>>>]<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   696
<<<<[->>>>>>+<<<<<<]<+<<<<<<<<<]>+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>+<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   697
<<[<<<<<<<<<]>>>>>>>>>[>>>>>[-<<<<<->>>>>]+<<<<<[->>>>>->>[-<<<<<<<+>>>>>>>]<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   698
<<<[->>>>>>>+<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>[-<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   699
<<<<<<->>>>>>>]+<<<<<<<[->>>>>>>-<<[-<<<<<+>>>>>]<<<<<[->>>>>+<<<<<<<<<<<<<<[<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   700
<<<<<<]>>>[-]+>>>>>>[>>>>>>>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   701
<<[<<<<<<<<<]>>>>[-]<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>-<<<<<[<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   702
<<]]>>>]<<<<.>>>>>>>>>>[>>>>>>[-]>>>]<<<<<<<<<[<<<<<<<<<]>++++++++++[-[->>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   703
>+<<<<<<<<<]>>>>>>>>>]>>>>>+>>>>>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   704
<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+[-]>[>>>>>>>>>]<<<<<<<<<[>>>>>>>>[-<<<<<<<+>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   705
>]<<<<<<<[->>>>>>>+<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+>>]<<<<<<<<<<]]>>>>>>>>[-<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   706
<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+>[>+>>>>>[-<<<<<->>>>>]<<<<<[->>>>>+<<<<<]>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   707
>>]<+<<<<<<<<[>>>>>>[->>+<<]<<<<<<<<<<<<<<<]>>>>>>>>>[>>>>>>>>>]<<<<<<<<<[>[-]<-
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   708
>>>>>>>>[-<<<<<<<<+>[<->-<<+>>]<[->+<]>>>>>>>>]<<<<<<<[->>>>>>>+<<<<<<<]<+<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   709
<<<]>>>>>>>>-<<<<<[-]+<<<]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>->[>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   710
>>>[->>+<<]>>>]<<<<<<<<<[>[-]<->>>>>>>>[-<<<<<<<<+>[<->-<<+>>]<[->+<]>>>>>>>>]<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   711
<<<<<[->>>>>>>+<<<<<<<]<+<<<<<<<<<]>+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   712
+>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>>>>[-<<<<<<->>>>>>]+<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   713
<<<<<[->>>>>>->>[-<<<<<<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   714
<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   715
-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   716
>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   717
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   718
<<<<<]]>>>]""", "mand")
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   719
183663740fb7 updated
Christian Urban <urbanc@in.tum.de>
parents: 708
diff changeset
   720