-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlispc.scala
318 lines (286 loc) · 10.1 KB
/
lispc.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package lispc
object ast {
trait Value
case object Undefined extends Value
case class B(b: Boolean) extends Value // Boolean
case class I(n: Int) extends Value // Int
case class S(sym: String) extends Value // Symbol
case object N extends Value // Nil
class P(var car: Value, var cdr: Value) extends Value // Pair
{
override def toString = s"@${super.toString}($car, $cdr)"
}
object P {
def apply(a: Value, b: Value): P = new P(a, b)
def unapply(v: Value): Option[(Value, Value)] = v match {
case p:P => Some((p.car, p.cdr))
case _ => None
}
}
case class F(f: Value => Value) extends Value // Functions
case class Fsubr(f: Value => Value) extends Value // FSUBR -- for exposed interpreter functions
case class Fexpr(f: Value => Value) extends Value // FEXPR -- unevaluated arguments
// Env is a list of frames (each a list of key/value pairs)
// We use object structures for easy reification/reflection.
type Env = P
// Similarly, continuations are values too...
type Cont = F
def list(e: Value): List[Value] = e match {
case N => Nil
case P(first, rest) => first :: list(rest)
}
def valueOf(es: List[Value]): Value = es match {
case Nil => N
case first::rest => P(first, valueOf(rest))
}
def fsubrOf(f: (Value, Env, Cont) => Value) = Fsubr{ v => v match {
case P(exp, P(env:P, P(cont:F, N))) => f(exp, env, cont)
}}
}
import ast._
object eval {
def base_eval(exp: Value, env: Env, cont: Cont): Value = {
exp match {
case I(_) | B(_) => cont.f(exp)
case S(sym) => eval_var(exp, env, cont)
case P(fun, args) => base_apply(exp, env, cont)
}
}
def base_apply(exp: Value, env: Env, cont: Cont): Value = exp match {
case P(fun, args) => base_eval(fun, env, F{ vf => vf match {
case F(f) => evlist(args, env, F{ vas => cont.f(f(vas)) })
case Fsubr(f) => f(P(exp, P(env, P(cont, N))))
case Fexpr(f) => cont.f(f(args))
}})
}
def eval_var(exp: Value, env: Env, cont: Cont): Value = exp match {
case S(x) => cont.f(get(env, x))
}
def eval_quote(exp: Value, env: Env, cont: Cont): Value = exp match {
case P(_, P(x, N)) => cont.f(x)
}
def eval_if(exp: Value, env: Env, cont: Cont): Value = exp match {
case P(_, P(c, P(a, P(b, N)))) => base_eval(c, env, F{ cv => cv match {
case B(false) => base_eval(b, env, cont)
case B(true) => base_eval(a, env, cont)
}})
}
def eval_set_bang(exp: Value, env: Env, cont: Cont): Value = exp match {
case P(_, P(S(x), P(rhs, N))) => base_eval(rhs, env, F{ v =>
cont.f(set(env, x, v))
})
}
def eval_fun(c: (Value => Value) => Value)(exp: Value, env: Env, cont: Cont): Value = exp match {
case P(_, P(params, body)) => cont.f(c({args =>
eval_begin(body, extend(env, params, args), F{v => v})
}))
}
def eval_lambda = eval_fun(F.apply) _
def eval_fsubr = eval_fun(Fsubr.apply) _
def eval_fexpr = eval_fun(Fexpr.apply) _
def eval_begin_exp(exp: Value, env: Env, cont: Cont): Value = exp match {
case P(_, body) => eval_begin(body, env, cont)
}
def eval_begin(exp: Value, env: Env, cont: Cont): Value = exp match {
case P(e, N) => base_eval(e, env, cont)
case P(e, es) => base_eval(e, env, F{ _ => eval_begin(es, env, cont) })
}
def eval_define(exp: Value, env: Env, cont: Cont): Value = exp match {
case P(_, P(r@S(name), body)) => {
val p = P(r,Undefined)
env.car = P(p, env.car)
eval_begin(body, env, F{v =>
p.cdr = v
cont.f(r)})
}
}
def evlist(exp: Value, env: Env, cont: Cont): Value = exp match {
case N => cont.f(N)
case P(first, rest) => base_eval(first, env, F{v => evlist(rest, env, F{vs => cont.f(P(v,vs))})})
}
def extend(env: Env, params: Value, args: Value): Env = {
val frame = valueOf((list(params) zip list(args)).map{t => P(t._1, t._2)})
P(frame, env)
}
def findFrame(frame: Value, x: String): Option[P] = frame match {
case N => None
case P(P(S(y),_), _) if (x==y) => Some(frame.asInstanceOf[P].car.asInstanceOf[P])
case P(_, rest) => findFrame(rest, x)
}
def find(env: Env, x: String): P = env match {
case P(first,rest) => findFrame(first, x) match {
case Some(p) => p
case None => rest match {
case next:Env => find(next, x)
case _ => sys.error(s"unbound variable $x")
}
}
}
def get(env: Env, x: String): Value = find(env, x).cdr
def set(env: Env, x: String, v: Value): Value = {
val p = find(env, x)
p.cdr = v
v
}
def make_init_env(): Env = {
lazy val init_env: Env = P(valueOf(List(
P(S("<"), F({args => args match { case P(I(a), P(I(b), N)) => B(a<b) }})),
P(S("*"), F({args => args match { case P(I(a), P(I(b), N)) => I(a*b) }})),
P(S("-"), F({args => args match { case P(I(a), P(I(b), N)) => I(a-b) }})),
P(S("eq?"), F({args => args match { case P(a, P(b, N)) => B(a==b) }})),
P(S("cons"), F({args => args match { case P(a, P(b, N)) => P(a, b) }})),
P(S("car"), F({args => args match { case P(P(a, d), N) => a }})),
P(S("cdr"), F({args => args match { case P(P(a, d), N) => d }})),
P(S("list"), F({args => args})),
P(S("quote"), fsubrOf(eval_quote)),
P(S("if"), fsubrOf(eval_if)),
P(S("set!"), fsubrOf(eval_set_bang)),
P(S("lambda"), fsubrOf(eval_lambda)),
P(S("fsubr"), fsubrOf(eval_fsubr)),
P(S("fexpr"), fsubrOf(eval_fexpr)),
P(S("begin"), fsubrOf(eval_begin_exp)),
P(S("define"), fsubrOf(eval_define)),
P(S("eval"), F({args => args match { case P(a, N) => base_eval(a, init_env, F{v => v}) }}))
)), N)
init_env
}
}
import scala.util.parsing.combinator._
object parser extends JavaTokenParsers {
def exp: Parser[Value] =
"#f" ^^ { case _ => B(false) }
| "#t" ^^ { case _ => B(true) }
| wholeNumber ^^ { case s => I(s.toInt) }
| """[^\s\(\)'"]+""".r ^^ { case s => S(s) }
| "'" ~> exp ^^ { case s => P(S("quote"), P(s, N)) }
| "()" ^^ { case _ => N }
| "(" ~> exps <~ ")" ^^ { case vs => vs }
def exps: Parser[Value] =
exp ~ exps ^^ { case v~vs => P(v, vs) }
| exp ^^ { case v => P(v, N) }
}
import eval._
import parser._
object repl {
var global_env = make_init_env()
def parse(s: String) = {
val Success(e, _) = parseAll(exp, s)
e
}
def evl(e: Value) = { base_eval(e, global_env, F{ v => v } ) }
def ev(s: String) = evl(parse(s))
def clean() = {
global_env = make_init_env()
}
}
object pp {
def addParen(p: (Boolean, String)) = {
val (need_paren, s) = p
if (need_paren) "("+s+")" else s
}
def pp(v: Value): (Boolean, String) = v match {
case B(b) => (false, if (b) "#t" else "#f")
case I(n) => (false, n.toString)
case S(s) => (false, s)
case N => (true, "")
case P(a, N) => (true, addParen(pp(a)))
case P(a, d) =>
val s1 = addParen(pp(a))
val (need_paren2, s2) = pp(d)
if (need_paren2) (true, s1+" "+s2)
else (true, s1+" . "+s2)
case _ => (false, v.toString)
}
def show(v: Value) = addParen(pp(v))
def display(v: Value) = print(show(v))
def newline() = println("")
}
import repl._
import pp._
import utils._
class lispc_Tests extends TestSuite { before { clean() }
test("(factorial 6)") {
ev("""(define factorial (lambda (n) (if (< n 2) n (* n (factorial (- n 1))))))""")
assertResult(I(720))(ev("(factorial 6)"))
}
test("eq?") {
assertResult(B(true))(ev("(eq? 1 1)"))
assertResult(B(false))(ev("(eq? 1 2)"))
assertResult(B(false))(ev("(eq? (list 1) (list 1))"))
}
test("(odd 7)") {
ev("""(begin
(define even (lambda (n) (if (eq? n 0) #t (odd (- n 1)))))
(define odd (lambda (n) (if (eq? n 0) #f (even (- n 1)))))
)""")
assertResult(B(true))(ev("(odd 7)"))
}
test("eval") {
ev("(define x 1)")
assertResult(I(1))(ev("(eval 'x)"))
assertResult(I(2))(ev("(* (eval 'x) 2)"))
}
test("fexpr if") {
ev("(define my-if (fexpr (c a b) (if (eval c) (eval a) (eval b))))")
assertResult(I(1))(ev("(my-if #t 1 bad)"))
}
test("list") {
// NOTE: we use `show` to compare pairs,
// to by-pass referential equality.
assertResult(ev("'()"))(ev("(list)"))
assertResult(N)(ev("(list)"))
assertResult(show(P(I(10), N)))(show(ev("(list 10)")))
assertResult(show(P(I(10), N)))(show(ev("(cons '10 '())")))
ev("(define history '())")
assertResult(N)(ev("history"))
assertResult(show(P(I(4), N)))(show(ev("(cons 4 history)")))
}
test("fexpr history") {
ev("(define history '())")
ev("""(define save! (fexpr (lhs rhs)
((lambda (old-val)
(eval (list 'set! lhs rhs))
(set! history (cons (list
lhs
old-val (eval lhs)) history)))
(eval lhs))))""")
ev("(define test 1)")
ev("(save! test (* test 2))")
assertResult(I(2))(ev("test"))
assertResult("((test 1 2))")(show(ev("history")))
ev("(save! test (* test 2))")
assertResult(I(4))(ev("test"))
assertResult("((test 2 4) (test 1 2))")(show(ev("history")))
}
test("fsubr") {
ev("(define my-exp (fsubr (exp env cont) exp))")
assertResult("(my-exp x)")(show(ev("(my-exp x)")))
ev("(define jump (fsubr (exp env cont) (eval (car (cdr exp)))))")
assertResult(I(2))(ev("(- 1 (jump 2))"))
ev("(define fall (fsubr (exp env cont) 1))")
assertResult(I(1))(ev("(* 2 (fall))"))
// NOTE: to work nicely with composing continuations,
// we would have to adjust the calling conventions...
}
test("fsubr history") {
ev("(define old-set! set!)")
ev("(define history '())")
ev("""(define save! (fexpr (lhs rhs)
((lambda (old-val)
(eval (list 'old-set! lhs rhs))
(old-set! history (cons (list
lhs
old-val (eval lhs)) history)))
(eval lhs))))""")
ev("""(set! set! (fsubr (exp env cont)
(eval (list 'save! (car (cdr exp)) (car (cdr (cdr exp)))))
(cont (car (cdr exp)))))""")
ev("(define test 1)")
ev("(set! test (* test 2))")
assertResult(I(2))(ev("test"))
assertResult("((test 1 2))")(show(ev("history")))
ev("(set! test (* test 2))")
assertResult(I(4))(ev("test"))
assertResult("((test 2 4) (test 1 2))")(show(ev("history")))
}
}