Many closure tests, mainly from lox
parent
03fd228c9b
commit
2338430e0a
|
@ -0,0 +1,3 @@
|
|||
## Credits
|
||||
|
||||
Many of the tests are adapted from [craftinginterpriters](https://github.com/munificent/craftinginterpreters/tree/master/test).
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass r7rs-spec\\4-1-6-Assignments.scm"
|
||||
|
||||
---
|
||||
3.05.0
|
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\assign-closures.scm"
|
||||
|
||||
---
|
||||
1.0
|
||||
2.0
|
||||
2.0
|
||||
3.0
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\assign-shadowed-later.scm"
|
||||
|
||||
---
|
||||
1.0
|
||||
0.0
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\bagel-donut.scm"
|
||||
|
||||
---
|
||||
4.0
|
||||
2.0
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\close-over-function-param.scm"
|
||||
|
||||
---
|
||||
77.0
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\close-over-later-variable.scm"
|
||||
|
||||
---
|
||||
2.0
|
||||
1.0
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\close-unused.scm"
|
||||
|
||||
---
|
||||
7.0
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\closed-closure-in-function.scm"
|
||||
|
||||
---
|
||||
1.0
|
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\gnarly-i.scm"
|
||||
|
||||
---
|
||||
3.0
|
||||
2.0
|
||||
1.0
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\mutate-levels.scm"
|
||||
|
||||
---
|
||||
2.0
|
||||
3.0
|
||||
3.0
|
||||
3.0
|
||||
7.0
|
||||
8.0
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\nested-assign.scm"
|
||||
|
||||
---
|
||||
1.0
|
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\nested-closure.scm"
|
||||
|
||||
---
|
||||
1.0
|
||||
2.0
|
||||
3.0
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\objects-are-a-poor-mans-closure.scm"
|
||||
|
||||
---
|
||||
1.0
|
||||
7.0
|
||||
1.0
|
||||
7.0
|
||||
|
||||
8.0
|
||||
2.0
|
||||
8.0
|
||||
2.0
|
||||
|
||||
9.0
|
||||
9.0
|
||||
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\open-closure-in-function.scm"
|
||||
|
||||
---
|
||||
1.0
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\reference-closure-multiple-times.scm"
|
||||
|
||||
---
|
||||
1.0
|
||||
1.0
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\reuse-closure-slot.scm"
|
||||
|
||||
---
|
||||
1.0
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\scope.scm"
|
||||
|
||||
---
|
||||
7.0
|
||||
2.0
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\shadow-closure-with-local.scm"
|
||||
|
||||
---
|
||||
0.0
|
||||
1.0
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\unused-later-closures.scm"
|
||||
|
||||
---
|
||||
1.0
|
||||
1.0
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\use-after-close.scm"
|
||||
|
||||
---
|
||||
1.0
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: "run-pass closure\\val-or-var.scm"
|
||||
|
||||
---
|
||||
0.0
|
||||
1.0
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
; https://github.com/munificent/craftinginterpreters/blob/master/test/closure/assign_to_closure.lox
|
||||
|
||||
(define f #f)
|
||||
(define g #f)
|
||||
|
||||
(define (displayln x) (display x) (newline))
|
||||
|
||||
((lambda ()
|
||||
(define local 1)
|
||||
|
||||
(define (f_)
|
||||
(displayln local)
|
||||
(set! local 2)
|
||||
(displayln local))
|
||||
|
||||
(set! f f_)
|
||||
|
||||
(define (g_)
|
||||
(displayln local)
|
||||
(set! local 3)
|
||||
(displayln local))
|
||||
|
||||
(set! g g_)))
|
||||
|
||||
(f)
|
||||
(g)
|
|
@ -0,0 +1,16 @@
|
|||
; https://github.com/munificent/craftinginterpreters/blob/master/test/closure/assign_to_shadowed_later.lox
|
||||
|
||||
(define a 0)
|
||||
|
||||
(define (displayln x) (display x) (newline))
|
||||
|
||||
|
||||
((lambda ()
|
||||
(define (assign) (set! a 1))
|
||||
|
||||
(define a 2)
|
||||
(assign)
|
||||
(displayln a)
|
||||
))
|
||||
|
||||
(displayln a)
|
|
@ -0,0 +1,11 @@
|
|||
(define (displayln x) (display x) (newline))
|
||||
|
||||
(define (make-print x)
|
||||
(define (print) (displayln x))
|
||||
print)
|
||||
|
||||
(define print-2 (make-print 2))
|
||||
(define print-4 (make-print 4))
|
||||
|
||||
(print-4)
|
||||
(print-2)
|
|
@ -0,0 +1,9 @@
|
|||
(define f #f)
|
||||
|
||||
(define (foo param)
|
||||
(define (f_) (display param))
|
||||
(set! f f_))
|
||||
|
||||
(foo 77)
|
||||
|
||||
(f)
|
|
@ -0,0 +1,14 @@
|
|||
; https://github.com/munificent/craftinginterpreters/blob/master/test/closure/close_over_later_variable.lox
|
||||
|
||||
(define (f)
|
||||
(define a 1)
|
||||
(define b 2)
|
||||
|
||||
(define (g)
|
||||
(display b)
|
||||
(newline)
|
||||
(display a))
|
||||
|
||||
(g))
|
||||
|
||||
(f)
|
|
@ -0,0 +1,10 @@
|
|||
((lambda ()
|
||||
(define a 1)
|
||||
|
||||
(if #f
|
||||
(lambda () a)
|
||||
0
|
||||
)
|
||||
))
|
||||
|
||||
(display 7)
|
|
@ -0,0 +1,11 @@
|
|||
; https://github.com/munificent/craftinginterpreters/blob/master/test/closure/closed_closure_in_function.lox
|
||||
|
||||
(define f #f)
|
||||
|
||||
((lambda ()
|
||||
(define local 1)
|
||||
(define (f_) (display local))
|
||||
(set! f f_)
|
||||
))
|
||||
|
||||
(f)
|
|
@ -0,0 +1,19 @@
|
|||
#lang scheme
|
||||
|
||||
(define (displayln x) (display x) (newline))
|
||||
|
||||
(define (outer)
|
||||
(define x 1)
|
||||
|
||||
(define (middle)
|
||||
(define (inner) (displayln x))
|
||||
(displayln 2)
|
||||
inner)
|
||||
|
||||
(displayln 3)
|
||||
|
||||
middle)
|
||||
|
||||
(define mid (outer))
|
||||
(define in (mid))
|
||||
(in)
|
|
@ -0,0 +1,20 @@
|
|||
(define x 2)
|
||||
|
||||
(define (show-x) (display x) (newline))
|
||||
|
||||
(show-x)
|
||||
(set! x 3)
|
||||
(show-x)
|
||||
|
||||
((lambda ()
|
||||
(define x 7)
|
||||
(show-x)
|
||||
(set! x 8)
|
||||
(show-x)))
|
||||
|
||||
((lambda ()
|
||||
(define (show-x) (display x) (newline))
|
||||
(define x 7)
|
||||
(show-x)
|
||||
(set! x 8)
|
||||
(show-x)))
|
|
@ -0,0 +1,7 @@
|
|||
(define (outer)
|
||||
(define x 0)
|
||||
(define (y) (set! x 1))
|
||||
(y)
|
||||
(display x))
|
||||
|
||||
(outer)
|
|
@ -0,0 +1,25 @@
|
|||
; https://github.com/munificent/craftinginterpreters/blob/master/test/closure/nested_closure.lox
|
||||
#lang scheme
|
||||
|
||||
|
||||
(define (displayln x) (display x) (newline))
|
||||
|
||||
(define f #f)
|
||||
|
||||
(define (f1)
|
||||
(define a 1)
|
||||
(define (f2)
|
||||
(define b 2)
|
||||
(define (f3)
|
||||
(define c 3)
|
||||
(define (f4)
|
||||
(displayln a)
|
||||
(displayln b)
|
||||
(displayln c))
|
||||
(set! f f4))
|
||||
(f3))
|
||||
(f2))
|
||||
|
||||
(f1)
|
||||
|
||||
(f)
|
|
@ -0,0 +1,32 @@
|
|||
#lang scheme
|
||||
|
||||
(define get-x 1)
|
||||
(define get-y 2)
|
||||
(define do-print 3)
|
||||
(define add 4)
|
||||
|
||||
(define (displayln x) (display x) (newline))
|
||||
|
||||
(define (vector x y)
|
||||
(define (add-to other)
|
||||
(vector (+ x (other get-x)) (+ y (other get-y))))
|
||||
|
||||
(lambda (message)
|
||||
(if (= message get-x) x
|
||||
(if (= message get-y) y
|
||||
(if (= message add) add-to
|
||||
(if (= message do-print) ((lambda () (displayln x) (displayln y) (newline)))
|
||||
((lambda () (displayln -99) 0))))))))
|
||||
|
||||
(define v-1-7 (vector 1 7))
|
||||
(displayln (v-1-7 get-x))
|
||||
(displayln (v-1-7 get-y))
|
||||
(v-1-7 do-print)
|
||||
|
||||
(define v-8-2 (vector 8 2))
|
||||
(displayln (v-8-2 get-x))
|
||||
(displayln (v-8-2 get-y))
|
||||
(v-8-2 do-print)
|
||||
|
||||
(define v-9-9 ((v-8-2 add) v-1-7))
|
||||
(v-9-9 do-print)
|
|
@ -0,0 +1,9 @@
|
|||
#lang scheme
|
||||
|
||||
; https://github.com/munificent/craftinginterpreters/blob/master/test/closure/open_closure_in_function.lox
|
||||
|
||||
|
||||
((lambda ()
|
||||
(define local 1)
|
||||
(define (f) (display local))
|
||||
(f)))
|
|
@ -0,0 +1,16 @@
|
|||
#lang scheme
|
||||
; https://github.com/munificent/craftinginterpreters/blob/master/test/closure/reference_closure_multiple_times.lox
|
||||
|
||||
(define f #f)
|
||||
|
||||
((lambda ()
|
||||
(define a 1)
|
||||
(define (f_)
|
||||
(display a)
|
||||
(newline)
|
||||
(display a))
|
||||
(set! f f_)))
|
||||
|
||||
(f)
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#lang scheme
|
||||
|
||||
; https://github.com/munificent/craftinginterpreters/blob/master/test/closure/reuse_closure_slot.lox
|
||||
|
||||
((lambda ()
|
||||
(define f #f)
|
||||
((lambda ()
|
||||
(define a 1)
|
||||
(define (f_) (display a))
|
||||
(set! f f_)))
|
||||
|
||||
((lambda ()
|
||||
(define b 2)
|
||||
(f)))))
|
|
@ -0,0 +1,9 @@
|
|||
(define x 2)
|
||||
|
||||
(define (displayln x) (display x) (newline))
|
||||
|
||||
(define (gy y) x)
|
||||
(define (gx x) x)
|
||||
|
||||
(displayln (gx 7))
|
||||
(displayln (gy 7))
|
|
@ -0,0 +1,14 @@
|
|||
; https://github.com/munificent/craftinginterpreters/blob/master/test/closure/shadow_closure_with_local.lox
|
||||
; TODO: Forbid this https://discordapp.com/channels/571040468092321801/618895179343986688/925533402592079912
|
||||
|
||||
|
||||
(define (displayln x) (display x) (newline))
|
||||
|
||||
((lambda ()
|
||||
(define foo 0)
|
||||
(define (f)
|
||||
((lambda ()
|
||||
(displayln foo)
|
||||
(define foo 1)
|
||||
(displayln foo))))
|
||||
(f)))
|
|
@ -0,0 +1,25 @@
|
|||
(define closure #f)
|
||||
|
||||
((lambda ()
|
||||
(define a 1)
|
||||
|
||||
|
||||
((lambda ()
|
||||
(define b 2)
|
||||
|
||||
(define (get-a) a)
|
||||
|
||||
(set! closure get-a)
|
||||
|
||||
(if #f
|
||||
(lambda () b)
|
||||
0)
|
||||
|
||||
))
|
||||
|
||||
(display (closure))
|
||||
|
||||
))
|
||||
|
||||
(newline)
|
||||
(display (closure))
|
|
@ -0,0 +1,8 @@
|
|||
(define (outer)
|
||||
(define x 1)
|
||||
(define (inner) (display x))
|
||||
inner)
|
||||
|
||||
(define clousre (outer))
|
||||
(clousre)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
(define globalSet #f)
|
||||
(define globalGet #f)
|
||||
|
||||
(define (main)
|
||||
(define a 0)
|
||||
|
||||
(define (set) (set! a 1))
|
||||
(define (get) (display a) (newline))
|
||||
|
||||
(set! globalSet set)
|
||||
(set! globalGet get)
|
||||
)
|
||||
|
||||
(main)
|
||||
(globalGet)
|
||||
(globalSet)
|
||||
(globalGet)
|
|
@ -0,0 +1,4 @@
|
|||
(define x 2)
|
||||
(display (+ x 1))
|
||||
(set! x 4)
|
||||
(display (+ x 1))
|
Loading…
Reference in New Issue