progs/compile_arr.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 06 Nov 2019 23:17:05 +0000
changeset 683 c6c79d21f8a8
parent 616 24bbe4e4b37b
child 694 b6ed836ce59b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// A Small Compiler for the WHILE Language
611
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     2
// (stub for including arrays)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// the abstract syntax trees
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
abstract class Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
abstract class AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
abstract class BExp 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
type Block = List[Stmt]
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// statements
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
case object Skip extends Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
case class While(b: BExp, bl: Block) extends Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
case class Assign(s: String, a: AExp) extends Stmt
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
    15
case class AssignA(s: String, a1: AExp, a2: AExp) extends Stmt
611
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    16
case class Write(s: String) extends Stmt  // writes out a variable
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
    17
case class Array(s: String, n: Int) extends Stmt 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
// arithmetic expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case class Var(s: String) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case class Num(i: Int) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
    23
case class Ref(s: String, a1: AExp) extends AExp
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
// boolean expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case object True extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case object False extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
// compiler headers needed for the JVM
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
// (contains an init method, as well as methods for read and write)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
val beginning = """
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
.class public XXX.XXX
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
.super java/lang/Object
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
.method public <init>()V
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
   aload_0
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
   invokenonvirtual java/lang/Object/<init>()V
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
   return
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
.method public static write(I)V 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    .limit locals 1 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    .limit stack 2 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
    getstatic java/lang/System/out Ljava/io/PrintStream; 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
    iload 0
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
    48
    i2c
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
    49
    invokevirtual java/io/PrintStream/print(C)V 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    return 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
.method public static main([Ljava/lang/String;)V
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
   .limit locals 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
   .limit stack 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
val ending = """
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
   return
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
println("Start compilation")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
// for generating new labels
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
var counter = -1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
def Fresh(x: String) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  counter += 1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  x ++ "_" ++ counter.toString()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
// environments and instructions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
type Env = Map[String, String]
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
type Instrs = List[String]
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// arithmetic expression compilation
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
def compile_aexp(a: AExp, env : Env) : Instrs = a match {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  case Num(i) => List("ldc " + i.toString + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  case Var(s) => List("iload " + env(s) + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  case Aop("+", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
    compile_aexp(a1, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
    compile_aexp(a2, env) ++ List("iadd\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  case Aop("-", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("isub\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  case Aop("*", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("imul\n")
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
    92
  case Ref(s, a1) => 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
    93
    List("aload " + env(s) + "\n") ++ compile_aexp(a1, env) ++ List("iaload \n")
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
// boolean expression compilation
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
def compile_bexp(b: BExp, env : Env, jmp: String) : Instrs = b match {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  case True => Nil
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  case False => List("goto " + jmp + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  case Bop("=", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
    List("if_icmpne " + jmp + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  case Bop("!=", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
    List("if_icmpeq " + jmp + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
  case Bop("<", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
    List("if_icmpge " + jmp + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
// statement compilation
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
def compile_stmt(s: Stmt, env: Env) : (Instrs, Env) = s match {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  case Skip => (Nil, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  case Assign(x, a) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
    val index = if (env.isDefinedAt(x)) env(x) else 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
                    env.keys.size.toString
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
    (compile_aexp(a, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
     List("istore " + index + "\n"), env + (x -> index))
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
  } 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  case If(b, bl1, bl2) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
    val if_else = Fresh("If_else")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
    val if_end = Fresh("If_end")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
    val (instrs1, env1) = compile_block(bl1, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
    val (instrs2, env2) = compile_block(bl2, env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
    (compile_bexp(b, env, if_else) ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
     instrs1 ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
     List("goto " + if_end + "\n") ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
     List("\n" + if_else + ":\n\n") ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
     instrs2 ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
     List("\n" + if_end + ":\n\n"), env2)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
  case While(b, bl) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
    val loop_begin = Fresh("Loop_begin")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
    val loop_end = Fresh("Loop_end")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
    val (instrs1, env1) = compile_block(bl, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
    (List("\n" + loop_begin + ":\n\n") ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
     compile_bexp(b, env, loop_end) ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
     instrs1 ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
     List("goto " + loop_begin + "\n") ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
     List("\n" + loop_end + ":\n\n"), env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  case Write(x) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
    (List("iload " + env(x) + "\n" + 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
           "invokestatic XXX/XXX/write(I)V\n"), env)
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   145
  case Array(s, n) => {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   146
    val index = if (env.isDefinedAt(s)) throw new Exception("Array already defined") else 
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   147
                    env.keys.size.toString
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   148
    (List("ldc " ++ n.toString ++ "\n",
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   149
          "newarray int \n",
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   150
          "astore " ++ index ++ "\n"), env + (s -> index))
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   151
  } 
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   152
  case AssignA(s, a1, a2) => {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   153
    val index = if (env.isDefinedAt(s)) env(s) else 
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   154
                    throw new Exception("Array not yet defined")
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   155
    (List("aload " + index + "\n") ++
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   156
     compile_aexp(a1, env) ++
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   157
     compile_aexp(a2, env) ++
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   158
     List("iastore \n"), env)
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   159
  } 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
// compilation of a block (i.e. list of instructions)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
def compile_block(bl: Block, env: Env) : (Instrs, Env) = bl match {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
  case Nil => (Nil, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
  case s::bl => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
    val (instrs1, env1) = compile_stmt(s, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
    val (instrs2, env2) = compile_block(bl, env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
    (instrs1 ++ instrs2, env2)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
// main compilation function for blocks
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
def compile(bl: Block, class_name: String) : String = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  val instructions = compile_block(bl, Map.empty)._1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
  (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
// compiling and running files
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
// JVM files can be assembled with 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
//    java -jar jvm/jasmin-2.4/jasmin.jar fib.j
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
// and started with
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
//    java fib/fib
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
import scala.util._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
import scala.sys.process._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
import scala.io
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
def compile_tofile(bl: Block, class_name: String) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
  val output = compile(bl, class_name)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
  val fw = new java.io.FileWriter(class_name + ".j") 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
  fw.write(output) 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
  fw.close()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
def compile_all(bl: Block, class_name: String) : Unit = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
  compile_tofile(bl, class_name)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
  println("compiled ")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
  val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!!
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
  println("assembled ")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
def time_needed[T](i: Int, code: => T) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
  val start = System.nanoTime()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
  for (j <- 1 to i) code
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
  val end = System.nanoTime()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
  (end - start)/(i * 1.0e9)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
def compile_run(bl: Block, class_name: String) : Unit = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
  println("Start compilation")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
  compile_all(bl, class_name)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
  println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!))
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
// Fibonacci numbers as a test-case
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
val fib_test = 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
  List(Assign("n", Num(10)),            //  n := 10;                     
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
       Assign("minus1",Num(0)),         //  minus1 := 0;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
       Assign("minus2",Num(1)),         //  minus2 := 1;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
       Assign("temp",Num(0)),           //  temp := 0;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
       While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
          List(Assign("temp",Var("minus2")),    //  temp := minus2;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
               Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
                                        //  minus2 := minus1 + minus2;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
               Assign("minus1",Var("temp")), //  minus1 := temp;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
               Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
       Write("minus1"))                 //  write minus1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
compile_run(fib_test, "fib")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
611
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   241
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   242
val arr_test = 
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   243
  List(Array("a", 10),                 // a[10]
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   244
       Array("b", 2),                  // b[2]
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   245
       AssignA("a", Num(0), Num(10)),  // a[0] := 10
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   246
       Assign("x", Ref("a", Num(0))),  // x := a[0]
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   247
       Write("x"),                     // write x
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   248
       AssignA("b", Num(1), Num(5)),   // b[1] := 5
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   249
       Assign("x", Ref("b", Num(1))),  // x := b[1]
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   250
       Write("x"))                     // write x
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   251
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   252
compile_run(arr_test, "a")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   253
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   254
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   255
//====================
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   256
// Parser Combinators
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   257
//====================
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   258
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   259
import scala.language.implicitConversions
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   260
import scala.language.reflectiveCalls
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   261
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   262
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   263
abstract class Parser[I <% Seq[_], T] {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   264
  def parse(ts: I): Set[(T, I)]
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   265
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   266
  def parse_all(ts: I) : Set[T] =
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   267
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   268
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   269
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   270
class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, (T, S)] {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   271
  def parse(sb: I) = 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   272
    for ((head1, tail1) <- p.parse(sb); 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   273
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   274
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   275
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   276
class AltParser[I <% Seq[_], T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   277
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   278
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   279
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   280
class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   281
  def parse(sb: I) = 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   282
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   283
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   284
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   285
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   286
import scala.util.matching.Regex
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   287
case class RegexParser(reg: Regex) extends Parser[String, String] {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   288
  def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   289
    case None => Set()
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   290
    case Some(m) => Set((m.matched, m.after.toString))  
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   291
  }
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   292
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   293
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   294
def StringParser(s: String) = RegexParser(Regex.quote(s).r)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   295
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   296
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   297
implicit def string2parser(s : String) = StringParser(s)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   298
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   299
implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   300
  def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   301
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   302
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   303
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   304
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   305
implicit def StringOps(s: String) = new {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   306
  def | (q : => Parser[String, String]) = new AltParser[String, String](s, q)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   307
  def | (r: String) = new AltParser[String, String](s, r)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   308
  def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   309
  def ~[S](q : => Parser[String, S]) = 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   310
    new SeqParser[String, String, S](s, q)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   311
  def ~ (r: String) = 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   312
    new SeqParser[String, String, String](s, r)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   313
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   314
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   315
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   316
val NumParser = RegexParser("[0-9]+".r) ==> (s => s.toInt : Int)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   317
val IdParser = RegexParser("[a-z][a-z,0-9]*".r)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   318
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   319
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   320
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   321
// Grammar Rules
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   322
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   323
lazy val AExp: Parser[String, AExp] = 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   324
  (Te ~ "+" ~ AExp) ==> { case ((x, _), z) => Aop("+", x, z):AExp } |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   325
  (Te ~ "-" ~ AExp) ==> { case ((x, _), z) => Aop("-", x, z):AExp } | Te
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   326
lazy val Te: Parser[String, AExp] = 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   327
  (Fa ~ "*" ~ Te) ==> { case ((x, _), z) => Aop("*", x, z):AExp } | Fa
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   328
lazy val Fa: Parser[String, AExp] = 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   329
   ("(" ~ AExp ~ ")") ==> { case ((_, y), _) => y } | 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   330
   (IdParser ~ "[" ~ AExp ~ "]") ==> { case (((x, _), y), _) => Ref(x, y) } |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   331
   IdParser ==> Var | 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   332
   NumParser ==> Num
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   333
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   334
// boolean expressions
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   335
lazy val BExp: Parser[String, BExp] = 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   336
   (AExp ~ "=" ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z):BExp } | 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   337
   (AExp ~ "!=" ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z):BExp } | 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   338
   (AExp ~ "<" ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z):BExp } | 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   339
   (AExp ~ ">" ~ AExp) ==> { case ((x, y), z) => Bop("<", z, x):BExp } | 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   340
   ("true" ==> ((_) => True:BExp )) | 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   341
   ("false" ==> ((_) => False:BExp )) |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   342
   ("(" ~ BExp ~ ")") ==> { case ((x, y), z) => y}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   343
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   344
lazy val Stmt: Parser[String, Stmt] =
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   345
   ("skip" ==> (_ => Skip: Stmt)) |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   346
   (IdParser ~ ":=" ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   347
   (IdParser ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp) ==> { 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   348
     case (((((x, y), z), v), w), u) => AssignA(x, z, u): Stmt } |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   349
   ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   350
    { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   351
   ("while" ~ BExp ~ "do" ~ Block) ==> { case (((x, y), z), w) => While(y, w) } |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   352
   ("new" ~ IdParser ~ "[" ~ NumParser ~ "]") ==> { case ((((x, y), z), u), v) => Array(y, u) } |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   353
   ("write" ~ IdParser) ==> { case (_, y) => Write(y) } 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   354
 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   355
lazy val Stmts: Parser[String, Block] =
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   356
  (Stmt ~ ";" ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } |
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   357
  (Stmt ==> ((s) => List(s) : Block)) 
611
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   358
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   359
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   360
lazy val Block: Parser[String, Block] =
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   361
   ("{" ~ Stmts ~ "}") ==> { case ((x, y), z) => y} | 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   362
   (Stmt ==> (s => List(s)))
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   363
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   364
Stmts.parse_all("x2:=5+a")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   365
Stmts.parse_all("x2:=5+a[3+a]")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   366
Stmts.parse_all("a[x2+3]:=5+a[3+a]")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   367
Block.parse_all("{x:=5;y:=8}")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   368
Block.parse_all("if(false)then{x:=5}else{x:=10}")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   369
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   370
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   371
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   372
val fib = """
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   373
   n := 10;
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   374
   minus1 := 0;
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   375
   minus2 := 1;
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   376
   temp:=0;
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   377
   while (n > 0) do {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   378
     temp := minus2; 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   379
     minus2 := minus1 + minus2;
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   380
     minus1 := temp;
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   381
     n := n - 1};
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   382
   result := minus2;
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   383
   write result
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   384
   """.replaceAll("\\s+", "")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   385
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   386
val fib_prog = Stmts.parse_all(fib).toList
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   387
//compile_run(fib_prog.head, "fib")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   388
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   389
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   390
// BF 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   391
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   392
// simple instructions
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   393
def instr(c: Char) : String = c match {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   394
  case '>' => "ptr := ptr + 1;"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   395
  case '<' => "ptr := ptr - 1;"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   396
  case '+' => "field[ptr] := field[ptr] + 1;"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   397
  case '-' => "field[ptr] := field[ptr] - 1;"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   398
  case '.' => "x := field[ptr]; write x;"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   399
  //case ',' => "XXX" // "ptr = getchar();\n"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   400
  case '['  => "while (field[ptr] != 0) do {"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   401
  case ']'  => "skip};"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   402
  case _ => ""
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   403
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   404
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   405
def instrs(prog: String) : String =
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   406
  prog.toList.map(instr).mkString
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   407
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   408
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   409
def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   410
  case (Nil, acc) => acc
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   411
  case (c :: cs, Nil) => splice(cs, List((c, 1)))
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   412
  case (c :: cs, (d, n) :: acc) => 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   413
    if (c == d) splice(cs, (c, n + 1) :: acc)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   414
    else splice(cs, (c, 1) :: (d, n) :: acc)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   415
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   416
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   417
def spl(s: String) = splice(s.toList, Nil).reverse
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   418
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   419
def instr2(c: Char, n: Int) : String = c match {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   420
  case '>' => "ptr := ptr + " + n.toString + ";"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   421
  case '<' => "ptr := ptr - " + n.toString + ";"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   422
  case '+' => "field[ptr] := field[ptr] + " + n.toString + ";"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   423
  case '-' => "field[ptr] := field[ptr] - " + n.toString + ";"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   424
  case '.' => "x := field[ptr]; write x;" //* n
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   425
  //case ',' => "*ptr = getchar();\n" * n
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   426
  case '['  => "while (field[ptr] != 0) do {" * n 
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   427
  case ']'  => "skip};" * n
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   428
  case _ => ""
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   429
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   430
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   431
def instrs2(prog: String) : String =
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   432
  spl(prog).map{ case (c, n) => instr2(c, n) }.mkString
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   433
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   434
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   435
def bf_str(prog: String) : String = {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   436
  "\n" ++
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   437
  //"new field[30000];\n" ++
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   438
  "ptr := 15000;" ++
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   439
  instrs2(prog) ++
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   440
  "skip"
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   441
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   442
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   443
def bf_run(prog: String, name: String) = {
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   444
  println("BF processing start")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   445
  val bf_string = bf_str(prog).replaceAll("\\s", "")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   446
  println(s"BF parsing start (string length ${bf_string.length})")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   447
  val bf_prog = Stmts.parse_all(bf_string).toList.head
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   448
  println("BF Compile start")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   449
  compile_run(Array("field", 30000) :: bf_prog, name)
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   450
}
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   451
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   452
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   453
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   454
val bf1 = """++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   455
      ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   456
      ]>.>+[>>]>+]"""
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   457
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   458
bf_run(bf1, "sier")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   459
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   460
bf_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   461
       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", "hello")
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   462
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   463
bf_run("""+++++++++++
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   464
      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   465
      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   466
      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   467
      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   468
      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   469
      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   470
      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   471
      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   472
      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   473
      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""", "fibs")