author | Christian Urban <urbanc@in.tum.de> |
Tue, 01 Oct 2019 23:40:25 +0100 | |
changeset 642 | 064afa8fc1d9 |
parent 625 | 6709fa87410b |
child 670 | 551d018cbbac |
permissions | -rw-r--r-- |
642 | 1 |
// A simple lexer inspired by work of Sulzmann & Lu |
2 |
//================================================== |
|
3 |
||
581 | 4 |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
5 |
import scala.language.implicitConversions |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
import scala.language.reflectiveCalls |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
|
642 | 8 |
// regular expressions including records |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
9 |
abstract class Rexp |
426
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
10 |
case object ZERO extends Rexp |
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
11 |
case object ONE extends Rexp |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
case class CHAR(c: Char) extends Rexp |
579 | 13 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
14 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
15 |
case class STAR(r: Rexp) extends Rexp |
579 | 16 |
case class RECD(x: String, r: Rexp) extends Rexp |
642 | 17 |
|
18 |
// values |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
19 |
abstract class Val |
354
86b2aeae3e98
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
352
diff
changeset
|
20 |
case object Empty extends Val |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
21 |
case class Chr(c: Char) extends Val |
520 | 22 |
case class Sequ(v1: Val, v2: Val) extends Val |
579 | 23 |
case class Left(v: Val) extends Val |
24 |
case class Right(v: Val) extends Val |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
25 |
case class Stars(vs: List[Val]) extends Val |
579 | 26 |
case class Rec(x: String, v: Val) extends Val |
27 |
||
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
28 |
// some convenience for typing in regular expressions |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
29 |
def charlist2rexp(s : List[Char]): Rexp = s match { |
426
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
30 |
case Nil => ONE |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
case c::Nil => CHAR(c) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
} |
581 | 34 |
implicit def string2rexp(s : String) : Rexp = |
35 |
charlist2rexp(s.toList) |
|
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
implicit def RexpOps(r: Rexp) = new { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
def | (s: Rexp) = ALT(r, s) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
def % = STAR(r) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
def ~ (s: Rexp) = SEQ(r, s) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
implicit def stringOps(s: String) = new { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
def | (r: Rexp) = ALT(s, r) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
def | (r: String) = ALT(s, r) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
def % = STAR(s) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
def ~ (r: Rexp) = SEQ(s, r) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
def ~ (r: String) = SEQ(s, r) |
579 | 49 |
def $ (r: Rexp) = RECD(s, r) |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
|
642 | 52 |
def nullable(r: Rexp) : Boolean = r match { |
426
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
53 |
case ZERO => false |
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
54 |
case ONE => true |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
case CHAR(_) => false |
579 | 56 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
case STAR(_) => true |
579 | 59 |
case RECD(_, r1) => nullable(r1) |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
|
642 | 62 |
def der(c: Char, r: Rexp) : Rexp = r match { |
426
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
63 |
case ZERO => ZERO |
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
64 |
case ONE => ZERO |
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
65 |
case CHAR(d) => if (c == d) ONE else ZERO |
579 | 66 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
case SEQ(r1, r2) => |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
else SEQ(der(c, r1), r2) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
579 | 71 |
case RECD(_, r1) => der(c, r1) |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
72 |
} |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
73 |
|
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
74 |
|
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
75 |
// extracts a string from value |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
76 |
def flatten(v: Val) : String = v match { |
354
86b2aeae3e98
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
352
diff
changeset
|
77 |
case Empty => "" |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
78 |
case Chr(c) => c.toString |
579 | 79 |
case Left(v) => flatten(v) |
80 |
case Right(v) => flatten(v) |
|
81 |
case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
82 |
case Stars(vs) => vs.map(flatten).mkString |
579 | 83 |
case Rec(_, v) => flatten(v) |
84 |
} |
|
85 |
||
642 | 86 |
|
624 | 87 |
// extracts an environment from a value; |
642 | 88 |
// used for tokenise a string |
579 | 89 |
def env(v: Val) : List[(String, String)] = v match { |
90 |
case Empty => Nil |
|
91 |
case Chr(c) => Nil |
|
92 |
case Left(v) => env(v) |
|
93 |
case Right(v) => env(v) |
|
94 |
case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
95 |
case Stars(vs) => vs.flatMap(env) |
|
96 |
case Rec(x, v) => (x, flatten(v))::env(v) |
|
97 |
} |
|
98 |
||
642 | 99 |
// The Injection Part of the lexer |
624 | 100 |
|
579 | 101 |
def mkeps(r: Rexp) : Val = r match { |
102 |
case ONE => Empty |
|
103 |
case ALT(r1, r2) => |
|
104 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
|
105 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
106 |
case STAR(r) => Stars(Nil) |
|
107 |
case RECD(x, r) => Rec(x, mkeps(r)) |
|
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
110 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
520 | 111 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
112 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
579 | 113 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
114 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
115 |
case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
|
116 |
case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
|
354
86b2aeae3e98
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
352
diff
changeset
|
117 |
case (CHAR(d), Empty) => Chr(c) |
579 | 118 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
119 |
} |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
120 |
|
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
121 |
// some "rectification" functions for simplification |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
122 |
def F_ID(v: Val): Val = v |
579 | 123 |
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
124 |
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
|
125 |
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
126 |
case Right(v) => Right(f2(v)) |
|
127 |
case Left(v) => Left(f1(v)) |
|
128 |
} |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
129 |
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
520 | 130 |
case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
131 |
} |
354
86b2aeae3e98
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
352
diff
changeset
|
132 |
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = |
520 | 133 |
(v:Val) => Sequ(f1(Empty), f2(v)) |
354
86b2aeae3e98
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
352
diff
changeset
|
134 |
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = |
520 | 135 |
(v:Val) => Sequ(f1(v), f2(Empty)) |
579 | 136 |
def F_RECD(f: Val => Val) = (v:Val) => v match { |
137 |
case Rec(x, v) => Rec(x, f(v)) |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
138 |
} |
579 | 139 |
def F_ERROR(v: Val): Val = throw new Exception("error") |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
140 |
|
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
141 |
def simp(r: Rexp): (Rexp, Val => Val) = r match { |
579 | 142 |
case ALT(r1, r2) => { |
143 |
val (r1s, f1s) = simp(r1) |
|
144 |
val (r2s, f2s) = simp(r2) |
|
145 |
(r1s, r2s) match { |
|
146 |
case (ZERO, _) => (r2s, F_RIGHT(f2s)) |
|
147 |
case (_, ZERO) => (r1s, F_LEFT(f1s)) |
|
148 |
case _ => if (r1s == r2s) (r1s, F_LEFT(f1s)) |
|
149 |
else (ALT (r1s, r2s), F_ALT(f1s, f2s)) |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
150 |
} |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
151 |
} |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
152 |
case SEQ(r1, r2) => { |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
153 |
val (r1s, f1s) = simp(r1) |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
154 |
val (r2s, f2s) = simp(r2) |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
155 |
(r1s, r2s) match { |
426
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
156 |
case (ZERO, _) => (ZERO, F_ERROR) |
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
157 |
case (_, ZERO) => (ZERO, F_ERROR) |
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
158 |
case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s)) |
0debe6f41396
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
159 |
case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
160 |
case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
161 |
} |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
162 |
} |
579 | 163 |
case RECD(x, r1) => { |
164 |
val (r1s, f1s) = simp(r1) |
|
165 |
(RECD(x, r1s), F_RECD(f1s)) |
|
166 |
} |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
167 |
case r => (r, F_ID) |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
168 |
} |
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
169 |
|
624 | 170 |
// lexing functions including simplification |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
171 |
def lex_simp(r: Rexp, s: List[Char]) : Val = s match { |
642 | 172 |
case Nil => if (nullable(r)) mkeps(r) else |
173 |
{ throw new Exception("lexing error") } |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
174 |
case c::cs => { |
579 | 175 |
val (r_simp, f_simp) = simp(der(c, r)) |
176 |
inj(r, c, f_simp(lex_simp(r_simp, cs))) |
|
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
177 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
178 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
179 |
|
642 | 180 |
def lexing_simp(r: Rexp, s: String) = |
181 |
env(lex_simp(r, s.toList)) |
|
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
182 |
|
549 | 183 |
|
642 | 184 |
// The Lexing Rules for the Fun Language |
352
1e1b0fe66107
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
165
diff
changeset
|
185 |
|
579 | 186 |
def PLUS(r: Rexp) = r ~ r.% |
549 | 187 |
|
642 | 188 |
def Range(s : List[Char]) : Rexp = s match { |
189 |
case Nil => ZERO |
|
190 |
case c::Nil => CHAR(c) |
|
191 |
case c::s => ALT(CHAR(c), Range(s)) |
|
192 |
} |
|
193 |
def RANGE(s: String) = Range(s.toList) |
|
194 |
||
195 |
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
|
196 |
val DIGIT = RANGE("0123456789") |
|
579 | 197 |
val ID = SYM ~ (SYM | DIGIT).% |
198 |
val NUM = PLUS(DIGIT) |
|
642 | 199 |
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" |
579 | 200 |
val SEMI: Rexp = ";" |
642 | 201 |
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">" |
579 | 202 |
val WHITESPACE = PLUS(" " | "\n" | "\t") |
642 | 203 |
val RPAREN: Rexp = "{" |
204 |
val LPAREN: Rexp = "}" |
|
579 | 205 |
val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
549 | 206 |
|
207 |
||
579 | 208 |
val WHILE_REGS = (("k" $ KEYWORD) | |
209 |
("i" $ ID) | |
|
210 |
("o" $ OP) | |
|
211 |
("n" $ NUM) | |
|
212 |
("s" $ SEMI) | |
|
213 |
("str" $ STRING) | |
|
214 |
("p" $ (LPAREN | RPAREN)) | |
|
215 |
("w" $ WHITESPACE)).% |
|
549 | 216 |
|
542 | 217 |
|
579 | 218 |
// Two Simple While Tests |
219 |
//======================== |
|
542 | 220 |
|
642 | 221 |
println("test: read n") |
579 | 222 |
|
642 | 223 |
val prog0 = """read n""" |
224 |
println(lexing_simp(WHILE_REGS, prog0)) |
|
579 | 225 |
|
642 | 226 |
println("test: read n; write n ") |
227 |
||
228 |
val prog1 = """read n; write n""" |
|
229 |
println(lexing_simp(WHILE_REGS, prog1)) |
|
579 | 230 |
|
231 |
||
642 | 232 |
// Bigger Tests |
233 |
//============== |
|
234 |
||
235 |
// escapes strings and prints them out as "", "\n" and so on |
|
236 |
def esc(raw: String): String = { |
|
237 |
import scala.reflect.runtime.universe._ |
|
238 |
Literal(Constant(raw)).toString |
|
239 |
} |
|
240 |
||
241 |
def escape(tks: List[(String, String)]) = |
|
242 |
tks.map{ case (s1, s2) => (s1, esc(s2))} |
|
579 | 243 |
|
244 |
val prog2 = """ |
|
642 | 245 |
write "Fib"; |
579 | 246 |
read n; |
247 |
minus1 := 0; |
|
248 |
minus2 := 1; |
|
249 |
while n > 0 do { |
|
250 |
temp := minus2; |
|
251 |
minus2 := minus1 + minus2; |
|
252 |
minus1 := temp; |
|
253 |
n := n - 1 |
|
254 |
}; |
|
617 | 255 |
write "Result"; |
256 |
write minus2 |
|
257 |
""" |
|
258 |
||
642 | 259 |
println("lexing Fib") |
260 |
println(escape(lexing_simp(WHILE_REGS, prog2)).mkString("\n")) |
|
261 |
||
262 |
||
263 |
||
264 |
val prog3 = """ |
|
265 |
start := 1000; |
|
266 |
x := start; |
|
267 |
y := start; |
|
268 |
z := start; |
|
269 |
while 0 < x do { |
|
270 |
while 0 < y do { |
|
271 |
while 0 < z do { |
|
272 |
z := z - 1 |
|
273 |
}; |
|
274 |
z := start; |
|
275 |
y := y - 1 |
|
276 |
}; |
|
277 |
y := start; |
|
278 |
x := x - 1 |
|
279 |
} |
|
280 |
""" |
|
281 |
||
282 |
println("lexing Loops") |
|
283 |
println(escape(lexing_simp(WHILE_REGS, prog3)).mkString("\n")) |
|
284 |