summaryrefslogtreecommitdiff
path: root/util/constructors.scm
blob: 2fa8bd32565ec6d2000fe482e8eb0586ee2ee2a3 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
;;; This file contains ast construction functions.  These
;;; functions are supplied for commonly used ast structures to
;;; avoid the longer `make' normally required.

;;; Function names are the type names with a `**' prefix.  For reference
;;; nodes, the /def for builds the node from a definition instead of a name.

;;; Note: maybe these should be made automagicly someday.

;;; from exp-structs:

(define (**lambda args body)
  (**lambda/pat (map (function **pat) args) body))

(define (**lambda/pat pats body)
  (if (null? pats)
      body
      (make lambda (pats pats) (body body))))



;;; Make a case expression.

(define (**case exp alts)
  (make case (exp exp) (alts alts)))

(define (**alt/simple pat exp)
  (**alt pat 
	 (list (make guarded-rhs
		     (guard (make omitted-guard))
		     (rhs exp)))
	 '()))

(define (**alt pat rhs-list where-decls)
  (make alt (pat pat) (rhs-list rhs-list) (where-decls where-decls)))




(define (**let decls body)
  (if decls
      (make let (decls decls) (body body))
      body))

(define (**if test then-exp else-exp)
  (make if (test-exp test) (then-exp then-exp) (else-exp else-exp)))

(define (**app fn . args)  ; any number of args
  (**app/l fn args))

(define (**app/l fn args)  ; second args is a list
  (if (null? args)
      fn
      (**app/l (make app (fn fn) (arg (car args)))
	       (cdr args))))

(define (**var name)
  (make var-ref (name name) (var (dynamic *undefined-def*)) (infix? '#f)))

(define (**var/def def)  ; arg is an entry
  (make var-ref (var def) (name (def-name def)) (infix? '#f)))
	    
(define (**con/def def)
  (make con-ref (name (def-name def)) (con def) (infix? '#f)))

(define (**int x)
  (make integer-const (value x)))

(define (**char x)
  (make char-const (value x)))

(define (**string x)
  (make string-const (value x)))

(define (**listcomp exp quals)
  (make list-comp (exp exp) (quals quals)))

(define (**gen pat exp)
  (make qual-generator (pat (**pat pat)) (exp exp)))

(define (**omitted-guard)
  (make omitted-guard))

(define (**con-number exp algdata)
  (make con-number (type algdata) (value exp)))

(define (**sel con exp i)
  (make sel (constructor con) (value exp) (slot i)))

(define (**is-constructor exp con)
  (make is-constructor (value exp) (constructor con)))

;;; From valdef-structs

(define (**signdecl vars type)
  (make signdecl (vars (map (function **var) vars)) (signature type)))

(define (**signdecl/def vars type)
  (make signdecl (vars (map (function **var/def) vars)) (signature type)))

(define (**define name args val)
  (**valdef (**pat name) (map (function **pat) args) val))

(define (**valdef/def var exp)
  (**valdef/pat (**var-pat/def var) exp))

(define (**valdef/pat pat exp)
  (**valdef pat '() exp))

(define (**valdef lhs args rhs)
  (make valdef
	(lhs lhs)
	(definitions
	  (list (make single-fun-def
		      (args args)
		      (rhs-list
		        (list (make guarded-rhs
				    (guard (**omitted-guard))
				    (rhs rhs))))
		      (where-decls '())
		      (infix? '#f))))))


;;; Patterns (still in valdef-structs)

;;; The **pat function converts a very simple lisp-style pattern representation
;;; into corresponding ast structure.  The conversion:
;;;   a) _ => wildcard
;;;   b) a symbol => Var pattern
;;;   c) an integer / string => const pattern
;;;   d) a list of pats starting with 'tuple => Pcon
;;;   e) a list of pats starting with a con definition => Pcon

(define (**pat v)
  (cond ((eq? v '_) (**wildcard-pat))
	((symbol? v)
	 (make var-pat (var (**var v))))
	((var? v)
	 (make var-pat (var (**var/def v))))
	((integer? v)
	 (make const-pat (value (**int v))))
	((string? v)
	 (make const-pat (value (**string v))))
	((and (pair? v) (eq? (car v) 'tuple))
	 (**pcon/tuple (map (function **pat) (cdr v))))
	((and (pair? v) (con? (car v)))
	 (**pcon/def (car v) (map (function **pat) (cdr v))))
	(else
	 (error "Bad pattern in **pat: ~A~%" v))))

(define (**pcon name pats)
  (make pcon (name (add-con-prefix/symbol name))
	     (con (dynamic *undefined-def*)) (pats pats) (infix? '#f)))

(define (**pcon/def def pats)
  (make pcon (name (def-name def)) (con def) (pats pats) (infix? '#f)))

(define (**pcon/tuple pats)
  (**pcon/def (tuple-constructor (length pats)) pats))

;;; Make a variable pattern from the var

(define (**var-pat/def var)
  (make var-pat
	(var (**var/def var))))

(define (**wildcard-pat)
  (make wildcard-pat))


;;; Either make a tuple, or return the single element of a list.

(define (**tuple-pat pats)
  (cond ((null? pats)
	 (**pcon/def (core-symbol "UnitConstructor") '()))
	((null? (cdr pats))
	 (car pats))
	(else
	 (**pcon/tuple pats))))


;;; From type-structs.scm

(define (**tycon name args)
  (make tycon (name name) (args args) (def (dynamic *undefined-def*))))

(define (**tycon/def def args)
  (make tycon (name (def-name def)) (def def) (args args)))

(define (**tyvar name)
  (make tyvar (name name)))

(define (**signature context type)
  (make signature (context context) (type type)))

(define (**class/def def)
  (make class-ref (name (def-name def)) (class def)))

(define (**context tycls tyvar)
  (make context (class tycls) (tyvar tyvar)))

;;; From tc-structs

(define (**ntyvar)
  (make ntyvar (value '#f) (context '()) (dict-params '())))

(define (**ntycon tycon args)
  (make ntycon (tycon tycon) (args args)))

(define (**arrow . args) 
  (**arrow/l args))

(define (**arrow/l args)
  (if (null? (cdr args))
      (car args)
      (**ntycon (core-symbol "Arrow")
		(list (car args) (**arrow/l (cdr args))))))

(define (**arrow/l-2 args final-val)
  (if (null? args)
      final-val
      (**ntycon (core-symbol "Arrow")
		(list (car args) (**arrow/l-2 (cdr args) final-val)))))

(define (**list-of arg)
  (**ntycon (core-symbol "List") (list arg)))

(define (**recursive-placeholder var edecls)
  (make recursive-placeholder (var var) (exp '#f)
	(enclosing-decls edecls)))

(define (**dict-placeholder class tyvar edecls var)
  (make dict-placeholder
	(class class) (exp '#f) (overloaded-var var)
	(tyvar tyvar) (enclosing-decls edecls)))

(define (**method-placeholder method tyvar edecls var)
  (make method-placeholder
	(method method) (exp '#f) (overloaded-var var)
	(tyvar tyvar) (enclosing-decls edecls)))

;;; Some less primitive stuff

(define (**tuple-sel n i exp)  ;; 0 <= i < n
  (if (eqv? n 1)
      exp
      (**sel (tuple-constructor n) exp i)))

(define (**abort msg)
  (**app (**var/def (core-symbol "error"))
	 (**string msg)))

(define (**tuple/l args)
  (cond ((null? args)
	 (**con/def (core-symbol "UnitConstructor")))
	((null? (cdr args))
	 (car args))
	(else
	 (**app/l (**con/def (tuple-constructor (length args)))
		  args))))

(define (**tuple . args)
  (**tuple/l args))

(define (**tuple-type/l args)
  (cond ((null? args)
	 (**tycon/def (core-symbol "UnitConstructor") '()))
	((null? (cdr args))
	 (car args))
	(else
	 (**tycon/def (tuple-tycon (length args)) args))))

(define (**tuple-type . args)
  (**tuple-type/l args))

(define (**arrow-type . args)
  (**arrow-type/l args))

(define (**arrow-type/l args)
  (if (null? (cdr args))
      (car args)
      (**tycon/def (core-symbol "Arrow") (list (car args)
					       (**arrow-type/l (cdr args))))))

(define (**fromInteger x)
  (**app (**var/def (core-symbol "fromInteger")) x))

(define (**fromRational x)
  (**app (**var/def (core-symbol "fromRational")) x))

(define (**gtyvar n)
  (make gtyvar (varnum n)))

(define (**gtype context type)
  (make gtype (context context) (type type)))

(define (**fixity a p)
  (make fixity (associativity a) (precedence p)))

(define (**ntycon/tuple . args)
  (let ((arity  (length args)))
    (**ntycon (tuple-tycon arity) args)))

(define (**ntycon/arrow . args)
  (**ntycon/arrow-l args))

(define (**ntycon/arrow-l args)
  (let ((arg (if (integer? (car args))
		 (**gtyvar (car args))
		 (car args))))
    (if (null? (cdr args))
	arg
	(**arrow arg (**ntycon/arrow-l (cdr args))))))

(define (**save-old-exp old new)
  (make save-old-exp (old-exp old) (new-exp new)))



;;; These are used by the CFN.

(define (**case-block block-name exps)
  (make case-block
	(block-name block-name)
	(exps exps)))

(define (**return-from block-name exp)
  (make return-from
	(block-name block-name)
	(exp exp)))

(define (**and-exp . exps)
  (cond ((null? exps)
	 (**con/def (core-symbol "True")))
	((null? (cdr exps))
	 (car exps))
	(else
	 (make and-exp (exps exps)))))