Skip to content

Commit e6ca88c

Browse files
committed
crook
1 parent fe5e660 commit e6ca88c

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

fraud-plus/test/parse.rkt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,12 @@
4343
(check-equal? (parse-closed '(let ((x 1)) (let ((x 2)) x)))
4444
(p (Let '(x) (list (Lit 1)) (Let '(x) (list (Lit 2)) (Var 'x)))))
4545
(check-equal? (parse-closed '(let* ((x 1) (x 2)) x))
46-
(p (Let* '(x x) (list (Lit 1) (Lit 2)) (Var 'x)))))
46+
(p (Let* '(x x) (list (Lit 1) (Lit 2)) (Var 'x))))
47+
(check-equal? (parse '(let ((let 1) (x 2)) let))
48+
(p (Let '(let x) (list (Lit 1) (Lit 2)) (Var 'let))))
49+
(check-equal? (parse '(let* ((let* 1) (x 2)) let*))
50+
(p (Let* '(let* x) (list (Lit 1) (Lit 2)) (Var 'let*))))
51+
(check-equal? (parse '(let* ((let* 1) (let* 2)) let*))
52+
(p (Let* '(let* let*) (list (Lit 1) (Lit 2)) (Var 'let*))))
53+
(check-exn exn:fail? (λ () (parse '(let ((let 1)) (let ((x 1)) x))))))
4754

iniquity-plus/parse.rkt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
(define (rec ss fs)
3636
(match ss
3737
[(list s) fs]
38-
[(cons (cons 'define sd) sr)
38+
[(cons (cons (? (not-in fs) 'define) sd) sr)
3939
(match (parse-defn-name sd)
4040
[f (if (memq f fs)
4141
(error "duplicate definition" f)
@@ -79,7 +79,7 @@
7979
(provide (all-defined-out))
8080
(define (parse-define/acc s fs xs ys gs)
8181
(match s
82-
[(list 'define (? symbol? f) (cons 'case-lambda sr))
82+
[(list 'define (? symbol? f) (cons (? (not-in (append fs xs)) 'case-lambda) sr))
8383
(match (parse-case-lambda/acc sr fs xs ys gs)
8484
[(list ys gs fun)
8585
(list ys gs (Defn f fun))])]
@@ -113,9 +113,13 @@
113113
[(list (cons (? symbol? x) r) s)
114114
(match (parse-define-plain-or-rest-fun/acc (list r s) fs (cons x xs) ys gs)
115115
[(list ys gs (FunPlain xs e))
116-
(list ys gs (FunPlain (cons x xs) e))]
116+
(if (memq x xs)
117+
(error "duplicate identifier" x)
118+
(list ys gs (FunPlain (cons x xs) e)))]
117119
[(list ys gs (FunRest xs r e))
118-
(list ys gs (FunRest (cons x xs) r e))])]
120+
(if (or (memq x xs) (eq? x r))
121+
(error "duplicate identifier" x)
122+
(list ys gs (FunRest (cons x xs) r e)))])]
119123
[_ (error "parse error")]))
120124

121125
;; S-Expr [Listof Id] [Listof Id] [Listof Id] [Listof Id] -> (list [Listof Id] [Listof Id] Expr)

iniquity-plus/test/parse.rkt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,38 @@
4848
(check-equal? (parse '(make-string 10 #\a))
4949
(p (Prim2 'make-string (Lit 10) (Lit #\a)))))
5050

51+
(begin ; Iniquity (in Iniquity+)
52+
(check-equal? (parse '(define (f x) x) 1)
53+
(Prog (list (Defn 'f (FunPlain '(x) (Var 'x)))) (Lit 1)))
54+
(check-equal? (parse '(define (define) 0) '(define))
55+
(Prog (list (Defn 'define (FunPlain '() (Lit 0))))
56+
(App 'define '())))
57+
(check-exn exn:fail? (λ () (parse '(define (f y y) y) 1)))
58+
(check-exn exn:fail? (λ () (parse '(define (f y) y) '(define (f x) x) 1)))
59+
(check-equal? (parse-closed '(define (f x) (g x))
60+
'(define (g x) (f x))
61+
'(f 0))
62+
(Prog (list (Defn 'f (FunPlain '(x) (App 'g (list (Var 'x)))))
63+
(Defn 'g (FunPlain '(x) (App 'f (list (Var 'x))))))
64+
(App 'f (list (Lit 0)))))
65+
(check-equal? (parse '(define (define x) x)
66+
'(define 1))
67+
(Prog (list (Defn 'define (FunPlain '(x) (Var 'x))))
68+
(App 'define (list (Lit 1)))))
69+
(check-exn exn:fail? (λ () (parse '(define (define x) x)
70+
'(define (g x) x)
71+
'(define (g 1))))))
72+
73+
(begin ; Iniquity+
74+
(check-equal? (parse '(define (f . x) x) 1)
75+
(Prog (list (Defn 'f (FunRest '() 'x (Var 'x)))) (Lit 1)))
76+
(check-exn exn:fail? (λ () (parse '(define (f x . x)) 1)))
77+
(check-exn exn:fail? (λ () (parse '(define (f x x . y)) 1)))
78+
(check-exn exn:fail? (λ () (parse '(define (f . x)) '(define (f y) y) 1)))
79+
(check-equal? (parse '(define f (case-lambda)) '(f))
80+
(Prog (list (Defn 'f (FunCase '()))) (App 'f '())))
81+
(check-exn exn:fail? (λ () (parse '(define case-lambda (case-lambda)) 1)))
82+
(check-exn exn:fail? (λ () (parse '(define f (case-lambda [(x x) x])) 1)))
83+
(check-equal? (parse '(define f (case-lambda [(x) x] [(x y) y])) 1)
84+
(Prog (list (Defn 'f (FunCase (list (FunPlain '(x) (Var 'x)) (FunPlain '(x y) (Var 'y)))))) (Lit 1))))
85+

knock-plus/parse.rkt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
(define (rec ss fs)
4242
(match ss
4343
[(list s) fs]
44-
[(cons (cons 'define sd) sr)
44+
[(cons (cons (? (not-in fs) 'define) sd) sr)
4545
(match (parse-defn-name sd)
4646
[f (if (memq f fs)
4747
(error "duplicate definition" f)
@@ -126,14 +126,14 @@
126126
(list ys gs (Let x e1 e2))])])]
127127
[_ (error "let: bad syntax" s)])]
128128
['match
129-
(match sr
130-
[(cons s sr)
131-
(match (rec s xs ys gs)
132-
[(list ys gs e)
133-
(match (parse-match-clauses/acc sr fs xs ys gs)
134-
[(list ys gs ps es)
135-
(list ys gs (Match e ps es))])])]
136-
[_ (error "match: bad syntax" s)])]
129+
(match sr
130+
[(cons s sr)
131+
(match (rec s xs ys gs)
132+
[(list ys gs e)
133+
(match (parse-match-clauses/acc sr fs xs ys gs)
134+
[(list ys gs ps es)
135+
(list ys gs (Match e ps es))])])]
136+
[_ (error "match: bad syntax" s)])]
137137
[_
138138
(match (parse-es/acc sr fs xs ys gs)
139139
[(list ys gs es)

knock-plus/test/parse.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
(check-equal? (parse "asdf") (p (Lit "asdf")))
4848
(check-equal? (parse '(make-string 10 #\a))
4949
(p (Prim2 'make-string (Lit 10) (Lit #\a)))))
50+
5051
(begin ; Iniquity
5152
(check-equal? (parse '(define (f x) x) 1)
5253
(Prog (list (Defn 'f '(x) (Var 'x))) (Lit 1)))

0 commit comments

Comments
 (0)