Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion fraud-plus/test/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,12 @@
(check-equal? (parse-closed '(let ((x 1)) (let ((x 2)) x)))
(p (Let '(x) (list (Lit 1)) (Let '(x) (list (Lit 2)) (Var 'x)))))
(check-equal? (parse-closed '(let* ((x 1) (x 2)) x))
(p (Let* '(x x) (list (Lit 1) (Lit 2)) (Var 'x)))))
(p (Let* '(x x) (list (Lit 1) (Lit 2)) (Var 'x))))
(check-equal? (parse '(let ((let 1) (x 2)) let))
(p (Let '(let x) (list (Lit 1) (Lit 2)) (Var 'let))))
(check-equal? (parse '(let* ((let* 1) (x 2)) let*))
(p (Let* '(let* x) (list (Lit 1) (Lit 2)) (Var 'let*))))
(check-equal? (parse '(let* ((let* 1) (let* 2)) let*))
(p (Let* '(let* let*) (list (Lit 1) (Lit 2)) (Var 'let*))))
(check-exn exn:fail? (λ () (parse '(let ((let 1)) (let ((x 1)) x))))))

5 changes: 1 addition & 4 deletions iniquity-plus/ast.rkt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#lang racket
(provide Lit Prim0 Prim1 Prim2 Prim3 If
Eof Begin
Let Var Prog Defn App
Apply FunPlain FunRest FunCase)
Let Var Prog Defn App FunPlain FunRest FunCase)

;; type Prog = (Prog (Listof Defn) Expr)
(struct Prog (ds e) #:prefab)
Expand All @@ -29,7 +28,6 @@
;; | (Let Id Expr Expr)
;; | (Var Id)
;; | (App Id (Listof Expr))
;; | (Apply Id (Listof Expr) Expr)

;; type ClosedExpr = { e ∈ Expr | e contains no free variables }

Expand Down Expand Up @@ -66,5 +64,4 @@
(struct Let (x e1 e2) #:prefab)
(struct Var (x) #:prefab)
(struct App (f es) #:prefab)
(struct Apply (f es e) #:prefab)

12 changes: 8 additions & 4 deletions iniquity-plus/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
(define (rec ss fs)
(match ss
[(list s) fs]
[(cons (cons 'define sd) sr)
[(cons (cons (? (not-in fs) 'define) sd) sr)
(match (parse-defn-name sd)
[f (if (memq f fs)
(error "duplicate definition" f)
Expand Down Expand Up @@ -79,7 +79,7 @@
(provide (all-defined-out))
(define (parse-define/acc s fs xs ys gs)
(match s
[(list 'define (? symbol? f) (cons 'case-lambda sr))
[(list 'define (? symbol? f) (cons (? (not-in (append fs xs)) 'case-lambda) sr))
(match (parse-case-lambda/acc sr fs xs ys gs)
[(list ys gs fun)
(list ys gs (Defn f fun))])]
Expand Down Expand Up @@ -113,9 +113,13 @@
[(list (cons (? symbol? x) r) s)
(match (parse-define-plain-or-rest-fun/acc (list r s) fs (cons x xs) ys gs)
[(list ys gs (FunPlain xs e))
(list ys gs (FunPlain (cons x xs) e))]
(if (memq x xs)
(error "duplicate identifier" x)
(list ys gs (FunPlain (cons x xs) e)))]
[(list ys gs (FunRest xs r e))
(list ys gs (FunRest (cons x xs) r e))])]
(if (or (memq x xs) (eq? x r))
(error "duplicate identifier" x)
(list ys gs (FunRest (cons x xs) r e)))])]
[_ (error "parse error")]))

;; S-Expr [Listof Id] [Listof Id] [Listof Id] [Listof Id] -> (list [Listof Id] [Listof Id] Expr)
Expand Down
35 changes: 35 additions & 0 deletions iniquity-plus/test/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,38 @@
(check-equal? (parse '(make-string 10 #\a))
(p (Prim2 'make-string (Lit 10) (Lit #\a)))))

(begin ; Iniquity (in Iniquity+)
(check-equal? (parse '(define (f x) x) 1)
(Prog (list (Defn 'f (FunPlain '(x) (Var 'x)))) (Lit 1)))
(check-equal? (parse '(define (define) 0) '(define))
(Prog (list (Defn 'define (FunPlain '() (Lit 0))))
(App 'define '())))
(check-exn exn:fail? (λ () (parse '(define (f y y) y) 1)))
(check-exn exn:fail? (λ () (parse '(define (f y) y) '(define (f x) x) 1)))
(check-equal? (parse-closed '(define (f x) (g x))
'(define (g x) (f x))
'(f 0))
(Prog (list (Defn 'f (FunPlain '(x) (App 'g (list (Var 'x)))))
(Defn 'g (FunPlain '(x) (App 'f (list (Var 'x))))))
(App 'f (list (Lit 0)))))
(check-equal? (parse '(define (define x) x)
'(define 1))
(Prog (list (Defn 'define (FunPlain '(x) (Var 'x))))
(App 'define (list (Lit 1)))))
(check-exn exn:fail? (λ () (parse '(define (define x) x)
'(define (g x) x)
'(define (g 1))))))

(begin ; Iniquity+
(check-equal? (parse '(define (f . x) x) 1)
(Prog (list (Defn 'f (FunRest '() 'x (Var 'x)))) (Lit 1)))
(check-exn exn:fail? (λ () (parse '(define (f x . x)) 1)))
(check-exn exn:fail? (λ () (parse '(define (f x x . y)) 1)))
(check-exn exn:fail? (λ () (parse '(define (f . x)) '(define (f y) y) 1)))
(check-equal? (parse '(define f (case-lambda)) '(f))
(Prog (list (Defn 'f (FunCase '()))) (App 'f '())))
(check-exn exn:fail? (λ () (parse '(define case-lambda (case-lambda)) 1)))
(check-exn exn:fail? (λ () (parse '(define f (case-lambda [(x x) x])) 1)))
(check-equal? (parse '(define f (case-lambda [(x) x] [(x y) y])) 1)
(Prog (list (Defn 'f (FunCase (list (FunPlain '(x) (Var 'x)) (FunPlain '(x y) (Var 'y)))))) (Lit 1))))

18 changes: 9 additions & 9 deletions knock-plus/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
(define (rec ss fs)
(match ss
[(list s) fs]
[(cons (cons 'define sd) sr)
[(cons (cons (? (not-in fs) 'define) sd) sr)
(match (parse-defn-name sd)
[f (if (memq f fs)
(error "duplicate definition" f)
Expand Down Expand Up @@ -126,14 +126,14 @@
(list ys gs (Let x e1 e2))])])]
[_ (error "let: bad syntax" s)])]
['match
(match sr
[(cons s sr)
(match (rec s xs ys gs)
[(list ys gs e)
(match (parse-match-clauses/acc sr fs xs ys gs)
[(list ys gs ps es)
(list ys gs (Match e ps es))])])]
[_ (error "match: bad syntax" s)])]
(match sr
[(cons s sr)
(match (rec s xs ys gs)
[(list ys gs e)
(match (parse-match-clauses/acc sr fs xs ys gs)
[(list ys gs ps es)
(list ys gs (Match e ps es))])])]
[_ (error "match: bad syntax" s)])]
[_
(match (parse-es/acc sr fs xs ys gs)
[(list ys gs es)
Expand Down
1 change: 1 addition & 0 deletions knock-plus/test/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
(check-equal? (parse "asdf") (p (Lit "asdf")))
(check-equal? (parse '(make-string 10 #\a))
(p (Prim2 'make-string (Lit 10) (Lit #\a)))))

(begin ; Iniquity
(check-equal? (parse '(define (f x) x) 1)
(Prog (list (Defn 'f '(x) (Var 'x))) (Lit 1)))
Expand Down