summaryrefslogtreecommitdiff
path: root/ast/valdef-structs.scm
blob: eb0dc88245b7535fcd6b1750aba3bb83c5bce49c (about) (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
;;; File: ast/valdef-structs    Author: John

;;; Ast structure for local declarations

;;; <decl> -> <signdecl>
;;;        -> <valdef>

;;; decl contains value declarations and type signatures.(
;;; type related decls are topdecls and are separated from
;;; these decls.

(define-struct decl   
  (include ast-node))
                      


;;; <signdecl> -> <vars> :: [<context> =>] <type>
;;;
;;; <vars>     -> <var> , ... , <var>
;;;

(define-struct signdecl ; this affixes a signature to a list of variables
  (include decl)
  (predicate signdecl?)
  (slots
   (vars (type (list var-ref)))
   (signature (type signature))))

;;; This is introduced into decl lists by dependency analysis
(define-struct recursive-decl-group
  (include decl)
  (slots
   ;; none of these are recursive decl groups
   (decls (type (list decl)))
   ))

;;; <valdef>  -> <lhs> = <exp> [where { <decls> [;] }]
;;;           -> <lhs> <gdrhs> [where { <decls> [;] }]
;;;
;;; <lhs>     -> <apat>
;;;           -> <funlhs>
;;;
;;; <funlhs>  -> <afunlhs>
;;;           -> <pat> <varop> <pat>
;;;           -> <lpat> <varop> <pat>
;;;           -> <pat> <varop> <rpat>
;;;
;;; <afunlhs> -> <var> <apat>
;;;           -> ( <funlhs> ) <apat>    (infix operator with more than 2 args)
;;;           -> <afunlhs> <apat>       (multiple argument pattern)

(define-struct valdef  ; this defines values.
  (include decl)
  (predicate valdef?)
  (slots
   ;; this pattern contains all new variables defined.
   ;; For a function definition the pattern will always
   ;; be a simple variable.
   (lhs (type pattern))
   ;; this is a list of right hand sides.
   ;; for a pattern definition, this list is always a singleton.  For
   ;; a function definition, there is a member for every successive
   ;; alternative for the function.
   (definitions (type (list single-fun-def)))
   ;; this is used internally by dependency analysis
   (depend-val (type int) (uninitialized? #t))
   ;; this is filled in by the type phase
   (dictionary-args (type (list var)) (uninitialized? #t))
   ;; used for defaulting
   (module (type symbol) (default '|Prelude|))
   ))

(define-struct single-fun-def
  (include ast-node)
  (slots
   ;; this list is always empty for pattern definition
   ;; and always non-empty for function definition.
   ;; The length of this list is the arity of the function.
   ;; All single-fun-defs for a function have the same arity.
   (args (type (list pattern)))
   ;; <gdrhs>, this contains a list of guard , expression pairs
   (rhs-list (type (list guarded-rhs)))
   ;; this contains declarations local to the
   ;; single fun def.  It scopes over the args.  The
   ;; guarded-rhs may refer to these values.
   (where-decls (type (list decl)))
   ;; true when declared in infix style.  Used for printing
   ;; and to check precs in prec parsing.
   (infix? (type bool) (bit #t))
   ))



;;; <gdrhs>   -> <gd> = <exp> [<gdrhs>]
;;;
;;; <gd>      -> | <exp>

(define-struct guarded-rhs ; a single guarded expression.  A special expression
  (include ast-node)
  (slots
   ;; node - omitted-guard - is used when no guard given
   (guard (type exp))
   (rhs (type exp))))


;;; Some examples of the above:
;;; (a,b) | z>y = (z,y)
;;;       | otherwise = (1,2)
;;;   where z = x-2
;;;
;;;  valdef:
;;;    lhs = (a,b)
;;;    definitions =
;;;       [single-fun-def:
;;;         args = []
;;;         rhs-list = [guarded-rhs: guard = z>y
;;;                                  rhs = (z,y),
;;;                     guarded-rhs: guard = otherwise
;;;                                  rhs = (1,2)]
;;;         where-decls = [valdef: lhs = z
;;;                                definitions =
;;;                                   [single-fun-def:
;;;                                      args = []
;;;                                      rhs-list = [guarded-rhs:
;;;                                                    guard = omitted-guard
;;;                                                    exp = x-2]
;;;                                      where-decls = []]]]
;;;
;;;  fact 0 = 1
;;;  fact (n+1) = (n+1)*fact n
;;;
;;;  valdef:
;;;    lhs = fact
;;;    definitions =
;;;       [single-fun-def:
;;;         args = [0]
;;;         rhs-list = [guarded-rhs: guard = omitted-guard
;;;                                  rhs = 1]
;;;         where-decls = [],
;;;        single-fun-def:
;;;         args = [n+1]
;;;         rhs-list = [guarded-rhs: guard = omitted-guard
;;;                                  rhs = (n+1)*fact n]
;;;         where-decls = []]




;;; Definitions for patterns

;;; This is a simplification; the real syntax is complicated by
;;; rules for precedence and associativity.
;;;
;;; <pat>   -> <pat> <conop> <pat>           pcon
;;;         -> <pat> + <integer>             plus-pat
;;;         -> - <integer-or-float>          *** ???  const-pat?
;;;         -> <apat>
;;;         -> <con> <apat> .... <apat>      pcon
;;;
;;; <apat>  -> <var>                         var-pat
;;;         -> <var> @ <apat>                as-pat
;;;         -> <con>                         *** ??? var-pat?
;;;         -> <literal>                     const-pat
;;;         -> _                             wildcard-pat
;;;         -> ()                            pcon special case
;;;         -> ( <pat> )                     (grouping syntax)
;;;         -> ( <pat> , ... , <pat> )       pcon special case
;;;         -> [ <pat> , ... , <pat> ]       list-pat
;;;         -> ~ <apat>                      irr-pat

(define-struct pattern
  (include ast-node))

(define-struct apat
  (include pattern))

(define-struct as-pat  ;; var@pat
  (include apat)
  (slots
   (var (type var-ref))
   (pattern (type pattern))))

(define-struct irr-pat ;; ~pat
  (include apat)
  (slots
   (pattern (type pattern))))

(define-struct var-pat  ;; v
  (include apat)
  (predicate var-pat?)
  (slots
   (var (type var-ref))))

(define-struct wildcard-pat  ;; _
  (include apat)
  (predicate wildcard-pat?))

(define-struct const-pat  ;; literal
  (include apat)
  (predicate const-pat?)
  (slots
   (value (type const))
   ;; this is the code that actually performs the match.
   ;; it's filled in by type phase.
   (match-fn (type exp) (uninitialized? #t))))

(define-struct plus-pat  ;; p+k
  (include pattern)
  (slots
   (pattern (type pattern))
   (k (type integer))
   ;; code to check for match, filled in by type phase
   (match-fn (type exp) (uninitialized? #t))
   ;; code to bind result, filled in by type phase
   (bind-fn (type exp) (uninitialized? #t))
   ))

(define-struct pcon      ;; con pat1 pat2 ...
  (include pattern)      ;; pat1 con pat2
  (predicate pcon?)
  (slots
   (name (type symbol))
   (con (type def))
   (pats (type (list pattern)))
   (infix? (type bool) (bit #t))))

(define-struct list-pat   ;; [p1,p2,...]
  (include apat)
  (slots
   (pats (type (list pattern)))))

;;; The following structs deal with prec parsing of patterns.

(define-struct pp-pat-list
  (include pattern)
  (slots
   (pats (type (list pattern)))))

(define-struct pp-pat-plus
  (include pattern)
  (predicate pp-pat-plus?))

(define-struct pp-pat-negated
  (include pattern)
  (predicate pp-pat-negated?))



;;; Structs for annotations

(define-struct annotation
  (include decl)
  (predicate annotation?))

(define-struct annotation-decl
  (include annotation)
  (predicate annotation-decl?)
  (slots
   (names (type (list symbol)))
   (annotations (type (list annotation-value)))))

(define-struct annotation-value
  (include annotation)
  (predicate annotation-value?)
  (slots
   (name (type symbol))
   (args (type (list t)))))

;;; This is a list of annotations placed in where decls lists in the same
;;; manner a signdecls.

(define-struct annotation-decls
  (include annotation)
  (predicate annotation-decls?)
  (slots
    (annotations (type (list annotation)))))