blob: f2d06f79c1d60f4bbafba72953f6d607bab2dbd2 (
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
;; file warmelt-normatch.bysl -*- Lisp -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(comment "
Copyright 2008 Free Software Foundation, Inc.
Contributed by Basile Starynkevitch <basile@starynkevitch.net>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>.
")
;; the copyright notice above apply both to warmelt-normatch.bysl and
;; to the generated file warmelt-normatch*.c
;; This file is the fourth part of a bootstrapping compiler for the
;; basilys/MELT lisp dialect, compiler which should be able to
;; compile itself (into generated C file[s])
;;; class for the context of normalization of patterns
(defclass class_patterncontext
:super class_root
:fields (pctn_normctxt ;the class_normcontext
pctn_src ;the source expressioon
pctn_env ;the environment
;; mapping symbols to patternvars
pctn_mapatvar ;objmap of patternvars
pctn_mapatcst ;objmap of patternconst
pctn_bindlist ;binding list
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; selector to scan a pattern
;;; invoked with :
;;; * the source pattern as reciever
;;; * a parent source location
;;; * actype
;;; * a normpatcontext
(defselector scan_pattern class_selector
)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;;; scanning nil
(defun scanpat_nil (recv psloc ctyp pcn)
(debug_msg recv "scanpat_nil recv")
(assert_msg "check nil recv" (null recv))
(assert_msg "check pcn" (is_a pcn class_patterncontext))
)
(install_method discr_nullrecv scan_pattern scanpat_nil)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;;; catch-all for scanning any reciever (by failing)
(defun scanpat_anyrecv (recv psloc ctyp pcn)
(debug_msg recv "scanpat_anypattern recv")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(let ( (myclass (discrim recv))
(myclassname (get_field :named_name myclass)) )
(error_strv psloc
"unimplemented scan_pattern for any " myclassname)
(assert_msg "catchall scan_pattern unimplemented" ())
))
(install_method discr_anyrecv scan_pattern scanpat_anyrecv)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;;; catch-all for scanning any pattern (by failing)
(defun scanpat_anypattern (recv psloc ctyp pcn)
(debug_msg recv "scanpat_anypattern recv")
(assert_msg "check pcn" (is_a recv class_srcpattern_any))
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(let ( (myclass (discrim recv))
(myclassname (get_field :named_name myclass)) )
(error_strv psloc
"unimplemented scan_pattern for pattern " myclassname)
(assert_msg "catchall scan_pattern unimplemented" ())
))
(install_method class_srcpattern_any scan_pattern scanpat_anypattern)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;; scanning or pattern
(defun scanpat_srcpator (recv psloc ctyp pcn)
(debug_msg recv "scanpat_srcpator recv")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(assert_msg "check recv" (is_a recv class_srcpattern_or))
(let ( (sloc (unsafe_get_field :src_loc recv))
(sdisj (unsafe_get_field :orpat_disj recv))
)
(foreach_in_multiple
(sdisj)
(dis :long ix)
(scan_pattern dis sloc ctyp pcn)
)
(debug_msg recv "scanpat_srcpator end recv")
))
(install_method class_srcpattern_or scan_pattern scanpat_srcpator)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;; scanning and pattern
(defun scanpat_srcpatand (recv psloc ctyp pcn)
(debug_msg recv "scanpat_srcpatand recv")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(assert_msg "check recv" (is_a recv class_srcpattern_and))
(let ( (sloc (unsafe_get_field :src_loc recv))
(sconj (unsafe_get_field :andpat_conj recv))
)
(foreach_in_multiple
(sconj)
(dis :long ix)
(scan_pattern dis sloc ctyp pcn)
)
(debug_msg recv "scanpat_srcpatand end recv")
))
(install_method class_srcpattern_and scan_pattern scanpat_srcpatand)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;; scanning pattern variable
(defun scanpat_srcpatvar (recv psloc ctyp pcn)
(debug_msg recv "scanpat_srcpatvar recv")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(assert_msg "check recv" (is_a recv class_srcpattern_variable))
(let ( (sloc (unsafe_get_field :src_loc recv))
(svar (unsafe_get_field :spat_var recv))
(mapvar (unsafe_get_field :pctn_mapatvar pcn))
(mvar (mapobject_get mapvar svar))
)
(cond ( (null mvar)
(mapobject_put mapvar svar recv)
(debug_msg svar "scanpat_srcpatvar return new svar")
(return)
)
(:else
(debug_msg mvar "scanpat_srcpatvar return found mvar")
(return)
))
))
(install_method class_srcpattern_variable scan_pattern scanpat_srcpatvar)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;; scanning pattern joker
(defun scanpat_srcpatjoker (recv psloc ctyp pcn)
(debug_msg recv "scanpat_srcpatjoker recv")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(assert_msg "check recv" (is_a recv class_srcpattern_jokervar))
;; just return
(return)
)
(install_method class_srcpattern_jokervar scan_pattern scanpat_srcpatjoker)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;; scanning a pattern constant
(defun scanpat_srcpatconst (recv psloc ctyp pcn)
(debug_msg recv "scanpat_srcpatconst recv")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(assert_msg "check recv" (is_a recv class_srcpattern_constant))
(let ( (sconst (unsafe_get_field :spat_constx recv))
(sloc (unsafe_get_field :src_loc recv))
(ncx (unsafe_get_field :pctn_normctxt pcn))
(mapcst (unsafe_get_field :pctn_mapatcst pcn))
(mcst (if (is_object sconst) (mapobject_get mapcst sconst)))
(bindlist (unsafe_get_field :pctn_bindlist pcn))
(env (unsafe_get_field :pctn_env pcn))
(rectyp (get_ctype recv env))
)
(assert_msg "check rectyp" (is_a rectyp class_ctype))
(cond ((!= rectyp ctyp)
(error_strv sloc "invalid ctype in constant pattern - expecting"
(unsafe_get_field :named_name rectyp))
(warning_strv sloc "got ctype"
(unsafe_get_field :named_name ctyp))
))
(assert_msg "check bindlist" (is_list bindlist))
(cond
( (not (is_object sconst))
(debug_msg sconst "scanpat_srcpatconst nonobj literal const")
(return)
)
( (null mcst)
(multicall
(nconst nbindconst)
(normal_exp sconst env ncx sloc)
(debug_msg nconst "scanpat_srcpatconst nconst")
(mapobject_put mapcst sconst nconst)
(if nbindconst (list_append2list bindlist nbindconst))
(return)
))
(:else
(debug_msg mcst "scanpat_srcpatconst got mcst")
(return)
))
))
(install_method class_srcpattern_constant scan_pattern scanpat_srcpatconst)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;; scanning a pattern object or instance
(defun scanpat_srcpatobject (recv psloc ctyp pcn)
(debug_msg recv "scanpat_srcpatobject recv")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(assert_msg "check recv" (is_a recv class_srcpattern_object))
(let ( (sloc (unsafe_get_field :src_loc recv))
(scla (unsafe_get_field :spat_class recv))
(sfields (unsafe_get_field :spat_fields recv))
)
(assert_msg "check scla" (is_a scla class_class))
(assert_msg "check sfields" (is_multiple sfields))
(foreach_in_multiple
(sfields)
(fldpa :long ix)
(assert_msg "check fldp" (is_a fldpa class_srcfieldpattern))
(let ( (fld (let ( ( f (get_field :spaf_field fldpa)) )
(assert_msg "check fld" (is_a f class_field))
f))
(flcla (get_field :fld_ownclass fld))
(fpat (unsafe_get_field :spaf_pattern fldpa))
)
(cond ( (not (subclass_or_eq scla flcla))
(error_strv sloc "bad field in object pattern"
(unsafe_get_field :named_name fld))
(inform_strv sloc "class in pattern is" (get_field :named_name scla))
(inform_strv sloc "class of field is" (get_field :named_name flcla))
(return)))
(scan_pattern fpat sloc ctype_value pcn)
)
)
(debug_msg recv "scanpat_srcpatobject end recv")
)
)
(install_method class_srcpattern_object scan_pattern scanpat_srcpatobject)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;; scanning a composite pattern
(defun scanpat_srcpatcomposite (recv psloc ctyp pcn)
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(assert_msg "check recv" (is_a recv class_srcpattern_composite))
(debug_msg recv "scanpat_srcpatcomposite recv")
(let ( (sloc (unsafe_get_field :src_loc recv))
(soper (let ((sop (unsafe_get_field :spac_operator recv)))
(assert_msg "check soper" (is_a sop class_any_matcher))
sop))
(sins (unsafe_get_field :spac_inargs recv))
(souts (unsafe_get_field :spac_outargs recv))
(matmb (let ( (mb (unsafe_get_field :amatch_matchbind soper)) )
(debug_msg mb "scanpat_srcpatcomposite matmb")
(assert_msg "check matmb" (is_a mb class_formal_binding))
mb))
(matin (unsafe_get_field :amatch_in soper))
(matout (unsafe_get_field :amatch_out soper))
(opnam (unsafe_get_field :named_name soper))
(matctyp (unsafe_get_field :fbind_type matmb))
(env (unsafe_get_field :pctn_env pcn))
(ncx (unsafe_get_field :pctn_normctxt pcn))
(bindlist (unsafe_get_field :pctn_bindlist pcn))
)
(assert_msg "check ctyp" (is_a ctyp class_ctype))
(assert_msg "check matctyp" (is_a matctyp class_ctype))
(cond ((!= ctyp matctyp)
(error_strv sloc "invalid ctype in composite partner - expecting"
(unsafe_get_field :named_name matctyp))
(warning_strv sloc "got ctype"
(unsafe_get_field :named_name ctyp))
(return)))
(multicall
(nins bindins)
(normalize_tuple sins env ncx sloc)
(debug_msg nins "scanpat_srcpatcomposite nins")
(if bindins (list_append2list bindlist bindins))
(debug_msg souts "scanpat_srcpatcomposite souts")
(foreach_in_multiple
(souts)
(pout :long ix)
(if (null pout)
(progn
(debug_msg discr_nullrecv "scanpat_srcpatcomposite null pout discr_nullrecv")
(debug_msg discr_anyrecv "scanpat_srcpatcomposite null pout discr_anyrecv")
))
(debug_msg pout "scanpat_srcpatcomposite pout")
(scan_pattern pout sloc ctyp pcn)
)
(debug_msg recv "scanpat_srcpatcomposite end recv")
)))
(install_method class_srcpattern_composite scan_pattern scanpat_srcpatcomposite)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; backpoints are "backtracking" points in matching nreps
(defclass class_nrep_backpoint
:super class_nrep_typexpr
:fields (nbkp_do
nbkp_fail))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; any testpoints
(defclass class_nrep_testpoint
:super class_nrep_typexpr
:fields (ntsp_failbp ;backpoint for failure
ntsp_succp ;success point
ntsp_test ;normal tested expression
))
;;; testpoint with matcher
(defclass class_nrep_testp_matcher
:super class_nrep_testpoint
:fields (ntsm_matcher ;the matcher
ntsm_binds ;the local bindings
))
;; testpoint for is_a
(defclass class_nrep_testp_isa
:super class_nrep_testpoint
:fields (ntsi_class ;tested is a class
))
;; testpoint for exact discrim
(defclass class_nrep_testp_discrim
:super class_nrep_testp_isa
:fields (
))
;; conditional testpoint
(defclass class_nrep_testp_cond
:super class_nrep_testpoint
:fields (ntsc_cond ;the condition
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; selector to normalize a pattern
;;; reciever is the pattern to normalize
;;; arguments are
;;; the list of src patterns
;;; the list of test points
;;; the pattern context
(defselector normal_pattern class_selector)
;;; expansion of tuples in cmatcher should be done in warmelt-genobj
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;;; catch-all for normpat any reciever (by failing)
(defun normpat_anyrecv (recv spatlist testptlist pcn)
(debug_msg recv "normpat_anyrecv recv")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(let ( (myclass (discrim recv))
(myclassname (get_field :named_name myclass))
(psloc (get_field :src_loc (get_field :pctn_src pcn)))
)
(error_strv psloc
"unimplemented normal_pattern for any " myclassname)
(assert_msg "catchall normal_pattern unimplemented" ())
))
(install_method discr_anyrecv normal_pattern normpat_anyrecv)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;;; catch-all for normpat any pattern (by failing)
(defun normpat_anypat (recv spatlist testptlist pcn)
(debug_msg recv "normpat_anyrecv recv")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(assert_msg "check recv" (is_a recv class_srcpattern_any))
(let ( (myclass (discrim recv))
(myclassname (get_field :named_name myclass))
(psloc (get_field :src_loc (get_field :pctn_src pcn)))
)
(error_strv psloc
"unimplemented normal_pattern for pattern of " myclassname)
(assert_msg "catchall normal_pattern unimplemented on pattern" ())
))
(install_method class_srcpattern_any normal_pattern normpat_anypat)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;;; normalize cmatcher pattern
(defun normpat_cmatchpat (recv spatlist testptlist pcn)
(debug_msg recv "normpat_cmatchpat recv")
(debug_msg spatlist "normpat_cmatchpat spatlist")
(debug_msg testptlist "normpat_cmatchpat testptlist")
(debug_msg pcn "normpat_cmatchpat pcn")
(assert_msg "check pcn" (is_a pcn class_patterncontext))
(assert_msg "check recv" (is_a recv class_srcpattern_cmatch))
(let (
(sloc (get_field :src_loc recv))
(cmat (get_field :spac_operator recv))
(inargs (get_field :spac_inargs recv))
(outargs (get_field :spac_outargs recv))
(env (unsafe_get_field :pctn_env pcn))
(ncx (unsafe_get_field :pctn_normctxt pcn))
(bindlist (unsafe_get_field :pctn_bindlist pcn))
)
(assert_msg "check cmat" (is_a cmat class_cmatcher))
(debug_msg inargs "normpat_cmatchpat inargs")
(debug_msg cmat "normpat_cmatchpat cmat")
(multicall
(nins bindins)
(normalize_tuple inargs env ncx sloc)
(debug_msg nins "normpat_cmatchpat nins")
(debug_msg bindins "normpat_cmatchpat bindins")
(if bindins (list_append2list bindlist bindins))
(let ( (curtestp (instance class_nrep_testp_matcher
:nrep_loc sloc
:nexpr_ctyp (get_field :amatch_matchbind cmat)
:ntsp_succp ()
:ntsp_test ()
:ntsm_matcher cmat
:ntsm_binds ()
))
)
(debug_msg curtestp "normpat_cmatchpat curtestp")
)
(assert_msg "normpat_cmatchpat @@NOT IMPLEMENTED@@" ())
(compile_warning "normpat_cmatchpat INCOMPLETE" ())
)
))
(install_method class_srcpattern_cmatch normal_pattern normpat_cmatchpat)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; normalize a match
(defun normexp_match (recv env ncx psloc)
(assert_msg "check match recv" (is_a recv class_src_match))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normcontext))
(debug_msg recv "normexp_match recv")
(let ( (sloc (unsafe_get_field :src_loc recv))
(smatsx (unsafe_get_field :smat_matchedx recv))
(scases (unsafe_get_field :smat_cases recv))
(:long nbcases (multiple_length scases))
(tupvarmap (make_multiple discr_multiple nbcases))
(tupcstmap (make_multiple discr_multiple nbcases))
(tupbindlist (make_multiple discr_multiple nbcases))
(spatlist (make_list discr_list))
(testptlist (make_list discr_list))
)
(debug_msg smatsx "normexp_match smatsx")
(multicall
(nmatx nbindmatx)
(normal_exp smatsx env ncx sloc)
(debug_msg nmatx "normexp_match nmatx")
(debug_msg scases "normexp_match scases")
(let ( (ctyp (get_ctype nmatx env)) )
(debug_msg ctyp "normexp_match ctyp")
(foreach_in_multiple
(scases)
(curcas :long ix)
(debug_msg curcas "normexp_match curcas")
(assert_msg "check curcas" (is_a curcas class_src_casematch))
(let (
(curloc (unsafe_get_field :src_loc curcas))
(curpat (unsafe_get_field :scam_patt curcas))
(curbody (unsafe_get_field :scam_body curcas))
(mapvar (make_mapobject discr_mapobjects 13))
(mapcst (make_mapobject discr_mapobjects 11))
(bindlist (make_list discr_list))
(pcn (instance class_patterncontext
:pctn_normctxt ncx
:pctn_src recv
:pctn_env env
:pctn_mapatvar mapvar
:pctn_mapatcst mapcst
:pctn_bindlist bindlist
))
)
(scan_pattern curpat curloc ctyp pcn)
(debug_msg mapvar "normexp_match mapvar")
(debug_msg mapcst "normexp_match mapcst")
(debug_msg bindlist "normexp_match bindlist")
(multiple_put_nth tupvarmap ix mapvar)
(multiple_put_nth tupcstmap ix mapcst)
(multiple_put_nth tupbindlist ix bindlist)
(debug_msg curpat "normexp_match curpat before normal_pattern")
(normal_pattern curpat spatlist testptlist pcn)
(debug_msg curcas "normexp_match curcas after normal_pattern")
)
) ;end foreach_in_multiple
(debug_msg tupvarmap "normexp_match tupvarmap")
(debug_msg tupcstmap "normexp_match tupcstmap")
(debug_msg tupbindlist "normexp_match tupbindlist")
(assert_msg "unimplemented normexp_match" ())
(compile_warning "@@unimplemented normexp_match" ())
))))
(install_method class_src_match normal_exp normexp_match)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; export our classes
(export_class
class_patterncontext
)
;;; export our values
(export_values
scan_pattern
normal_pattern
)
;; eof warmelt-normatch.bysl
|