|
6 | 6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
7 | 7 |
|
8 | 8 | ;; type Expr =
|
9 |
| -;; | Integer |
10 |
| -;; | Boolean |
11 |
| -;; | Character |
12 |
| -;; | Variable |
13 |
| -;; | Prim PrimOp Expr |
14 |
| -;; | if Expr Expr Expr |
15 |
| -;; | cond (Clause list) Expr |
16 |
| -;; | let (Binding list) Expr |
17 |
| - |
18 |
| -;; type PrimOp = |
19 |
| -;; | add1 | sub1 | zero? | abs | - | char? | integer? | boolean? |
20 |
| -;; | integer->char | char->integer |
21 |
| - |
22 |
| -;; type Clause = Expr Expr |
23 |
| -;; type Binding = Variable Expr |
24 |
| - |
25 |
| -;; type Variable = Symbol (except 'let, 'add1, etc.) |
| 9 | +;; | (Eof) |
| 10 | +;; | (Int Integer) |
| 11 | +;; | (Bool Boolean) |
| 12 | +;; | (Char Character) |
| 13 | +;; | (Prim0 Op0) |
| 14 | +;; | (Prim1 Op1 Expr) |
| 15 | +;; | (Prim2 Op2 Expr Expr) |
| 16 | +;; | (PrimVar OpVar (ListOf Expr)) |
| 17 | +;; | (If Expr Expr Expr) |
| 18 | +;; | (Begin Expr Expr) |
| 19 | +;; | (Let (ListOf Binding) Expr) |
| 20 | +;; | (Let* (ListOf Binding) Expr) |
| 21 | +;; | (Var Id) |
| 22 | +;; | (Cond Clauses (Expr)) |
| 23 | + |
| 24 | +;; type Clauses = Listof (Expr, Expr) |
| 25 | +;; type Id = Symbol |
| 26 | +;; type Op0 = 'read-byte |
| 27 | +;; type Op1 = 'add1 | 'sub1 | 'zero? | abs | - | not |
| 28 | +;; | 'char? | integer? | boolean? |
| 29 | +;; | 'integer->char | 'char->integer |
| 30 | +;; | 'write-byte | 'eof-object? |
| 31 | +;; type Op2 = '+ | '- |
| 32 | +;; type OpVar = '+ |
| 33 | +;; type Binding = (Id, Expr) |
26 | 34 |
|
27 | 35 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
28 | 36 | ;;;;;; The AST data structure
|
29 | 37 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
30 | 38 |
|
31 | 39 | ;; The AST can be viewed as having 'kinds' of nodes.
|
32 | 40 | ;;
|
33 |
| -;; * The nodes that represnt an expression themselves |
| 41 | +;; * The nodes that represent an expression themselves |
34 | 42 | ;;
|
35 | 43 | ;; * The nodes that are part of an expression, but no an expression themselves
|
36 | 44 |
|
37 | 45 | ;; The below are the former:
|
38 | 46 |
|
39 |
| -(struct int-e (i) #:transparent) |
40 |
| -(struct bool-e (b) #:transparent) |
41 |
| -(struct char-e (c) #:transparent) |
42 |
| -(struct var-e (v) #:transparent) |
43 |
| -(struct prim-e (p e) #:transparent) |
44 |
| -(struct if-e (e t f) #:transparent) |
45 |
| -(struct cond-e (cs el) #:transparent) |
46 |
| -(struct let-e (bs b) #:transparent) |
| 47 | +(struct Eof () #:prefab) |
| 48 | +(struct Int (i) #:prefab) |
| 49 | +(struct Bool (b) #:prefab) |
| 50 | +(struct Char (c) #:prefab) |
| 51 | +(struct Prim0 (p) #:prefab) |
| 52 | +(struct Prim1 (p e) #:prefab) |
| 53 | +(struct Prim2 (p e1 e2) #:prefab) |
| 54 | +(struct PrimVar (p es) #:prefab) |
| 55 | +(struct If (e1 e2 e3) #:prefab) |
| 56 | +(struct Begin (e1 e2) #:prefab) |
| 57 | +(struct Let (bs e) #:prefab) |
| 58 | +(struct Let* (bs e) #:prefab) |
| 59 | +(struct Var (x) #:prefab) |
| 60 | +(struct Cond (cs e) #:prefab) |
47 | 61 |
|
48 | 62 | ;; The next two are the latter:
|
49 | 63 |
|
50 |
| -;; a clause now takes an _arbitrary_ expression and a body. |
51 |
| -;; This is different than assignment 3! If you want to understand |
52 |
| -;; why, look at the lecture notes for Dupe. |
53 |
| -(struct clause (e body) #:transparent) |
| 64 | +;; A clause takes an _arbitrary_ expression and a body. |
| 65 | +(struct Clause (e body) #:prefab) |
54 | 66 |
|
55 | 67 | ;; A binding holds a symbol representing the bound variable and
|
56 | 68 | ;; Expr that represents the value that will be bound to that variable
|
57 |
| -(struct binding (v e) #:transparent) |
| 69 | +(struct Binding (v e) #:prefab) |
| 70 | + |
| 71 | +;; Finally, here's a struct to return when an expression is ill-formed. |
| 72 | +(struct IllFormedError () #:prefab) |
58 | 73 |
|
59 | 74 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
60 | 75 | ;;;;;; AST utility functions (getters)
|
|
66 | 81 | (define (get-vars bs)
|
67 | 82 | (match bs
|
68 | 83 | ['() '()]
|
69 |
| - [(cons (binding v _) bs) (cons v (get-vars bs))])) |
| 84 | + [(cons (Binding v _) bs) (cons v (get-vars bs))])) |
70 | 85 |
|
71 | 86 | ;; Get all of the _definitions_ from a list of bindings
|
72 | 87 | ;; [Binding] -> [Expr]
|
73 | 88 | (define (get-defs bs)
|
74 | 89 | (match bs
|
75 | 90 | ['() '()]
|
76 |
| - [(cons (binding _ def) bs) (cons def (get-defs bs))])) |
| 91 | + [(cons (Binding _ def) bs) (cons def (get-defs bs))])) |
77 | 92 |
|
78 | 93 |
|
79 | 94 | ;; Get all of the predicate expressions from a list of clauses
|
80 |
| -;; [Binding] -> [Expr] |
| 95 | +;; [Clause] -> [Expr] |
81 | 96 | (define (get-preds cs)
|
82 | 97 | (match cs
|
83 | 98 | ['() '()]
|
84 |
| - [(cons (clause p _) cs) (cons p (get-preds cs))])) |
| 99 | + [(cons (Clause p _) cs) (cons p (get-preds cs))])) |
85 | 100 |
|
86 | 101 | ;; Get all of the bodies expressions from a list of clauses
|
87 |
| -;; [Binding] -> [Expr] |
| 102 | +;; [Clause] -> [Expr] |
88 | 103 | (define (get-bods cs)
|
89 | 104 | (match cs
|
90 | 105 | ['() '()]
|
91 |
| - [(cons (clause _ b) cs) (cons b (get-preds cs))])) |
92 |
| - |
93 |
| -;; Given an AST, construct an sexpr that has the same shape |
94 |
| -(define (ast->sexpr a) |
95 |
| - (match a |
96 |
| - [(int-e i) `(int-e ,i)] |
97 |
| - [(bool-e b) `(bool-e ,b)] |
98 |
| - [(char-e c) `(char-e ,c)] |
99 |
| - [(var-e v) `(var-e ,v)] |
100 |
| - [(prim-e p e) `(prim-e ,p ,e)] |
101 |
| - [(if-e e t f) `(if-e ,(ast->sexpr e) |
102 |
| - ,(ast->sexpr t) |
103 |
| - ,(ast->sexpr f))] |
104 |
| - [(cond-e cs f) `(cond-e ,(clauses->sexpr cs) ,(ast->sexpr f))] |
105 |
| - [(let-e bs b) `(let-e ,(binding->sexpr bs) ,(ast->sexpr b))])) |
106 |
| - |
107 |
| -(define (binding->sexpr bnds) |
108 |
| - (match bnds |
109 |
| - ['() '()] |
110 |
| - [(cons (binding v e) bnds) `((,v ,(ast->sexpr e)) ,@(binding->sexpr bnds))])) |
| 106 | + [(cons (Clause _ b) cs) (cons b (get-preds cs))])) |
111 | 107 |
|
112 |
| -(define (clauses->sexpr cs) |
113 |
| - (match cs |
114 |
| - ['() '()] |
115 |
| - [(cons (clause e b) cs) `((,(ast->sexpr e) ,(ast->sexpr b)) ,@(clauses->sexpr cs))])) |
0 commit comments