-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScope.java
1161 lines (1128 loc) · 43 KB
/
Scope.java
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
import java.util.HashSet;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
// This program 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 of the License, or
// (at your option) any later version.
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
// Dr.scient. Frank Thomas Tveter, 2016
public class Scope {
Scope parent=null;
//
public List<Program> programs = new ArrayList<Program>();
List<Block> blocks = new ArrayList<Block>();
// subroutines and functions in this scope only
HashMap<String,Function> functions = new HashMap<String,Function>();
HashMap<String,Subroutine> subroutines = new HashMap<String,Subroutine>();
// global list of unlinked subroutines and functions
HashMap<String,List<Call>> unlinkedFunctionCallLists; // only defined for global/top scope...
HashMap<String,List<Call>> unlinkedSubroutineCallLists; // only defined for global/top scope...
//
public Scope() { // global/top scope
this.parent=null;
unlinkedFunctionCallLists = new HashMap<String,List<Call>>();
unlinkedSubroutineCallLists = new HashMap<String,List<Call>>();
}
public Scope(Scope parent) {
this.parent=parent;
}
public void link(RegexNode nodeTree) {
//
HashMap<String,Subroutine> globalSubroutines = new HashMap<String,Subroutine>();
HashMap<String,Function> globalFunctions = new HashMap<String,Function>();
System.out.format("looping programs.\n");
// loop over programs
RegexNode programNode=nodeTree.getNode("$","*","Program"); // one level(filenode) below top-level
while (programNode != null) {
Program program =new Program(programNode);
programNode.setAttribute("dataStructure",program);
programs.add(program);
programNode=nodeTree.getNode("$","*","Program"); // one level(filenode) below top-legel
}
System.out.format("looping subroutines.\n");
// loop over subroutines
RegexNode subroutineNode=nodeTree.getNode("$","*","Subroutine"); // one level(filenode) below top-level
while (subroutineNode != null) {
Subroutine subroutine =new Subroutine(subroutineNode,null);
subroutineNode.setAttribute("dataStructure",subroutine);
globalSubroutines.put(subroutine.name.toLowerCase(),subroutine);
subroutineNode=nodeTree.getNode("$","*","Subroutine");
}
System.out.format("looping functions.\n");
// loop over functions
RegexNode functionNode=nodeTree.getNode("$","*","Function"); // one level(filenode) below top-level
while (functionNode != null) {
Function function =new Function(functionNode,null);
functionNode.setAttribute("dataStructure",function);
globalFunctions.put(function.name.toLowerCase(),function);
System.out.format("************Archiving global function: %s\n",function.name);
functionNode=nodeTree.getNode("$","*","Function");
}
System.out.format("looping block statements.\n");
// loop over block data statements
RegexNode blockNode=nodeTree.getNode("$","*","Block"); // one level(filenode) below top-level
while (blockNode != null) {
System.out.format("\nFound block data\n");
Block block =new Block(blockNode);
blocks.add(block);
blockNode.setAttribute("dataStructure",block);
blockNode=nodeTree.getNode("$","*","Block");
}
System.out.format("linking.\n");
addSubroutine(globalSubroutines);
addFunction(globalFunctions);
link();
link(programs);
clear();
System.out.format("Printing unlinked subroutines:\n%s\n",toString());
}
public void addSubroutine(HashMap<String,Subroutine> subroutines) {
for (Map.Entry<String, Subroutine> entry : subroutines.entrySet()) {
String name = entry.getKey();
Subroutine subroutine = entry.getValue();
addSubroutine(subroutine);
}
}
public void addSubroutine(Subroutine subroutine) {
// a subroutine may have many entries
for (Map.Entry<String, RegexNode> subroutineEntry : subroutine.entries.entrySet()) {
String name = subroutineEntry.getKey();
subroutines.put(name.toLowerCase(),subroutine);
}
}
public void addFunction(HashMap<String,Function> functions) {
for (Map.Entry<String, Function> entry : functions.entrySet()) {
String name = entry.getKey();
Function function = entry.getValue();
addFunction(function);
}
}
public void addFunction(Function function) {
// a function may have many entries
for (Map.Entry<String, RegexNode> functionEntry : function.entries.entrySet()) {
String name = functionEntry.getKey();
functions.put(name.toLowerCase(),function);
}
}
public void addUnlinkedFunction(Call functionCall) {
String name=functionCall.name;
List<Call> functionCallList = unlinkedFunctionCallLists.get(name);
if (functionCallList==null) {
functionCallList=new ArrayList<Call>();
//System.out.format("unlinkedFunctionCallLists Adding: %s\n",name);
unlinkedFunctionCallLists.put(name,functionCallList);
}
functionCallList.add(functionCall);
}
public void addUnlinkedSubroutine(Call subroutineCall) {
String name=subroutineCall.name;
List<Call> subroutineCallList = unlinkedSubroutineCallLists.get(name);
if (subroutineCallList==null) {
subroutineCallList=new ArrayList<Call>();
//System.out.format("unlinkedSubroutineCallLists Adding: %s\n",name);
unlinkedSubroutineCallLists.put(name,subroutineCallList);
}
subroutineCallList.add(subroutineCall);
}
public void link() {
// link scope functions
for (Map.Entry<String, Function> entry : functions.entrySet()) {
String name = entry.getKey();
Function function = entry.getValue();
// link global calls to this function
if (unlinkedFunctionCallLists != null) { // global scope
List<Call> functionCallList = unlinkedFunctionCallLists.get(name);
if (functionCallList!=null) {
for (Call functionCall: functionCallList) {
functionCall.setTargetProcedure(function);
}
unlinkedFunctionCallLists.remove(name);
}
}
// link calls in function
if (name.equals(function.name.toLowerCase())) {
function.link(this);
}
}
// link scope subroutines
for (Map.Entry<String, Subroutine> entry : subroutines.entrySet()) {
String name = entry.getKey();
Subroutine subroutine = entry.getValue();
// link global calls to this subroutine
if (unlinkedSubroutineCallLists != null) { // global scope
List<Call> subroutineCallList = unlinkedSubroutineCallLists.get(name);
if (subroutineCallList!=null) {
for (Call subroutineCall: subroutineCallList) {
subroutineCall.setTargetProcedure(subroutine);
}
unlinkedSubroutineCallLists.remove(name);
}
}
// link calls in subroutine
if (name.equals(subroutine.name.toLowerCase())) {
subroutine.link(this);
}
}
}
public void link(Program program) {
program.link(this);
}
public void link(List<Program> programs) {
for (Program program : programs) {
link(program);
}
}
public void clear() {
subroutines = new HashMap<String,Subroutine>();
functions = new HashMap<String,Function>();
}
public String toString() {
String s="";
if (unlinkedFunctionCallLists != null) {
for (Map.Entry<String,List<Call>> entry : unlinkedFunctionCallLists.entrySet()) {
String name = entry.getKey();
s=s+"=>:"+name+":\n";
List<Call> procedureCallList = entry.getValue();
if (procedureCallList != null) {
for (Call procedureCall: procedureCallList) {
s=s+procedureCall.toString(" ");
}
}
}
}
if (unlinkedSubroutineCallLists != null) {
for (Map.Entry<String,List<Call>> entry : unlinkedSubroutineCallLists.entrySet()) {
String name = entry.getKey();
s=s+"=>:"+name+":\n";
List<Call> procedureCallList = entry.getValue();
if (procedureCallList != null) {
for (Call procedureCall: procedureCallList) {
s=s+procedureCall.toString(" ");
}
}
}
}
return s;
}
public class Call {
String name=null; // name of procedure
RegexNode callerNode=null; // link to caller procedure
Procedure caller=null; // link to caller procedure
Procedure target=null; // link to procedure
public boolean bExternal=false; // is procedure declared?
public boolean bLocal=false; // is procedure declared?
public boolean bCalled=false; // is procedure called?
public Call(Procedure caller, String name) {
this.callerNode=null;
this.caller=caller;
this.name=name;
}
public Call(RegexNode callerNode, Procedure caller, String name) {
this.callerNode=callerNode;
this.caller=caller;
this.name=name;
}
public String getName(){
return name;
}
public void setTargetProcedure(Procedure procedure) {
this.target=procedure;
}
public String toString(String prefix) {
return toString(prefix,-1);
}
public String toString(String prefix, int printid) {
String l=prefix+"¤"+String.format("%-"+Math.max(10,name.length())+"s","\""+name+"\"");
String s="[";
if (bCalled) {
s=s+"c";
} else {
s=s+"-";
}
if (bLocal) {
s=s+"l";
} else if (target != null) {
s=s+"g";
} else {
s=s+"-";
}
if (bExternal) {
s=s+"e";
} else {
s=s+"-";
}
if (target instanceof Subroutine) {
s=s+"s";
} else if (target instanceof Function) {
s=s+"f";
} else {
s=s+"-";
}
s=s+"]";
s=String.format("%-20s %-5s",l,s);
if (target != null) {
s=s+"\n"+target.toString(prefix+" ",printid);
} else {
s=s+" => <no target>\n";
}
return s;
}
}
public class CommonBlock {
RegexNode declaration;
Variable[] variables;
}
private class CommonParent extends CommonBlock{
String Name;
CommonBlock[] commonBlocks;
Block block;
}
public class Block extends Procedure{
public Block(RegexNode node) {
super(node);
name=node.getText("$","Block","start","name");
System.out.format("%s Found block\n",name);
analyseDeclarations(node); // analyse declarations
}
}
public class Program extends Procedure {
public Program(RegexNode node) {
super(node);
name=node.getText("$","Program","start","name");
System.out.format("\n%s Found program\n",name);
analyseDeclarations(node); // analyse declarations
System.out.format("%s Contains\n",name);
analyseContains(node); // analyse contains section
System.out.format("%s Main\n",name);
analyseMain(node); // analyse main contents
}
}
public class Subroutine extends Procedure {
Integer narguments=0;
public Subroutine(RegexNode node, Procedure parent) {
super(node);
System.out.format("\nFound subroutine: %s\n",name);
importVariables(parent); // import variables from the parent procedure
System.out.format("%s Declarations\n",name);
analyseDeclarations(parent,node); // analyse declarations
System.out.format("%s Interface\n",name);
analyseInterface(parent,node); // analyse main contents
System.out.format("%s Contains\n",name);
analyseContains(parent,node); // analyse contains section
System.out.format("%s Main\n",name);
analyseMain(parent,node); // analyse main contents
declared=true;
}
}
public class Function extends Procedure {
Integer narguments=null;
public Function(RegexNode node, Procedure parent) {
super(node);
System.out.format("\nFound function: %s\n",name);
importVariables(parent); // import variables from the parent procedure
Variable variable=defineFunctionVariable(parent,node);
System.out.format("Defined local variable: %s\n",variable.toString());
System.out.format("%s Declarations\n",name);
analyseDeclarations(parent,node); // analyse declarations
System.out.format("%s Interface\n",name);
analyseInterface(parent,node); // analyse main contents
System.out.format("%s Contains\n",name);
analyseContains(parent,node); // analyse contains section
System.out.format("%s Main\n",name);
analyseMain(parent,node); // analyse main contents
narguments=(Integer) node.getAttribute("narguments","$","Function","start");
declared=true;
}
public Variable defineFunctionVariable(Procedure parent, RegexNode node) {
// define name of function as a local variable
Variable variable = new Variable(this.name);
node.setAttribute("dataStructure",variable);
localVariables.put(name.toLowerCase(),variable);
String type=node.getText("$","Function","start","type");
if (type == null && parent != null) {
type=parent.implicitList.getType(null,name);
}
variable.type=type;
return variable;
}
}
abstract class Procedure {
protected String name;
protected RegexNode declareNode;
protected HashMap<String,RegexNode> entries = new HashMap<String,RegexNode>();
//
protected ImplicitList implicitList=new ImplicitList();
protected String type=null;
protected Boolean declared=false;
//
protected HashMap<String,Variable> localVariables = new HashMap<String,Variable>();
protected HashMap<String,Subroutine> localSubroutines= new HashMap<String,Subroutine>();
protected HashMap<String,Function> localFunctions = new HashMap<String,Function>();
//
protected List<Call> subroutineCalls = new ArrayList<Call>();
protected List<Call> functionCalls = new ArrayList<Call>();
protected List<Call> calledBy = new ArrayList<Call>();
//
protected HashSet<String> externalFunctions = new HashSet<String>();
protected HashSet<String> declaredFunctions = new HashSet<String>();
//
protected HashMap<String,RegexNode> commonBlocks = new HashMap<String,RegexNode>();
//
int printid=-1;
//
public Procedure(RegexNode node) {
declareNode=node;
RegexNode entryNode=node.getNode("$","*","start");
node.getNodeReset("$","*","start");
name=entryNode.getText("$","*","name");
if (name == null) name ="null";
entries.put(name.toLowerCase(),entryNode);
entryNode=node.getNode("$","*","Main","entry");
while (entryNode != null) {
String ename=entryNode.getText("$","*","name");
if (ename == null) ename="null";
entries.put(ename.toLowerCase(),entryNode);
entryNode=node.getNode("$","*","Main","entry");
}
}
public String getName() {
return name;
}
public String toString() {
return toString("",-1);
}
public String toString(String prefix) {
return toString(prefix, -1);
}
public String toString(int printid) {
return toString("",printid);
}
public String toString(String prefix, int printid) {
String s="";
if (printid==-1 || printid!=this.printid) {
this.printid=printid;
if (subroutineCalls.size() > 0) {
s=s+prefix+"\""+name+"\"\n";
for (Call subroutineCall : subroutineCalls) {
String name = subroutineCall.getName();
s=s+subroutineCall.toString(prefix+" ",printid);
}
}
if (functionCalls.size() > 0) {
s=s+prefix+"\""+name+"\"\n";
for (Call functionCall : functionCalls) {
String name = functionCall.getName();
s=s+functionCall.toString(prefix+" ",printid);
}
}
} else if (printid == this.printid) {
s=s+prefix+" ...\n";
}
return s;
}
public void link(Scope scope) {
Scope localScope=new Scope(scope);
localScope.addFunction(localFunctions);
localScope.addSubroutine(localSubroutines);
// link local scope
localScope.link();
linkSubroutineCalls(scope);
linkFunctionCalls(scope);
}
public void linkFunctionCalls (Scope scope) {
// link function pointers
for (Call functionCall : functionCalls) {
String name = functionCall.getName();
// look for match
Scope currentScope=scope;
Scope topScope=currentScope;
while (currentScope != null) {
Function target=currentScope.functions.get(name);
if (target!=null) {
functionCall.target=target;
target.calledBy.add(functionCall);
currentScope=null;
} else {
topScope=currentScope;
currentScope=currentScope.parent;
}
}
// add to global unlinked list if no match was found...
if (functionCall.target == null & topScope != null) {
topScope.addUnlinkedFunction(functionCall);
}
}
}
public void linkSubroutineCalls (Scope scope) {
// link subroutine pointers
for (Call subroutineCall : subroutineCalls) {
String name = subroutineCall.getName();
// look for match
Scope currentScope=scope;
Scope topScope=currentScope;
while (currentScope != null) {
Subroutine target=currentScope.subroutines.get(name);
if (target!=null) {
subroutineCall.target=target;
target.calledBy.add(subroutineCall);
currentScope=null;
} else {
topScope=currentScope;
currentScope=currentScope.parent;
}
}
// add to global unlinked list if no match was found...
if (subroutineCall.target == null & topScope != null) {
topScope.addUnlinkedSubroutine(subroutineCall);
}
}
}
public void importVariables(Procedure parent) {
// link to variables in parent procedure...
if (parent != null ) {
for (Map.Entry<String, Variable> entry : parent.localVariables.entrySet()) {
String name = entry.getKey();
Variable variable = entry.getValue();
localVariables.put(name.toLowerCase(),variable);
}
}
}
public void analyseDeclarations (RegexNode procedureNode) {
analyseDeclarations(null,procedureNode);
}
public void analyseDeclarations (Procedure parent, RegexNode procedureNode) {
String prefix=" ";
RegexNode declarationsNode=procedureNode.getNode("$","*","Declarations"); // only analyse first occurence of Main
if (declarationsNode != null) {
// analyse implicit rules
RegexNode implicitNode=declarationsNode.getNode("$","Declarations","implicit");
while (implicitNode != null ) {
RegexNode typeNode=implicitNode.getNode("$","implicit","arguments","argument","type");
while (typeNode != null) {
String type=canonical(typeNode.getText("type","name"));
if (type.equals("none")) {
int from=-100000000;
int to=100000000;
implicitList.add(type,from,to);
System.out.format("%s[implicit] none...\n",prefix);
} else {
Integer arguments=(Integer) typeNode.getAttribute("narguments");
RegexNode rangeNode=typeNode.getNode("$","type","arguments","argument");
int from=0;
int to=0;
while (rangeNode != null) {
RegexNode variableNode=rangeNode.getNode("$","argument","variable"); // e.g. implicit real(a,b,c)
if (variableNode != null) {
from=(int) variableNode.getText("name").substring(0,1).charAt(0);
to=from;
} else { // argument may contain intrinsic "-"...
variableNode=rangeNode.getNode("$","argument","intrinsic","argument","variable"); // e.g. implicit real(a-b,b-c)
if (variableNode != null) {
from=(int) variableNode.getText("name").substring(0,1).charAt(0);
variableNode=rangeNode.getNode("$","argument","intrinsic","argument","variable");
if (variableNode != null) {
to=(int) variableNode.getText("name").substring(0,1).charAt(0);
} else {
to=from;
}
}
rangeNode.getNodeReset("$","argument","intrinsic","argument","variable");
}
rangeNode.getNodeReset("$","argument","variable");
implicitList.add(type,from,to);
System.out.format("%s[implicit] %s (%s - %s)\n",prefix,type,(char)from,(char)to);
rangeNode=typeNode.getNode("$","type","arguments","argument");
}
}
typeNode=implicitNode.getNode("$","implicit","arguments","argument","type");
}
implicitNode=declarationsNode.getNode("$","Declarations","implicit");
}
// analyse externals
RegexNode externalNode=declarationsNode.getNode("$","Declarations","external");
while (externalNode != null ) {
RegexNode variableNode=externalNode.getNode("$","external","arguments","argument","variable");
while (variableNode != null) {
String name=variableNode.getText("name");
name=canonical(name);
externalFunctions.add(name.toLowerCase());
variableNode=externalNode.getNode("$","external","arguments","argument","variable");
}
externalNode=declarationsNode.getNode("$","Declarations","external");
}
// analyse dimensions.
RegexNode dimensionNode=declarationsNode.getNode("$","Declarations","dimension");
while (dimensionNode != null ) {
RegexNode variableNode=dimensionNode.getNode("$","dimension","arguments","argument","variable");
while (variableNode != null) {
String name=variableNode.getText("name");
name=canonical(name);
Variable variable = new Variable(name);
variableNode.setAttribute("dataStructure",variable);
localVariables.put(name.toLowerCase(),variable);
variable.ndimensions=(Integer) variableNode.getAttribute("narguments");
Integer dim=0;
RegexNode argumentNode = variableNode.getNode("$","variable","arguments","argument");
while (argumentNode != null) {
dim=dim+1;
variable.dimensions.put(dim,argumentNode);
argumentNode = variableNode.getNode("$","variable","arguments","argument");
}
String type=implicitList.getType(parent,name);
variable.type=type;
System.out.format("%s[dimension] %s %d(%d) %s\n",prefix,name,variable.ndimensions,dim, type);
variableNode=dimensionNode.getNode("$","dimension","arguments","argument","variable");
}
dimensionNode=declarationsNode.getNode("$","Declarations","dimension");
}
// analyse declarations
RegexNode declarationNode=declarationsNode.getNode("$","Declarations","declaration");
while (declarationNode != null ) {
String type=declarationNode.getText("type");
RegexNode variableNode=declarationNode.getNode("$","declaration","variables","variable");
while (variableNode != null) {
String name=variableNode.getText("name");
name=canonical(name);
Variable variable=new Variable(name);
variableNode.setAttribute("dataStructure",variable);
localVariables.put(name.toLowerCase(),variable);
variable.type=type;
variable.ndimensions=(Integer) variableNode.getAttribute("narguments");
Integer dim=0;
RegexNode argumentNode = variableNode.getNode("$","variable","arguments","argument");
while (argumentNode != null) {
dim=dim+1;
variable.dimensions.put(dim,argumentNode);
argumentNode = variableNode.getNode("$","variable","arguments","argument");
}
variable.declaration=variableNode;
System.out.format("%s[declaration] %s %s\n",prefix,type,name);
variableNode=declarationNode.getNode("$","declaration","variables","variable");
}
declarationNode=declarationsNode.getNode("$","Declarations","declaration");
}
// analyse common blocks
RegexNode commonNode=declarationsNode.getNode("$","Declarations","common");
while (commonNode != null ) {
RegexNode variableNode=commonNode.getNode("$","common","variables","variable");
while (variableNode != null) {
String name=variableNode.getText("name");
name=canonical(name);
Variable variable=localVariables.get(name.toLowerCase());
if (variable == null) {
variable = new Variable(name);
localVariables.put(name.toLowerCase(),variable);
}
variableNode.setAttribute("dataStructure",variable);
variable.commonBlock=variableNode;
System.out.format("%s[common] %s\n",prefix,name);
variableNode=commonNode.getNode("$","common","arguments","argument","variable");
}
commonNode=declarationsNode.getNode("$","Declarations","common");
}
// analyse data statements
RegexNode dataNode=declarationsNode.getNode("$","Declarations","data");
while (dataNode != null ) {
RegexNode itemNode=dataNode.getNode("$","data","list","item");
while (itemNode != null) {
RegexNode valueNode=itemNode.getNode("$","item","expressions","expression");
RegexNode variableNode=itemNode.getNode("$","item","variables","variable");
while (variableNode != null) {
String name=variableNode.getText("name");
System.out.format("%s[data] %s\n",prefix,name);
name=canonical(name);
Variable variable=localVariables.get(name.toLowerCase());
if (variable == null) {
variable = new Variable(name);
localVariables.put(name.toLowerCase(),variable);
}
variableNode.setAttribute("dataStructure",variable);
Integer ndimensions=(Integer) itemNode.getAttribute("narguments");
if (ndimensions == null) ndimensions=0;
// ********************
if (ndimensions > 0) { // if we have array arguments, we may be trying to store a single element-value...
Integer dim=0;
RegexNode argumentNode = itemNode.getNode("$","variable","arguments","argument");
while (argumentNode != null) {
dim=dim+1;
variable.dimensions.put(dim,argumentNode);
argumentNode = variableNode.getNode("$","variable","arguments","argument");
} }
// *************** Here we assume 1 to 1 relationship between variable and value...
variable.data=valueNode;
valueNode=itemNode.getNode("$","item","expressions","expression");
// ********************
variableNode=itemNode.getNode("$","item","variables","variable");
}
itemNode=dataNode.getNode("$","data","list","item");
}
dataNode=declarationsNode.getNode("$","Declarations","data");
}
}
procedureNode.getNodeReset("$","*","Declarations");
}
public void analyseInterface(RegexNode node) {
analyseInterface(null,node);
}
public void analyseInterface(Procedure parent, RegexNode procedureNode) {
String prefix=" ";
RegexNode startNode=procedureNode.getNode("$","*","start");
while (startNode != null) {
Integer narguments=(Integer) startNode.getAttribute("narguments");
System.out.format("%s[arguments] %s %dD (",prefix,name,narguments);
int narg=0;
RegexNode variableNode=startNode.getNode("$","start","arguments","argument","variable");
while (variableNode != null) {
String vname=variableNode.getText("name");
narg++;
Variable variable=localVariables.get(vname.toLowerCase());
if (variable == null) {
variable = new Variable(vname);
String type=implicitList.getType(parent, name);
}
variableNode.setAttribute("dataStructure",variable);
localVariables.put(vname.toLowerCase(),variable);
variable.setArgumentNumber(narg,variableNode);
System.out.format(" %s ",vname);
variableNode=startNode.getNode("$","start","arguments","argument","variable");
}
type=startNode.getText("$","start","type");
System.out.format(")\n");
startNode=procedureNode.getNode("$","*","start");
}
}
public void analyseMain (RegexNode procedureNode) {
analyseMain (null,procedureNode);
}
public void analyseMain (Procedure parent, RegexNode procedureNode) {
String prefix=" ";
RegexNode mainNode=procedureNode.getNode("$","*","Main"); // only analyse first occurence of Main
if (mainNode != null) {
// analyse all variables
RegexNode variableNode=mainNode.getNode("$","...","variable");
while (variableNode != null) {
// get variable name, and number of dimensions
String name=variableNode.getText("name");
if (name == null) {
System.out.format("Invalid structure: a variable is missing name attribute: %s\n",
variableNode.toString());
}
Integer ndimensions=(Integer) variableNode.getAttribute("narguments");
//
name=canonical(name);
Variable variable=localVariables.get(name.toLowerCase());
boolean processed=false;
// check for allocation statement (must be declared)
if (! processed) {
RegexNode node=variableNode.getParent("allocate","variables","variable");
if (node != null ) {
System.out.format("%s[allocate] %s",prefix,name);
if (variable == null) {
System.out.format(" **not declared**");
} else {
variable.assigned=true;
}
System.out.format("\n");
processed=true;
};
}
// check for deallocation statement (must be declared)
if (! processed) {
RegexNode node=variableNode.getParent("deallocate","variables","variable");
if (node != null ) {
System.out.format("%s[deallocate] %s",prefix,name);
if (variable == null) {
System.out.format(" **not declared**");
} else {
variable.assigned=true;
}
System.out.format("\n");
processed=true;
};
}
// check for call statement (must be declared or implicit)
if (! processed) {
RegexNode node=variableNode.getParent("call","arguments","argument","variable");
if (node != null ) {
System.out.format("%s[call] %s",prefix,name);
if (ndimensions != null) {
System.out.format(" %dD",ndimensions);
}
if (variable == null) { // implicit variable or function
if (externalFunctions.contains(name.toLowerCase()) ||
declaredFunctions.contains(name.toLowerCase())) { // check if declared (external/contains) function
Call functionCall=new Call(variableNode,this,name);
functionCall.bCalled=true;
functionCalls.add(functionCall);
variableNode.setAttribute("dataStructure",functionCall);
System.out.format(" **existing function**");
} else {
if (ndimensions == null) { // no dimensions: implicit variable
variable=createVariable(name,variableNode); // implicit variable
variableNode.setAttribute("dataStructure",variable);
localVariables.put(name.toLowerCase(),variable);
variable.type=implicitList.getType(parent,name);
variable.calls.add(variableNode);
} else { // must be an unknown function
Call functionCall=new Call(variableNode,this,name);
functionCall.bCalled=true;
functionCalls.add(functionCall);
variableNode.setAttribute("dataStructure",functionCall);
System.out.format(" function(%d)",ndimensions);
System.out.format(" **new function**");
};
}
} else { // this is a local variable
variableNode.setAttribute("dataStructure",variable);
variable.calls.add(variableNode);
}
System.out.format("\n");
processed=true;
};
}
// check for write statement (must be declared or implicit)
if (! processed) {
RegexNode node=variableNode.getParent("write","arguments","argument","variable");
if (node != null ) {
System.out.format("%s[write] %s",prefix,name);
if (variable == null) {
if (externalFunctions.contains(name.toLowerCase()) ||
declaredFunctions.contains(name.toLowerCase())) { // check if declared (external/contains) function
// found declared function
Call functionCall=new Call(variableNode,this,name);
functionCall.bCalled=true;
functionCalls.add(functionCall);
variableNode.setAttribute("dataStructure",functionCall);
} else {
if (ndimensions == null) { // no dimensions: implicit variable
variable=createVariable(name,variableNode); // implicit variable
variableNode.setAttribute("dataStructure",variable);
localVariables.put(name.toLowerCase(),variable);
variable.type=implicitList.getType(parent,name);
} else { // must be an unknown function
Call functionCall=new Call(variableNode,this,name);
functionCall.bCalled=true;
functionCalls.add(functionCall);
variableNode.setAttribute("dataStructure",functionCall);
};
}
} else {
// found declared variable
variableNode.setAttribute("dataStructure",variable);
}
System.out.format("\n");
processed=true;
};
}
// check for read statement (must be declared or implicit)
if (! processed) {
RegexNode node=variableNode.getParent("read","expressions","expression","variable");
if (node != null ) {
System.out.format("%s[read] %s",prefix,name);
if (variable == null) {
if (ndimensions == null) {
variable=createVariable(name,variableNode);
variableNode.setAttribute("dataStructure",variable);
localVariables.put(name.toLowerCase(),variable);
variable.type=implicitList.getType(parent,name);
variable.assigned=true;
} else { // "variable" is a function...
System.out.format(" ** error: can not write to function **");
};
} else {
variableNode.setAttribute("dataStructure",variable);
variable.assigned=true;
}
System.out.format("\n");
processed=true;
};
}
// check for assignment statement (must be declared or implicit)
if (! processed) {
RegexNode node=variableNode.getParent("assignment","variable");
if (node != null ) {
System.out.format("%s[assignment] %s",prefix,name);
if (variable == null) {
if (ndimensions == null) {
variable=createVariable(name,variableNode);
variableNode.setAttribute("dataStructure",variable);
localVariables.put(name.toLowerCase(),variable);
variable.type=implicitList.getType(parent,name);
variable.assigned=true;
} else {
System.out.format(" ** error: can not write to function **");
}
} else {
variableNode.setAttribute("dataStructure",variable);
variable.assigned=true;
}
System.out.format("\n");
processed=true;
};
}
// check for do statement (must be declared or implicit)
if (! processed) {
RegexNode node=variableNode.getParent("do","variable");
if (node != null ) {
System.out.format("%s[do] %s",prefix,name);
if (variable == null) {
if (ndimensions == null) {
variable=createVariable(name,variableNode);
variableNode.setAttribute("dataStructure",variable);
localVariables.put(name.toLowerCase(),variable);
variable.type=implicitList.getType(parent,name);
variable.assigned=true;
} else {
System.out.format(" ** error: can not use function as iterator **");
}
} else {
variableNode.setAttribute("dataStructure",variable);
variable.assigned=true;
}
System.out.format("\n");
processed=true;
};
}
// if not processed so far, this is "access only" variable
if ( ! processed) {
if (variable == null) {
if (externalFunctions.contains(name.toLowerCase()) ||
declaredFunctions.contains(name.toLowerCase())) { // check if declared (external/contains) function
// found declared function
Call functionCall=new Call(variableNode,this,name);
functionCall.bCalled=true;
functionCalls.add(functionCall);
variableNode.setAttribute("dataStructure",functionCall);
} else {
if (ndimensions == null) { // no dimensions: implicit variable
variable=createVariable(name,variableNode); // implicit variable
variableNode.setAttribute("dataStructure",variable);
localVariables.put(name.toLowerCase(),variable);
variable.type=implicitList.getType(parent,name);
} else { // must be an unknown function
Call functionCall=new Call(variableNode,this,name);
functionCall.bCalled=true;
functionCalls.add(functionCall);
variableNode.setAttribute("dataStructure",functionCall);
};
}
System.out.format("%s[undefined] %s",prefix,name);
} else {
variableNode.setAttribute("dataStructure",variable);
System.out.format("%s[peeked] %s",prefix,name);
};
System.out.format("\n");
}
// check how variable is used
// access to value
//System.out.format(" %s\n",variable.toString());
variableNode=mainNode.getNode("$","...","variable");
}
// analyse subroutine calls
RegexNode callNode=mainNode.getNode("$","Main","call");
while (callNode != null) {
// get call name, and number of dimensions
String name=callNode.getText("name");
Integer narguments=(Integer) callNode.getAttribute("narguments");
name=canonical(name);
Call subroutineCall=new Call(callNode,this,name);
subroutineCalls.add(subroutineCall);
subroutineCall.bCalled=true;
callNode.setAttribute("dataStructure",subroutineCall);
callNode=mainNode.getNode("$","Main","call");
}
}
procedureNode.getNodeReset("$","*","Main");
}
public void analyseContains (RegexNode procedureNode) {
analyseContains(null,procedureNode);
}
public void analyseContains (Procedure parent, RegexNode procedureNode) {
RegexNode containsNode=procedureNode.getNode("$","*","Contains");
while (containsNode != null) {
// analyse subroutines in Contains statement
RegexNode subroutineNode=containsNode.getNode("$","*","Subroutine");
while (subroutineNode != null) {
String name=canonical(subroutineNode.getText("$","Subroutine","start","name"));
if (name == null) {
System.out.format("Strange name s=%s\n",subroutineNode.toString(2));
}
System.out.format("Here s=%s\n",name);
Subroutine subroutine=localSubroutines.get(name.toLowerCase());
if (subroutine == null) {
System.out.format("Here A1 s=%s\n",name);
subroutine = new Subroutine(subroutineNode,this);
System.out.format("Here A2 s=%s\n",name);