forked from fricas/fricas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fortran.spad
1762 lines (1506 loc) · 67.3 KB
/
fortran.spad
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
)abbrev domain RESULT Result
++ Author: Didier Pinchon and Mike Dewar
++ Date Created: 8 April 1994
++ Basic Operations:
++ Related Domains:
++ Also See:
++ AMS Classifications:
++ Keywords:
++ Examples:
++ References:
++ Description: A domain used to return the results from a call to the NAG
++ Library. It prints as a list of names and types, though the user may
++ choose to display values automatically if he or she wishes.
Result() : Exports==Implementation where
O ==> OutputForm
Exports ==> Join(TableAggregate(Symbol, Any), finiteAggregate) with
showScalarValues : Boolean -> Boolean
++ showScalarValues(true) forces the values of scalar components to be
++ displayed rather than just their types.
showArrayValues : Boolean -> Boolean
++ showArrayValues(true) forces the values of array components to be
++ displayed rather than just their types.
Implementation ==> Table(Symbol, Any) add
-- Constant
colon := ": "::Symbol::O
elide := "..."::Symbol::O
-- Flags
showScalarValuesFlag : Boolean := false
showArrayValuesFlag : Boolean := false
cleanUpDomainForm(d : SExpression) : O ==
not list? d => d::O
#d = 1 => (car d)::O
-- If the car is an atom then we have a domain constructor, if not
-- then we have some kind of value. Since we often can't print these
-- ^^ers we just elide them.
not atom? car d => elide
prefix((car d)::O, [cleanUpDomainForm(u) for u in destruct cdr(d)]$List(O))
display(v : Any, d : SExpression) : O ==
not list? d => error "Domain form is non-list"
#d = 1 =>
showScalarValuesFlag => objectOf v
cleanUpDomainForm d
car(d) = convert('Complex)@SExpression =>
showScalarValuesFlag => objectOf v
cleanUpDomainForm d
showArrayValuesFlag => objectOf v
cleanUpDomainForm d
makeEntry(k : Symbol, v : Any) : O ==
hconcat [k::O, colon, display(v, dom v)]
coerce(r : %) : O ==
bracket [makeEntry(key, r.key) for key in reverse! keys(r)]
showArrayValues(b : Boolean) : Boolean == showArrayValuesFlag := b
showScalarValues(b : Boolean) : Boolean == showScalarValuesFlag := b
)abbrev domain FC FortranCode
-- The FortranCode domain is used to represent operations which are to be
-- translated into FORTRAN.
++ Author: Mike Dewar
++ Date Created: April 1991
++ Basic Operations:
++ Related Constructors: FortranProgram, Switch, FortranType
++ Also See:
++ AMS Classifications:
++ Keywords:
++ References:
++ Description:
++ This domain builds representations of program code segments for use with
++ the FortranProgram domain.
FortranCode() : public == private where
L ==> List
PI ==> PositiveInteger
PIN ==> Polynomial Integer
LS ==> List String
O ==> OutputForm
OP ==> Union(Null:"null",
Assignment:"assignment",
Conditional:"conditional",
Return:"return",
Block:"block",
Comment:"comment",
Call:"call",
For:"for",
While:"while",
Repeat:"repeat",
Goto:"goto",
Continue:"continue",
ArrayAssignment:"arrayAssignment",
Save:"save",
Stop:"stop",
Common:"common",
Print:"print")
ARRAYASS ==> Record(var : Symbol, rand : O, ints2Floats? : Boolean)
EXPRESSION ==> Record(ints2Floats? : Boolean, expr : O)
ASS ==> Record(var : Symbol,
arrayIndex : L PIN,
rand : EXPRESSION
)
COND ==> Record(switch : Switch(),
thenClause : %,
elseClause : %
)
RETURN ==> Record(empty? : Boolean, value : EXPRESSION)
BLOCK ==> List %
COMMENT ==> List String
COMMON ==> Record(name : Symbol, contents : List Symbol)
CALL ==> String
FOR ==> Record(range : SegmentBinding PIN, span : PIN, body : %)
LABEL ==> SingleInteger
LOOP ==> Record(switch : Switch(), body : %)
PRINTLIST ==> List O
OPREC ==> Union(nullBranch:"null", assignmentBranch:ASS,
arrayAssignmentBranch : ARRAYASS,
conditionalBranch : COND, returnBranch : RETURN,
blockBranch : BLOCK, commentBranch : COMMENT, callBranch : CALL,
forBranch : FOR, labelBranch : LABEL, loopBranch : LOOP,
commonBranch : COMMON, printBranch : PRINTLIST)
public == SetCategory with
coerce : % -> O
++ coerce(f) returns an object of type OutputForm.
forLoop : (SegmentBinding PIN, %) -> %
++ forLoop(i=1..10, c) creates a representation of a FORTRAN DO loop with
++ \spad{i} ranging over the values 1 to 10.
forLoop : (SegmentBinding PIN, PIN, %) -> %
++ forLoop(i=1..10, n, c) creates a representation of a FORTRAN DO loop with
++ \spad{i} ranging over the values 1 to 10 by n.
whileLoop : (Switch, %) -> %
++ whileLoop(s, c) creates a while loop in FORTRAN.
repeatUntilLoop : (Switch, %) -> %
++ repeatUntilLoop(s, c) creates a repeat ... until loop in FORTRAN.
gotoJump : SingleInteger -> %
++ gotoJump(l) creates a representation of a FORTRAN GOTO statement
continue : SingleInteger -> %
++ continue(l) creates a representation of a FORTRAN CONTINUE labelled
++ with l
comment : String -> %
++ comment(s) creates a representation of the String s as a single FORTRAN
++ comment.
comment : List String -> %
++ comment(s) creates a representation of the Strings s as a multi-line
++ FORTRAN comment.
call : String -> %
++ call(s) creates a representation of a FORTRAN CALL statement
returns : () -> %
++ returns() creates a representation of a FORTRAN RETURN statement.
returns : Expression MachineFloat -> %
++ returns(e) creates a representation of a FORTRAN RETURN statement
++ with a returned value.
returns : Expression MachineInteger -> %
++ returns(e) creates a representation of a FORTRAN RETURN statement
++ with a returned value.
returns : Expression MachineComplex -> %
++ returns(e) creates a representation of a FORTRAN RETURN statement
++ with a returned value.
returns : Expression Float -> %
++ returns(e) creates a representation of a FORTRAN RETURN statement
++ with a returned value.
returns : Expression Integer -> %
++ returns(e) creates a representation of a FORTRAN RETURN statement
++ with a returned value.
returns : Expression Complex Float -> %
++ returns(e) creates a representation of a FORTRAN RETURN statement
++ with a returned value.
cond : (Switch, %) -> %
++ cond(s, e) creates a representation of the FORTRAN expression
++ IF (s) THEN e.
cond : (Switch, %, %) -> %
++ cond(s, e, f) creates a representation of the FORTRAN expression
++ IF (s) THEN e ELSE f.
assign : (Symbol, String) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Expression MachineInteger) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Expression MachineFloat) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Expression MachineComplex) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Matrix MachineInteger) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Matrix MachineFloat) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Matrix MachineComplex) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Vector MachineInteger) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Vector MachineFloat) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Vector MachineComplex) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Matrix Expression MachineInteger) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Matrix Expression MachineFloat) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Matrix Expression MachineComplex) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Vector Expression MachineInteger) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Vector Expression MachineFloat) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Vector Expression MachineComplex) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, L PIN, Expression MachineInteger) -> %
++ assign(x, l, y) creates a representation of the assignment of \spad{y}
++ to the \spad{l}'th element of array \spad{x} (\spad{l} is a list of
++ indices).
assign : (Symbol, L PIN, Expression MachineFloat) -> %
++ assign(x, l, y) creates a representation of the assignment of \spad{y}
++ to the \spad{l}'th element of array \spad{x} (\spad{l} is a list of
++ indices).
assign : (Symbol, L PIN, Expression MachineComplex) -> %
++ assign(x, l, y) creates a representation of the assignment of \spad{y}
++ to the \spad{l}'th element of array \spad{x} (\spad{l} is a list of
++ indices).
assign : (Symbol, Expression Integer) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Expression Float) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Expression Complex Float) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Matrix Expression Integer) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Matrix Expression Float) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Matrix Expression Complex Float) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Vector Expression Integer) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Vector Expression Float) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, Vector Expression Complex Float) -> %
++ assign(x, y) creates a representation of the FORTRAN expression
++ x=y.
assign : (Symbol, L PIN, Expression Integer) -> %
++ assign(x, l, y) creates a representation of the assignment of \spad{y}
++ to the \spad{l}'th element of array \spad{x} (\spad{l} is a list of
++ indices).
assign : (Symbol, L PIN, Expression Float) -> %
++ assign(x, l, y) creates a representation of the assignment of \spad{y}
++ to the \spad{l}'th element of array \spad{x} (\spad{l} is a list of
++ indices).
assign : (Symbol, L PIN, Expression Complex Float) -> %
++ assign(x, l, y) creates a representation of the assignment of \spad{y}
++ to the \spad{l}'th element of array \spad{x} (\spad{l} is a list of
++ indices).
block : List(%) -> %
++ block(l) creates a representation of the statements in l as a block.
stop : () -> %
++ stop() creates a representation of a STOP statement.
save : () -> %
++ save() creates a representation of a SAVE statement.
printStatement : List O -> %
++ printStatement(l) creates a representation of a PRINT statement.
common : (Symbol, List Symbol) -> %
++ common(name, contents) creates a representation a named common block.
operation : % -> OP
++ operation(f) returns the name of the operation represented by \spad{f}.
code : % -> OPREC
++ code(f) returns the internal representation of the object represented
++ by \spad{f}.
printCode : % -> Void
++ printCode(f) prints out \spad{f} in FORTRAN notation.
getCode : % -> LS
++ getCode(f) returns a list of strings representing \spad{f}
++ in Fortran notation. This is used by the FortranProgram domain.
setLabelValue : SingleInteger -> SingleInteger
++ setLabelValue(i) resets the counter which produces labels to i
private == add
import from Void
import from ASS
import from COND
import from RETURN
import from L PIN
import from O
import from LS
import from FortranType
import from TheSymbolTable
import from FortranCodeTools
get_assignment(name : O,e : O, int_to_floats? : Boolean) : LS ==
getStatement(elt('=::O, [name, e]), int_to_floats?)
format_switch(switch1 : O, l : LS) : List(LS) ==
if LISTP(switch1)$Lisp then
l1 : List(O) := switch1 pretend List(O)
if EQ(first(l1), 'NULL)$Lisp then
switch1 := first rest l1
r := reverse!(statement2Fortran switch1)
while not(empty?(r)) and not(first(r) = "%l") repeat
l := cons(first(r), l)
r := rest(r)
[l, r]
fortFormatIf1(switch1 : O, i : Integer, kind : String) : LS ==
l : LS := [")THEN"]
changeExprLength(-i) -- Leave room for IF( ... )THEN
(l, r) := format_switch(switch1, l)
changeExprLength(i)
reverse! append(reverse!(l), cons(kind, r))
fortFormatIf(switch1 : O) : LS ==
do_with_error_env1(() +-> fortFormatIf1(switch1, 8, "IF("))
fortFormatElseIf(switch1 : O) : LS ==
do_with_error_env1(() +-> fortFormatIf1(switch1, 12, "ELSEIF("))
fortFormatIfGoto1(switch1 : O, lab : SingleInteger) : LS ==
l : LS := [")GOTO ", convert(lab)@String]
changeExprLength(-8) -- Leave room for IF( ... )THEN
(l, r) := format_switch(switch1, l)
changeExprLength(8)
reverse! append(reverse!(l), cons("IF(", r))
fortFormatIfGoto(switch1 : O, lab : SingleInteger) : LS ==
do_with_error_env1(() +-> fortFormatIfGoto1(switch1, lab))
fortFormatLabelledIfGoto1(switch1 : O, lab1 : SingleInteger,
lab2 : SingleInteger) : LS ==
l := fortFormatIfGoto1(switch1, lab2)
labString := convert(lab1)@String
for i in #(labString)..5 repeat labString := concat(labString, " ")
l := fort_clean_lines(l)
cons(concat(labString, first(l)(7..)), rest(l))
fortFormatLabelledIfGoto(switch1 : O, lab1 : SingleInteger,
lab2 : SingleInteger) : LS ==
fortFormatLabelledIfGoto1(switch1, lab1, lab2)
getfortarrayexp1(name : Symbol, of : O, int_to_floats? : Boolean) : LS ==
l := expression2Fortran1(() +-> name, of, int_to_floats?)
first(l, (#l - 2)::NonNegativeInteger)
getfortarrayexp(name : Symbol, of : O, int_to_floats? : Boolean) : LS ==
do_with_error_env2(int_to_floats?,
() +-> getfortarrayexp1(name, of, int_to_floats?))
fortFormatDo1(var1 : Symbol, lo : O, hi : O, incr : O,
lab : SingleInteger) : LS ==
lol := statement2Fortran lo
hil := statement2Fortran hi
incl : LS :=
EQUAL(incr, 1@Integer)$Lisp => cons(",", statement2Fortran incr)
[]
il := append(lol, cons(",", append(hil, incl)))
append(["DO ", convert(lab)@String, " ", string(var1), "="], il)
fortFormatDo(var1 : Symbol, lo : O, hi : O, inc : O,
lab : SingleInteger) : LS ==
do_with_error_env2(false,
() +-> fortFormatDo1(var1, lo, hi, inc, lab))
addCommas(l : List Symbol) : LS ==
empty?(l) => []
r := [string(first l)]
for e in rest l repeat r := cons(string(e), cons(",", r))
reverse!(r)
Rep := Record(op : OP, data : OPREC)
-- We need to be able to generate unique labels
labelValue : SingleInteger := 25000::SingleInteger
setLabelValue(u : SingleInteger) : SingleInteger == labelValue := u
newLabel() : SingleInteger ==
labelValue := labelValue + 1$SingleInteger
labelValue
commaSep(l : List String) : List(String) ==
[(l.1),:[:[",",u] for u in rest(l)]]
getReturn(rec : RETURN) : LS ==
returnToken : O := "RETURN"::Symbol::O
elt(rec, empty?)$RETURN =>
getStatement(returnToken, false)
rt : EXPRESSION := elt(rec, value)$RETURN
rv : O := elt(rt, expr)$EXPRESSION
getStatement(elt(returnToken, [rv]),
elt(rt, ints2Floats?)$EXPRESSION)
getStop() : LS ==
fort_clean_lines(["STOP"]$LS)
getSave() : LS ==
fort_clean_lines(["SAVE"])
getCommon(u : COMMON) : LS ==
fort_clean_lines(append(["COMMON", " /", string (u.name), "/ "]$LS,
addCommas(u.contents)))
getPrint(l : PRINTLIST) : LS ==
ll : LS := ["PRINT*"]
for i in l repeat
ll := append(ll, cons(",", expression2Fortran(i)))
fort_clean_lines(ll)
getBlock(rec : BLOCK) : LS ==
indentFortLevel(1)
expr : LS := []
for u in rec repeat
expr := append(expr, getCode(u))
indentFortLevel(-1)
expr
getBody(f : %) : LS ==
operation(f) case Block => getCode f
indentFortLevel(1@Integer)
expr := getCode f
indentFortLevel(-1@Integer)
expr
getElseIf(f : %) : LS ==
rec := code f
expr : LS :=
fortFormatElseIf(elt(rec.conditionalBranch, switch)$COND::O)
expr :=
append(expr, getBody elt(rec.conditionalBranch, thenClause)$COND)
elseBranch := elt(rec.conditionalBranch, elseClause)$COND
not(operation(elseBranch) case Null) =>
operation(elseBranch) case Conditional =>
append(expr, getElseIf elseBranch)
expr := append(expr, getStatement('ELSE::O, false))
expr := append(expr, getBody elseBranch)
expr
getContinue(label : SingleInteger) : LS ==
lab := convert(label)@String
if #lab > 6 then error "Label too big"
cnt := "CONTINUE"
sp : O := hspace(get_fort_indent() - #lab)
[STRCONC(lab, sp, cnt)$Lisp]$LS
getGoto(label : SingleInteger) : LS ==
fort_clean_lines([concat("GOTO ", convert(label)@String)])
getRepeat(repRec : LOOP) : LS ==
sw : Switch := NOT elt(repRec, switch)$LOOP
lab := newLabel()
bod := elt(repRec, body)$LOOP
append(getContinue lab,
append(getBody bod, fortFormatIfGoto(sw::O, lab)))
getWhile(whileRec : LOOP) : LS ==
sw := NOT elt(whileRec, switch)$LOOP
lab1 := newLabel()
lab2 := newLabel()
bod := elt(whileRec, body)$LOOP
ig : LS := fortFormatLabelledIfGoto(sw::O, lab1, lab2)
rl1 := [ig, getBody bod, getBody gotoJump(lab1),
getContinue lab2]$List(LS)
concat(rl1)$LS
getArrayAssign(rec : ARRAYASS) : LS ==
getfortarrayexp(rec.var, rec.rand, rec.ints2Floats?)
getAssign(rec : ASS) : LS ==
indices : L PIN := elt(rec, arrayIndex)$ASS
lhs := elt(rec, var)$ASS::O
if not(empty?(indices)) then
lhs := elt(lhs, map((ii : PIN) : O +-> ii::O, indices
)$ListFunctions2(PIN, O))
ass := elt(rec, rand)$ASS
ex := elt(ass, expr)$EXPRESSION
get_assignment(lhs, ex, elt(ass, ints2Floats?)$EXPRESSION)
getCond(rec : COND) : LS ==
expr := append(fortFormatIf(elt(rec, switch)$COND::O),
getBody elt(rec, thenClause)$COND)
elseBranch := elt(rec, elseClause)$COND
if not(operation(elseBranch) case Null) then
operation(elseBranch) case Conditional =>
expr := append(expr, getElseIf elseBranch)
expr := append(expr,
append(getStatement('ELSE::O, false),
getBody elseBranch))
append(expr, getStatement('ENDIF::O, false))
getComment(rec : COMMENT) : LS ==
[concat("C ", c)$String for c in rec]
getCall(rec : CALL) : LS ==
expr := concat("CALL ",rec)$String
#expr > 1320 => error "Fortran CALL too large"
fort_clean_lines([expr])
getFor(rec : FOR) : LS ==
rnge : SegmentBinding PIN := elt(rec, range)$FOR
increment : PIN := elt(rec, span)$FOR
lab : SingleInteger := newLabel()
declare!(variable rnge, fortranInteger())
expr : LS := fortFormatDo(variable rnge, (low(segment(rnge)))::O, _
(high(segment(rnge)))::O, increment::O, lab)
append(expr, append(getBody elt(rec, body)$FOR, getContinue(lab)))
getCode(f : %) : LS ==
opp : OP := operation f
rec : OPREC := code f
opp case Assignment => getAssign(rec.assignmentBranch)
opp case ArrayAssignment => getArrayAssign(rec.arrayAssignmentBranch)
opp case Conditional => getCond(rec.conditionalBranch)
opp case Return => getReturn(rec.returnBranch)
opp case Block => getBlock(rec.blockBranch)
opp case Comment => getComment(rec.commentBranch)
opp case Call => getCall(rec.callBranch)
opp case For => getFor(rec.forBranch)
opp case Continue => getContinue(rec.labelBranch)
opp case Goto => getGoto(rec.labelBranch)
opp case Repeat => getRepeat(rec.loopBranch)
opp case While => getWhile(rec.loopBranch)
opp case Save => getSave()
opp case Stop => getStop()
opp case Print => getPrint(rec.printBranch)
opp case Common => getCommon(rec.commonBranch)
error "Unsupported program construct."
printCode(f : %) : Void ==
displayLines(getCode f)
void()$Void
code (f : %) : OPREC ==
elt(f, data)$Rep
operation (f : %) : OP ==
elt(f, op)$Rep
common(name : Symbol, contents : List Symbol) : % ==
[["common"]$OP,[[name,contents]$COMMON]$OPREC]$Rep
stop() : % ==
[["stop"]$OP,["null"]$OPREC]$Rep
save() : % ==
[["save"]$OP,["null"]$OPREC]$Rep
printStatement(l : List O) : % ==
[["print"]$OP,[l]$OPREC]$Rep
comment(s : List String) : % ==
[["comment"]$OP,[s]$OPREC]$Rep
comment(s : String) : % ==
[["comment"]$OP,[list s]$OPREC]$Rep
forLoop(r : SegmentBinding PIN, body : %) : % ==
[["for"]$OP,[[r,(incr segment r)::PIN,body]$FOR]$OPREC]$Rep
forLoop(r : SegmentBinding PIN, increment : PIN, body : %) : % ==
[["for"]$OP,[[r,increment,body]$FOR]$OPREC]$Rep
gotoJump(l : SingleInteger) : % ==
[["goto"]$OP,[l]$OPREC]$Rep
continue(l : SingleInteger) : % ==
[["continue"]$OP,[l]$OPREC]$Rep
whileLoop(sw : Switch, b : %) : % ==
[["while"]$OP,[[sw,b]$LOOP]$OPREC]$Rep
repeatUntilLoop(sw : Switch, b : %) : % ==
[["repeat"]$OP,[[sw,b]$LOOP]$OPREC]$Rep
returns() : % ==
v := [false, 0::O]$EXPRESSION
[["return"]$OP,[[true,v]$RETURN]$OPREC]$Rep
returns(v : Expression MachineInteger) : % ==
[["return"]$OP,[[false,[false,v::O]$EXPRESSION]$RETURN]$OPREC]$Rep
returns(v : Expression MachineFloat) : % ==
[["return"]$OP,[[false,[false,v::O]$EXPRESSION]$RETURN]$OPREC]$Rep
returns(v : Expression MachineComplex) : % ==
[["return"]$OP,[[false,[false,v::O]$EXPRESSION]$RETURN]$OPREC]$Rep
returns(v : Expression Integer) : % ==
[["return"]$OP,[[false,[false,v::O]$EXPRESSION]$RETURN]$OPREC]$Rep
returns(v : Expression Float) : % ==
[["return"]$OP,[[false,[false,v::O]$EXPRESSION]$RETURN]$OPREC]$Rep
returns(v : Expression Complex Float) : % ==
[["return"]$OP,[[false,[false,v::O]$EXPRESSION]$RETURN]$OPREC]$Rep
block(l : List %) : % ==
[["block"]$OP,[l]$OPREC]$Rep
cond(sw : Switch, thenC : %) : % ==
[["conditional"]$OP,
[[sw,thenC,[["null"]$OP,["null"]$OPREC]$Rep]$COND]$OPREC]$Rep
cond(sw : Switch, thenC : %, elseC : %) : % ==
[["conditional"]$OP,[[sw,thenC,elseC]$COND]$OPREC]$Rep
coerce(f : %) : O ==
(f.op)::O
assign(v : Symbol, rhs : String) : % ==
[["assignment"]$OP,[[v, []::L PIN,[false,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, rhs : Matrix MachineInteger) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,false]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Matrix MachineFloat) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Matrix MachineComplex) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Vector MachineInteger) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,false]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Vector MachineFloat) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Vector MachineComplex) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Matrix Expression MachineInteger) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,false]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Matrix Expression MachineFloat) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Matrix Expression MachineComplex) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Vector Expression MachineInteger) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,false]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Vector Expression MachineFloat) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Vector Expression MachineComplex) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, index : L PIN, rhs : Expression MachineInteger) : % ==
[["assignment"]$OP,[[v,index,[false,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, index : L PIN, rhs : Expression MachineFloat) : % ==
[["assignment"]$OP,[[v,index,[true,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, index : L PIN, rhs : Expression MachineComplex) : % ==
[["assignment"]$OP,[[v,index,[true,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, rhs : Expression MachineInteger) : % ==
[["assignment"]$OP,[[v, []::L PIN,[false,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, rhs : Expression MachineFloat) : % ==
[["assignment"]$OP,[[v, []::L PIN,[true,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, rhs : Expression MachineComplex) : % ==
[["assignment"]$OP,[[v, []::L PIN,[true,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, rhs : Matrix Expression Integer) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,false]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Matrix Expression Float) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Matrix Expression Complex Float) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Vector Expression Integer) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,false]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Vector Expression Float) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, rhs : Vector Expression Complex Float) : % ==
[["arrayAssignment"]$OP,[[v,rhs::O,true]$ARRAYASS]$OPREC]$Rep
assign(v : Symbol, index : L PIN, rhs : Expression Integer) : % ==
[["assignment"]$OP,[[v,index,[false,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, index : L PIN, rhs : Expression Float) : % ==
[["assignment"]$OP,[[v,index,[true,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, index : L PIN, rhs : Expression Complex Float) : % ==
[["assignment"]$OP,[[v,index,[true,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, rhs : Expression Integer) : % ==
[["assignment"]$OP,[[v, []::L PIN,[false,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, rhs : Expression Float) : % ==
[["assignment"]$OP,[[v, []::L PIN,[true,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
assign(v : Symbol, rhs : Expression Complex Float) : % ==
[["assignment"]$OP,[[v, []::L PIN,[true,rhs::O]$EXPRESSION]$ASS]$OPREC]$Rep
call(s : String) : % ==
[["call"]$OP,[s]$OPREC]$Rep
)abbrev domain FORTRAN FortranProgram
++ Author: Mike Dewar
++ Date Created: October 1992
++ Basic Operations:
++ Related Constructors: FortranType, FortranCode, Switch
++ Also See:
++ AMS Classifications:
++ Keywords:
++ References:
++ Description: \spadtype{FortranProgram} allows the user to build and manipulate simple
++ models of FORTRAN subprograms. These can then be transformed into actual FORTRAN
++ notation.
FortranProgram(name, returnType, arguments, symbols) : Exports == Implement where
name : Symbol
returnType : Union(fst:FortranScalarType,void:"void")
arguments : List Symbol
symbols : SymbolTable
O ==> OutputForm
FC ==> FortranCode
EXPR ==> Expression
INT ==> Integer
CMPX ==> Complex
MINT ==> MachineInteger
MFLOAT ==> MachineFloat
MCMPLX ==> MachineComplex
REP ==> Record(localSymbols : SymbolTable, code : List FortranCode)
Exports ==> FortranProgramCategory with
coerce : FortranCode -> %
++ coerce(fc) \undocumented{}
coerce : List FortranCode -> %
++ coerce(lfc) \undocumented{}
coerce : REP -> %
++ coerce(r) \undocumented{}
coerce : EXPR MINT -> %
++ coerce(e) \undocumented{}
coerce : EXPR MFLOAT -> %
++ coerce(e) \undocumented{}
coerce : EXPR MCMPLX -> %
++ coerce(e) \undocumented{}
coerce : Equation EXPR MINT -> %
++ coerce(eq) \undocumented{}
coerce : Equation EXPR MFLOAT -> %
++ coerce(eq) \undocumented{}
coerce : Equation EXPR MCMPLX -> %
++ coerce(eq) \undocumented{}
coerce : EXPR INT -> %
++ coerce(e) \undocumented{}
coerce : EXPR Float -> %
++ coerce(e) \undocumented{}
coerce : EXPR CMPX Float -> %
++ coerce(e) \undocumented{}
coerce : Equation EXPR INT -> %
++ coerce(eq) \undocumented{}
coerce : Equation EXPR Float -> %
++ coerce(eq) \undocumented{}
coerce : Equation EXPR CMPX Float -> %
++ coerce(eq) \undocumented{}
Implement ==> add
Rep := REP
LS ==> List(String)
import from TheSymbolTable
import from FortranCode
import from FortranCodeTools
makeRep(b : List FortranCode) : % ==
construct(empty()$SymbolTable, b)$REP
codeFrom(u : %) : List FortranCode ==
elt(u::Rep, code)$REP
outputAsFortran(p : %) : Void ==
setLabelValue(25000::SingleInteger)$FC
-- Do this first to catch any extra type declarations:
tempName := 'FPTEMP
newSubProgram(tempName)
clear_used_intrinsics()
body : List LS := [getCode(l)$FortranCode for l in codeFrom(p)]
intrinsics : LS := get_used_intrinsics()
endSubProgram()
fortFormatHead(name, returnType, arguments)
printTypes(symbols)$SymbolTable
printTypes((p::Rep).localSymbols)$SymbolTable
printTypes(tempName)$TheSymbolTable
if not(empty?(intrinsics)) then
fortFormatTypeLines("INTRINSIC", intrinsics)
clearTheSymbolTable(tempName)
for expr in body repeat displayLines(expr)
dispStatement('END::OutputForm)
mkString(l : List Symbol) : String ==
unparse(convert(l)@InputForm)$InputForm
checkVariables(user : List Symbol, target : List Symbol) : Void ==
-- We don't worry about whether the user has subscripted the
-- variables or not.
setDifference(map(name$Symbol, user), target) ~= empty()$List(Symbol) =>
s1 : String := mkString(user)
s2 : String := mkString(target)
error ["Incompatible variable lists:", s1, s2]
void()$Void
coerce(u : EXPR MINT) : % ==
checkVariables(variables(u)$EXPR(MINT), arguments)
l : List(FC) := [assign(name, u)$FC, returns()$FC]
makeRep l
coerce(u : Equation EXPR MINT) : % ==
retractIfCan(lhs u)@Union(Kernel(EXPR MINT),"failed") case "failed" =>
error "left hand side is not a kernel"
vList : List Symbol := variables lhs u
#vList ~= #arguments =>
error "Incorrect number of arguments"
veList : List EXPR MINT := [w::EXPR(MINT) for w in vList]
aeList : List EXPR MINT := [w::EXPR(MINT) for w in arguments]
eList : List Equation EXPR MINT :=
[equation(w, v) for w in veList for v in aeList]
(subst(rhs u, eList))::%
coerce(u : EXPR MFLOAT) : % ==
checkVariables(variables(u)$EXPR(MFLOAT), arguments)
l : List(FC) := [assign(name, u)$FC, returns()$FC]
makeRep l
coerce(u : Equation EXPR MFLOAT) : % ==
retractIfCan(lhs u)@Union(Kernel(EXPR MFLOAT),"failed") case "failed" =>
error "left hand side is not a kernel"
vList : List Symbol := variables lhs u
#vList ~= #arguments =>
error "Incorrect number of arguments"
veList : List EXPR MFLOAT := [w::EXPR(MFLOAT) for w in vList]
aeList : List EXPR MFLOAT := [w::EXPR(MFLOAT) for w in arguments]
eList : List Equation EXPR MFLOAT :=
[equation(w, v) for w in veList for v in aeList]
(subst(rhs u, eList))::%
coerce(u : EXPR MCMPLX) : % ==
checkVariables(variables(u)$EXPR(MCMPLX), arguments)
l : List(FC) := [assign(name, u)$FC, returns()$FC]
makeRep l
coerce(u : Equation EXPR MCMPLX) : % ==
retractIfCan(lhs u)@Union(Kernel EXPR MCMPLX,"failed") case "failed"=>
error "left hand side is not a kernel"
vList : List Symbol := variables lhs u
#vList ~= #arguments =>
error "Incorrect number of arguments"
veList : List EXPR MCMPLX := [w::EXPR(MCMPLX) for w in vList]
aeList : List EXPR MCMPLX := [w::EXPR(MCMPLX) for w in arguments]
eList : List Equation EXPR MCMPLX :=
[equation(w, v) for w in veList for v in aeList]
(subst(rhs u, eList))::%
coerce(u : REP) : % ==
u@Rep
coerce(u : %) : OutputForm ==
coerce(name)$Symbol
coerce(c : List FortranCode) : % ==
makeRep c
coerce(c : FortranCode) : % ==
makeRep [c]
coerce(u : EXPR INT) : % ==
checkVariables(variables(u)$EXPR(INT), arguments)
l : List(FC) := [assign(name, u)$FC, returns()$FC]
makeRep l
coerce(u : Equation EXPR INT) : % ==
retractIfCan(lhs u)@Union(Kernel(EXPR INT),"failed") case "failed" =>
error "left hand side is not a kernel"
vList : List Symbol := variables lhs u
#vList ~= #arguments =>
error "Incorrect number of arguments"
veList : List EXPR INT := [w::EXPR(INT) for w in vList]
aeList : List EXPR INT := [w::EXPR(INT) for w in arguments]
eList : List Equation EXPR INT :=
[equation(w, v) for w in veList for v in aeList]
(subst(rhs u, eList))::%
coerce(u : EXPR Float) : % ==
checkVariables(variables(u)$EXPR(Float), arguments)
l : List(FC) := [assign(name, u)$FC, returns()$FC]
makeRep l
coerce(u : Equation EXPR Float) : % ==
retractIfCan(lhs u)@Union(Kernel(EXPR Float),"failed") case "failed" =>
error "left hand side is not a kernel"
vList : List Symbol := variables lhs u
#vList ~= #arguments =>
error "Incorrect number of arguments"
veList : List EXPR Float := [w::EXPR(Float) for w in vList]
aeList : List EXPR Float := [w::EXPR(Float) for w in arguments]
eList : List Equation EXPR Float :=
[equation(w, v) for w in veList for v in aeList]
(subst(rhs u, eList))::%
coerce(u : EXPR Complex Float) : % ==
checkVariables(variables(u)$EXPR(Complex Float), arguments)
l : List(FC) := [assign(name, u)$FC, returns()$FC]
makeRep l
coerce(u : Equation EXPR CMPX Float) : % ==
retractIfCan(lhs u)@Union(Kernel EXPR CMPX Float,"failed") case "failed"=>
error "left hand side is not a kernel"
vList : List Symbol := variables lhs u
#vList ~= #arguments =>
error "Incorrect number of arguments"
veList : List EXPR CMPX Float := [w::EXPR(CMPX Float) for w in vList]
aeList : List EXPR CMPX Float := [w::EXPR(CMPX Float) for w in arguments]
eList : List Equation EXPR CMPX Float :=
[equation(w, v) for w in veList for v in aeList]
(subst(rhs u, eList))::%
)abbrev domain M3D ThreeDimensionalMatrix
++ Author: William Naylor
++ Date Created: 20 October 1993
++ BasicFunctions:
++ Related Constructors: Matrix
++ Also See: PrimitiveArray
++ AMS Classification:
++ Keywords:
++ References:
++ Description:
++ This domain represents three dimensional matrices over a general object type
-- Currently unused.
ThreeDimensionalMatrix(R) : Exports == Implementation where
R : SetCategory
L ==> List
NNI ==> NonNegativeInteger
A1AGG ==> OneDimensionalArrayAggregate
ARRAY1 ==> OneDimensionalArray
PA ==> PrimitiveArray
INT ==> Integer
PI ==> PositiveInteger
Exports ==> HomogeneousAggregate(R) with
if R has Ring then
zeroMatrix : (NNI, NNI, NNI) -> %
++ zeroMatrix(i, j, k) create a matrix with all zero terms
identityMatrix : (NNI) -> %
++ identityMatrix(n) create an identity matrix
++ we note that this must be square
plus : (%, %) -> %
++ plus(x, y) adds two matrices, term by term
++ we note that they must be the same size
construct : (L L L R) -> %
++ construct(lll) creates a 3-D matrix from a List List List R lll
elt : (%, NNI, NNI, NNI) -> R
++ elt(x, i, j, k) extract an element from the matrix x
setelt! : (%, NNI, NNI, NNI, R) -> R
++ setelt!(x, i, j, k, s) (or x.i.j.k := s) sets a specific element of the array to some value of type R
coerce : (PA PA PA R) -> %
++ coerce(p) moves from the representation type
++ (PrimitiveArray PrimitiveArray PrimitiveArray R)
++ to the domain
coerce : % -> (PA PA PA R)
++ coerce(x) moves from the domain to the representation type
matrixConcat3D : (Symbol, %, %) -> %
++ matrixConcat3D(s, x, y) concatenates two 3-D matrices along a specified axis
matrixDimensions : % -> Vector NNI
++ matrixDimensions(x) returns the dimensions of a matrix
Implementation ==> (PA PA PA R) add