author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Wed, 05 Dec 2012 04:13:12 +0000 | |
changeset 86 | 6a7fe83820c8 |
parent 85 | 1a4065f965fb |
permissions | -rw-r--r-- |
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
1 |
// A parser and evaluator for teh while language |
d65525aeca08
added
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
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
// some regular expressions |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
7 |
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
68
a09ecb1e7384
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
66
diff
changeset
|
8 |
val DIGIT = RANGE("0123456789") |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
9 |
val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
10 |
val NUM = PLUS(DIGIT) |
76
373cf55a3ca5
tuned
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
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
12 |
val SEMI: Rexp = ";" |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
13 |
val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!=", "<", ">") |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
val WHITESPACE = PLUS(RANGE(" \n")) |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
15 |
val RPAREN: Rexp = ")" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
16 |
val LPAREN: Rexp = "(" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
17 |
val BEGIN: Rexp = "{" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
18 |
val END: Rexp = "}" |
75
898c25a4e399
tuned
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
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
|
70
e6868bd2942b
tuned
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
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
abstract class Token |
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
case object T_WHITESPACE extends Token |
75
898c25a4e399
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
24 |
case object T_COMMENT extends Token |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
25 |
case object T_SEMI extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
26 |
case object T_LPAREN extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
27 |
case object T_RPAREN extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
28 |
case object T_BEGIN extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
29 |
case object T_END extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
30 |
case class T_ID(s: String) extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
31 |
case class T_OP(s: String) extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
32 |
case class T_NUM(s: String) extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
33 |
case class T_KWD(s: String) extends Token |
cc3f7908b942
tuned
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
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
36 |
List((KEYWORD, (s) => T_KWD(s.mkString)), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
37 |
(ID, (s) => T_ID(s.mkString)), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
38 |
(OP, (s) => T_OP(s.mkString)), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
39 |
(NUM, (s) => T_NUM(s.mkString)), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
40 |
(SEMI, (s) => T_SEMI), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
41 |
(LPAREN, (s) => T_LPAREN), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
42 |
(RPAREN, (s) => T_RPAREN), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
43 |
(BEGIN, (s) => T_BEGIN), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
44 |
(END, (s) => T_END), |
75
898c25a4e399
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
45 |
(WHITESPACE, (s) => T_WHITESPACE), |
898c25a4e399
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
46 |
(COMMENT, (s) => T_COMMENT)) |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
|
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
// the tokenizer |
75
898c25a4e399
tuned
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
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
50 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
51 |
// the abstract syntax trees |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
52 |
abstract class Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
53 |
abstract class AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
54 |
abstract class BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
55 |
type Block = List[Stmt] |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
56 |
case object Skip extends Stmt |
cc3f7908b942
tuned
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 |
cc3f7908b942
tuned
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 |
cc3f7908b942
tuned
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
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
60 |
case class Write(s: String) extends Stmt |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
61 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
62 |
case class Var(s: String) extends AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
63 |
case class Num(i: Int) extends AExp |
cc3f7908b942
tuned
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
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
65 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
66 |
case object True extends BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
67 |
case object False extends BExp |
76
373cf55a3ca5
tuned
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
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
69 |
|
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
70 |
// atomic parsers |
69
cc3f7908b942
tuned
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] { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
72 |
def parse(ts: List[Token]) = ts match { |
cc3f7908b942
tuned
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)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
74 |
case _ => Set () |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
75 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
76 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
77 |
implicit def token2tparser(t: Token) = TokParser(t) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
78 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
79 |
case object NumParser extends Parser[List[Token], Int] { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
80 |
def parse(ts: List[Token]) = ts match { |
cc3f7908b942
tuned
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)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
82 |
case _ => Set () |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
83 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
84 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
85 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
86 |
case object IdParser extends Parser[List[Token], String] { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
87 |
def parse(ts: List[Token]) = ts match { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
88 |
case T_ID(s)::ts => Set((s, ts)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
89 |
case _ => Set () |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
90 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
91 |
} |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
|
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
|
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
94 |
// arithmetic expressions |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
95 |
lazy val AExp: Parser[List[Token], AExp] = |
cc3f7908b942
tuned
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 } || |
cc3f7908b942
tuned
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 |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
98 |
lazy val T: Parser[List[Token], AExp] = |
cc3f7908b942
tuned
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 |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
100 |
lazy val F: Parser[List[Token], AExp] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
101 |
(T_LPAREN ~> AExp <~ T_RPAREN) || |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
102 |
IdParser ==> Var || |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
103 |
NumParser ==> Num |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
104 |
|
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
105 |
// boolean expressions |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
106 |
lazy val BExp: Parser[List[Token], BExp] = |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
107 |
(T_KWD("true") ==> ((_) => True: BExp)) || |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
108 |
(T_KWD("false") ==> ((_) => False: BExp)) || |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
109 |
(T_LPAREN ~> BExp <~ T_RPAREN) || |
373cf55a3ca5
tuned
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 } || |
373cf55a3ca5
tuned
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 } || |
373cf55a3ca5
tuned
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 } || |
373cf55a3ca5
tuned
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
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
114 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
115 |
lazy val Stmt: Parser[List[Token], Stmt] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
116 |
(T_KWD("skip") ==> ((_) => Skip: Stmt)) || |
cc3f7908b942
tuned
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 } || |
cc3f7908b942
tuned
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
898c25a4e399
tuned
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
373cf55a3ca5
tuned
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
898c25a4e399
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
122 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
123 |
lazy val Stmts: Parser[List[Token], Block] = |
cc3f7908b942
tuned
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 } || |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
125 |
(Stmt ==> ((s) => List(s) : Block)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
126 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
127 |
lazy val Block: Parser[List[Token], Block] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
128 |
(T_BEGIN ~> Stmts <~ T_END) || |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
129 |
(Stmt ==> ((s) => List(s))) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
130 |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
131 |
// compiler |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
132 |
val beginning = """ |
80
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
133 |
.class public XXX.XXX |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
134 |
.super java/lang/Object |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
135 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
136 |
.method public <init>()V |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
137 |
aload_0 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
138 |
invokenonvirtual java/lang/Object/<init>()V |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
139 |
return |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
140 |
.end method |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
141 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
142 |
.method public static write(I)V |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
143 |
.limit locals 5 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
144 |
.limit stack 5 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
145 |
iload 0 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
146 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
147 |
swap |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
148 |
invokevirtual java/io/PrintStream/println(I)V |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
149 |
return |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
150 |
.end method |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
151 |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
152 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
153 |
.method public static main([Ljava/lang/String;)V |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
154 |
.limit locals 200 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
155 |
.limit stack 200 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
156 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
157 |
""" |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
158 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
159 |
val ending = """ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
160 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
161 |
return |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
162 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
163 |
.end method |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
164 |
""" |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
165 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
166 |
// for generating new labels |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
167 |
var counter = -1 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
168 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
169 |
def Fresh(x: String) = { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
170 |
counter += 1 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
171 |
x ++ "_" ++ counter.toString() |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
} |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
173 |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
174 |
type Env = Map[String, String] |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
175 |
type Instrs = List[String] |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
176 |
|
373cf55a3ca5
tuned
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 { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
178 |
case Num(i) => List("ldc " + i.toString + "\n") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
179 |
case Var(s) => List("iload " + env(s) + "\n") |
373cf55a3ca5
tuned
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") |
373cf55a3ca5
tuned
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") |
373cf55a3ca5
tuned
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
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
183 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
184 |
|
76
373cf55a3ca5
tuned
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 { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
186 |
case True => Nil |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
187 |
case False => List("goto " + jmp + "\n") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
188 |
case Relop("=", a1, a2) => |
373cf55a3ca5
tuned
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") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
190 |
case Relop("!=", a1, a2) => |
373cf55a3ca5
tuned
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") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
192 |
case Relop("<", a1, a2) => |
373cf55a3ca5
tuned
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
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
194 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
195 |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
196 |
|
373cf55a3ca5
tuned
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 { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
198 |
case Skip => (Nil, env) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
199 |
case Assign(x, a) => { |
373cf55a3ca5
tuned
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 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
201 |
(compile_aexp(a, env) ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
202 |
List("istore " + index + "\n"), env + (x -> index)) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
203 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
204 |
case If(b, bl1, bl2) => { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
205 |
val if_else = Fresh("If_else") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
206 |
val if_end = Fresh("If_end") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
207 |
val (instrs1, env1) = compile_bl(bl1, env) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
208 |
val (instrs2, env2) = compile_bl(bl2, env1) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
209 |
(compile_bexp(b, env, if_else) ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
210 |
instrs1 ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
211 |
List("goto " + if_end + "\n") ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
212 |
List("\n" + if_else + ":\n\n") ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
213 |
instrs2 ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
214 |
List("\n" + if_end + ":\n\n"), env2) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
215 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
216 |
case While(b, bl) => { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
217 |
val loop_begin = Fresh("Loop_begin") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
218 |
val loop_end = Fresh("Loop_end") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
219 |
val (instrs1, env1) = compile_bl(bl, env) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
220 |
(List("\n" + loop_begin + ":\n\n") ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
221 |
compile_bexp(b, env, loop_end) ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
222 |
instrs1 ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
223 |
List("goto " + loop_begin + "\n") ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
224 |
List("\n" + loop_end + ":\n\n"), env1) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
225 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
226 |
case Write(x) => |
80
191daa3ee29e
tuned
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
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
228 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
229 |
|
76
373cf55a3ca5
tuned
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 { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
231 |
case Nil => (Nil, env) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
232 |
case s::bl => { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
233 |
val (instrs1, env1) = compile_stmt(s, env) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
234 |
val (instrs2, env2) = compile_bl(bl, env1) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
235 |
(instrs1 ++ instrs2, env2) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
236 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
237 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
238 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
239 |
def compile(input: String) : String = { |
80
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
240 |
val class_name = input.split('.')(0) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
241 |
val tks = Tok.fromFile(input) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
242 |
val ast = Stmts.parse_single(tks) |
373cf55a3ca5
tuned
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
191daa3ee29e
tuned
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
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
245 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
246 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
247 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
248 |
def compile_to(input: String, output: String) = { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
249 |
val fw = new java.io.FileWriter(output) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
250 |
fw.write(compile(input)) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
251 |
fw.close() |
75
898c25a4e399
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
252 |
} |
898c25a4e399
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
253 |
|
80
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
254 |
// |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
255 |
val tks = Tok.fromString("x := x + 1") |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
256 |
val ast = Stmt.parse_single(tks) |
191daa3ee29e
tuned
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) |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
258 |
|
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
259 |
|
75
898c25a4e399
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
260 |
|
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
261 |
//examples |
75
898c25a4e399
tuned
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
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
264 |
//compile_to("fib.while", "fib.j") |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
265 |
|
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
266 |
|
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
267 |
// testing cases for time measurements |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
268 |
|
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
269 |
def time_needed[T](i: Int, code: => T) = { |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
270 |
val start = System.nanoTime() |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
271 |
for (j <- 1 to i) code |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
272 |
val end = System.nanoTime() |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
273 |
(end - start)/(i * 1.0e9) |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
274 |
} |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
275 |
|
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
276 |
// for testing |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
277 |
import scala.sys.process._ |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
278 |
|
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
279 |
val test_prog = """ |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
280 |
start := XXX; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
281 |
x := start; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
282 |
y := start; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
283 |
z := start; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
284 |
while 0 < x do { |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
285 |
while 0 < y do { |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
286 |
while 0 < z do { |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
287 |
z := z - 1 |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
288 |
}; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
289 |
z := start; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
290 |
y := y - 1 |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
291 |
}; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
292 |
y := start; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
293 |
x := x - 1 |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
294 |
}; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
295 |
write x; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
296 |
write y; |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
297 |
write z |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
298 |
""" |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
299 |
|
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
300 |
|
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
301 |
def compile_test(n: Int) : Unit = { |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
302 |
val class_name = "LOOP" |
191daa3ee29e
tuned
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)) |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
304 |
val ast = Stmts.parse_single(tks) |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
305 |
val instructions = compile_bl(ast, Map.empty)._1 |
191daa3ee29e
tuned
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) |
191daa3ee29e
tuned
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") |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
308 |
fw.write(assembly) |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
309 |
fw.close() |
191daa3ee29e
tuned
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").!! |
191daa3ee29e
tuned
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).!!)) |
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
312 |
} |
191daa3ee29e
tuned
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
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
316 |
|
191daa3ee29e
tuned
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
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
319 |
// |
191daa3ee29e
tuned
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
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
321 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
322 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
323 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
324 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
325 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
326 |