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