-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlinj.lisp
6931 lines (5939 loc) · 310 KB
/
linj.lisp
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; -*- Mode: Common-Lisp; Syntax: Common-Lisp; Package: LINJ; Base: 10 -*-
;;; Copyright (C) Antonio Menezes Leitao Created on Fri Oct 13 09:30:05 2000
;;; Copyright (C) eValuator, Lda
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package "LINJ")
;;This file requires the Linj readtable
(eval-when (:compile-toplevel :load-toplevel)
(setq *readtable* *linj-readtable*))
;;14.8 Expression Statements
;; Certain kinds of expressions may be used as statements by following them with semicolons:
;; ExpressionStatement:
;; StatementExpression ;
;; StatementExpression:
;; Assignment
;; PreIncrementExpression
;; PreDecrementExpression
;; PostIncrementExpression
;; PostDecrementExpression
;; MethodInvocation
;; ClassInstanceCreationExpression
;;Let's define the statement-expression category as a subcategory of expression:
(def-category statement-expression (expression)
())
;;Now, the expression-statement as a subcategory of a statement:
(def-syntax expression-statement (statement)
;; ?expression/statement-expression)
;;We don't use the above definition because we want to infer return
;;statements. This forces us to accept any expression as
;;expression-statement and only latter we might check whether all
;;expression-statements-expressions are, in fact, statement-expressions.
?expression/expression)
(defmethod get-type ((e expression-statement))
(get-type (expression-statement-expression e)))
(def-unparse expression-statement (e)
(format t "~:/ppexp/;" (expression-statement-expression e)))
;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Assignments and such:
(def-syntax setf-expression (statement-expression)
(setf ?l-value/expression ?r-value/expression))
;;The type of an assignment is the type of the r-value in Common Lisp
;;and the type of the l-value in Java. In the past, I decided to
;;treat it as void but now I prefer to approach the Common Lisp way.
;;Note that this requires the type inferencer to treat void as an
;;absorvent element in type-merges.
(defmethod get-type ((e setf-expression))
(get-type (setf-expression-l-value e)))
(defparameter *check-assignments-to-closure-variables* t)
(defun check-not-final-variable (var)
(when (and *check-assignments-to-closure-variables* (reference-p var))
(let ((decl (find-declaration var)))
(when (and (variable-declaration-p decl)
(get-option :final decl))
(linj-error "Can't assign a value to the variable ~A that is accessed in a closure" var)))))
(defmethod visit-current ((e setf-expression) (visitor cast-and-wrap))
(check-not-final-variable (setf-expression-l-value e))
(setf (setf-expression-r-value e)
(convert-type-to-lvalue (setf-expression-l-value e) (setf-expression-r-value e))))
;; 5.2 Assignment Conversion
;; Assignment conversion occurs when the value of an expression is
;; assigned (§15.26) to a variable: the type of the expression must be
;; converted to the type of the variable. Assignment contexts allow
;; the use of an identity conversion (§5.1.1), a widening primitive
;; conversion (§5.1.2), or a widening reference conversion
;; (§5.1.4). In addition, a narrowing primitive conversion may be used
;; if all of the following conditions are satisfied:
;; * The expression is a constant expression of type byte, short,
;; char or int.
;; * The type of the variable is byte, short, or char.
;; * The value of the expression (which is known at compile time,
;; because it is a constant expression) is representable in the
;; type of the variable.
(defun assignment-convertion (value type error-fn)
(let ((val-type (get-type value)))
(if (literal-p value)
(if (or (byte-type-p val-type)
(short-type-p val-type)
(char-type-p val-type)
(int-type-p val-type))
(let ((rval (literal-value value)))
(flet ((bogus ()
(error "Trying to convert the literal value ~A to the type ~A that doesn't contain it" rval type)))
(cond ((byte-type-p type) (if (byte-value-p rval) value (bogus)))
((short-type-p type) (if (short-value-p rval) value (bogus)))
((char-type-p type) (if (char-value-p rval) value (bogus)))
(t (cast-and-wrap-if-needed type val-type value error-fn)))))
(cast-and-wrap-if-needed type val-type value error-fn))
(cast-and-wrap-if-needed type val-type value error-fn))))
(defun convert-type-to-lvalue (lvalue rvalue)
(assignment-convertion
rvalue
(get-type lvalue)
#'(lambda (expected current expr)
(linj-error "Can't assign variable ~S of type ~S~%with value ~S of type ~S"
lvalue
expected
expr
current))))
(def-unparse setf-expression (e)
(when *print-out-parenthesis*
(format t "("))
(format t "~/pp/ = ~:/ppexp/"
(setf-expression-l-value e)
(setf-expression-r-value e))
(when *print-out-parenthesis*
(format t ")")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def-category in-form ()
((object :reader in-form-object)))
(defmethod visit :before ((e in-form) (visitor adjust-parents))
;;Correct context for the initializer to jump over the in-expression
(setf (ast-node-parent (in-form-object e)) (ast-node-parent e)))
(defmethod find-declaration-in ((what t) (e in-form))
(or (find-declaration-in what (get-type-declaration (get-type (in-form-object e))))
(find-declaration-in what (ast-node-parent e))))
(def-syntax in-expression (in-form expression)
(in ?object/expression ?expression/expression))
(def-syntax in-type-expression (in-expression)
(in (the ?object/type-reference) ?expression/expression))
(defmethod get-type ((e in-expression))
(get-type (in-expression-expression e)))
(def-unparse in-expression (e)
(unparse-object (in-expression-expression e) *standard-output*))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def-syntax in-statement (in-form statement)
(in ?object/expression . ?body/statement-list))
(def-syntax in-type-statement (in-statement)
(in (the ?object/type-reference) . ?body/statement-list))
(def-unparse in-statement (e)
(format t "~{~/ppblk/~^~:@_~}"
(statement-list-elements (in-statement-body e))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun collect-upward-in-forms (node)
(cond ((or (static-block-p node)
(method-declaration-p node)
(slot-declaration-p node))
(list))
((in-expression-p node)
(cons (in-expression-object node)
(collect-upward-in-forms (ast-node-parent node))))
((in-statement-p node)
(cons (in-statement-object node)
(collect-upward-in-forms (ast-node-parent node))))
(t
(collect-upward-in-forms (ast-node-parent node)))))
;;if
(def-syntax if-statement (statement)
(if ?test/expression ?then/statement ?else/statement)
:strict-p t)
;;In fact, this will become a little more complicated because we want to
;;combine multiple ifs. The rule is that when the else statement is another
;;if-statement, then we print this as "else if ..." and not as "else { if ... }"
(defparameter *translate-reference-type-to-boolean* t)
(defun from-reference-type-to-boolean (expr)
(if *translate-reference-type-to-boolean*
(let ((type (get-type expr)))
(cond ((boolean-type-p type)
expr)
((cons-type-p type)
(with-parent ((ast-node-parent expr))
(parse `(not (endp ,expr)) 'expression)))
((class-or-interface-type-reference-p type)
(with-parent ((ast-node-parent expr))
(parse `(not (eq ,expr null)) 'expression)))
(t
(error "Can't use expression ~A in a context where a boolean is expected" expr))))
expr))
(def-unparse if-statement (e)
(format t
(cond ((and (if-statement-p (ast-node-parent e))
(or (if-statement-p (if-statement-else e))
(cond-statement-p (if-statement-else e))))
"if (~:/ppexp/) ~/ppstm/ else ~/pp/")
((and (if-statement-p (ast-node-parent e))
(eq (if-statement-else (ast-node-parent e)) e))
"if (~:/ppexp/) ~/ppstm/ else ~/ppstm/")
((or (if-statement-p (if-statement-else e))
(cond-statement-p (if-statement-else e)))
"~@<if (~:/ppexp/) ~/ppstm/ else ~/pp/~:>")
(t
"~@<if (~:/ppexp/) ~/ppstm/ else ~/ppstm/~:>"))
(from-reference-type-to-boolean (if-statement-test e))
(if-statement-then e)
(if-statement-else e)))
;;cond
(def-syntax clause (linj-node)
(?test/expression . ?body/statement-list)
:strict-p t)
(def-unparse clause (e)
(cond ((true-literal-p (clause-test e))
(format t "~/ppblk/" (clause-body e)))
(t
(format t "if (~:/ppexp/) ~/ppblk/"
(from-reference-type-to-boolean (clause-test e))
(clause-body e)))))
(def-list-syntax clause-list (linj-list-node) clause)
(def-unparse clause-list (e)
(let ((up (ast-node-parent (ast-node-parent e))))
(format t (if (or (and (if-statement-p up)
(eq (if-statement-else up) (ast-node-parent e)))
(cond-statement-p up))
"~/pp/~{ else ~/pp/~}"
"~@<~/pp/~{ else ~/pp/~}~:>")
(first (clause-list-elements e))
(rest (clause-list-elements e)))))
(def-syntax cond-statement (statement)
(cond . ?clauses/clause-list)
:strict-p t)
(def-unparse cond-statement (e)
(unparse-object (cond-statement-clauses e) *standard-output*))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;conditional expression
(def-syntax conditional-expression (expression)
(if ?test/expression ?then/expression ?else/expression))
;;; The type of a conditional expression is determined as follows:
;;; If the second and third operands have the same type (which may be the
;;; null type), then that is the type of the conditional expression.
;;; Otherwise, if the second and third operands have numeric type, then
;;; there are several cases:
;;; If one of the operands is of type byte and the other is of type
;;; short, then the type of the conditional expression is short.
;;; If one of the operands is of type T where T is byte, short, or char,
;;; and the other operand is a constant expression of type int whose value
;;; is representable in type T, then the type of the conditional expression
;;; is T.
;;; Otherwise, binary numeric promotion (§5.6.2) is applied to the
;;; operand types, and the type of the conditional expression is the
;;; promoted type of the second and third operands. Note that binary
;;; numeric promotion performs value set conversion (§5.1.8).
;;; If one of the second and third operands is of the null type and the
;;; type of the other is a reference type, then the type of the conditional
;;; expression is that reference type.
;;; If the second and third operands are of different reference types,
;;; then it must be possible to convert one of the types to the other type
;;; (call this latter type T) by assignment conversion (§5.2); the type of
;;; the conditional expression is T. It is a compile-time error if neither
;;; type is assignment compatible with the other type.
(defmethod get-type ((e conditional-expression))
(let ((then (conditional-expression-then e))
(else (conditional-expression-else e)))
(assert (not (and (null-literal-p then)
(null-literal-p else))))
(let ((type-then (get-type then))
(type-else (get-type else)))
(merge-types type-then type-else))))
;;I guess there's a problem here due to type promotions. When both then
;;and else are char, the return type is char? It should, but...
(def-unparse conditional-expression (e)
(format t (if *print-out-parenthesis*
"(~@<(~:/ppexp/) ? ~_~/pp/ : ~_~/pp/~:>)"
"~@<(~:/ppexp/) ? ~_~/pp/ : ~_~/pp/~:>")
(from-reference-type-to-boolean (conditional-expression-test e))
(conditional-expression-then e)
(conditional-expression-else e)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;We also have a 'cond' as an expression (this is beautifull) as long as we
;;convert it to an 'if'.
;;Note that we cannot translate clauses with multiple consequents because
;;Java does not have the C/C++ comma operator.
(def-transform expression (cond (t ?expression))
?expression)
(def-transform expression (cond (?test ?expression) . ?rest-clauses)
(if ?test
?expression
(cond . ?rest-clauses)))
(def-transform expression (cond (t ?expression1 ?expression2 . ?more-expressions))
(progn ?expression1 ?expression2 . ?more-expressions))
(def-transform expression (cond (?test ?expression1 ?expression2 . ?more-expressions) . ?rest-clauses)
(if ?test
(progn
?expression1 ?expression2 . ?more-expressions)
(cond . ?rest-clauses)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;Let's continue.
;;when
(def-syntax when-statement (statement)
(when ?test/expression . ?body/statement-list)
:strict-p t)
(def-unparse when-statement (e)
(format t "~@<if (~:/ppexp/) ~/ppblk/~:>"
(from-reference-type-to-boolean (when-statement-test e))
(when-statement-body e)))
;;unless
(def-transform statement (unless ?test . ?body)
(when (not ?test) . ?body))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass type-node ()
())
;;Some nodes have an explicit (or easily determinable type)
;;Let's create a class just for those nodes
(defclass explicit-type-node (type-node)
((type :accessor explicit-type-node-type :initarg :type)))
(defmethod get-type ((e explicit-type-node))
(explicit-type-node-type e))
;;In other cases, the type might be inferred
(defclass inferred-type-node (type-node)
((type :accessor inferred-type-node-type :initform (unknown-type) :initarg :type)))
(defmethod get-type ((e inferred-type-node))
(assert (not (null (inferred-type-node-type e)))) ;;I suspect this can't happen now
(when (unknown-type-reference-p (inferred-type-node-type e))
(setf (inferred-type-node-type e) (with-parent (e) (copy-type (infer-type e)))))
(inferred-type-node-type e))
;;;;;Let's do this now so that the errors are presented before
;;;unparsing HACK!!!!!! This __MUST__ be done now, otherwise terrible
;;;bugs show up. This must be studied in detail to understand what's
;;;happening. I'm really afraid there are some nasty bugs waiting to
;;;show up.
(defmethod visit :after ((e inferred-type-node) (v parse-tree-finish))
(get-type e))
;;HACK: shouldn't be doing this on around methods belonging to interfaces because it might need type-inference over call-next-method and this isn't defined!
;;let-bindings and such:
(def-category variable-declaration (declaration-options inferred-type-node linj-node)
((name :accessor variable-declaration-name :initarg :name)
(initializer :accessor variable-declaration-initializer :initform nil :initarg :initializer)))
(defun supplied-p (var)
(get-option :supplied-p var))
(defun (setf supplied-p) (val var)
(setf (get-option :supplied-p var) val))
(defmethod infer-type ((e variable-declaration))
(get-type (variable-declaration-initializer e)))
(defun initialized-variable-declaration-p (param)
(not (null (variable-declaration-initializer param))))
(defmethod visit-current ((e variable-declaration) (visitor cast-and-wrap))
(when (variable-declaration-initializer e)
(setf (variable-declaration-initializer e)
(with-parent (e) ;;initializer doesn't have auto-parent
(convert-to-type (variable-declaration-initializer e) (get-type e) :force-cast nil)))))
(def-unparse variable-declaration (e)
(format t "~@<~:[~;final ~]~/pp/ ~/unlinj-name/~:[~; = ~4I~_~:/ppexp/~I~]~:>"
(get-option :final e)
(get-type e)
(variable-declaration-name e)
(and *print-out-initializer*
(not (null (variable-declaration-initializer e))))
(variable-declaration-initializer e)))
;;The syntaxes for variable declarations (mainly parameters) are:
;; (defun xpto (a b/int (c 1) (d/long 1))
;; ...)
;; The possibilities are:
;; linj-name - untyped, uninitialized variable
;; linj-name/type-reference - typed, uninitialized variable
;; (linj-name expression) - untyped, initialized variable
;; (linj-name/type-reference expression) - typed, initialized variable
;;I'm not sure about the last form. It can be trivially implemented as
;; (linj-name (the type-reference expression))
;;thus reducing the number of cases to three.
;;However, there's a different decomposition which is more ortogonal, namely
;; var-or-var/type
;; (var-or-var/type expression)
;;where var-or-var/type is
;; linj-name
;; linj-name/type-reference
;;This suggests a more structured variable declaration. Unfortunately, I'm afraid
;;it would break a lot of code so I'll stick with the first four possibilities
;;complete with type name and initializer
(def-syntax full-variable-declaration (variable-declaration)
(?name/linj-name (the ?type/type-reference ?initializer/expression)))
;;lacking initializer
(def-syntax uninitialized-variable-declaration (variable-declaration)
(?name/linj-name (the ?type/type-reference)))
;;lacking initializer but with special syntax for type declaration
(def-syntax short-uninitialized-variable-declaration (variable-declaration)
(?is ?name name&type-p)
:constructor make-short-uninitialized-variable-declaration)
(defun make-short-uninitialized-variable-declaration (category &key original-form name)
(declare (ignore category))
(multiple-value-bind (name type)
(extract-name&type name)
(make-instance 'uninitialized-variable-declaration
:original-form original-form
:name (parse name 'linj-name)
:type (parse type 'type-reference))))
;;lacking type
;;Types might be computed by our "type inferencer"
(def-syntax untyped-variable-declaration (variable-declaration)
(?name/linj-name ?initializer/expression)
:components (?type/type-reference))
(def-syntax short-untyped-variable-declaration (untyped-variable-declaration)
((?is ?name name&type-p) ?initializer/expression)
:constructor make-short-untyped-variable-declaration
:components (?type/type-reference))
(defun make-short-untyped-variable-declaration (category &key original-form name initializer)
(declare (ignore category))
(multiple-value-bind (name type)
(extract-name&type name)
(make-instance 'untyped-variable-declaration
:original-form original-form
:name (parse name 'linj-name)
:type (parse type 'type-reference)
:initializer (parse initializer 'expression))))
;;lacking type and initializer
(def-syntax untyped-uninitialized-variable-declaration (variable-declaration)
?name/linj-name
:components (?type/type-reference))
(defmethod infer-type ((e untyped-uninitialized-variable-declaration))
;;There are several ways to infer the type of a function or method parameter:
;; - infer its type from the operations done with the parameter. This
;; might be problematic in the presence of polimorfic operators such as
;; the + or = operators that can deal with int our double
;; - look to a method call and identify what type of objects are being
;; passed as arguments to that parameter.
;;
;;We can try both venues. The idea is that a variable might have a set
;;of potential types that, at code generation time, must be resolved to a
;;singleton.
;;For the moment, I'll use just the first approach
;; (object-type))
(setf (inferred-type-node-type e) (object-type))
(let ((meth (containing-method-declaration e)))
(when meth
(let ((meth-inferred-type (inferred-type-node-type meth)))
(let ((type (get-type meth)))
(unless (or (cyclic-type-p type) (void-type-p type))
(force-type meth type))
;;Now, check if all my uses have a lower common type in casts expressions
(setf (inferred-type-node-type e)
(let ((casts (collect-all-type-info-for-references-to meth e)))
(cond ((endp casts) ;;No casts, (object-type) is OK
(object-type))
((endp (rest casts)) ;;Just one. Use it
(first casts))
(t
(block common-sub-type
(reduce #'(lambda (type1 type2)
(cond ((super-type-p type1 type2)
type2)
((super-type-p type2 type1)
type1)
(t ;;No good common subtype
(return-from common-sub-type (object-type)))))
casts)))))))
(setf (inferred-type-node-type meth) meth-inferred-type))))
(inferred-type-node-type e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;We will restrict bindings so that they only accept variable declarations
;;with initialization forms. If you want a non-initialized reference,
;;write (the <???> null)
(def-list-syntax bindings (linj-list-node) untyped-variable-declaration)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;I use a nice trick to overcome the stupid java limitation regarding shadowing:
;;names have a trailing number that expresses its shadowing level.
(defun shadowing-level (name node &optional (level 0))
(let ((ref (make-instance 'reference
:name name
:parent (ast-node-parent (ast-node-parent node)))))
(labels ((shadow-level (e)
(let ((decl (find-declaration-in ref e)))
(cond ((null decl)
0)
((slot-declaration-p decl)
level) ;;slots can be shadowed
((and (parameter-list-p (ast-node-parent decl))
(method-declaration-p (ast-node-parent (ast-node-parent decl))))
1) ;;parameters can't be shadowed but there's no need to look further up
(t ;;everything else (let, for, etc)
(1+ (shadow-level (ast-node-parent (ast-node-parent (ast-node-parent decl))))))))))
(shadow-level (ast-node-parent (ast-node-parent (ast-node-parent node)))))))
(defclass collect-references-to ()
((declaration :initarg :declaration :reader get-declaration)
(references :initform (list) :accessor get-references)))
(defmethod visit :before ((ref reference) (visitor collect-references-to))
(when (eq (find-declaration ref) (get-declaration visitor))
(push ref (get-references visitor))))
(defun all-references-to (var-decl &optional (within (ast-node-parent (ast-node-parent var-decl))))
(let ((visitor (make-instance 'collect-references-to :declaration var-decl)))
(visit within visitor)
(get-references visitor)))
;;HACK: this is operating too soon, preventing semantic macros that rebind variables to work properly. Consider the example:
;;(for-each (e l)
;; (for-each (e e)
;; ..))
(defmethod visit :before ((e bindings) (visitor parse-tree-finish))
(dolist (var (bindings-elements e))
(let ((level (shadowing-level (variable-declaration-name var) var)))
(when (> level 0)
(let ((new-name (unshadowed-name (variable-declaration-name var) var)))
(dolist (ref (all-references-to var))
(setf (reference-name ref) new-name))
(setf (variable-declaration-name var) new-name))))))
(defun unshadowed-name (name node)
(labels ((try (suf default-level)
(if (= (shadowing-level (conc-symbol name suf) node default-level) 0)
suf
(try (1+ suf) default-level))))
(do* ((first-try (try 0 0) second-try)
(second-try (try first-try 1) (try first-try 1)))
((eq first-try second-try) (conc-symbol name first-try)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def-syntax let-statement (statement)
(p-let ?bindings/bindings . ?body/statement-list)
:strict-p t)
(def-macro-transform let-statement (let ?bindings . ?body)
(multiple-value-bind (new-binds reordered-binds)
(serialize-parallel ?bindings)
(if new-binds
`(p-let ,new-binds
(p-let ,reordered-binds
(unlet-macro-transform ,(mapcar #'(lambda (bind)
`(expression ,(first bind)))
(append new-binds reordered-binds))
(p-let () ,@?body))))
(if reordered-binds
`(p-let ,reordered-binds
(unlet-macro-transform ,(mapcar #'(lambda (bind)
`(expression ,(first bind)))
reordered-binds)
(p-let () ,@?body)))
`(p-let () ,@?body)))))
(defmethod visit :before ((e let-statement) (visitor adjust-parents))
;;Correct context for the initializers to jump over the let
(dolist (bind (bindings-elements (let-statement-bindings e)))
(setf (ast-node-parent (variable-declaration-initializer bind)) (ast-node-parent e))))
;;In Common Lisp, references inside bindings can't refer to the bindings
;;themselves and this suggests that the following method should be active:
; (defmethod find-declaration-in ((ref reference) (e bindings))
; (find-declaration-in ref (ast-node-parent (ast-node-parent e))))
;;This is necessary to deal with code like (let ((x (- x))) ...) without
;;infinite regression.
;;However, in Java, this rule isn't valid. In fact, the specification says:
;; 14.4.2 Scope of Local Variable Declarations
;; The scope of a local variable declaration in a block (§14.4.2) is the
;; rest of the block in which the declaration appears, starting with its
;; own initializer (§14.4) and including any further declarators to the
;; right in the local variable declaration statement.
;; The name of a local variable v may not be redeclared as a local
;; variable of the directly enclosing method, constructor or initializer
;; block within the scope of v, or a compile-time error occurs. The name
;; of a local variable v may not be redeclared as an exception parameter
;; of a catch clause in a try statement of the directly enclosing method,
;; constructor or initializer block within the scope of v, or a
;; compile-time error occurs. However, a local variable of a method or
;; initializer block may be shadowed (§6.3.1) anywhere inside a class
;; declaration nested within the scope of the local variable.
;;All this implies that in order to translate the shadowing of let-bindings
;;or parameters, we need to rename the new bindings, that is, we should
;;translate (let ((x 1)) (let ((x (- x))) ...x...)) into something like
;;(let ((x 1)) (let ((x1 (- x))) ...x1...)). I'm not in the mood to
;;do this know.
(defmethod find-declaration-in ((ref reference) (e let-statement))
(or (find (reference-name ref)
(bindings-elements (let-statement-bindings e))
:key #'variable-declaration-name)
(call-next-method)))
;;Due to the fact that a statement-list is represented in Java using a
;;block and that blocks can introduce local variables (as long as they
;;don't shadow other variables - stupid language), we will define a special
;;(simplified) unparse for statement-lists composed by a unique let
;;statement.
;; (defclass references-p ()
;; ((name :initarg :name :reader get-name)
;; (exit :initarg :exit :reader get-exit)))
;; (defun unneded-braces-p (e)
;; (let ((all-bindings (collect-all-bindings e)))
;; (or (endp all-bindings)
;; (or (not (statement-list-p (ast-node-parent e)))
;; (let ((next-statements (rest (member e (statement-list-elements (ast-node-parent e))))))
;; (every #'(lambda (decl)
;; (notany #'(lambda (statement)
;; (references-p (variable-declaration-name decl) statement))
;; next-statements))
;; all-bindings))))))
(defun no-redeclarations-p (e out-bindings)
(or (not (statement-list-p (ast-node-parent e)))
(and (no-redeclarations-in-statement-list
(rest (member e (statement-list-elements (ast-node-parent e)))) out-bindings)
(no-redeclarations-p (ast-node-parent (ast-node-parent e)) out-bindings))))
(defclass operate-bindings-visitor ()
((function :initarg :function :reader operate-binding-function)))
(defmethod visit :before ((bindings bindings) (visitor operate-bindings-visitor))
(funcall (operate-binding-function visitor) (bindings-elements bindings)))
(defun no-redeclarations-in-statement-list (statements out-bindings)
(visit statements
(make-instance
'operate-bindings-visitor
:function #'(lambda (bindings)
(when (some #'(lambda (bind)
(some #'(lambda (out-bind)
(eq (variable-declaration-name bind) (variable-declaration-name out-bind)))
out-bindings))
bindings)
(return-from no-redeclarations-in-statement-list nil)))))
t)
(defun unneded-braces-p (e)
(let ((out-bindings (bindings-elements (let-statement-bindings e))))
(or (endp out-bindings)
(no-redeclarations-p e out-bindings))))
;; (def-unparse let-statement (e)
;; (let ((statements (statement-list-elements (let-statement-body e))))
;; (if (not (endp statements)) ;;more than zero?
;; (if (and *print-out-braces* (not (unneded-braces-p e)))
;; (format t "~@<{~4I~{~:@_~/pp/;~}~{~:@_~/ppblk/~}~:@_~:/ppblk/~I~:@_}~:>"
;; (bindings-elements (let-statement-bindings e))
;; (butlast statements)
;; (first (last statements)))
;; (format t "~@<~{~/pp/;~:@_~}~{~/ppblk/~:@_~}~:/ppblk/~:>"
;; (bindings-elements (let-statement-bindings e))
;; (butlast statements)
;; (first (last statements))))
;; (if (and *print-out-braces* (not (unneded-braces-p e)))
;; (format t "~@<{~4I~{~:@_~/pp/;~}~I~:@_}~:>"
;; (bindings-elements (let-statement-bindings e)))
;; (format t "~@<~{~/pp/;~:@_~}~:>"
;; (bindings-elements (let-statement-bindings e)))))))
(def-unparse let-statement (e)
(let ((statements (statement-list-elements (let-statement-body e))))
(if (not (endp statements)) ;;more than zero?
(if (and *print-out-braces* (not (unneded-braces-p e)))
(format t "~@<{~4I~{~:@_~/pp/;~}~{~:@_~/ppblk/~}~:@_~/ppblk/~I~:@_}~:>"
(bindings-elements (let-statement-bindings e))
(butlast statements)
(first (last statements)))
(format t "~@<~{~/pp/;~:@_~}~{~/ppblk/~:@_~}~/ppblk/~:>"
(bindings-elements (let-statement-bindings e))
(butlast statements)
(first (last statements))))
(if (and *print-out-braces* (not (unneded-braces-p e)))
(format t "~@<{~4I~{~:@_~/pp/;~}~I~:@_}~:>"
(bindings-elements (let-statement-bindings e)))
(format t "~@<~{~/pp/;~:@_~}~:>"
(bindings-elements (let-statement-bindings e)))))))
;; (defmethod visit :before ((ref reference) (visitor references-p))
;; (when (eq (reference-name ref) (get-name visitor))
;; (funcall (get-exit visitor))))
;; ;;This is not strictly a reference but it is also a situation where the let-statement can't be inlined.
;; (defmethod visit :before ((var variable-declaration) (visitor references-p))
;; (when (eq (variable-declaration-name var) (get-name visitor))
;; (funcall (get-exit visitor))))
;; (defun references-p (name node)
;; (let ((visitor (make-instance 'references-p
;; :name name
;; :exit #'(lambda () (return-from references-p t)))))
;; (visit node visitor)
;; nil))
;;;;;;;;;;;;;;;;;;;;;;
(defclass collect-bindings-visitor ()
((bindings :initform (list) :accessor get-bindings)))
(defmethod visit :before ((var variable-declaration) (visitor collect-bindings-visitor))
(push var (get-bindings visitor)))
(defun collect-all-bindings (node)
(let ((visitor (make-instance 'collect-bindings-visitor)))
(visit node visitor)
(get-bindings visitor)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def-macro-transform let-statement (let* ?bindings . ?body)
(if (endp ?bindings)
`(let ,?bindings
,@?body)
`(let (,(first ?bindings))
(let* ,(rest ?bindings)
,@?body))))
;;;;;;;;;;;;;;;;;;;;;
;;A very simple let-statement:
(def-transform let-statement (progn . ?body)
(let () . ?body))
;;A very special case:
(def-transform expression (prog1 ?form (incf ?form))
(post-incf ?form))
;;A not so simple let-statement:
(def-macro-transform let-statement (prog1 ?form . ?body)
(with-new-names (results)
`(let ((,results ,?form))
,@?body
,results)))
;;The equivalent form...
(def-transform let-statement (multiple-value-prog1 ?form . ?body)
(prog1 ?form . ?body))
;;;prog2
(def-transform let-statement (prog2 ?form1 ?form2 . ?body)
(progn ?form1 (prog1 ?form2 . ?body)))
;;;;;;;;;;;;;;;;;;;;;;;;
(def-list-syntax argument-list (linj-list-node) expression)
(defparameter *infer-principal-value* t)
(defun principal-value (arg)
(if *infer-principal-value*
(with-parent ((ast-node-parent arg))
(cast-and-wrap-if-needed
(get-principal-type arg)
(get-type arg)
arg
#'(lambda (expected current expr)
(declare (ignore expected current expr))
(linj-error "Something weird happen with ~A" arg))))
arg))
(def-unparse argument-list (e)
(format t "(~@<~{~:/ppexp/~^, ~_~}~:>)"
(mapcar #'principal-value (argument-list-elements e))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Another needed feature is generic iteration
;;We'll consider first a do-type form on top of which one can define
;;several other macros (including, presumably, the loop macro)
(def-syntax for-statement (statement)
(for (?bindings/bindings ?test/expression ?updates/argument-list)
. ?body/statement-list)
:strict-p t)
(defmethod visit :before ((e for-statement) (visitor adjust-parents))
;;Correct context for the initializers to jump over the for
(dolist (bind (bindings-elements (for-statement-bindings e)))
(setf (ast-node-parent (variable-declaration-initializer bind)) (ast-node-parent e))))
(defmethod find-declaration-in ((ref reference) (e for-statement))
(or (find (reference-name ref)
(bindings-elements (for-statement-bindings e))
:key #'variable-declaration-name)
(call-next-method)))
(def-unparse for-statement (e)
(if (and (null (bindings-elements (for-statement-bindings e)))
(null (argument-list-elements (for-statement-updates e))))
(format t "~@<while (~:/ppexp/) ~/ppblk/~:>"
(from-reference-type-to-boolean (for-statement-test e))
(for-statement-body e))
(format t "~@<for ~<(~{~/pp/~^, ~}; ~1I~_~:/ppexp/; ~_~{~:/ppexp/~^, ~_~})~:> ~/ppblk/~:>"
(list (bindings-elements (for-statement-bindings e))
(from-reference-type-to-boolean (for-statement-test e))
(argument-list-elements (for-statement-updates e)))
(for-statement-body e))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;An implementation for (block ?label . ?body)
;;An implementation for (return) (return ?expr) (return-from ?label) (return-from ?label ?expr)
;;;In Common Lisp
;;;do, do*, dolist, dotimes, simple loop, and complex loop
;;;establish implicit blocks (labeled nil)
;;;defun, defmethod, and the complex loop with the :named keyword
;;;establish implicit blocks (labelled the name of the definition)
;;;(block ?name . ?body) establishes an explicit block named ?name
;;;Moreover
;;;(return) = (return-from nil)
;;;(return ?expr) = (return-from nil ?expr)
(def-transform statement (return) (return-from nil))
(def-transform statement (return ?expr) (return-from nil ?expr))
;;;In Java
;;;A break statement with no label attempts to transfer
;;;control to the innermost enclosing switch, while, do, or for
;;;statement of the immediately enclosing method or initializer block;
;;;A break statement with label Identifier attempts to transfer
;;;control to the enclosing labeled statement that has the same
;;;Identifier as its label
;;There are special forms to break or 'continue' a block. Common Lisp
;;doesn't contain them, but Linj does. However, they can only be used
;;with a surrounding block. Obviously, in the 'continue' case, just
;;like in Java, for it to take any effect, the block must contain just
;;one loop.
;;Note also that I don't expect that programmers will use the break
;;statement in Linj because the "equivalent" return-from statement is
;;available and it spares the return/break Java duality.
(def-transform statement (break) (break nil))
(def-syntax break-statement (statement)
(break ?label/linj-name))
(def-unparse break-statement (e)
(format t "break ~/unlinj-name/;" (break-statement-label e)))
(def-syntax empty-break-statement (break-statement)
(break nil)
:slots ((label :initform nil)))
(def-unparse empty-break-statement (e)
(format t "break;"))
;;;;;;;;;;;;;;;;;;;;;;;;
(def-transform statement (continue) (continue nil))
(def-syntax continue-statement (statement)
(continue ?label/linj-name))
(def-unparse continue-statement (e)
(format t "continue ~/unlinj-name/;" (continue-statement-label e)))
(def-syntax empty-continue-statement (continue-statement)
(continue nil)
:slots ((label :initform nil)))
(def-unparse empty-continue-statement (e)
(format t "continue;"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Now, let's deal with processing all this return/break/continue mess.
;;First, we, create a special statement that will be used to implement
;;the proper Java return statement. Note that this statemtent (and
;;its subclasses) cannot be present in any Linj file and it is only
;;generated as the result of the compilation process
(def-category return-statement (statement)
((expression :accessor return-statement-expression :initform nil)))
(defmethod get-type ((e return-statement))
(if (null (return-statement-expression e))
(void-type)
(get-type (return-statement-expression e))))
(def-unparse return-statement (e)
(format t "return~@[ ~:/ppexp/~];"
(return-statement-expression e)))
;;Now, we create two varieties (the void and non-void return statement)
;;;HACK:: This must be changed
(def-syntax non-void-return-statement (return-statement)
(cannot-parse-this-return ?expression/expression)
:strict-p t)
(def-syntax void-return-statement (return-statement)
(cannot-parse-this-return)
:strict-p t)
;;Now, let's specify the Linj statements. We can write (return),
;;(return ...), (return-from ...), and (return-from ... ...) and they
;;will all be parsed as just one type of return, namely the
;;return-from-statement. Some of these 'returns' have the containing
;;method as target and these will be transformed into the above
;;return-statements. The remaining ones should be transformed into
;;break statements.
(def-syntax return-from-statement (statement)
(return-from ?label/linj-name &optional ?expression/expression))