-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-test.rkt
78 lines (60 loc) · 2.16 KB
/
parse-test.rkt
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
#lang racket/base
;; (require rackcheck)
(require racket/match
rackunit
"types.rkt"
"parse.rkt")
(define-syntax-rule (check-exp-parse? s e)
(check-equal? (parse-exp! s) e))
(define-syntax-rule (check-program-parse? s e)
(check-equal? (parse-program! s) e))
(define (to-exp x)
(match x
[(? integer?) (const-exp x)]
[(? symbol?) (var-exp x)]
[_ x]))
(define (call-with-to-exp fn . args)
(apply fn (map to-exp args)))
(define (diff-exp* e1 e2)
(call-with-to-exp diff-exp e1 e2))
(define (zero?-exp* e1)
(call-with-to-exp zero?-exp e1))
(define (if-exp* e1 e2 e3)
(call-with-to-exp if-exp e1 e2 e3))
(define (let-exp* e1 e2 e3)
(let-exp e1 (to-exp e2) (to-exp e3)))
(check-exp-parse? "1" (const-exp 1))
(check-exp-parse? "123" (const-exp 123))
(check-exp-parse? "-(1, 2)" (diff-exp* 1 2))
(check-exp-parse? "-(1,2)" (diff-exp* 1 2))
(check-exp-parse? "- ( 1 , 2 )" (diff-exp* 1 2))
(check-exp-parse? "zero?(1)" (zero?-exp* 1))
(check-exp-parse? "zero? ( 123 )" (zero?-exp* 123))
(check-exp-parse? "if 1 then 2 else 3" (if-exp* 1 2 3))
(check-exp-parse? "if if 1 then 2 else 3 then 2 else 3" (if-exp* (if-exp* 1 2 3) 2 3))
(check-exp-parse? "if1then2else3" (if-exp* 1 2 3)) ;maybe not :)
(check-exp-parse? "a" (var-exp 'a))
(check-exp-parse? "ab" (var-exp 'ab))
(check-exp-parse? "x1" (var-exp 'x1))
(check-exp-parse? "let x=1 in x" (let-exp* 'x 1 'x))
(check-exp-parse? "let x = 1 in -(x, 1)" (let-exp* 'x 1 (diff-exp* 'x 1)))
(check-exp-parse? "let x = -(y, 1) in x" (let-exp* 'x (diff-exp* 'y 1) 'x))
(check-exp-parse?
"if if -
( 123
,
15
)
then
-(2,3)
else 2 then 2 else 1"
(if-exp (if-exp (diff-exp* 123 15) (diff-exp* 2 3) (const-exp 2)) (const-exp 2) (const-exp 1)))
(check-program-parse? "-(55, -(x, 11))" (a-program (diff-exp* 55 (diff-exp* 'x 11))))
(check-program-parse? " -(55, -(x, 11))" (a-program (diff-exp* 55 (diff-exp* 'x 11))))
;; (check-property
;; (property ([n (gen:integer-in 3 100)])
;; (define numbers (stream->list (stream-take (fibs) n)))
;; (for ([n (cddr numbers)]
;; [y (cdr numbers)]
;; [x numbers])
;; (check-eqv? (+ x y) n))))