|
1 | 1 | #lang racket |
2 | | -(provide parse parse-define parse-e) |
| 2 | +(provide parse parse-define parse-e parse-struct) |
3 | 3 | (require "ast.rkt") |
4 | 4 |
|
5 | 5 | ;; [Listof S-Expr] -> Prog |
6 | 6 | (define (parse s) |
7 | 7 | (match s |
8 | 8 | [(cons (and (cons 'struct _) d) s) |
9 | 9 | (match (parse s) |
10 | | - [(Prog ds e) |
11 | | - (Prog (append (make-struct-defns (parse-struct d)) ds) e)])] |
| 10 | + [(Prog ds e) |
| 11 | + (Prog (append (parse-struct d) ds) e)])] |
12 | 12 | [(cons (and (cons 'define _) d) s) |
13 | 13 | (match (parse s) |
14 | 14 | [(Prog ds e) |
15 | 15 | (Prog (cons (parse-define d) ds) e)])] |
16 | 16 | [(cons e '()) (Prog '() (parse-e e))] |
17 | 17 | [_ (error "program parse error")])) |
18 | 18 |
|
19 | | -;; Struct -> [Listof Defn] |
20 | | -(define (make-struct-defns s) |
| 19 | +;; S-Expr -> [Listof Defn] |
| 20 | +(define (parse-struct s) |
21 | 21 | (match s |
22 | | - [(Struct n flds) |
23 | | - (list* (make-struct-defn-construct n flds) |
24 | | - (make-struct-defn-predicate n) |
25 | | - (make-struct-defn-accessors n (reverse flds)))])) |
| 22 | + [(list 'struct (? symbol? n) flds) |
| 23 | + (if (andmap symbol? flds) |
| 24 | + (list* (make-struct-defn-construct n flds) |
| 25 | + (make-struct-defn-predicate n) |
| 26 | + (make-struct-defn-accessors n (reverse flds))) |
| 27 | + (error "parse struct definition error"))] |
| 28 | + [_ (error "parse struct definition error")])) |
26 | 29 |
|
27 | 30 | ;; Id [Listof Id] -> [Listof Defn] |
28 | 31 | (define (make-struct-defn-construct n flds) |
|
33 | 36 | (define (make-struct-defn-predicate n) |
34 | 37 | (Defn (symbol-append n '?) (list 'x) |
35 | 38 | (Prim 'struct? (list (Quote n) (Var 'x))))) |
36 | | - |
| 39 | + |
37 | 40 | ;; Id [Listof Id] -> [Listof Defn] |
38 | 41 | (define (make-struct-defn-accessors n flds) |
39 | 42 | (match flds |
40 | 43 | ['() '()] |
41 | 44 | [(cons f flds) |
42 | 45 | (cons (Defn (symbol-append n '- f) (list 'x) |
43 | | - (Prim 'struct-ref (list (Quote n) (Quote (length flds)) (Var 'x)))) |
| 46 | + (Prim 'struct-ref |
| 47 | + (list (Quote n) |
| 48 | + (Quote (length flds)) |
| 49 | + (Var 'x)))) |
44 | 50 | (make-struct-defn-accessors n flds))])) |
45 | 51 |
|
46 | 52 | ;; Symbol ... -> Symbol |
47 | 53 | (define (symbol-append . ss) |
48 | | - (string->symbol (apply string-append (map symbol->string ss)))) |
49 | | - |
50 | | -;; S-Expr -> StructDefn |
51 | | -(define (parse-struct s) |
52 | | - (match s |
53 | | - [(list 'struct (? symbol? n) flds) |
54 | | - (if (andmap symbol? flds) |
55 | | - (Struct n flds) |
56 | | - (error "parse struct definition error"))] |
57 | | - [_ (error "parse struct definition error")])) |
| 54 | + (string->symbol |
| 55 | + (apply string-append (map symbol->string ss)))) |
58 | 56 |
|
59 | 57 | ;; S-Expr -> Defn |
60 | 58 | (define (parse-define s) |
|
76 | 74 | [(list (? (op? op1) p1) e) (Prim p1 (list (parse-e e)))] |
77 | 75 | [(list (? (op? op2) p2) e1 e2) (Prim p2 (list (parse-e e1) (parse-e e2)))] |
78 | 76 | [(list (? (op? op3) p3) e1 e2 e3) |
79 | | - (Prim p3 (list (parse-e e1) (parse-e e2) (parse-e e3)))] |
| 77 | + (Prim p3 (list (parse-e e1) (parse-e e2) (parse-e e3)))] |
80 | 78 | [(list 'begin e1 e2) |
81 | 79 | (Begin (parse-e e1) (parse-e e2))] |
82 | 80 | [(list 'if e1 e2 e3) |
|
91 | 89 | (Lam (gensym 'lambda) xs (parse-e e)) |
92 | 90 | (error "parse lambda error"))] |
93 | 91 | [(cons e es) |
94 | | - (App (parse-e e) (map parse-e es))] |
| 92 | + (App (parse-e e) (map parse-e es))] |
95 | 93 | [_ (error "Parse error" s)])) |
96 | 94 |
|
97 | 95 | (define (parse-match e ms) |
|
0 commit comments