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