progs/compile.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 23 Nov 2013 09:17:59 +0000
changeset 201 c813506e0ee8
parent 93 4794759139ea
child 323 4ce07c4abdb4
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     1
// A Compiler for the WHILE language
72
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 71
diff changeset
     2
// 
85
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 82
diff changeset
     3
import matcher._
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 82
diff changeset
     4
import parser._
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
// some regular expressions
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
     7
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
68
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 66
diff changeset
     8
val DIGIT = RANGE("0123456789")
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     9
val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    10
val NUM = PLUS(DIGIT)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    11
val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "true", "false", "write") 
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    12
val SEMI: Rexp = ";"
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    13
val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!=", "<", ">")
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
val WHITESPACE = PLUS(RANGE(" \n"))
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    15
val RPAREN: Rexp = ")"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    16
val LPAREN: Rexp = "("
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    17
val BEGIN: Rexp = "{"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    18
val END: Rexp = "}"
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
    19
val COMMENT = SEQS("/*", NOT(SEQS(STAR(ALLC), "*/", STAR(ALLC))), "*/")
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
    21
// tokens for classifying the strings that have been recognised
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
abstract class Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
case object T_WHITESPACE extends Token
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
    24
case object T_COMMENT extends Token
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    25
case object T_SEMI extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    26
case object T_LPAREN extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    27
case object T_RPAREN extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    28
case object T_BEGIN extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    29
case object T_END extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    30
case class T_ID(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    31
case class T_OP(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    32
case class T_NUM(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    33
case class T_KWD(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    34
85
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 82
diff changeset
    35
val lexing_rules: List[(Rexp, List[Char] => Token)] = 
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    36
  List((KEYWORD, (s) => T_KWD(s.mkString)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    37
       (ID, (s) => T_ID(s.mkString)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    38
       (OP, (s) => T_OP(s.mkString)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    39
       (NUM, (s) => T_NUM(s.mkString)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    40
       (SEMI, (s) => T_SEMI),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    41
       (LPAREN, (s) => T_LPAREN),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    42
       (RPAREN, (s) => T_RPAREN),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    43
       (BEGIN, (s) => T_BEGIN),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    44
       (END, (s) => T_END),
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
    45
       (WHITESPACE, (s) => T_WHITESPACE),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
    46
       (COMMENT, (s) => T_COMMENT))
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
// the tokenizer
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
    49
val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE, T_COMMENT))
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    50
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    51
// the abstract syntax trees
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    52
abstract class Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    53
abstract class AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    54
abstract class BExp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    55
type Block = List[Stmt]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    56
case object Skip extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    57
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    58
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    59
case class Assign(s: String, a: AExp) extends Stmt
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    60
case class Write(s: String) extends Stmt
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
    61
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    62
case class Var(s: String) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    63
case class Num(i: Int) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    64
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
    65
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    66
case object True extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    67
case object False extends BExp
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    68
case class Relop(o: String, a1: AExp, a2: AExp) extends BExp
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    69
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
    70
// atomic parsers
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    71
case class TokParser(tok: Token) extends Parser[List[Token], Token] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    72
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    73
    case t::ts if (t == tok) => Set((t, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    74
    case _ => Set ()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    75
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    76
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    77
implicit def token2tparser(t: Token) = TokParser(t)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    78
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    79
case object NumParser extends Parser[List[Token], Int] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    80
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    81
    case T_NUM(s)::ts => Set((s.toInt, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    82
    case _ => Set ()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    83
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    84
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    85
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    86
case object IdParser extends Parser[List[Token], String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    87
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    88
    case T_ID(s)::ts => Set((s, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    89
    case _ => Set ()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    90
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    91
}
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
72
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 71
diff changeset
    94
// arithmetic expressions
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    95
lazy val AExp: Parser[List[Token], AExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    96
  (T ~ T_OP("+") ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    97
  (T ~ T_OP("-") ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || T  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    98
lazy val T: Parser[List[Token], AExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    99
  (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || F
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   100
lazy val F: Parser[List[Token], AExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   101
  (T_LPAREN ~> AExp <~ T_RPAREN) || 
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
   102
  IdParser ==> Var || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
   103
  NumParser ==> Num
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   104
72
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 71
diff changeset
   105
// boolean expressions
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   106
lazy val BExp: Parser[List[Token], BExp] = 
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   107
  (T_KWD("true") ==> ((_) => True: BExp)) || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   108
  (T_KWD("false") ==> ((_) => False: BExp)) ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   109
  (T_LPAREN ~> BExp <~ T_RPAREN) ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   110
  (AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Relop("=", x, z): BExp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   111
  (AExp ~ T_OP("!=") ~ AExp) ==> { case ((x, y), z) => Relop("!=", x, z): BExp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   112
  (AExp ~ T_OP("<") ~ AExp) ==> { case ((x, y), z) => Relop("<", x, z): BExp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   113
  (AExp ~ T_OP(">") ~ AExp) ==> { case ((x, y), z) => Relop("<", z, x): BExp } 
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   114
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   115
lazy val Stmt: Parser[List[Token], Stmt] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   116
  (T_KWD("skip") ==> ((_) => Skip: Stmt)) ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   117
  (IdParser ~ T_OP(":=") ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   118
  (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==>
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 70
diff changeset
   119
    { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } ||
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   120
  (T_KWD("while") ~ BExp ~ T_KWD("do") ~ Block) ==> { case (((x, y), z), w) => While(y, w) } || 
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   121
  (T_KWD("write") ~ IdParser) ==> { case (x, y) => Write(y) } 
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   122
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   123
lazy val Stmts: Parser[List[Token], Block] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   124
  (Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   125
  (Stmt ==> ((s) => List(s) : Block))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   126
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   127
lazy val Block: Parser[List[Token], Block] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   128
  (T_BEGIN ~> Stmts <~ T_END) || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   129
  (Stmt ==> ((s) => List(s)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   130
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   131
// compiler
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   132
val beginning = """
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   133
.class public XXX.XXX
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   134
.super java/lang/Object
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   135
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   136
.method public <init>()V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   137
   aload_0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   138
   invokenonvirtual java/lang/Object/<init>()V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   139
   return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   140
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   141
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   142
.method public static write(I)V 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   143
    .limit locals 5 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   144
    .limit stack 5 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   145
    iload 0 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   146
    getstatic java/lang/System/out Ljava/io/PrintStream; 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   147
    swap 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   148
    invokevirtual java/io/PrintStream/println(I)V 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   149
    return 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   150
.end method
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   151
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   152
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   153
.method public static main([Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   154
   .limit locals 200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   155
   .limit stack 200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   156
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   157
"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   158
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   159
val ending = """
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   160
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   161
   return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   162
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   163
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   164
"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   165
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   166
// for generating new labels
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   167
var counter = -1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   168
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   169
def Fresh(x: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   170
  counter += 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   171
  x ++ "_" ++ counter.toString()
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
}
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   173
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   174
type Env = Map[String, String]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   175
type Instrs = List[String]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   176
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   177
def compile_aexp(a: AExp, env : Env) : Instrs = a match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   178
  case Num(i) => List("ldc " + i.toString + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   179
  case Var(s) => List("iload " + env(s) + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   180
  case Aop("+", a1, a2) => compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("iadd\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   181
  case Aop("-", a1, a2) => compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("isub\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   182
  case Aop("*", a1, a2) => compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("imul\n")
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   183
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   184
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   185
def compile_bexp(b: BExp, env : Env, jmp: String) : Instrs = b match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   186
  case True => Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   187
  case False => List("goto " + jmp + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   188
  case Relop("=", a1, a2) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   189
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpne " + jmp + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   190
  case Relop("!=", a1, a2) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   191
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpeq " + jmp + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   192
  case Relop("<", a1, a2) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   193
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpge " + jmp + "\n")
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   194
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   195
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   196
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   197
def compile_stmt(s: Stmt, env: Env) : (Instrs, Env) = s match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   198
  case Skip => (Nil, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   199
  case Assign(x, a) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   200
    val index = if (env.isDefinedAt(x)) env(x) else env.keys.size.toString
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   201
    (compile_aexp(a, env) ++ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   202
     List("istore " + index + "\n"), env + (x -> index))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   203
  } 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   204
  case If(b, bl1, bl2) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   205
    val if_else = Fresh("If_else")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   206
    val if_end = Fresh("If_end")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   207
    val (instrs1, env1) = compile_bl(bl1, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   208
    val (instrs2, env2) = compile_bl(bl2, env1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   209
    (compile_bexp(b, env, if_else) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   210
     instrs1 ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   211
     List("goto " + if_end + "\n") ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   212
     List("\n" + if_else + ":\n\n") ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   213
     instrs2 ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   214
     List("\n" + if_end + ":\n\n"), env2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   215
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   216
  case While(b, bl) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   217
    val loop_begin = Fresh("Loop_begin")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   218
    val loop_end = Fresh("Loop_end")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   219
    val (instrs1, env1) = compile_bl(bl, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   220
    (List("\n" + loop_begin + ":\n\n") ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   221
     compile_bexp(b, env, loop_end) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   222
     instrs1 ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   223
     List("goto " + loop_begin + "\n") ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   224
     List("\n" + loop_end + ":\n\n"), env1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   225
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   226
  case Write(x) => 
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   227
    (List("iload " + env(x) + "\n" + "invokestatic XXX/XXX/write(I)V\n"), env)
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   228
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   229
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   230
def compile_bl(bl: Block, env: Env) : (Instrs, Env) = bl match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   231
  case Nil => (Nil, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   232
  case s::bl => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   233
    val (instrs1, env1) = compile_stmt(s, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   234
    val (instrs2, env2) = compile_bl(bl, env1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   235
    (instrs1 ++ instrs2, env2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   236
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   237
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   238
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   239
def compile(input: String) : String = {
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   240
  val class_name = input.split('.')(0)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   241
  val tks = Tok.fromFile(input)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   242
  val ast = Stmts.parse_single(tks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   243
  val instructions = compile_bl(ast, Map.empty)._1
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   244
  (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   245
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   246
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   247
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   248
def compile_to(input: String, output: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   249
  val fw = new java.io.FileWriter(output) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   250
  fw.write(compile(input)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   251
  fw.close()
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   252
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   253
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   254
//
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   255
val tks = Tok.fromString("x := x + 1")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   256
val ast = Stmt.parse_single(tks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   257
println(compile_stmt(ast, Map("x" -> "n"))._1.mkString)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   258
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   259
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   260
72
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 71
diff changeset
   261
//examples
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   262
85
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 82
diff changeset
   263
compile_to("loops.while", "loops.j")
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   264
//compile_to("fib.while", "fib.j")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   265
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   266
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   267
// testing cases for time measurements
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   268
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   269
def time_needed[T](i: Int, code: => T) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   270
  val start = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   271
  for (j <- 1 to i) code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   272
  val end = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   273
  (end - start)/(i * 1.0e9)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   274
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   275
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   276
// for testing
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   277
import scala.sys.process._
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   278
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   279
val test_prog = """
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   280
start := XXX;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   281
x := start;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   282
y := start;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   283
z := start;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   284
while 0 < x do {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   285
 while 0 < y do {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   286
  while 0 < z do {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   287
    z := z - 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   288
  };
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   289
  z := start;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   290
  y := y - 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   291
 };     
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   292
 y := start;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   293
 x := x - 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   294
};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   295
write x;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   296
write y;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   297
write z
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   298
"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   299
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   300
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   301
def compile_test(n: Int) : Unit = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   302
  val class_name = "LOOP"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   303
  val tks = Tok.fromString(test_prog.replaceAllLiterally("XXX", n.toString))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   304
  val ast = Stmts.parse_single(tks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   305
  val instructions = compile_bl(ast, Map.empty)._1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   306
  val assembly = (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   307
  val fw = new java.io.FileWriter(class_name + ".j") 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   308
  fw.write(assembly) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   309
  fw.close()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   310
  val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!!
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   311
  println(n + " " + time_needed(2, ("java " + class_name + "/" + class_name).!!))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   312
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   313
82
06c3ec0b452e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   314
List(1, 5000, 10000, 50000, 100000, 250000, 500000, 750000, 1000000).map(compile_test(_))
06c3ec0b452e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   315
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   316
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   317
85
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 82
diff changeset
   318
// Javabyte code assmbler
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   319
//
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   320
// java -jar jvm/jasmin-2.4/jasmin.jar loops.j
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   321
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   322
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   323
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   324
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   325
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   326