author | Christian Urban <urbanc@in.tum.de> |
Tue, 27 Nov 2018 00:45:26 +0000 | |
changeset 609 | e33545bb2eba |
parent 548 | c6ba1d17aaf3 |
child 626 | 2d91b2107656 |
permissions | -rw-r--r-- |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
import scala.language.implicitConversions |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
import scala.language.reflectiveCalls |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
import scala.util._ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
import scala.annotation.tailrec |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
import scala.sys.process._ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
def fromFile(name: String) : String = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
io.Source.fromFile(name).mkString |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
abstract class Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
case object NULL extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
case object EMPTY extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
case class CHAR(c: Char) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
case class RANGE(cs: List[Char]) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
case class PLUS(r: Rexp) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
case class STAR(r: Rexp) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
case class NTIMES(r: Rexp, n: Int) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
case class NUPTOM(r: Rexp, n: Int, m: Int) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
object RANGE { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
def apply(s: String) : RANGE = RANGE(s.toList) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
def NMTIMES(r: Rexp, n: Int, m: Int) = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
if (m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
else NUPTOM(r, n, m - n) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
case class NOT(r: Rexp) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
case class OPT(r: Rexp) extends Rexp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
// some convenience for typing in regular expressions |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
def charlist2rexp(s : List[Char]) : Rexp = s match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
case Nil => EMPTY |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
case c::Nil => CHAR(c) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
implicit def RexpOps (r: Rexp) = new { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
def | (s: Rexp) = ALT(r, s) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
def % = STAR(r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
def ~ (s: Rexp) = SEQ(r, s) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
implicit def stringOps (s: String) = new { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
def | (r: Rexp) = ALT(s, r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
def | (r: String) = ALT(s, r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
def % = STAR(s) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
def ~ (r: Rexp) = SEQ(s, r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
def ~ (r: String) = SEQ(s, r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
// nullable function: tests whether the regular |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
// expression can recognise the empty string |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
def nullable (r: Rexp) : Boolean = r match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
case NULL => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
case EMPTY => true |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
case CHAR(_) => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
case STAR(_) => true |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
case PLUS(r) => nullable(r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
case NUPTOM(r, i, j) => if (i == 0) true else nullable(r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
case RANGE(_) => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
case NOT(r) => !(nullable(r)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
case OPT(_) => true |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
// derivative of a regular expression w.r.t. a character |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
def der (c: Char, r: Rexp) : Rexp = r match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
case NULL => NULL |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
case EMPTY => NULL |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
case CHAR(d) => if (c == d) EMPTY else NULL |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
case SEQ(r1, r2) => |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
else SEQ(der(c, r1), r2) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
case PLUS(r) => SEQ(der(c, r), STAR(r)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
case NTIMES(r, i) => |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
if (i == 0) NULL else der(c, SEQ(r, NTIMES(r, i - 1))) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
case NUPTOM(r, i, j) => |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
if (i == 0 && j == 0) NULL else |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
if (i == 0) ALT(der(c, NTIMES(r, j)), der(c, NUPTOM(r, 0, j - 1))) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
else der(c, SEQ(r, NUPTOM(r, i - 1, j))) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
case RANGE(cs) => if (cs contains c) EMPTY else NULL |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
case NOT(r) => NOT(der (c, r)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
case OPT(r) => der(c, r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
def zeroable (r: Rexp) : Boolean = r match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
case NULL => true |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
case EMPTY => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
case CHAR(_) => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
case ALT(r1, r2) => zeroable(r1) && zeroable(r2) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
case SEQ(r1, r2) => zeroable(r1) || zeroable(r2) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
case STAR(_) => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
case PLUS(r) => zeroable(r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
case NTIMES(r, i) => if (i == 0) false else zeroable(r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
case NUPTOM(r, i, j) => if (i == 0) false else zeroable(r) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
case RANGE(_) => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
case NOT(r) => !(zeroable(r)) // bug: incorrect definition for NOT |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
case OPT(_) => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
// derivative w.r.t. a string (iterates der) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
case Nil => r |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
case c::s => ders(s, der(c, r)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
// regular expressions for the While language |
548 | 118 |
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_".toList) |
119 |
val DIGIT = RANGE("0123456789".toList) |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
val ID = SYM ~ (SYM | DIGIT).% |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
val NUM = PLUS(DIGIT) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
val KEYWORD : Rexp = "if" | "then" | "else" | "write" | "def" |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
val SEMI: Rexp = ";" |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
val COMMA: Rexp = "," |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<=" | "=>" | "<" | ">" | "%" | "=" | "/" |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
val WHITESPACE = PLUS(" " | "\n" | "\t") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
val RPAREN: Rexp = ")" |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
val LPAREN: Rexp = "(" |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
val ALL = SYM | DIGIT | OP | " " | ":" | ";" | "\"" | "=" | "," | "(" | ")" |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
val ALL2 = ALL | "\n" |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
//val COMMENT2 = ("/*" ~ NOT(ALL.% ~ "*/" ~ ALL.%) ~ "*/") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
val COMMENT = ("/*" ~ ALL2.% ~ "*/") | ("//" ~ ALL.% ~ "\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
133 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
135 |
// token for While language |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
136 |
abstract class Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
case object T_WHITESPACE extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
138 |
case object T_SEMI extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
139 |
case object T_COMMA extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
140 |
case object T_LPAREN extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
case object T_RPAREN extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
142 |
case object T_COMMENT extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
143 |
case class T_ID(s: String) extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
144 |
case class T_OP(s: String) extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
145 |
case class T_NUM(s: String) extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
146 |
case class T_KWD(s: String) extends Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
147 |
case class T_ERR(s: String) extends Token // special error token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
148 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
149 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
150 |
type TokenFun = String => Token |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
151 |
type LexRules = List[(Rexp, TokenFun)] |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
152 |
val While_lexing_rules: LexRules = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
153 |
List((KEYWORD, (s) => T_KWD(s)), |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
154 |
(ID, (s) => T_ID(s)), |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
155 |
(COMMENT, (s) => T_COMMENT), |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
156 |
(OP, (s) => T_OP(s)), |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
157 |
(NUM, (s) => T_NUM(s)), |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
158 |
(SEMI, (s) => T_SEMI), |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
159 |
(COMMA, (s) => T_COMMA), |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
(LPAREN, (s) => T_LPAREN), |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
(RPAREN, (s) => T_RPAREN), |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
(WHITESPACE, (s) => T_WHITESPACE)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
// calculates derivatives until all of them are zeroable |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
@tailrec |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |
def munch(s: List[Char], |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
168 |
pos: Int, |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
169 |
rs: LexRules, |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
170 |
last: Option[(Int, TokenFun)]): Option[(Int, TokenFun)] = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
rs match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
case Nil => last |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
173 |
case rs if (s.length <= pos) => last |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
174 |
case rs => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
val ders = rs.map({case (r, tf) => (der(s(pos), r), tf)}) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
val rs_nzero = ders.filterNot({case (r, _) => zeroable(r)}) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
177 |
val rs_nulls = ders.filter({case (r, _) => nullable(r)}) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
178 |
val new_last = if (rs_nulls != Nil) Some((pos, rs_nulls.head._2)) else last |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
179 |
munch(s, 1 + pos, rs_nzero, new_last) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
180 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
181 |
}} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
182 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
183 |
// iterates the munching function and returns a Token list |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
184 |
def tokenize(s: String, rs: LexRules) : List[Token] = munch(s.toList, 0, rs, None) match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
185 |
case None if (s == "") => Nil |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
186 |
case None => List(T_ERR(s"Lexing error: $s")) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
187 |
case Some((n, tf)) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
188 |
val (head, tail) = s.splitAt(n + 1) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
189 |
tf(head)::tokenize(tail, rs) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
190 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
191 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
192 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
193 |
def tokenizer(s:String) : List[Token] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
194 |
tokenize(s, While_lexing_rules).filter { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
195 |
case T_ERR(s) => { println(s); sys.exit(-1) } |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
196 |
case T_WHITESPACE => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
197 |
case T_COMMENT => false |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
198 |
case _ => true |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
199 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
200 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
201 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
202 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
203 |
// Parser - Abstract syntax trees |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
204 |
abstract class Exp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
205 |
abstract class BExp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
206 |
abstract class Decl |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
207 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
208 |
case class Def(name: String, args: List[String], body: Exp) extends Decl |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
209 |
case class Main(e: Exp) extends Decl |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
210 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
211 |
case class Call(name: String, args: List[Exp]) extends Exp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
212 |
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
213 |
case class Write(e: Exp) extends Exp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
214 |
case class Var(s: String) extends Exp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
215 |
case class Num(i: Int) extends Exp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
216 |
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
217 |
case class Sequ(e1: Exp, e2: Exp) extends Exp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
218 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
219 |
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
220 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
221 |
// calculating the maximal needed stack size |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
222 |
def max_stack_exp(e: Exp): Int = e match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
223 |
case Call(_, args) => args.map(max_stack_exp).sum |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
224 |
case If(a, e1, e2) => max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
225 |
case Write(e) => max_stack_exp(e) + 1 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
226 |
case Var(_) => 1 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
227 |
case Num(_) => 1 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
228 |
case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
229 |
case Sequ(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
230 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
231 |
def max_stack_bexp(e: BExp): Int = e match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
232 |
case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
233 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
234 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
235 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
236 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
237 |
// Parser combinators |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
238 |
abstract class Parser[I <% Seq[_], T] { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
239 |
def parse(ts: I): Set[(T, I)] |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
240 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
241 |
def parse_all(ts: I) : Set[T] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
242 |
for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
243 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
244 |
def parse_single(ts: I) : T = parse_all(ts).toList match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
245 |
case List(t) => t |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
246 |
case _ => { println ("Parse Error") ; sys.exit(-1) } |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
247 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
248 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
249 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
250 |
class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, (T, S)] { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
251 |
def parse(sb: I) = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
252 |
for ((head1, tail1) <- p.parse(sb); |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
253 |
(head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
254 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
255 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
256 |
class AltParser[I <% Seq[_], T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
257 |
def parse(sb: I) = p.parse(sb) ++ q.parse(sb) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
258 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
259 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
260 |
class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
261 |
def parse(sb: I) = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
262 |
for ((head, tail) <- p.parse(sb)) yield (f(head), tail) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
263 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
264 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
265 |
case class TokParser(tok: Token) extends Parser[List[Token], Token] { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
266 |
def parse(ts: List[Token]) = ts match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
267 |
case t::ts if (t == tok) => Set((t, ts)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
268 |
case _ => Set () |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
269 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
270 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
271 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
272 |
implicit def token2tparser(t: Token) = TokParser(t) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
273 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
274 |
case object NumParser extends Parser[List[Token], Int] { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
275 |
def parse(ts: List[Token]) = ts match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
276 |
case T_NUM(s)::ts => Set((s.toInt, ts)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
277 |
case _ => Set () |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
278 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
279 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
280 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
281 |
case object IdParser extends Parser[List[Token], String] { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
282 |
def parse(ts: List[Token]) = ts match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
283 |
case T_ID(s)::ts => Set((s, ts)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
284 |
case _ => Set () |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
285 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
286 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
287 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
288 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
289 |
implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
290 |
def || (q : => Parser[I, T]) = new AltParser[I, T](p, q) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
291 |
def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
292 |
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
293 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
294 |
implicit def TokOps(t: Token) = new { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
295 |
def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
296 |
def ==>[S] (f: => Token => S) = new FunParser[List[Token], Token, S](t, f) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
297 |
def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
298 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
299 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
300 |
def ListParser[I <% Seq[_], T, S](p: => Parser[I, T], q: => Parser[I, S]): Parser[I, List[T]] = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
301 |
(p ~ q ~ ListParser(p, q)) ==> { case ((x, y), z) => x :: z : List[T] } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
302 |
(p ==> ((s) => List(s))) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
303 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
304 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
305 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
306 |
// expressions |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
307 |
lazy val Exp: Parser[List[Token], Exp] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
308 |
(T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp) ==> |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
309 |
{ case (((((x, y), z), u), v), w) => If(y, u, w): Exp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
310 |
(M ~ T_SEMI ~ Exp) ==> { case ((x, y), z) => Sequ(x, z): Exp } || M |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
311 |
lazy val M: Parser[List[Token], Exp] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
312 |
(T_KWD("write") ~ L) ==> { case (x, y) => Write(y): Exp } || L |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
313 |
lazy val L: Parser[List[Token], Exp] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
314 |
(T ~ T_OP("+") ~ Exp) ==> { case ((x, y), z) => Aop("+", x, z): Exp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
315 |
(T ~ T_OP("-") ~ Exp) ==> { case ((x, y), z) => Aop("-", x, z): Exp } || T |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
316 |
lazy val T: Parser[List[Token], Exp] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
317 |
(F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): Exp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
318 |
(F ~ T_OP("/") ~ T) ==> { case ((x, y), z) => Aop("/", x, z): Exp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
319 |
(F ~ T_OP("%") ~ T) ==> { case ((x, y), z) => Aop("%", x, z): Exp } || F |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
320 |
lazy val F: Parser[List[Token], Exp] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
321 |
(IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
322 |
{ case (((x, y), z), w) => Call(x, z): Exp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
323 |
(T_LPAREN ~ Exp ~ T_RPAREN) ==> { case ((x, y), z) => y: Exp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
324 |
IdParser ==> { case x => Var(x): Exp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
325 |
NumParser ==> { case x => Num(x): Exp } |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
326 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
327 |
// boolean expressions |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
328 |
lazy val BExp: Parser[List[Token], BExp] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
329 |
(Exp ~ T_OP("==") ~ Exp) ==> { case ((x, y), z) => Bop("==", x, z): BExp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
330 |
(Exp ~ T_OP("!=") ~ Exp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
331 |
(Exp ~ T_OP("<") ~ Exp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
332 |
(Exp ~ T_OP(">") ~ Exp) ==> { case ((x, y), z) => Bop("<", z, x): BExp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
333 |
(Exp ~ T_OP("<=") ~ Exp) ==> { case ((x, y), z) => Bop("<=", x, z): BExp } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
334 |
(Exp ~ T_OP("=>") ~ Exp) ==> { case ((x, y), z) => Bop("<=", z, x): BExp } |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
335 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
336 |
lazy val Defn: Parser[List[Token], Decl] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
337 |
(T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(IdParser, T_COMMA) ~ T_RPAREN ~ T_OP("=") ~ Exp) ==> |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
338 |
{ case ((((((x, y), z), w), u), v), r) => Def(y, w, r): Decl } |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
339 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
340 |
lazy val Prog: Parser[List[Token], List[Decl]] = |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
341 |
(Defn ~ T_SEMI ~ Prog) ==> { case ((x, y), z) => x :: z : List[Decl] } || |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
342 |
(Exp ==> ((s) => List(Main(s)) : List[Decl])) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
343 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
344 |
// compiler - built-in functions |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
345 |
// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
346 |
// |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
347 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
348 |
val library = """ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
349 |
.class public XXX.XXX |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
350 |
.super java/lang/Object |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
351 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
352 |
.method public <init>()V |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
353 |
aload_0 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
354 |
invokenonvirtual java/lang/Object/<init>()V |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
355 |
return |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
356 |
.end method |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
357 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
358 |
.method public static write(I)V |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
359 |
.limit locals 5 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
360 |
.limit stack 5 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
361 |
iload 0 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
362 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
363 |
swap |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
364 |
invokevirtual java/io/PrintStream/println(I)V |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
365 |
return |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
366 |
.end method |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
367 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
368 |
""" |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
369 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
370 |
// for generating new labels |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
371 |
var counter = -1 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
372 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
373 |
def Fresh(x: String) = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
374 |
counter += 1 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
375 |
x ++ "_" ++ counter.toString() |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
376 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
377 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
378 |
type Mem = Map[String, Int] |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
379 |
type Instrs = List[String] |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
380 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
381 |
def compile_expT(a: Exp, env : Mem, name: String) : Instrs = a match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
382 |
case Num(i) => List("ldc " + i.toString + "\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
383 |
case Var(s) => List("iload " + env(s).toString + "\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
384 |
case Aop("+", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("iadd\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
385 |
case Aop("-", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("isub\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
386 |
case Aop("*", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("imul\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
387 |
case Aop("/", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("idiv\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
388 |
case Aop("%", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("irem\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
389 |
case If(b, a1, a2) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
390 |
val if_else = Fresh("If_else") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
391 |
val if_end = Fresh("If_end") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
392 |
compile_bexpT(b, env, if_else) ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
393 |
compile_expT(a1, env, name) ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
394 |
List("goto " + if_end + "\n") ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
395 |
List("\n" + if_else + ":\n\n") ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
396 |
compile_expT(a2, env, name) ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
397 |
List("\n" + if_end + ":\n\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
398 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
399 |
case Call(n, args) => if (name == n) { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
400 |
val stores = args.zipWithIndex.map { case (x, y) => "istore " + y.toString + "\n" } |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
401 |
args.flatMap(a => compile_expT(a, env, "")) ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
402 |
stores.reverse ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
403 |
List ("goto " + n + "_Start\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
404 |
} else { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
405 |
val is = "I" * args.length |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
406 |
args.flatMap(a => compile_expT(a, env, "")) ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
407 |
List ("invokestatic XXX/XXX/" + n + "(" + is + ")I\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
408 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
409 |
case Sequ(a1, a2) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
410 |
compile_expT(a1, env, "") ++ List("pop\n") ++ compile_expT(a2, env, name) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
411 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
412 |
case Write(a1) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
413 |
compile_expT(a1, env, "") ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
414 |
List("dup\n", |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
415 |
"invokestatic XXX/XXX/write(I)V\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
416 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
417 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
418 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
419 |
def compile_bexpT(b: BExp, env : Mem, jmp: String) : Instrs = b match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
420 |
case Bop("==", a1, a2) => |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
421 |
compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("if_icmpne " + jmp + "\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
422 |
case Bop("!=", a1, a2) => |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
423 |
compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("if_icmpeq " + jmp + "\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
424 |
case Bop("<", a1, a2) => |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
425 |
compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("if_icmpge " + jmp + "\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
426 |
case Bop("<=", a1, a2) => |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
427 |
compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("if_icmpgt " + jmp + "\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
428 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
429 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
430 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
431 |
def compile_decl(d: Decl) : Instrs = d match { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
432 |
case Def(name, args, a) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
433 |
val env = args.zipWithIndex.toMap |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
434 |
val is = "I" * args.length |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
435 |
List(".method public static " + name + "(" + is + ")I \n", |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
436 |
".limit locals " + args.length.toString + "\n", |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
437 |
".limit stack " + (1 + max_stack_exp(a)).toString + "\n", |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
438 |
name + "_Start:\n") ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
439 |
compile_expT(a, env, name) ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
440 |
List("ireturn\n", |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
441 |
".end method \n\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
442 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
443 |
case Main(a) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
444 |
List(".method public static main([Ljava/lang/String;)V\n", |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
445 |
".limit locals 200\n", |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
446 |
".limit stack 200\n") ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
447 |
compile_expT(a, Map(), "") ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
448 |
List("invokestatic XXX/XXX/write(I)V\n", |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
449 |
"return\n", |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
450 |
".end method\n") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
451 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
452 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
453 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
454 |
def compile(class_name: String, input: String) : String = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
455 |
val tks = tokenizer(input) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
456 |
//println(Prog.parse(tks)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
457 |
val ast = Prog.parse_single(tks) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
458 |
val instructions = ast.flatMap(compile_decl).mkString |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
459 |
(library + instructions).replaceAllLiterally("XXX", class_name) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
460 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
461 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
462 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
463 |
def compile_file(file_name: String) = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
464 |
val class_name = file_name.split('.')(0) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
465 |
val output = compile(class_name, fromFile(file_name)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
466 |
val fw = new java.io.FileWriter(class_name + ".j") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
467 |
fw.write(output) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
468 |
fw.close() |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
469 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
470 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
471 |
def time_needed[T](i: Int, code: => T) = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
472 |
val start = System.nanoTime() |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
473 |
for (j <- 1 to i) code |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
474 |
val end = System.nanoTime() |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
475 |
(end - start)/(i * 1.0e9) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
476 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
477 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
478 |
def compile_run(file_name: String) : Unit = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
479 |
val class_name = file_name.split('.')(0) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
480 |
compile_file(file_name) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
481 |
val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!! |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
482 |
println("Time: " + time_needed(2, ("java " + class_name + "/" + class_name).!)) |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
483 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
484 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
485 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
486 |
//examples |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
487 |
compile_run("defs.rec") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
488 |
//compile_run("fact.rec") |
548 | 489 |
|
490 |
||
491 |
||
492 |
||
493 |
||
494 |
// a problem with the parser |
|
609 | 495 |
/* |
548 | 496 |
val text = "(((((1)))))" |
497 |
val tokens = tokenizer(text) |
|
498 |
println(tokens) |
|
499 |
val ast = Prog.parse_single(tokens) |
|
500 |
println(ast) |
|
501 |
||
502 |
Exp.parse_single(tokens) |
|
503 |
||
504 |
||
505 |
||
506 |
val text = "((((1))))" |
|
507 |
val tokens = tokenizer(text) |
|
508 |
println(tokens) |
|
509 |
Exp.parse(tokens) |
|
609 | 510 |
*/ |