forked from adesutherland/CMS-370-BREXX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.c
2016 lines (1808 loc) · 71 KB
/
compile.c
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
/*
* $Id: compile.c,v 1.16 2008/07/15 07:40:25 bnv Exp $
* $Log: compile.c,v $
* Revision 1.16 2008/07/15 07:40:25 bnv
* #include changed from <> to ""
*
* Revision 1.15 2008/07/14 13:08:42 bnv
* MVS,CMS support
*
* Revision 1.14 2006/01/26 10:24:40 bnv
* Added: Indirect exposure to variables
*
* Revision 1.13 2004/08/16 15:28:15 bnv
* Changed: name of mnemonic operands from xxx_mn to O_XXX
*
* Revision 1.12 2004/04/30 15:30:01 bnv
* Fixed: When tracing no clause was appearing after END
*
* Revision 1.11 2004/03/27 08:33:34 bnv
* Corrected: If procedure was following on the next line after the label
*
* Revision 1.10 2003/10/30 13:16:10 bnv
* Variable name change
*
* Revision 1.9 2002/08/22 12:25:41 bnv
* Corrected: copy2tmp added before any call to C_template, to avoid errors like parse var a a b, that b is always null
*
* Revision 1.8 2002/06/11 12:37:38 bnv
* Added: CDECL
*
* Revision 1.7 2001/06/25 18:51:48 bnv
* Header -> Id
*
* Revision 1.6 1999/11/26 13:13:47 bnv
* Changed: To use the new macros
*
* Revision 1.5 1999/05/28 07:20:58 bnv
* ITERATE was jumping to the wrong position inside a "DO UNTIL" loop
*
* Revision 1.5 1999/05/27 10:13:37 bnv
* ITERATE was pointing to a wrong place in a "DO xxx UNTIL" loop
*
* Revision 1.4 1999/05/14 12:31:22 bnv
* Corrected a bug when registering a label_sy
*
* Revision 1.3 1999/03/15 10:08:09 bnv
* Changed: Do not create IdentInfo for non Symbol strings
*
* Revision 1.2 1999/03/10 16:53:32 bnv
* LoopCtrl changed from word to size_t
*
* Revision 1.1 1998/07/02 17:34:50 bnv
* Initial revision
*
*/
#define __COMPILE_C__
#include <setjmp.h>
#include <cmssys.h>
#include "lerror.h"
#include "lstring.h"
#include "bintree.h"
#include "dqueue.h"
#include "rexx.h"
#include "trace.h"
#include "compile.h"
#include "nextsymb.h"
#include "context.h"
/*
//
// Need To DO
// DO NOT ACCEPT LABELS inside DO-END, IF THEN SELECT etc... blocks
// Kill after the interpetation the extra CLAUSES!!!!!
//
*/
/* ------------ local defines ------------ */
#ifndef ALIGN
#define CODEFIXUPB(p,v) *(byte *)(LSTR(*(context->compileCompileCode)) + (p)) = (v)
#define CODEFIXUP(p,v) *(word *)(LSTR(*(context->compileCompileCode)) + (p)) = (v)
#define CLAUSESTEP sizeof(byte)
#else
#define CODEFIXUP(p, v) *(dword *)(LSTR(*(context->compileCompileCode)) + (p)) = (v)
#define CODEFIXUPB(p, v) CODEFIXUP(p,v)
#define CLAUSESTEP sizeof(dword)
#endif
/* ---- function prototypes ---- */
int __CDECL C_expr(int);
void __CDECL C_template(void);
TBltFunc *__CDECL C_isBuiltin(PLstr);
typedef struct loop_st {
size_t Citerate;
size_t Cleave;
int noofvars;
PLstr ctrlvar;
} LoopCtrl;
/* ------------ local prototypes ------------ */
static bool C_instr(bool);
static int C_chk4assign(void);
static void C_address(void);
static void C_assign(void);
static void C_arg(void);
static void C_call(void);
static void C_do(void);
static void C_drop(void);
static void C_error(void);
static void C_exit(void);
static void C_if(void);
static void C_interpret(void);
static void C_iterate(void);
static void C_leave(void);
static void C_lower(void);
static void C_nop(void);
static void C_numeric(void);
static void C_parse(void);
static void C_pull(void);
static void C_push(void);
static void C_queue(void);
static void C_return(void);
static void C_say(void);
static void C_select(void);
static void C_signal(void);
static void C_trace(void);
static void C_upper(void);
/* ---------------------------------------------------------- */
/* because ths is const we shall keep it really global */
static const
struct sort_list_st {
char *name;
void (*func)(void);
}
/* WARNING THE LIST MUST BE SORTED!!!!!!!!!!!! */
statements_list[] = {
{"ADDRESS", C_address},
{"ARG", C_arg},
{"CALL", C_call},
{"DO", C_do},
{"DROP", C_drop},
{"ELSE", C_error},
{"EXIT", C_exit},
{"IF", C_if},
{"INTERPRET", C_interpret},
{"ITERATE", C_iterate},
{"LEAVE", C_leave},
{"LOWER", C_lower},
{"NOP", C_nop},
{"NUMERIC", C_numeric},
{"OTHERWISE", C_error},
{"PARSE", C_parse},
{"PROCEDURE", C_error},
{"PULL", C_pull},
{"PUSH", C_push},
{"QUEUE", C_queue},
{"RETURN", C_return},
{"SAY", C_say},
{"SELECT", C_select},
{"SIGNAL", C_signal},
{"THEN", C_error},
{"TRACE", C_trace},
{"UPPER", C_upper},
{"WHEN", C_error}
};
/* ---------------- crloopctrl ------------------- */
static LoopCtrl *
crloopctrl(size_t it, size_t le, int vars, PLstr cv) {
LoopCtrl *lc;
Context *context = (Context *) CMSGetPG();
lc = (LoopCtrl *) MALLOC(sizeof(LoopCtrl), "LoopCtrl");
lc->Citerate = it;
lc->Cleave = le;
lc->noofvars = vars;
lc->ctrlvar = cv;
DQPUSH(&(context->compile_Loop), lc);
return lc;
} /* LoopCtrl */
/* ------------- CreateClause ---------------- */
static void
CreateClause(void) {
Context *context = (Context *) CMSGetPG();
/* --- Check if the previous mnemonic was a NEWCLAUSE also --- */
if ((context->compileCompileCurClause) &&
(context->compileCompileClause)[(context->compileCompileCurClause) -
1].code ==
(context->compileCompileCodeLen) - CLAUSESTEP)
return;
/* --- create a clause --- */
(context->compileCompileClause)[(context->compileCompileCurClause)].ptr =
(context->nextsymbsymbolprevptr);
(context->compileCompileClause)[(context->compileCompileCurClause)].line =
(context->nextsymbsymboline);
(context->compileCompileClause)[(context->compileCompileCurClause)].code =
(context->compileCompileCodeLen);
(context->compileCompileClause)[(context->compileCompileCurClause)]
.nesting = (context->compileCompileNesting);
(context->compileCompileClause)[(context->compileCompileCurClause)].fptr =
(context->compileCompileRxFile);
(context->compileCompileCurClause)++;
if ((context->compileCompileCurClause) ==
(context->compileCompileClauseItems)) {
(context->compileCompileClause) = (Clause *) REALLOC(
(context->compileCompileClause),
((context->compileCompileClauseItems) + CLAUSE_INC) *
sizeof(Clause));
(context->compileCompileClauseItems) += CLAUSE_INC;
}
_CodeAddByte(OP_NEWCLAUSE);
} /* CreateClause */
/* --------------- _mustbe -------------------- */
void __CDECL
_mustbe(enum symboltype sym, int errno, int subno) {
Context *context = (Context *) CMSGetPG();
if ((context->nextsymbsymbol) == sym)
nextsymbol();
else
(context->lstring_Lerror)(errno, subno, &(context->nextsymbsymbolstr));
/*
///// This is not correct!!!!!
*/
} /* _mustbe */
#ifndef ALIGN
/* ### If not defined ALIGN ### */
/* --------------- _CodeInsByte --------------- */
word
_CodeInsByte( word pos, byte b )
{
Context *context = (Context*)CMSGetPG();
if ((context->compileCompileCodeLen)+sizeof(b) >= LMAXLEN(*(context->compileCompileCode))) {
Lfx((context->compileCompileCode), (context->compileCompileCodeLen) + CODE_INC);
(context->compileCompileCodePtr) = (byte*)LSTR(*(context->compileCompileCode)) + (context->compileCompileCodeLen);
}
/* shift entire code by one byte */
MEMMOVE(LSTR(*(context->compileCompileCode))+pos+1,
LSTR(*(context->compileCompileCode))+pos,
(context->compileCompileCodeLen)-pos);
LSTR(*(context->compileCompileCode))[pos] = b;
(context->compileCompileCodePtr)++;
return (context->compileCompileCodeLen)++;
} /* _CodeInsByte */
/* --------------- _CodeAddByte --------------- */
word
_CodeAddByte( byte b )
{
Context *context = (Context*)CMSGetPG();
if ((context->compileCompileCodeLen)+sizeof(b) >= LMAXLEN(*(context->compileCompileCode))) {
Lfx((context->compileCompileCode), (context->compileCompileCodeLen) + CODE_INC);
(context->compileCompileCodePtr) = (byte*)LSTR(*(context->compileCompileCode)) + (context->compileCompileCodeLen);
}
*(context->compileCompileCodePtr)++ = b;
return (context->compileCompileCodeLen)++;
} /* _CodeAddByte */
/* --------------- _CodeAddWord --------------- */
word
_CodeAddWord( word w )
{
word pos;
Context *context = (Context*)CMSGetPG();
if ((context->compileCompileCodeLen)+sizeof(w) >= LMAXLEN(*(context->compileCompileCode))) {
Lfx((context->compileCompileCode), (context->compileCompileCodeLen) + CODE_INC);
(context->compileCompileCodePtr) = (byte*)LSTR(*(context->compileCompileCode)) + (context->compileCompileCodeLen);
}
pos = (context->compileCompileCodeLen);
*(word *)(context->compileCompileCodePtr) = w;
(context->compileCompileCodePtr) += sizeof(w);
(context->compileCompileCodeLen) += sizeof(w);
return pos;
} /* _CodeAddWord */
/* ### If defined ALIGN ### */
#else
/* --------------- _CodeInsByte --------------- */
dword
_CodeInsByte(dword pos, dword d) {
Context *context = (Context *) CMSGetPG();
if ((context->compileCompileCodeLen) + sizeof(d) >=
LMAXLEN(*(context->compileCompileCode))) {
Lfx((context->compileCompileCode),
(context->compileCompileCodeLen) + CODE_INC);
(context->compileCompileCodePtr) =
(byte *) LSTR(*(context->compileCompileCode)) +
(context->compileCompileCodeLen);
}
/* shift entire code by one dword */
MEMMOVE(LSTR(*(context->compileCompileCode)) + pos + sizeof(dword),
LSTR(*(context->compileCompileCode)) + pos,
(context->compileCompileCodeLen) - pos);
*(dword *) (LSTR(*(context->compileCompileCode)) + pos) = d;
(context->compileCompileCodePtr) += sizeof(d);
(context->compileCompileCodeLen) += sizeof(d);
return (context->compileCompileCodeLen);
} /* _CodeInsByte */
/* --------------- _CodeAddDWord --------------- */
dword
_CodeAddDWord(dword d) {
dword pos;
Context *context = (Context *) CMSGetPG();
if ((context->compileCompileCodeLen) + sizeof(d) >=
LMAXLEN(*(context->compileCompileCode))) {
Lfx((context->compileCompileCode),
(context->compileCompileCodeLen) + CODE_INC);
(context->compileCompileCodePtr) =
(byte *) LSTR(*(context->compileCompileCode)) +
(context->compileCompileCodeLen);
}
pos = (context->compileCompileCodeLen);
*(dword *) (context->compileCompileCodePtr) = d;
(context->compileCompileCodePtr) += sizeof(d);
(context->compileCompileCodeLen) += sizeof(d);
return pos;
} /* _CodeAddWord */
#endif
/* --------------- _CodeAddPtr ---------------- */
dword
_CodeAddPtr(void *ptr) {
dword pos;
Context *context = (Context *) CMSGetPG();
if ((context->compileCompileCodeLen) + sizeof(ptr) >=
LMAXLEN(*(context->compileCompileCode))) {
Lfx((context->compileCompileCode),
(context->compileCompileCodeLen) + CODE_INC);
(context->compileCompileCodePtr) =
(byte *) LSTR(*(context->compileCompileCode)) +
(context->compileCompileCodeLen);
}
pos = (context->compileCompileCodeLen);
*(dword *) (context->compileCompileCodePtr) = (dword) ptr;
(context->compileCompileCodePtr) += sizeof(ptr);
(context->compileCompileCodeLen) += sizeof(ptr);
return pos;
} /* _CodeAddPtr */
/* -------------- _Add2Lits ----------------- */
void *
_Add2Lits(PLstr lit, int hasdot) {
PBinLeaf leaf;
PLstr tosearch;
Lstr newstr, numstr, aux;
char *ch;
int i, t, cnt, start, stop;
IdentInfo *inf;
Context *context = (Context *) CMSGetPG();
/* Find in tree */
tosearch = lit;
LINITSTR(numstr);
t = _Lisnum(lit);
if (t == LINTEGER_TY) {
Lstrcpy(&numstr, lit);
L2int(&numstr);
if (!Lstrcmp(&numstr, lit)) {
L2int(&numstr);
tosearch = &numstr;
}
} else if (t == LREAL_TY) {
Lstrcpy(&numstr, lit);
L2real(&numstr);
if (!Lstrcmp(&numstr, lit)) {
L2real(&numstr);
tosearch = &numstr;
}
}
leaf = BinFind(&(context->rexxrxLitterals), tosearch);
if (leaf == NULL)
if (tosearch == &numstr)
leaf = BinFind(&(context->rexxrxLitterals), tosearch);
if (leaf == NULL) {
LINITSTR(newstr);
Lfx(&newstr, 1);
Lstrcpy(&newstr, tosearch);
#ifdef USEOPTION
/* set the option for faster recognition */
if (LTYPE(newstr) == LINTEGER_TY)
LSETOPT(newstr,LOPTINT);
else
if (LTYPE(newstr) == LREAL_TY)
LSETOPT(newstr,LOPTREAL);
#endif
if ((LTYPE(newstr) == LSTRING_TY) &&
!LISNULL(newstr) && Ldatatype(&newstr, 'S')) {
LASCIIZ(newstr);
if (hasdot) {
/* count number of dots */
for (cnt = 1, ch = LSTR(newstr); *ch; ch++)
if (*ch == '.')
cnt++;
/* allocate space */
inf = (IdentInfo *) MALLOC
(sizeof(IdentInfo) + (cnt - 1) * sizeof(PBinLeaf),
"ID_INFO_STEM");
/* count substrings and add them to lits */
inf->id = NO_CACHE;
inf->stem = cnt;
/* scan for substrings */
LINITSTR(aux);
_Lsubstr(&aux, &newstr, 1, hasdot);
inf->leaf[0] = _Add2Lits(&aux, FALSE);
stop = hasdot + 1; /* after the dot */
ch = LSTR(newstr) + hasdot;
i = 1;
while (1) {
while (*ch == '.') {
inf->leaf[i++] = NULL;
ch++;
stop++;
}
if (!*ch) {
inf->leaf[i++] = NULL;
break;
}
start = stop; /* find next dot */
while (*ch && *ch != '.') {
ch++;
stop++;
}
_Lsubstr(&aux, &newstr, start, stop - start);
inf->leaf[i++] = _Add2Lits(&aux, FALSE);
if (!*ch) break;
if (*ch == '.') {
ch++;
stop++;
}
}
LFREESTR(aux);
} else {
inf = (IdentInfo *) MALLOC(sizeof(IdentInfo), "ID_INFO");
inf->stem = 0;
inf->leaf[0] = NULL;
}
inf->id = NO_CACHE;
} else
inf = NULL;
leaf = BinAdd(&(context->rexxrxLitterals), &newstr, inf);
}
LFREESTR(numstr);
return leaf;
} /* _Add2Lits */
/* ------------------- _AddLabel ------------------ */
PBinLeaf
_AddLabel(int type, size_t offset) {
PBinLeaf leaf;
Lstr newstr;
RxFunc *func;
TBltFunc *isbuiltin;
Context *context = (Context *) CMSGetPG();
/* --- check to see if we are interpeting a string --- */
if ((context->compile_str_interpreted) && type == FT_LABEL &&
offset != UNKNOWN_LABEL)
(context->lstring_Lerror)(ERR_UNEXPECTED_LABEL, 1,
&(context->nextsymbsymbolstr));
/* --- Find in tree --- */
leaf = BinFind(&(context->rexx_labels), &(context->nextsymbsymbolstr));
if (leaf == NULL) {
LINITSTR(newstr);
Lstrcpy(&newstr, &(context->nextsymbsymbolstr));
func = (RxFunc *) MALLOC(sizeof(RxFunc), "FuncLabel");
func->type = type;
func->builtin = NULL;
func->label = offset;
if ((context->nextsymbsymbolisstr)) {
func->type = FT_SYSTEM;
func->systype = SYST_UNKNOWN;
} else if (type == FT_FUNCTION) {
isbuiltin = C_isBuiltin(&(context->nextsymbsymbolstr));
if (isbuiltin == NULL) {
/* then it might be internal external or system */
func->type = FT_FUNCTION;
func->label = offset;
} else {
func->type = FT_BUILTIN;
func->builtin = isbuiltin;
}
}
leaf = BinAdd(&(context->rexx_labels), &newstr, func);
} else if (offset != UNKNOWN_LABEL) {
func = (RxFunc *) (leaf->value);
if (func->label == UNKNOWN_LABEL) {
/* label found change function type - Internal calls have priority */
if (func->type == FT_BUILTIN || func->type == FT_SYSTEM)
func->type = FT_INTERNAL;
func->label = offset;
}
}
return leaf;
} /* _AddLabel */
/* -------------------------------------------------------------- */
/* C_error, reports an error, when an illegal (context->nextsymbsymbol) is found. */
/* -------------------------------------------------------------- */
static void
C_error(void) {
Context *context = (Context *) CMSGetPG();
if (!CMP("ELSE")) (context->lstring_Lerror)(ERR_THEN_UNEXCEPTED, 2);
else if (!CMP("OTHERWISE")) (context->lstring_Lerror)(ERR_WHEN_UNCEPTED, 0);
else if (!CMP("PROCEDURE"))
(context->lstring_Lerror)(ERR_UNEXPECTED_PROC, 0);
else if (!CMP("THEN")) (context->lstring_Lerror)(ERR_THEN_UNEXCEPTED, 1);
else if (!CMP("WHEN")) (context->lstring_Lerror)(ERR_WHEN_UNCEPTED, 0);
else
(context->lstring_Lerror)(ERR_INVALID_EXPRESSION, 0);
} /* C_error */
/* -------------------------------------------------------------- */
/* ADDRESS [<(context->nextsymbsymbol) | string> [expr]] ; */
/* redirect commands or a single command to a new */
/* environment. ADDRESS VALUE expr may be used */
/* for an evaluated enviroment name. */
/* -------------------------------------------------------------- */
static void
C_address(void) {
Context *context = (Context *) CMSGetPG();
if ((context->nextsymbsymbol) == semicolon_sy) {
_CodeAddByte(OP_PUSH);
_CodeAddPtr((context->rexxsystemStr));
TraceByte(other_middle);
_CodeAddByte(OP_STOREOPT);
_CodeAddByte(environment_opt);
return;
}
if ((context->nextsymbsymbol) == le_parent || identCMP("VALUE")) {
if ((context->nextsymbsymbol) == ident_sy) nextsymbol();
C_expr(exp_normal);
#ifdef MSDOS
_CodeAddByte(OP_COPY2TMP);
_CodeAddByte(OP_UPPER);
#endif
_CodeAddByte(OP_STOREOPT);
_CodeAddByte(environment_opt);
} else if ((context->nextsymbsymbol) == literal_sy ||
(context->nextsymbsymbol) == ident_sy) {
_CodeAddByte(OP_PUSH);
_CodeAddPtr(SYMBOLADD2LITS_KEY);
TraceByte(other_middle);
#ifdef MSDOS
_CodeAddByte(OP_COPY2TMP);
_CodeAddByte(OP_UPPER);
#endif
nextsymbol();
if ((context->nextsymbsymbol) != semicolon_sy) {
C_expr(exp_normal);
_CodeAddByte(OP_SYSTEM);
} else {
_CodeAddByte(OP_STOREOPT);
_CodeAddByte(environment_opt);
}
} else
(context->lstring_Lerror)(ERR_INVALID_EXPRESSION, 0);
} /* C_address */
/* -------------------------------------------------------------- */
/* ARG <template> ; */
/* parse argument string(s), translated into uppercase, */
/* into variables. Short for PARSE UPPER ARG. */
/* -------------------------------------------------------------- */
static void
C_arg(void) {
int ai;
Context *context = (Context *) CMSGetPG();
ai = 0;
do {
if ((context->nextsymbsymbol) == comma_sy) nextsymbol();
_CodeAddByte(OP_LOADARG);
_CodeAddByte(ai);
_CodeAddByte(OP_COPY2TMP);
_CodeAddByte(OP_UPPER);
C_template();
ai++;
} while ((context->nextsymbsymbol) == comma_sy);
} /* C_arg */
/* -------------------------------------------------------------- */
/* CALL [(context->nextsymbsymbol) | string] [<expr>] [,<expr>]... ; */
/* call an internal routine, an external routine or program, */
/* or a built-in function. Depending on the type of */
/* routine called, the variable RESULT contains the result */
/* of the routine. RESULT is uninitialized if no result is */
/* returned. */
/* -------------------------------------------------------------- */
static void
C_call(void) {
int ia, func, line, realarg = 0;
word existarg = 0, bp = 1, lastarg = 0; /* bit mapped, if arguments exist */
void *lbl;
Context *context = (Context *) CMSGetPG();
/* keep line number */
line = (context->nextsymbsymboline);
func = ((context->nextsymbsymbol) == function_sy);
if (((context->nextsymbsymbol) != ident_sy) &&
((context->nextsymbsymbol) != literal_sy) &&
((context->nextsymbsymbol) != function_sy))
(context->lstring_Lerror)(ERR_STRING_EXPECTED, 0);
/* ///////// NOT CORRECT //////////// */
/* Make it work as the SIGNAL ON ... */
if (!CMP("OFF") || (!CMP("ON"))) {
C_signal();
return;
}
/* ///////////////////////////////// */
lbl = _AddLabel(FT_FUNCTION, UNKNOWN_LABEL);
/* since we don't know if it is going to return
a result then we create a stack space */
_CodeAddByte(OP_PUSHTMP);
nextsymbol();
ia = 0;
if (((context->nextsymbsymbol) != semicolon_sy) ||
(func && ((context->nextsymbsymbol) != ri_parent))) {
if ((context->nextsymbsymbol) != comma_sy) {
C_expr(exp_normal);
realarg++;
lastarg = ia + 1;
existarg |= bp; /* argument exist */
}
ia++;
bp <<= 1; /* increase bit position */
while ((context->nextsymbsymbol) == comma_sy) {
nextsymbol();
if (ia >= MAXARGS)
(context->lstring_Lerror)(ERR_INCORRECT_CALL, 0);
if (!(((context->nextsymbsymbol) == comma_sy) ||
((context->nextsymbsymbol) == ri_parent) ||
((context->nextsymbsymbol) == semicolon_sy))) {
C_expr(exp_normal);
realarg++;
lastarg = ia + 1;
existarg |= bp;
}
ia++;
bp <<= 1; /* increase bit position */
}
}
_CodeAddByte(OP_CALL);
_CodeAddPtr(lbl); /* call pointer */
_CodeAddByte(lastarg); /* arguments */
_CodeAddByte(realarg); /* real args */
_CodeAddWord(existarg); /* which exist */
_CodeAddWord(line); /* (context->nextsymbsymbol) line */
_CodeAddByte(CT_PROCEDURE); /* call type */
TraceByte(nothing_middle);
if (func)
_mustbe(ri_parent, ERR_UNMATCHED_PARAN, 0);
} /* C_call */
/* -------------------------------------------------------------- */
/* DO [ [name=expri [TO exprt] [BY exprb] */
/* [FOR exprf]] | [ FOREVER | exprr ] */
/* [UNTIL expru | WHILE exprw] ; */
/* [instr]... ; */
/* END [(context->nextsymbsymbol)] ; */
/* group instructions together with optional repetition and */
/* condition. NAME is stepped from EXPRI to EXPRT in */
/* steps of EXPRB, for a maximum of EXPRF iterations. */
/* These variables are evaluated only once at the top of */
/* the loop and must result in numbers. The iterative */
/* phrase may be replaced by EXPRR, which is a loop */
/* count (no variable used), or by the keyword */
/* FOREVER. If a WHILE or UNTIL is given, EXPRU or */
/* EXPRW must evaluate to "0" or "1". The condition is */
/* tested at the top of the loop if WHILE or at the bottom */
/* if UNTIL. */
/* -------------------------------------------------------------- */
/* --- DO until END --- */
static void
DOuntilEND(void) {
Context *context = (Context *) CMSGetPG();
while (1) { /* no need for leave & iterate label */
SKIP_SEMICOLONS;
if (C_instr(TRUE)) break;
}
nextsymbol(); /* Skip END */
} /* DOuntilEND */
#define DO_ASSIGN 0x0001
#define DO_TO 0x0002
#define DO_BY 0x0004
#define DO_FOR 0x0008
#define DO_FOREVER 0x0010
#define DO_WHILE 0x0020
#define DO_UNTIL 0x0040
/* --- C_do --- */
static void
C_do(void) {
Context *context = (Context *) CMSGetPG();
/*
* I must define a label for ITERATE and LEAVE in all the cases
* except the case of single loop; DO ... END
*/
enum stat_type old_statement = (context->nextsymbsymbolstat);
LoopCtrl *lc;
PLstr CtrlVar = NULL;
void *cv_ptr = NULL;
size_t body_p, iterate_p, fix_iterate = 0, leave_p, fix_leave = 0;
size_t untilexpr = 0, overuntil, untilend = 0;
size_t pat = 0, tmp;
word idx = 0, idxTO = 0, idxBY = 0, idxFOR = 0;
int dotype = 0;
/* Simplest case */
if ((context->nextsymbsymbol) == semicolon_sy) { /* SINGLE LOOP */
(context->nextsymbsymbolstat) = in_do_st;
DOuntilEND();
(context->nextsymbsymbolstat) = old_statement;
return;
}
(context->nextsymbsymbolstat) = in_do_init_st;
/* --- First check for repetition --- */
/* --- and construct header --- */
if (C_chk4assign()) {
cv_ptr = SYMBOLADD2LITS;
CtrlVar = &(((PBinLeaf) cv_ptr)->key);
C_assign();
dotype |= DO_ASSIGN;
while ((context->nextsymbsymbol) == to_sy ||
(context->nextsymbsymbol) == for_sy ||
(context->nextsymbsymbol) == by_sy)
switch ((context->nextsymbsymbol)) {
case to_sy:
if (idxTO)
(context->lstring_Lerror)(ERR_INVALID_DO_SYNTAX, 0);
idxTO = ++idx;
nextsymbol(); /* Skip TO */
dotype |= DO_TO;
C_expr(exp_tmp);
break;
case by_sy:
if (idxBY)
(context->lstring_Lerror)(ERR_INVALID_DO_SYNTAX, 0);
idxBY = ++idx;
nextsymbol(); /* Skip BY */
dotype |= DO_BY;
C_expr(exp_normal);
_CodeAddByte(OP_BYINIT);
pat = _CodeAddWord(0); /* Patch position */
break;
case for_sy:
if (idxFOR)
(context->lstring_Lerror)(ERR_INVALID_DO_SYNTAX, 0);
idxFOR = ++idx;
nextsymbol(); /* Skip FOR */
dotype |= DO_FOR;
C_expr(exp_normal);
_CodeAddByte(OP_FORINIT);
break;
default:
(context->lstring_Lerror)(ERR_INVALID_DO_SYNTAX, 0);
}
} else /* end of C_chk4assign */
if (identCMP("FOREVER")) {
dotype |= DO_FOREVER;
nextsymbol(); /* Skip FOREVER */
} else
/* --- Check for WHILE/UNTIL --- */
if (identCMP("WHILE")) /* do nothing */;
else if (identCMP("UNTIL")) /* do nothing */;
else { /* ----- REPETITION LOOP ----- */
if (idxFOR)
(context->lstring_Lerror)(ERR_INVALID_DO_SYNTAX, 0);
idxFOR = ++idx;
if (identCMP("FOR")) nextsymbol(); /* skip FOR */
dotype |= DO_FOR;
C_expr(exp_normal);
_CodeAddByte(OP_FORINIT);
}
/* Create a jmp reference at the end and to the iterate pos */
/* jump over leave "jmp" */
_CodeAddByte(OP_JMP);
tmp = _CodeAddWord(0);
leave_p = (context->compileCompileCodeLen);
_CodeAddByte(OP_JMP);
fix_leave = _CodeAddWord(0);
if (dotype & DO_ASSIGN) {
iterate_p = (context->compileCompileCodeLen);
_CodeAddByte(OP_JMP);
fix_iterate = _CodeAddWord(0);
}
CODEFIXUP(tmp, (context->compileCompileCodeLen));
body_p = (context->compileCompileCodeLen);
/* --- Create the main loop control --- */
if (dotype & DO_ASSIGN)
lc = crloopctrl(iterate_p, leave_p, idx, CtrlVar);
else
lc = crloopctrl(body_p, leave_p, idx, CtrlVar);
if ((context->nextsymbsymbol) == while_sy || identCMP("WHILE")) {
dotype |= DO_WHILE;
nextsymbol(); /* Skip WHILE */
C_expr(exp_normal);
_CodeAddByte(OP_JF);
_CodeAddWord(leave_p);
} else if ((context->nextsymbsymbol) == until_sy || identCMP("UNTIL")) {
dotype |= DO_UNTIL;
nextsymbol(); /* Skip UNTIL */
_CodeAddByte(OP_JMP);
overuntil = _CodeAddWord(0);
untilexpr = (context->compileCompileCodeLen);
/* modify to the correct iterate address */
/* to check the UNTIL expr after the iteration */
lc->Citerate = (context->compileCompileCodeLen);
C_expr(exp_normal);
_CodeAddByte(OP_JT);
_CodeAddWord(leave_p);
_CodeAddByte(OP_JMP);
untilend = _CodeAddWord(0);
CODEFIXUP(overuntil, (context->compileCompileCodeLen));
}
/* --- create code for TO,BY,FOR --- */
if (idxTO) {
_CodeAddByte(OP_DUP);
_CodeAddByte(idx - idxTO);
_CodeAddByte(OP_LOAD);
_CodeAddPtr(cv_ptr);
TraceByte(nothing_middle);
if (idxBY) {
CODEFIXUP(pat,
(context->compileCompileCodeLen)); /* Patch reference */
}
_CodeAddByte(OP_TGE); /* This byte will be patched */
TraceByte(nothing_middle);
_CodeAddByte(OP_JF);
_CodeAddWord(leave_p);
}
if (idxFOR) {
_CodeAddByte(OP_DECFOR);
_CodeAddByte(idx - idxFOR); /* variable */
_CodeAddWord(leave_p);
}
/* ===== main body ====== */
_CodeAddByte(OP_NEWCLAUSE);
(context->nextsymbsymbolstat) = in_do_st;
_mustbe(semicolon_sy, ERR_INVALID_DO_SYNTAX, 0);
DOuntilEND();
if (CtrlVar) {
if ((context->nextsymbsymbol) == ident_sy) {
if (!Lstrcmp(&(context->nextsymbsymbolstr), CtrlVar))
nextsymbol();
else
(context->lstring_Lerror)(ERR_UNMATCHED_END, 2,
&(context->nextsymbsymbolstr));
} else if ((context->nextsymbsymbol) != semicolon_sy)
(context->lstring_Lerror)(ERR_SYMBOL_EXPECTED, 1,
&(context->nextsymbsymbolstr));
} else if ((context->nextsymbsymbol) == ident_sy)
(context->lstring_Lerror)(ERR_UNMATCHED_END, 3,
&(context->nextsymbsymbolstr));
/* --- if UNTIL in DO --- */
if (dotype & DO_UNTIL) {
_CodeAddByte(OP_JMP);
_CodeAddWord(untilexpr);
CODEFIXUP(untilend, (context->compileCompileCodeLen));
}
/* --- calc next value --- */
if (dotype & DO_ASSIGN) {
CODEFIXUP(fix_iterate, (context->compileCompileCodeLen));
if (idxBY) {
_CodeAddByte(OP_LOAD);
_CodeAddPtr(cv_ptr);
TraceByte(nothing_middle);
_CodeAddByte(OP_DUP);
_CodeAddByte(0);
_CodeAddByte(OP_DUP);
_CodeAddByte(idx - idxBY + 2);
_CodeAddByte(OP_ADD);
TraceByte(other_middle);
_CodeAddByte(OP_POP);
_CodeAddByte(1);
} else {
_CodeAddByte(OP_LOAD);
_CodeAddPtr(cv_ptr);
TraceByte(nothing_middle);
_CodeAddByte(OP_INC);
TraceByte(other_middle);
}
}
/* --- end of loop, add a jump to the beggining --- */
_CodeAddByte(OP_JMP);
_CodeAddWord(body_p);
CODEFIXUP(fix_leave, (context->compileCompileCodeLen));
lc = DQPop(&(context->compile_Loop)); /* delete loop control */
FREE(lc);
if (dotype & (DO_TO | DO_FOR | DO_BY)) {
_CodeAddByte(OP_POP); /* pop the last value from stack */
_CodeAddByte(idx);
}
(context->nextsymbsymbolstat) = old_statement;
} /* C_do */
/* -------------------------------------------------------------- */
/* DROP name [name]... ; */
/* drop (reset) the named variables or group of variables. */
/* */
/* *** if an exposed variable is named, the variable itself */
/* in the older generation will be dropped! */
/* -------------------------------------------------------------- */
static void
C_drop(void) {
Context *context = (Context *) CMSGetPG();
while (1) {
if ((context->nextsymbsymbol) == ident_sy) {