864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// A tokeniser for the Fun language
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
//==================================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
// call with
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
// amm fun_tokens.sc fact.fun
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
// amm fun_tokens.sc defs.fun
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
import scala.language.implicitConversions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
import scala.language.reflectiveCalls
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
abstract class Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
case object ZERO extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
case object ONE extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
case class CHAR(c: Char) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case class STAR(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
case class RECD(x: String, r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
abstract class Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
case object Empty extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
case class Chr(c: Char) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
case class Sequ(v1: Val, v2: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
case class Left(v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
case class Right(v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
case class Stars(vs: List[Val]) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
case class Rec(x: String, v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
// some convenience for typing in regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
def charlist2rexp(s : List[Char]): Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
case Nil => ONE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
case c::Nil => CHAR(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
case c::s => SEQ(CHAR(c), charlist2rexp(s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
implicit def string2rexp(s : String) : Rexp =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
charlist2rexp(s.toList)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
implicit def RexpOps(r: Rexp) = new {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
def | (s: Rexp) = ALT(r, s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
def % = STAR(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
def ~ (s: Rexp) = SEQ(r, s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
implicit def stringOps(s: String) = new {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
def | (r: Rexp) = ALT(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
def | (r: String) = ALT(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
def % = STAR(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
def ~ (r: Rexp) = SEQ(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
def ~ (r: String) = SEQ(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
def $ (r: Rexp) = RECD(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
def nullable (r: Rexp) : Boolean = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
case ZERO => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
case ONE => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
case CHAR(_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
case ALT(r1, r2) => nullable(r1) || nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
case STAR(_) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
case RECD(_, r1) => nullable(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
def der (c: Char, r: Rexp) : Rexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
case ZERO => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
case ONE => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
case CHAR(d) => if (c == d) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
case SEQ(r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
else SEQ(der(c, r1), r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
case STAR(r) => SEQ(der(c, r), STAR(r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
case RECD(_, r1) => der(c, r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
// extracts a string from value
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
def flatten(v: Val) : String = v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
case Empty => ""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
case Chr(c) => c.toString
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
case Left(v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
case Right(v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
case Sequ(v1, v2) => flatten(v1) + flatten(v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
case Stars(vs) => vs.map(flatten).mkString
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
case Rec(_, v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
// extracts an environment from a value;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
// used for tokenise a string
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
def env(v: Val) : List[(String, String)] = v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
case Empty => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
case Chr(c) => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
case Left(v) => env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
case Right(v) => env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
case Sequ(v1, v2) => env(v1) ::: env(v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
case Stars(vs) => vs.flatMap(env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
case Rec(x, v) => (x, flatten(v))::env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
// The Injection Part of the lexer
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
def mkeps(r: Rexp) : Val = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
case ONE => Empty
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
case ALT(r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
case STAR(r) => Stars(Nil)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
case RECD(x, r) => Rec(x, mkeps(r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
case (CHAR(d), Empty) => Chr(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
case _ => { println ("Injection error") ; sys.exit(-1) }
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
// some "rectification" functions for simplification
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
def F_ID(v: Val): Val = v
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
case Right(v) => Right(f2(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
case Left(v) => Left(f1(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
(v:Val) => Sequ(f1(Empty), f2(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
(v:Val) => Sequ(f1(v), f2(Empty))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
def F_RECD(f: Val => Val) = (v:Val) => v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
case Rec(x, v) => Rec(x, f(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
def F_ERROR(v: Val): Val = throw new Exception("error")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
def simp(r: Rexp): (Rexp, Val => Val) = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
case ALT(r1, r2) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
val (r1s, f1s) = simp(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
val (r2s, f2s) = simp(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
(r1s, r2s) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
case (ZERO, _) => (r2s, F_RIGHT(f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
case (_, ZERO) => (r1s, F_LEFT(f1s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
else (ALT (r1s, r2s), F_ALT(f1s, f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
case SEQ(r1, r2) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
val (r1s, f1s) = simp(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
val (r2s, f2s) = simp(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
(r1s, r2s) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
162 |
case (ZERO, _) => (ZERO, F_ERROR)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
case (_, ZERO) => (ZERO, F_ERROR)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
164 |
case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
165 |
case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
166 |
case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
167 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
168 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
case RECD(x, r1) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
val (r1s, f1s) = simp(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
171 |
(RECD(x, r1s), F_RECD(f1s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
172 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
173 |
case r => (r, F_ID)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
175 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
176 |
// lexing functions including simplification
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
177 |
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
case Nil => if (nullable(r)) mkeps(r) else { println ("Lexing Error") ; sys.exit(-1) }
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
case c::cs => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
180 |
val (r_simp, f_simp) = simp(der(c, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
181 |
inj(r, c, f_simp(lex_simp(r_simp, cs)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
183 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
184 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
186 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
187 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
188 |
// The Lexing Rules for the Fun Language
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
189 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
190 |
def PLUS(r: Rexp) = r ~ r.%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
191 |
def OPT(r: Rexp) = r | ONE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
192 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
193 |
val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
194 |
"l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
195 |
"w" | "x" | "y" | "z" | "A" | "B" | "C" | "D" |"E" | "F" | "G" |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
196 |
"H" | "I" | "J" | "K" |"L" | "M" | "N" |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
197 |
"O" | "P" | "Q" | "R" |"S" | "T" | "U" |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
198 |
"V" | "W" | "X" | "Y" | "Z" | "_"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
199 |
val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
200 |
val ID = SYM ~ (SYM | DIGIT).%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
201 |
val NUM = PLUS(DIGIT)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
202 |
val FNUM = OPT("-") ~ NUM ~ "." ~ NUM
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
203 |
val KEYWORD : Rexp = "if" | "then" | "else" | "def" | "val"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
204 |
val TYPE : Rexp = "Void" | "Int" | "Double"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
205 |
val SEMI: Rexp = ";"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
206 |
val COLON: Rexp = ":"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
207 |
val COMMA: Rexp = ","
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
208 |
val OP: Rexp = "=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
209 |
val WHITESPACE = PLUS(" " | "\n" | "\t" | "\r")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
210 |
val RPAREN: Rexp = ")" | "}"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
211 |
val LPAREN: Rexp = "(" | "{"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
212 |
val ALL = SYM | DIGIT | OP | " " | ":" | ";" | "-" | "." | "\"" | "=" | "," | "(" | ")" | "{" | "}"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
213 |
val ALL2 = ALL | "\n"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
214 |
val COMMENT = ("/*" ~ ALL2.% ~ "*/") | ("//" ~ ALL.% ~ "\n")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
215 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
216 |
val CHR :Rexp = "'" ~ (ALL | "\\n") ~ "'"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
217 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
218 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
219 |
val FUN_REGS = (("k" $ KEYWORD) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
220 |
("t" $ TYPE) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
221 |
("i" $ ID) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
222 |
("ch" $ CHR) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
223 |
("o" $ OP) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
224 |
("n" $ NUM) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
225 |
("f" $ FNUM) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
226 |
("s" $ SEMI) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
227 |
("co" $ COLON) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
228 |
("c" $ COMMA) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
229 |
("pl" $ LPAREN) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
230 |
("pr" $ RPAREN) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
231 |
("w" $ (WHITESPACE | COMMENT))).%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
232 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
233 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
234 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
235 |
// The tokens for the Fun language
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
236 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
237 |
abstract class Token extends Serializable
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
238 |
case object T_SEMI extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
239 |
case object T_COMMA extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
240 |
case object T_COLON extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
241 |
case object T_LPAREN extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
242 |
case object T_RPAREN extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
243 |
case class T_ID(s: String) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
244 |
case class T_FID(s: String) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
245 |
case class T_OP(s: String) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
246 |
case class T_NUM(n: Int) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
247 |
case class T_FNUM(x: Float) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
248 |
case class T_KWD(s: String) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
249 |
case class T_TY(s: String) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
250 |
case class T_CHR(i: Int) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
251 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
252 |
val token : PartialFunction[(String, String), Token] = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
253 |
case ("k", s) => T_KWD(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
254 |
case ("t", s) => T_TY(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
255 |
case ("i", s) => T_ID(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
256 |
case ("o", s) => T_OP(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
257 |
case ("n", s) => T_NUM(s.toInt)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
258 |
case ("ch", s) => if (s == "'\\n'") T_CHR(10) else T_CHR(s(1).toInt)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
259 |
case ("f", s) => T_FNUM(s.toFloat)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
260 |
case ("s", _) => T_SEMI
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
261 |
case ("c", _) => T_COMMA
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
262 |
case ("co", _) => T_COLON
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
263 |
case ("pl", _) => T_LPAREN
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
264 |
case ("pr", _) => T_RPAREN
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
265 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
266 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
267 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
268 |
def tokenise(s: String) : List[Token] = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
269 |
val tks = lexing_simp(FUN_REGS, s).collect(token)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
270 |
if (tks.length != 0) tks
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
271 |
else { println (s"Tokenise Error") ; sys.exit(-1) }
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
272 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
273 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
274 |
import ammonite.ops._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
275 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
276 |
//@doc("Tokenising a file.")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
277 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
278 |
def main(fname: String) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
279 |
println(tokenise(os.read(os.pwd / fname)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
280 |
}
|