-
Notifications
You must be signed in to change notification settings - Fork 4
/
undo.scala
73 lines (59 loc) · 1.56 KB
/
undo.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
package scala.lms.black
import eval._
import repl._
import org.scalatest.BeforeAndAfter
class TestUndo extends TestSuite with BeforeAndAfter {
val under = "undo_"
before {
clean()
}
def reify_env = """(begin
(define old-eval-var eval-var)
(set! eval-var
(clambda (e r k)
(if (eq? '_env e) (k r) (old-eval-var e r k))))
)"""
def mm_undo = """(begin
(define undo-list '())
(define old-eval-set! eval-set!)
(define list
(lambda args args))
(set! eval-set!
(clambda (e r k)
(let ((name (car (cdr e))))
(eval-var name r (lambda (v)
(set! undo-list (cons (cons name v) undo-list))
(old-eval-set! e r k))))))
(define reflect-undo!
(clambda (r)
(if (null? undo-list)
'()
(begin
(old-eval-set!
(list 'set! (car (car undo-list)) (list 'quote (cdr (car undo-list))))
r
(lambda (v) v))
(set! undo-list (cdr undo-list))))))
)"""
def m_undo = """
(define undo!
(clambda ()
((EM reflect-undo!) _env)))
"""
def all = s"""(begin
(EM (EM $reify_env))
(EM (EM $mm_undo))
(EM $m_undo)
)"""
test("undo") {
ev(all)
ev("(EM (define old-eval-var eval-var))")
ev("(EM (set! eval-var (clambda (e r k) (if (eq? e 'n) (k 0) (old-eval-var e r k)))))")
ev("(define n 1)")
assertResult(I(0)){ev("n")}
assertResult(B(false)){ev("(EM (eq? old-eval-var eval-var))")}
ev("(EM (undo!))")
assertResult(I(1)){ev("n")}
assertResult(B(true)){ev("(EM (eq? old-eval-var eval-var))")}
}
}