progs/compile_arr.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 18 Nov 2019 11:11:11 +0000
changeset 694 f16459e2c490
parent 616 96114cc02150
child 695 a936b1717b1b
permissions -rw-r--r--
updated
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 <init>()V
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
   aload_0
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
   invokenonvirtual java/lang/Object/<init>()V
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
   return
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
.end method
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
.method public static write(I)V 
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
    .limit locals 1 
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
    .limit stack 2 
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
    getstatic java/lang/System/out Ljava/io/PrintStream; 
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
    iload 0
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    50
    i2c       ; Int => Char
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    51
    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
    52
    return 
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
.end method
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    55
.method public static read()I 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    56
    .limit locals 10 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    57
    .limit stack 10
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    58
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    59
    ldc 0 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    60
    istore 1  ; this will hold our final integer 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    61
Label1: 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    62
    getstatic java/lang/System/in Ljava/io/InputStream; 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    63
    invokevirtual java/io/InputStream/read()I 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    64
    istore 2 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    65
    iload 2 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    66
    ldc 10   ; the newline delimiter 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    67
    isub 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    68
    ifeq Label2 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    69
    iload 2 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    70
    ldc 32   ; the space delimiter 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    71
    isub 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    72
    ifeq Label2
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    73
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    74
    iload 2 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    75
    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
    76
    isub 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    77
    ldc 10 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    78
    iload 1 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    79
    imul 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    80
    iadd 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    81
    istore 1 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    82
    goto Label1 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    83
Label2: 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    84
    ;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
    85
    iload 1 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    86
    ireturn 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    87
.end method
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    88
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
.method public static main([Ljava/lang/String;)V
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
   .limit locals 200
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
   .limit stack 200
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
"""
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
val ending = """
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
   return
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
.end method
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
"""
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   102
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
// for generating new labels
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
var counter = -1
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
def Fresh(x: String) = {
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
  counter += 1
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
  x ++ "_" ++ counter.toString()
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
}
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
// environments and instructions
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   114
type Env = Map[String, Int]
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   115
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   116
// convenient string interpolations 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   117
// for instructions and labels
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   118
import scala.language.implicitConversions
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   119
import scala.language.reflectiveCalls
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   120
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   121
implicit def sring_inters(sc: StringContext) = new {
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   122
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   123
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   124
}
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   125
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   126
def compile_op(op: String) = op match {
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   127
  case "+" => i"iadd"
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   128
  case "-" => i"isub"
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   129
  case "*" => i"imul"
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   130
}
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
// arithmetic expression compilation
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   133
def compile_aexp(a: AExp, env : Env) : String = a match {
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   134
  case Num(i) => i"ldc $i"
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   135
  case Var(s) => i"iload ${env(s)} \t\t; $s"
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   136
  case Aop(op, a1, a2) => 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   137
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   138
  case Ref(s, a) =>
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   139
    i"aload ${env(s)}" ++ compile_aexp(a, env) ++  i"iaload"
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
}
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
// boolean expression compilation
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   143
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   144
    case True => ""
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   145
  case False => i"goto $jmp"
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   146
  case Bop("==", a1, a2) => 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   147
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpne $jmp"
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  case Bop("!=", a1, a2) => 
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   149
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpeq $jmp"
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  case Bop("<", a1, a2) => 
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   151
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpge $jmp"
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
}
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
// statement compilation
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   155
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match {
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   156
  case Skip => ("", env)
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
  case Assign(x, a) => {
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   158
     val index = env.getOrElse(x, env.keys.size)
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   159
    (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
   160
  } 
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
  case If(b, bl1, bl2) => {
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
    val if_else = Fresh("If_else")
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
    val if_end = Fresh("If_end")
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
    val (instrs1, env1) = compile_block(bl1, env)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
    val (instrs2, env2) = compile_block(bl2, env1)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
    (compile_bexp(b, env, if_else) ++
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
     instrs1 ++
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   168
     i"goto $if_end" ++
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   169
     l"$if_else" ++
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
     instrs2 ++
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   171
     l"$if_end", env2)
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
  }
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
  case While(b, bl) => {
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
    val loop_begin = Fresh("Loop_begin")
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
    val loop_end = Fresh("Loop_end")
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
    val (instrs1, env1) = compile_block(bl, env)
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   177
    (l"$loop_begin" ++
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
     compile_bexp(b, env, loop_end) ++
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
     instrs1 ++
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   180
     i"goto $loop_begin" ++
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   181
     l"$loop_end", env1)
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
  }
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
  case Write(x) => 
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   184
    (i"iload ${env(x)} \t\t; $x" ++ 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   185
     i"invokestatic XXX/XXX/write(I)V", env)
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   186
  case Read(x) => {
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   187
    val index = env.getOrElse(x, env.keys.size) 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   188
    (i"invokestatic XXX/XXX/read()I" ++ 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   189
     i"istore $index \t\t; $x", env + (x -> index))
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   190
  }
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   191
  case Array(s: String, n: Int) => {
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   192
    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
   193
                    env.keys.size
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   194
    (i"ldc $n" ++
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   195
     i"newarray int" ++
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   196
     i"astore $index", env + (s -> index))
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   197
  }
612
274477667793 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   198
  case AssignA(s, a1, a2) => {
274477667793 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   199
    val index = if (env.isDefinedAt(s)) env(s) else 
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   200
                    throw new Exception("array not defined")
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   201
    (i"aload ${env(s)}" ++
612
274477667793 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   202
     compile_aexp(a1, env) ++
274477667793 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   203
     compile_aexp(a2, env) ++
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   204
     i"iastore", env)
612
274477667793 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   205
  } 
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
}
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
// compilation of a block (i.e. list of instructions)
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   209
def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   210
  case Nil => ("", env)
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
  case s::bl => {
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
    val (instrs1, env1) = compile_stmt(s, env)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
    val (instrs2, env2) = compile_block(bl, env1)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
    (instrs1 ++ instrs2, env2)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
  }
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
}
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   218
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
// main compilation function for blocks
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
def compile(bl: Block, class_name: String) : String = {
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
  val instructions = compile_block(bl, Map.empty)._1
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
  (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
}
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   226
// Fibonacci numbers as a test-case
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   227
val fib_test = 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   228
  List(Read("n"),                       //  read n;                     
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   229
       Assign("minus1",Num(0)),         //  minus1 := 0;
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   230
       Assign("minus2",Num(1)),         //  minus2 := 1;
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   231
       Assign("temp",Num(0)),           //  temp := 0;
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   232
       While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   233
          List(Assign("temp",Var("minus2")),    //  temp := minus2;
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   234
               Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   235
                                        //  minus2 := minus1 + minus2;
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   236
               Assign("minus1",Var("temp")), //  minus1 := temp;
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   237
               Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   238
       Write("minus1"))                 //  write minus1
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   239
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   240
// prints out the JVM-assembly program
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   241
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   242
// println(compile(fib_test, "fib"))
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   243
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   244
// can be assembled with 
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
//
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
//    java -jar jvm/jasmin-2.4/jasmin.jar fib.j
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
//
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
// and started with
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
//
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
//    java fib/fib
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
import scala.util._
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
import scala.sys.process._
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
import scala.io
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
def compile_tofile(bl: Block, class_name: String) = {
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
  val output = compile(bl, class_name)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
  val fw = new java.io.FileWriter(class_name + ".j") 
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
  fw.write(output) 
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
  fw.close()
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
}
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
def compile_all(bl: Block, class_name: String) : Unit = {
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
  compile_tofile(bl, class_name)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
  println("compiled ")
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
  val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!!
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
  println("assembled ")
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
}
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
def time_needed[T](i: Int, code: => T) = {
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
  val start = System.nanoTime()
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
  for (j <- 1 to i) code
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
  val end = System.nanoTime()
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
  (end - start)/(i * 1.0e9)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
}
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
def compile_run(bl: Block, class_name: String) : Unit = {
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
  println("Start compilation")
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
  compile_all(bl, class_name)
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
  println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!))
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
}
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   285
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   286
val arr_test = 
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   287
  List(Array("a", 10),
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   288
       Array("b", 2),
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   289
       AssignA("a", Num(0), Num(10)),
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   290
       Assign("x", Ref("a", Num(0))),
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   291
       Write("x"),
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   292
       AssignA("b", Num(1), Num(5)),
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   293
       Assign("x", Ref("b", Num(1))),
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   294
       Write("x"))       
609
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
ff17a6f694dd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   297
//compile_run(arr_test, "a")
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   298
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   299
//====================
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   300
// Parser Combinators
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   301
//====================
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   302
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   303
import scala.language.implicitConversions
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   304
import scala.language.reflectiveCalls
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   305
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   306
type IsSeq[A] = A => Seq[_]
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   307
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   308
abstract class Parser[I : IsSeq, T] {
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   309
  def parse(ts: I): Set[(T, I)]
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   310
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   311
  def parse_all(ts: I) : Set[T] =
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   312
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
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 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
   316
  def parse(sb: I) = 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   317
    for ((head1, tail1) <- p.parse(sb); 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   318
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   319
}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   320
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   321
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
   322
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
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
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   325
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
   326
  def parse(sb: I) = 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   327
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   328
}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   329
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
import scala.util.matching.Regex
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   332
case class RegexParser(reg: Regex) extends Parser[String, String] {
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   333
  def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   334
    case None => Set()
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   335
    case Some(m) => Set((m.matched, m.after.toString))  
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   336
  }
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   337
}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   338
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   339
def StringParser(s: String) = RegexParser(Regex.quote(s).r)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   340
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   341
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   342
implicit def string2parser(s : String) = StringParser(s)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   343
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   344
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   345
  def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   346
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   347
  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
   348
}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   349
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   350
implicit def StringOps(s: String) = new {
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   351
  def | (q : => Parser[String, String]) = new AltParser[String, String](s, q)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   352
  def | (r: String) = new AltParser[String, String](s, r)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   353
  def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   354
  def ~[S](q : => Parser[String, S]) = 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   355
    new SeqParser[String, String, S](s, q)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   356
  def ~ (r: String) = 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   357
    new SeqParser[String, String, String](s, r)
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
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   361
val NumParser = RegexParser("[0-9]+".r) ==> (s => s.toInt : Int)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   362
val IdParser = RegexParser("[a-z][a-z,0-9]*".r)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   363
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   364
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   365
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   366
// Grammar Rules
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   367
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   368
lazy val AExp: Parser[String, AExp] = 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   369
  (Te ~ "+" ~ AExp) ==> { case ((x, _), z) => Aop("+", x, z):AExp } |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   370
  (Te ~ "-" ~ AExp) ==> { case ((x, _), z) => Aop("-", x, z):AExp } | Te
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   371
lazy val Te: Parser[String, AExp] = 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   372
  (Fa ~ "*" ~ Te) ==> { case ((x, _), z) => Aop("*", x, z):AExp } | Fa
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   373
lazy val Fa: Parser[String, AExp] = 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   374
   ("(" ~ AExp ~ ")") ==> { case ((_, y), _) => y } | 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   375
   (IdParser ~ "[" ~ AExp ~ "]") ==> { case (((x, _), y), _) => Ref(x, y) } |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   376
   IdParser ==> Var | 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   377
   NumParser ==> Num
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   378
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   379
// boolean expressions
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   380
lazy val BExp: Parser[String, BExp] = 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   381
   (AExp ~ "=" ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z):BExp } | 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   382
   (AExp ~ "!=" ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z):BExp } | 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   383
   (AExp ~ "<" ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z):BExp } | 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   384
   (AExp ~ ">" ~ AExp) ==> { case ((x, y), z) => Bop("<", z, x):BExp } | 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   385
   ("true" ==> ((_) => True:BExp )) | 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   386
   ("false" ==> ((_) => False:BExp )) |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   387
   ("(" ~ BExp ~ ")") ==> { case ((x, y), z) => y}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   388
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   389
lazy val Stmt: Parser[String, Stmt] =
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   390
   ("skip" ==> (_ => Skip: Stmt)) |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   391
   (IdParser ~ ":=" ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   392
   (IdParser ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp) ==> { 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   393
     case (((((x, y), z), v), w), u) => AssignA(x, z, u): Stmt } |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   394
   ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   395
    { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   396
   ("while" ~ BExp ~ "do" ~ Block) ==> { case (((x, y), z), w) => While(y, w) } |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   397
   ("new" ~ IdParser ~ "[" ~ NumParser ~ "]") ==> { case ((((x, y), z), u), v) => Array(y, u) } |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   398
   ("write" ~ IdParser) ==> { case (_, y) => Write(y) } 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   399
 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   400
lazy val Stmts: Parser[String, Block] =
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   401
  (Stmt ~ ";" ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } |
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   402
  (Stmt ==> ((s) => List(s) : Block)) 
611
df7c3505e479 updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   403
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   404
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   405
lazy val Block: Parser[String, Block] =
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   406
   ("{" ~ Stmts ~ "}") ==> { case ((x, y), z) => y} | 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   407
   (Stmt ==> (s => List(s)))
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   408
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   409
//Stmts.parse_all("x2:=5+a")
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   410
//Stmts.parse_all("x2:=5+a[3+a]")
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   411
//Stmts.parse_all("a[x2+3]:=5+a[3+a]")
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   412
//Block.parse_all("{x:=5;y:=8}")
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   413
//Block.parse_all("if(false)then{x:=5}else{x:=10}")
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   414
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   415
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   416
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   417
val fib = """
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   418
   n := 10;
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   419
   minus1 := 0;
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   420
   minus2 := 1;
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   421
   temp:=0;
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   422
   while (n > 0) do {
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   423
     temp := minus2; 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   424
     minus2 := minus1 + minus2;
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   425
     minus1 := temp;
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   426
     n := n - 1};
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   427
   result := minus2;
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   428
   write result
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   429
   """.replaceAll("\\s+", "")
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
val fib_prog = Stmts.parse_all(fib).toList
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   432
//compile_run(fib_prog.head, "fib")
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   433
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   434
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   435
// BF 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   436
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   437
// simple instructions
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   438
def instr(c: Char) : String = c match {
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   439
  case '>' => "ptr := ptr + 1;"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   440
  case '<' => "ptr := ptr - 1;"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   441
  case '+' => "field[ptr] := field[ptr] + 1;"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   442
  case '-' => "field[ptr] := field[ptr] - 1;"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   443
  case '.' => "x := field[ptr]; write x;"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   444
  //case ',' => "XXX" // "ptr = getchar();\n"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   445
  case '['  => "while (field[ptr] != 0) do {"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   446
  case ']'  => "skip};"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   447
  case _ => ""
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   448
}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   449
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   450
def instrs(prog: String) : String =
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   451
  prog.toList.map(instr).mkString
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   452
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   453
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   454
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
   455
  case (Nil, acc) => acc
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   456
  case (c :: cs, Nil) => splice(cs, List((c, 1)))
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   457
  case (c :: cs, (d, n) :: acc) => 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   458
    if (c == d) splice(cs, (c, n + 1) :: acc)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   459
    else splice(cs, (c, 1) :: (d, n) :: acc)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   460
}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   461
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   462
def spl(s: String) = splice(s.toList, Nil).reverse
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   463
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   464
def instr2(c: Char, n: Int) : String = c match {
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   465
  case '>' => "ptr := ptr + " + n.toString + ";"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   466
  case '<' => "ptr := ptr - " + n.toString + ";"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   467
  case '+' => "field[ptr] := field[ptr] + " + n.toString + ";"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   468
  case '-' => "field[ptr] := field[ptr] - " + n.toString + ";"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   469
  case '.' => "x := field[ptr]; write x;" //* n
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   470
  //case ',' => "*ptr = getchar();\n" * n
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   471
  case '['  => "while (field[ptr] != 0) do {" * n 
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   472
  case ']'  => "skip};" * n
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   473
  case _ => ""
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   474
}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   475
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   476
def instrs2(prog: String) : String =
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   477
  spl(prog).map{ case (c, n) => instr2(c, n) }.mkString
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   478
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   479
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   480
def bf_str(prog: String) : String = {
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   481
  "\n" ++
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   482
  //"new field[30000];\n" ++
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   483
  "ptr := 15000;" ++
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   484
  instrs(prog) ++
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   485
  "skip"
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   486
}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   487
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   488
def bf_run(prog: String, name: String) = {
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   489
  println("BF processing start")
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   490
  val bf_string = bf_str(prog).replaceAll("\\s", "")
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   491
  
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   492
  println(s"BF parsing start (string length ${bf_string.length})")
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   493
  val bf_prog = Stmts.parse_all(bf_string).toList.head
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   494
  println(s"BF Compile start ${bf_string.toList.length} characters")
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   495
  compile_run(Array("field", 30000) :: bf_prog, name)
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   496
}
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   497
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   498
// a benchmark program (counts down from 'Z' to 'A')
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   499
val bf0 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   500
            [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   501
            ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   502
            +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   503
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   504
bf_run(bf0, "bench")
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   505
616
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   506
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
val bf1 = """++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   509
      ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   510
      ]>.>+[>>]>+]"""
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   511
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   512
bf_run(bf1, "sier")
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
bf_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   515
       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", "hello")
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   516
694
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   517
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   518
val bf2 = """+++++++++++
616
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
      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   522
      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   523
      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   524
      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   525
      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   526
      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
96114cc02150 updated
Christian Urban <urbanc@in.tum.de>
parents: 612
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
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   530
bf_run(bf2, "fibs")
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
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
   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
<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   674
-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   675
>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   676
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   677
<<<<<]]>>>]""", "mand")
f16459e2c490 updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   678