-
Notifications
You must be signed in to change notification settings - Fork 0
/
trans_model.adb
12278 lines (10529 loc) · 362 KB
/
trans_model.adb
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
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
with trans; use trans; --for the token types
with ada.characters.handling; use ada.characters.handling; --for To_Upper()
with ada.strings; use ada.strings; --for Backward, Forward
with ada.strings.fixed; use ada.strings.fixed; --for Index()
with ada.strings.unbounded; use ada.strings.unbounded;
with lego; --for Ada/Mindstorms builtins
with lego_builtins; --"
with type_membership_template; --for type membership generics below
with lists_generic;
package body trans_model is
--generic subprogram instantiations for Ada/Mindstorms type membership checking
--all of form Is_This_Type(This : String) return boolean
function Is_Ada_Mindstorms_Procedure is
new Type_Membership_Template(Discrete_Type => Lego.Ada_Mindstorms_Procedure);
function Is_Sensor_Port is
new Type_Membership_Template(Discrete_Type => Lego.Sensor_Port);
function Is_Output_Port is
new Type_Membership_Template(Discrete_Type => Lego.Output_Port);
function Is_Output_Mode is
new Type_Membership_Template(Discrete_Type => Lego.Output_Mode);
function Is_Output_Direction is
new Type_Membership_Template(Discrete_Type => Lego.Output_Direction);
function Is_Configuration is
new Type_Membership_Template(Discrete_Type => Lego.Configuration);
function Is_Sensor_Type is
new Type_Membership_Template(Discrete_Type => Lego.Sensor_Type);
function Is_Sound is
new Type_Membership_Template(Discrete_Type => Lego.Sound);
function Is_Sensor_Mode is
new Type_Membership_Template(Discrete_Type => Lego.Sensor_Mode);
function Is_Timer is
new Type_Membership_Template(Discrete_Type => Lego.Timer);
function Is_Transmitter_Power_Setting is
new Type_Membership_Template(Discrete_Type => Lego.Transmitter_Power_Setting);
--global variables, so sue me
Proc_Nesting_Level : Integer := 0; --current level of procedure nesting
Indent_Level : Integer := 0; --current indent level, so translated code looks nice
--constants
Max_NQC_Global_Variables : constant integer := 32; --comes directly from NQC
--need these for user type declarations (arrays)
--in a future release, may want to replace these with hash tables
package Type_Decl_Lists is new Lists_Generic(type_decl_nonterminal);
Global_Type_Decls : Type_Decl_Lists.list; --type declarations that are global to the program
Local_Type_Decls : Type_Decl_Lists.list; --type declarations local to the current procedure
package Object_Decl_Lists is new Lists_Generic(object_decl_nonterminal);
Global_Object_Decls : Object_Decl_Lists.list; --object declarations that are global to the program
Local_Object_Decls : Object_Decl_Lists.list; --object declarations local to the current procedure
function Is_Global_Variable(Id_String: in Unbounded_String) return boolean is
Current : Object_Decl_Lists.Position;
Decl : Object_Decl_nonterminal;
Id_Tkn : Parseable_Token_Ptr := new Parseable_Token;
begin
Current := Object_Decl_Lists.First(Global_Object_Decls);
while not Object_Decl_Lists.IsPastEnd(Global_Object_Decls, Current) loop
Decl := Object_Decl_Lists.Retrieve(Global_Object_Decls, Current);
--call to In_Id_List below requires a token, not a string
Id_Tkn.Line := 0;
Id_Tkn.Column := 0; --not worth it to set these to correct values
Id_Tkn.Token_String := new String'(To_String(Id_String));
Id_Tkn.Token_Type := Identifier_Token;
--Is this the right one?
if In_Id_List(Decl.def_id_s_part.all, Id_Tkn) and not Is_Constant_Declaration(Decl) then
return true;
end if;
Object_Decl_Lists.GoAhead(Global_Object_Decls, Current);
end loop;
return false;
end Is_Global_Variable;
function Is_Global_Constant(Id_String: in Unbounded_String) return boolean is
Current : Object_Decl_Lists.Position;
Decl : Object_Decl_nonterminal;
Id_Tkn : Parseable_Token_Ptr := new Parseable_Token;
begin
Current := Object_Decl_Lists.First(Global_Object_Decls);
while not Object_Decl_Lists.IsPastEnd(Global_Object_Decls, Current) loop
Decl := Object_Decl_Lists.Retrieve(Global_Object_Decls, Current);
--call to In_Id_List below requires a token, not a string
Id_Tkn.Line := 0;
Id_Tkn.Column := 0; --not worth it to set these to correct values
Id_Tkn.Token_String := new String'(To_String(Id_String));
Id_Tkn.Token_Type := Identifier_Token;
--Is this the right one?
if In_Id_List(Decl.def_id_s_part.all, Id_Tkn) then
return true;
end if;
Object_Decl_Lists.GoAhead(Global_Object_Decls, Current);
end loop;
return false;
end Is_Global_Constant;
function Is_Local_Variable(Id_String: in Unbounded_String) return boolean is
Current : Object_Decl_Lists.Position;
Decl : Object_Decl_nonterminal;
Id_Tkn : Parseable_Token_Ptr := new Parseable_Token;
begin
Current := Object_Decl_Lists.First(Local_Object_Decls);
while not Object_Decl_Lists.IsPastEnd(Local_Object_Decls, Current) loop
Decl := Object_Decl_Lists.Retrieve(Local_Object_Decls, Current);
--call to In_Id_List below requires a token, not a string
Id_Tkn.Line := 0;
Id_Tkn.Column := 0; --not worth it to set these to correct values
Id_Tkn.Token_String := new String'(To_String(Id_String));
Id_Tkn.Token_Type := Identifier_Token;
--Is this the right one?
if In_Id_List(Decl.def_id_s_part.all, Id_Tkn) and not Is_Constant_Declaration(Decl) then
return true;
end if;
Object_Decl_Lists.GoAhead(Local_Object_Decls, Current);
end loop;
return false;
end Is_Local_Variable;
function Is_Local_Constant(Id_String: in Unbounded_String) return boolean is
Current : Object_Decl_Lists.Position;
Decl : Object_Decl_nonterminal;
Id_Tkn : Parseable_Token_Ptr := new Parseable_Token;
begin
Current := Object_Decl_Lists.First(Local_Object_Decls);
while not Object_Decl_Lists.IsPastEnd(Local_Object_Decls, Current) loop
Decl := Object_Decl_Lists.Retrieve(Local_Object_Decls, Current);
--call to In_Id_List below requires a token, not a string
Id_Tkn.Line := 0;
Id_Tkn.Column := 0; --not worth it to set these to correct values
Id_Tkn.Token_String := new String'(To_String(Id_String));
Id_Tkn.Token_Type := Identifier_Token;
--Is this the right one?
if In_Id_List(Decl.def_id_s_part.all, Id_Tkn) then
return true;
end if;
Object_Decl_Lists.GoAhead(Local_Object_Decls, Current);
end loop;
return false;
end Is_Local_Constant;
-------------------
-- Number_Of_Vars --
-------------------
function Number_Of_Vars(decl_list : Object_Decl_Lists.list) return integer is
Current : Object_Decl_Lists.Position;
Decl : object_decl_nonterminal;
Total : integer := 0;
begin
Current := Object_Decl_Lists.First(decl_list);
while not Object_Decl_Lists.IsPastEnd(decl_list, Current) loop
Decl := Object_Decl_Lists.Retrieve(decl_list, Current);
Total := Total + Number_Of_Vars(Decl);
Object_Decl_Lists.GoAhead(decl_list, Current);
end loop;
return Total;
end Number_Of_Vars;
procedure Inc_Indent is
begin
Indent_Level := Indent_Level+1;
end Inc_Indent;
procedure Dec_Indent is
begin
Indent_Level := Indent_Level-1;
end Dec_Indent;
procedure my_new_line is
begin
new_line;
for i in 1..Indent_Level loop
put(" ");
end loop;
end my_new_line;
procedure put_with_space(Item: String) is
begin
put(Item);
put(" ");
end put_with_space;
procedure put_with_space(Item: Unbounded_String) is
begin
put(To_String(Item));
put(" ");
end put_with_space;
procedure put_with_space(Item : Integer) is
begin
put(Item => Item, Width => 0);
put(" ");
end put_with_space;
Input_Filename : Unbounded_String; --need this for error messages
--called from main program
procedure Set_Input_Filename(Name : in Unbounded_String) is
begin
--strip off everything but the trailing component of the full path
Input_Filename := To_Unbounded_String(Slice(
Source => Name,
Low => Index(Source => Name, Pattern => "\", Going => Backward)+1,
High => Length(Name)));
end Set_Input_Filename;
procedure Error_Msg_Prefix(Line, Column : in integer) is
begin
put(File => Standard_Error, Item => To_String(Input_Filename));
put(File => Standard_Error, Item => ":");
put(File => Standard_Error, Width => 0, Item => Line);
put(File => Standard_Error, Item => ":");
put(File => Standard_Error, Width => 0, Item => Column);
end Error_Msg_Prefix;
--Should have an entry for every Ada/Mindstorms identifier, even if
--it's just passed through. Must be kept consistent with lego.ads.
procedure Translate_Ada_Mindstorms_Identifier(This : String) is
ID_String : Unbounded_String := To_Unbounded_String(To_Upper(This));
begin
if ID_String = "ADD_TO_DATALOG" then
put("AddToDatalog");
elsif ID_String = "CLEAR_MESSAGE_BUFFER" then
put("ClearMessage");
elsif ID_String = "CLEAR_SENSOR" then
put("ClearSensor");
elsif ID_String = "CLEAR_SOUND_BUFFER" then
put("ClearSound");
elsif ID_String = "CLEAR_TIMER" then
put("ClearTimer");
elsif ID_String = "CONFIG_SENSOR" then
put("SetSensor");
elsif ID_String = "CREATE_DATALOG" then
put("CreateDatalog");
elsif ID_String = "DISPLAY_VALUE" then
put("SetUserDisplay");
elsif ID_String = "GET_BOOLEAN_SENSOR_VALUE" then
put("SensorValueBool");
elsif ID_String = "GET_GLOBAL_OUTPUT_MODE" then
put("GlobalOutputStatus");
elsif ID_String = "GET_MESSAGE" then
put("Message");
elsif ID_String = "GET_OUTPUT_MODE" then
put("OutputStatus");
elsif ID_String = "GET_RAW_SENSOR_VALUE" then
put("SensorValueRaw");
elsif ID_String = "GET_SENSOR_MODE" then
put("SensorMode");
elsif ID_String = "GET_SENSOR_TYPE" then
put("SensorType");
elsif ID_String = "GET_SENSOR_VALUE" then
put(""); --Get_Sensor_Value(Sensor_x) becomes (SENSOR_x) in NQC
elsif ID_String = "GET_TIMER" then
put("Timer");
elsif ID_String = "GET_WATCH" then
put("Watch");
elsif ID_String = "OUTPUT_FLOAT" then
put("Float");
elsif ID_String = "OUTPUT_FORWARD" then
put("Fwd");
elsif ID_String = "OUTPUT_OFF" then
put("Off");
elsif ID_String = "OUTPUT_ON" then
put("On");
elsif ID_String = "OUTPUT_ON_FOR" then
put("OnFor");
elsif ID_String = "OUTPUT_ON_FORWARD" then
put("OnFwd");
elsif ID_String = "OUTPUT_ON_REVERSE" then
put("OnRev");
elsif ID_String = "OUTPUT_REVERSE" then
put("Rev");
elsif ID_String = "OUTPUT_TOGGLE" then
put("Toggle");
elsif ID_String = "OUTPUT_POWER" then
put("SetPower");
elsif ID_String = "PLAY_SOUND" then
put("PlaySound");
elsif ID_String = "PLAY_TONE" then
put("PlayTone");
elsif ID_String = "RANDOM" then
put("Random");
elsif ID_String = "SELECT_DISPLAY" then
put("SelectDisplay");
elsif ID_String = "SEND_MESSAGE" then
put("SendMessage");
elsif ID_String = "SENSOR_1" then
put("SENSOR_1");
elsif ID_String = "SENSOR_2" then
put("SENSOR_2");
elsif ID_String = "SENSOR_3" then
put("SENSOR_3");
elsif ID_String = "SET_GLOBAL_OUTPUT_DIRECTION" then
put("SetGlobalDirection");
elsif ID_String = "SET_GLOBAL_OUTPUT_MODE" then
put("SetGlobalOutput");
elsif ID_String = "SET_MAX_POWER" then
put("SetMaxPower");
elsif ID_String = "SET_OUTPUT_DIRECTION" then
put("SetDirection");
elsif ID_String = "SET_OUTPUT_MODE" then
put("SetOutput");
elsif ID_String = "SET_SENSOR" then
put("SetSensor");
elsif ID_String = "SET_SENSOR_MODE" then
put("SetSensorMode");
elsif ID_String = "SET_SENSOR_TYPE" then
put("SetSensorType");
elsif ID_String = "SET_TRANSMITTER_POWER_LEVEL" then
put("SetTxPower");
elsif ID_String = "SPEAKER_OFF" then
put("MuteSound");
elsif ID_String = "SPEAKER_ON" then
put("UnmuteSound");
elsif ID_String = "STOP_ALL_TASKS" then
put("StopAllTasks");
elsif ID_String = "WAIT" then
put("Wait");
elsif Is_Configuration(To_String(ID_String)) then --sensor configuration type, replace "Config_" with "Sensor_"
put(To_String("SENSOR_" &
To_Unbounded_String(Slice(Source => ID_String, Low => Index(Source => ID_String, Pattern => "_")+1,
High => Length(ID_String)))));
elsif Is_Sensor_Type(To_String(ID_String)) then --sensor hardware type
put(To_String("SENSOR_" & ID_String));
elsif Is_Sound(To_String(ID_String)) then --sound type
put(To_String("SOUND_" & ID_String));
elsif Is_Sensor_Mode(To_String(ID_String)) then --mode type
put(To_String("SENSOR_" & ID_String));
elsif ID_String = "OUTPUT_A" then --output port designators
put("OUT_A");
elsif ID_String = "OUTPUT_B" then
put("OUT_B");
elsif ID_String = "OUTPUT_C" then
put("OUT_C");
elsif ID_String = "OUTPUT_MODE_ON" then --output modes
put("OUT_ON");
elsif ID_String = "OUTPUT_MODE_OFF" then
put("OUT_OFF");
elsif ID_String = "OUTPUT_MODE_FLOAT" then
put("OUT_FLOAT");
elsif ID_String = "OUTPUT_DIRECTION_FORWARD" then --output directions
put("OUT_FWD");
elsif ID_String = "OUTPUT_DIRECTION_REVERSE" then
put("OUT_REV");
elsif ID_String = "OUTPUT_DIRECTION_TOGGLE" then
put("OUT_TOGGLE");
elsif ID_String = "SET_WATCH" then
put("SetWatch");
elsif ID_String = "TIMER_0" then --timers
put("0");
elsif ID_String = "TIMER_1" then
put("1");
elsif ID_String = "TIMER_2" then
put("2");
elsif ID_String = "TIMER_3" then
put("3");
elsif ID_String = "POWER_LOW" then --power constants
put_with_space(Integer(Lego.Power_Low));
elsif ID_String = "POWER_HALF" then
put_with_space(Integer(Lego.Power_Half));
elsif ID_String = "POWER_HIGH" then
put_with_space(Integer(Lego.Power_High));
elsif ID_String = "TRUE" then --boolean constants, reserved words in NQC so must be forced to lower case
put_with_space("true");
elsif ID_String = "FALSE" then
put_with_space("false");
elsif ID_String = "DISPLAY_WATCH" then --display constants
put_with_space(Lego.Display_Watch);
elsif ID_String = "DISPLAY_SENSOR_1" then
put_with_space(Lego.Display_Sensor_1);
elsif ID_String = "DISPLAY_SENSOR_2" then
put_with_space(Lego.Display_Sensor_2);
elsif ID_String = "DISPLAY_SENSOR_3" then
put_with_space(Lego.Display_Sensor_3);
elsif ID_String = "DISPLAY_OUTPUT_A" then
put_with_space(Lego.Display_Output_A);
elsif ID_String = "DISPLAY_OUTPUT_B" then
put_with_space(Lego.Display_Output_B);
elsif ID_String = "DISPLAY_OUTPUT_C" then
put_with_space(Lego.Display_Output_C);
elsif ID_String = "DISPLAY_USER" then
put_with_space(Lego.Display_User);
elsif ID_String = "TRANSMITTER_POWER_LOW" then
put_with_space("TX_POWER_LO");
elsif ID_String = "TRANSMITTER_POWER_HIGH" then
put_with_space("TX_POWER_HI");
end if;
end Translate_Ada_Mindstorms_Identifier;
--must be kept consistent with power constant names in lego.ads
function Is_Power_Constant(This: in String) return boolean is
ID_String : Unbounded_String := To_Unbounded_String(To_Upper(This));
begin
if ID_String = "POWER_LOW" or
ID_String = "POWER_HALF" or
ID_String = "POWER_HIGH" then
return true;
else
return false;
end if;
end Is_Power_Constant;
--must be kept consistent with display constant names in lego.ads
function Is_Display_Constant(This: in String) return boolean is
ID_String : Unbounded_String := To_Unbounded_String(To_Upper(This));
begin
if ID_String = "DISPLAY_WATCH" or
ID_String = "DISPLAY_SENSOR_1" or
ID_String = "DISPLAY_SENSOR_2" or
ID_String = "DISPLAY_SENSOR_3" or
ID_String = "DISPLAY_OUTPUT_A" or
ID_String = "DISPLAY_OUTPUT_B" or
ID_String = "DISPLAY_OUTPUT_C" or
ID_String = "DISPLAY_USER" then
return true;
else
return false;
end if;
end Is_Display_Constant;
function Is_Boolean_Constant(This : in String) return boolean is
ID_String : Unbounded_String := To_Unbounded_String(To_Upper(This));
begin
if ID_String = "TRUE" or
ID_String = "FALSE" then
return true;
else
return false;
end if;
end Is_Boolean_Constant;
function Is_Ada_Mindstorms_Identifier(This : in String) return boolean is
begin
--call the generic subprograms instantiated at the beginning of this file
if Is_Ada_Mindstorms_Procedure(This) or
Is_Sensor_Port(This) or
Is_Sensor_Mode(This) or
Is_Sensor_Type(This) or
Is_Output_Port(This) or
Is_Output_Mode(This) or
Is_Output_Direction(This) or
Is_Configuration(This) or
Is_Sound(This) or
Is_Timer(This) or
Is_Transmitter_Power_Setting(This) then
return true;
elsif Is_Power_Constant(This) then --power constants treated specially
return true;
elsif Is_Display_Constant(This) then --display constants treated specially
return true;
elsif Is_Boolean_Constant(This) then --boolean constants treated specially
return true;
else
return false;
end if;
end Is_Ada_Mindstorms_Identifier;
function Is_In_Proc_Table(This : in String; Arity : in Integer) return boolean is
begin
for i in 1..Lego_Builtins.Max_Procedures loop
if (To_Upper(This) = To_Upper(To_String((Lego_Builtins.Procedure_Info_Table(i).Name)))) and
(Lego_Builtins.Procedure_Info_Table(i).arity = Arity) then
return true;
end if;
end loop;
return false;
end Is_In_Proc_Table;
procedure Get_Proc_Info(This : in String; Arity : in Integer; Line, Column : in Integer;
found : out boolean; Info : out Lego_Builtins.Procedure_Info_Record_Type) is
begin
found := false;
Info := Lego_Builtins.Procedure_Info_Table(1); --initialize this to something in case desired record is never found
for i in 1..Lego_Builtins.Max_Procedures loop
if (To_Upper(This) = To_Upper(To_String((Lego_Builtins.Procedure_Info_Table(i).Name)))) and
(Lego_Builtins.Procedure_Info_Table(i).arity = Arity) then
Info := Lego_Builtins.Procedure_Info_Table(i);
found := true;
return;
end if;
end loop;
end Get_Proc_Info;
function Name_Of_Formal_As_Declared (This : in indexed_comp_nonterminal; i : in Integer; Arity : in Integer) return Unbounded_String is
Info_Rec : Lego_Builtins.Procedure_Info_Record_Type;
Ignored1, Ignored2 : integer := 0;
Line, Column : integer;
Found : boolean;
begin
Get_Proc_Info(This => To_String(LHS_Of_Assoc(This.name_part.all)),
Arity => Arity, Line => Ignored1, Column => Ignored2, Found => Found, Info => Info_Rec);
if not Found then
Get_LC(This, Line, Column);
Error_Msg_Prefix(Line, Column);
put(file => standard_error, item => " Unable to find formal parameter information for procedure ");
put(file => standard_error, item => To_String(LHS_Of_Assoc(This.name_part.all)));
new_line(file => standard_error);
put(file => standard_error, item => "Did you run your program through AdaGIDE first?");
raise Parse_Error_Exception;
end if;
return Info_Rec.Formal_Parameters(i);
end Name_Of_Formal_As_Declared;
procedure Add_To_Proc_Info_Table(This : in Lego_Builtins.Procedure_Info_Record_Type; Line, Column : in integer) is
slot : integer := 0;
begin
for i in 1..Lego_Builtins.Max_Procedures loop
if Lego_Builtins.Procedure_Info_Table(i).Name = To_Unbounded_String("") then
slot := i;
exit;
end if;
end loop;
if slot = 0 then
Error_Msg_Prefix(Line, Column);
put(File => Standard_Error, Item => " Your program has too many procedures to be translated");
new_line(File => Standard_Error);
put(File => Standard_Error, Item => "(max is ");
put(File => Standard_Error, Item => Lego_Builtins.Max_Procedures, Width => 0);
put(File => Standard_Error, Item => " for Ada/Mindstorms). Reduce the number of procedures and recompile.");
new_line(File => Standard_Error);
raise Parse_Error_Exception;
else
Lego_Builtins.Procedure_Info_Table(slot) := This;
end if;
end Add_To_Proc_Info_Table;
-----------------------
-- Change_Identifier --
-----------------------
procedure Change_Identifier(This : in out Parseable_Token; From, To : in Parseable_Token_Ptr) is
begin
if To_Upper(This.Token_String.all) = To_Upper(From.Token_String.all) then
This.Token_String := new String'(To.Token_String.all);
end if;
end Change_Identifier;
---------------------------------
-- Check For Supported Package --
---------------------------------
procedure Check_For_Supported_Package(This : in Parseable_Token) is
Line : Integer := This.Line;
Column: Integer := This.Column;
begin
if To_Unbounded_String(To_Upper(This.Token_String.all)) /= To_Unbounded_String("LEGO") then
Error_Msg_Prefix(Line, Column);
put(File => Standard_Error, Item => " Package '");
put(File => Standard_Error, Item => This.Token_String.all);
put(File => Standard_Error, Item => "' is not supported by Ada/Mindstorms.");
new_line(File => Standard_Error);
put(File => Standard_Error, Item => "Currently, only the package 'lego' can be used here.");
new_line(File => Standard_Error);
raise Parse_Error_Exception;
end if;
end Check_For_Supported_Package;
------------
-- Get_LC --
------------
procedure Get_LC(This : in Parseable_Token; Line, Column : out integer) is
begin
Line := This.line;
Column := This.Column;
end Get_LC;
---------------
-- Translate --
---------------
--All tokens (keywords, identifiers, operators) translated here.
procedure Translate (This : in out Parseable_Token) is
ID_String : Unbounded_String;
begin
case This.Token_Type is
--unsupported tokens
when ABORT_TOKEN | ACCEPT_TOKEN | CHAR_LITERAL_TOKEN |
CONCAT_TOKEN | DECLARE_TOKEN | DIGITS_TOKEN | DO_TOKEN | DOT_TOKEN | DOUBLE_DOT_TOKEN |
EXCEPTION_TOKEN | EXPONENT_TOKEN | FUNCTION_TOKEN | GENERIC_TOKEN | GOTO_TOKEN | IN_TOKEN |
LEFT_LABEL_BRACKET_TOKEN | NEW_TOKEN | PACKAGE_TOKEN |
PRAGMA_TOKEN | PRIVATE_TOKEN | PROTECTED_TOKEN | RAISE_TOKEN | RANGE_TOKEN | REM_TOKEN |
RENAMES_TOKEN | RETURN_TOKEN | REVERSE_TOKEN | SELECT_TOKEN | SEPARATE_TOKEN | SUBTYPE_TOKEN |
TASK_TOKEN | TICK_TOKEN | XOR_TOKEN =>
Error_Msg_Prefix(This.Line, This.Column);
put(File => Standard_Error, Item => " Ada keyword/operator/literal ");
put(File => Standard_Error, Item => This.Token_String.all);
put(File => Standard_Error, Item => " not supported.");
new_line(File => Standard_Error);
raise Parse_Error_Exception;
--ignored tokens
when ARROW_TOKEN | IS_TOKEN | NULL_TOKEN | PIPE_TOKEN | USE_TOKEN | WHEN_TOKEN | WITH_TOKEN =>
null;
--translated tokens/keywords, forced to lower case
when AND_TOKEN =>
put_with_space("&&");
when ASSIGNMENT_TOKEN =>
put_with_space("=");
when BASED_LITERAL_TOKEN => --only OK if 16#xxxxxxx#
if Element(To_Unbounded_String(This.Token_String.all),1) = '1' and
Element(To_Unbounded_String(This.Token_String.all),2) = '6' then
put("0x");
--print out everything between the #'s
put(Slice(Source => To_Unbounded_String(This.Token_String.all), Low => 4,
High => Index(Source => This.Token_String.all, Pattern => "#",
Going => Backward)-1));
else
Error_Msg_Prefix(This.Line, This.Column);
put(File => Standard_Error, Item => ": non-hex based literals not supported.");
raise Parse_Error_Exception;
end if;
when BEGIN_TOKEN =>
my_new_line;
put("{");
Inc_Indent;
my_new_line;
when CASE_TOKEN =>
my_new_line;
put_with_space("switch");
when DECIMAL_LITERAL_TOKEN =>
if Index(Source => This.Token_String.all, Pattern => ".") > 0 or
Index(Source => This.Token_String.all, Pattern => "E") > 0 then
Error_Msg_Prefix(This.Line, This.Column);
put(File => Standard_Error,
Item => " floating point values/values with exponents not supported.");
raise Parse_Error_Exception;
end if;
--might contain _'s, use attribute to strip them off
put_with_space(Integer'Value(This.Token_String.all));
when ELSE_Token =>
put_with_space("else");
when ELSIF_Token =>
put_with_space("else if");
when END_TOKEN =>
Dec_Indent;
my_new_line;
put_with_space("}");
my_new_line;
when EQ_TOKEN =>
put_with_space("==");
when IF_Token =>
put_with_space("if");
when INEQUALITY_TOKEN =>
put_with_space("!=");
when MOD_TOKEN =>
put_with_space("%");
when NOT_TOKEN =>
put_with_space("!");
when OTHERS_TOKEN =>
put_with_space("default");
when OR_TOKEN =>
put_with_space("||");
when WHILE_Token =>
put_with_space("while");
--identifiers
when IDENTIFIER_TOKEN =>
--convert to upper case because Ada is case-insensitive
ID_String := To_Unbounded_String(To_Upper(This.Token_String.all));
--check for Ada/Mindstorm identifiers, they're usually treated specially
if Is_Ada_Mindstorms_Identifier(To_String(ID_String)) then
Translate_Ada_Mindstorms_Identifier(To_String(ID_String));
else --any other identifiers get passed through (previously converted to upper case)
put_with_space(ID_String);
end if;
when STRING_LITERAL_token =>
Error_Msg_Prefix(This.Line, This.Column);
put(File => Standard_Error, Item => " Ada string literals not supported.");
new_line(File => Standard_Error);
raise Parse_Error_Exception;
--anything else is passed through
when others =>
put_with_space(This.Token_string.all);
end case;
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out NUMERIC_LIT_nonterminal1) is
begin
Translate(This.DECIMAL_LITERAL_part.all);
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out NUMERIC_LIT_nonterminal2) is
begin
Translate(This.BASED_LITERAL_part.all);
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out CHAR_LIT_nonterminal) is
begin
Translate(This.CHAR_LITERAL_part.all);
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out GT_GT_nonterminal) is
begin
null;
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out LT_LT_nonterminal) is
begin
Translate(This.LEFT_LABEL_BRACKET_part.all);
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out GE_nonterminal) is
begin
Translate(This.GREATER_THAN_OR_EQUAL_part.all);
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out LT_EQ_nonterminal) is
begin
Translate(This.LESS_THAN_OR_EQUAL_part.all);
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out NE_nonterminal) is
begin
Translate(This.INEQUALITY_part.all);
end Translate;
------------
-- Get_LC --
------------
procedure Get_LC(This : in CHAR_STRING_nonterminal; Line, Column : out Integer) is
begin
Get_LC(This.STRING_LITERAL_part.all, Line, Column);
end Get_LC;
---------------
-- Translate --
---------------
procedure Translate (This : in out CHAR_STRING_nonterminal) is
begin
Translate(This.STRING_LITERAL_part.all);
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out pragma_sym_nonterminal1) is
begin
Translate(This.PRAGMA_part.all);
Translate(This.identifier_part.all);
Translate(This.SEMICOLON_part.all);
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out pragma_sym_nonterminal2) is
begin
Translate(This.PRAGMA_part.all);
Translate(This.simple_name_part.all);
Translate(This.L_PAREN_part.all);
Translate(This.pragma_arg_s_part.all);
Translate(This.R_PAREN_part.all);
Translate(This.SEMICOLON_part.all);
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out pragma_arg_s_nonterminal1) is
begin
null;
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out pragma_arg_s_nonterminal2) is
begin
null;
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out pragma_arg_nonterminal1) is
begin
null;
end Translate;
---------------
-- Translate --
---------------
procedure Translate (This : in out pragma_arg_nonterminal2) is
begin
null;
end Translate;
------------------------
-- Change_Identifiers --
------------------------
procedure Change_Identifiers(This : in out pragma_s_nonterminal1;
From, To : in Parseable_Token_Ptr) is
begin
null;
end Change_Identifiers;
---------------
-- Translate --
---------------
--empty pragma part
procedure Translate (This : in out pragma_s_nonterminal1) is
begin
null;
end Translate;
------------------------
-- Change_Identifiers --
------------------------
--pragma, not supported
procedure Change_Identifiers(This : in out pragma_s_nonterminal2;
From, To : in Parseable_Token_Ptr) is
begin
null;
end Change_Identifiers;
---------------
-- Translate --
---------------
procedure Translate (This : in out pragma_s_nonterminal2) is
begin
Translate(This.pragma_s_part.all);
Translate(This.pragma_sym_part.all);
end Translate;
-----------
-- Match --
-----------
function Match(This : in decl_nonterminal1; Id : Parseable_Token_Ptr) return boolean is
begin
return Match(This.object_decl_part.all, Id);
end Match;
---------------
-- Translate --
---------------
procedure Translate (This : in out decl_nonterminal1) is
begin
Translate(This.object_decl_part.all);
end Translate;
-----------
-- Match --
-----------
function Match(This : in decl_nonterminal2; Id : Parseable_Token_Ptr) return boolean is
begin
return Match(This.number_decl_part.all, Id);
end Match;
---------------
-- Translate --
---------------
procedure Translate (This : in out decl_nonterminal2) is
begin
Translate(This.number_decl_part.all);
end Translate;
-----------
-- Match --
-----------
function Match(This : in decl_nonterminal3; Id : Parseable_Token_Ptr) return boolean is
begin
return false;
end Match;
---------------
-- Translate --
---------------
procedure Translate (This : in out decl_nonterminal3) is
begin
Translate(This.type_decl_part.all);
end Translate;
-----------
-- Match --
-----------
function Match(This : in decl_nonterminal4; Id : Parseable_Token_Ptr) return boolean is
begin
return false;
end Match;
---------------
-- Translate --
---------------
procedure Translate (This : in out decl_nonterminal4) is
begin
Translate(This.subtype_decl_part.all);
end Translate;
-----------
-- Match --
-----------
function Match(This : in decl_nonterminal5; Id : Parseable_Token_Ptr) return boolean is
begin
return false;
end Match;
---------------
-- Translate --
---------------
procedure Translate (This : in out decl_nonterminal5) is
begin
Translate(This.subprog_decl_part.all);
end Translate;
-----------
-- Match --
-----------
function Match(This : in decl_nonterminal6; Id : Parseable_Token_Ptr) return boolean is
begin
return false;
end Match;
---------------
-- Translate --
---------------
procedure Translate (This : in out decl_nonterminal6) is
begin
Translate(This.pkg_decl_part.all);
end Translate;
-----------
-- Match --
-----------
function Match(This : in decl_nonterminal7; Id : Parseable_Token_Ptr) return boolean is
begin
return false;
end Match;
---------------
-- Translate --
---------------