forked from nerun/bwbasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbwb_stc.c
1433 lines (1258 loc) · 37.8 KB
/
bwb_stc.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
/***************************************************************
bwb_stc.c Commands Related to Structured Programming
for Bywater BASIC Interpreter
Copyright (c) 1993, Ted A. Campbell
Bywater Software
email: [email protected]
Copyright and Permissions Information:
All U.S. and international rights are claimed by the author,
Ted A. Campbell.
This software is released under the terms of the GNU General
Public License (GPL), which is distributed with this software
in the file "COPYING". The GPL specifies the terms under
which users may copy and use the software in this distribution.
A separate license is available for commercial distribution,
for information on which you should contact the author.
***************************************************************/
/*---------------------------------------------------------------*/
/* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
/* 11/1995 ([email protected]). */
/* */
/* Those additionally marked with "DD" were at the suggestion of */
/* Dale DePriest ([email protected]). */
/* */
/* Version 3.00 by Howard Wulf, AF5NE */
/* */
/* Version 3.10 by Howard Wulf, AF5NE */
/* */
/* Version 3.20 by Howard Wulf, AF5NE */
/* */
/*---------------------------------------------------------------*/
#include "bwbasic.h"
static int ErrorMessage (LineType * current);
static LineType *FindParentCommand (int cmdnum, unsigned int Indention,
LineType * Previous[]);
static LineType *find_BottomLineInCode (LineType * l);
static int MissingBottomLine (LineType * current, int cmdnum);
static int scan_readargs (UserFunctionType * f, LineType * l);
static int UserFunction_clear (void);
/***************************************************************
FUNCTION: UserFunction_init()
DESCRIPTION: This function initializes the FUNCTION-SUB
lookup table.
***************************************************************/
extern int
UserFunction_init (void)
{
assert( My != NULL );
My->UserFunctionHead = NULL;
return TRUE;
}
/***************************************************************
FUNCTION: bwb_scan()
DESCRIPTION: This function scans all lines of the
program in memory and creates a FUNCTION-
SUB lookup table (fslt) for the program.
***************************************************************/
static int
ErrorMessage (LineType * current)
{
char tbuf[64];
assert (current != NULL);
switch (current->cmdnum)
{
case C_FOR:
bwb_strcpy (tbuf, "FOR without NEXT");
break;
case C_EXIT_FOR:
bwb_strcpy (tbuf, "EXIT FOR without FOR");
break;
case C_NEXT:
bwb_strcpy (tbuf, "NEXT without FOR");
break;
case C_DO:
bwb_strcpy (tbuf, "DO without LOOP");
break;
case C_EXIT_DO:
bwb_strcpy (tbuf, "EXIT DO without DO");
break;
case C_LOOP:
bwb_strcpy (tbuf, "LOOP without DO");
break;
case C_REPEAT:
bwb_strcpy (tbuf, "REPEAT without UNTIL");
break;
case C_EXIT_REPEAT:
bwb_strcpy (tbuf, "EXIT REPEAT without REPEAT");
break;
case C_UNTIL:
bwb_strcpy (tbuf, "UNTIL without REPEAT");
break;
case C_WHILE:
bwb_strcpy (tbuf, "WHILE without WEND");
break;
case C_EXIT_WHILE:
bwb_strcpy (tbuf, "EXIT WHILE without WHILE");
break;
case C_WEND:
bwb_strcpy (tbuf, "WEND without WHILE");
break;
case C_SUB:
bwb_strcpy (tbuf, "SUB without END SUB");
break;
case C_EXIT_SUB:
bwb_strcpy (tbuf, "EXIT SUB without SUB");
break;
case C_END_SUB:
bwb_strcpy (tbuf, "END SUB without SUB");
break;
case C_FUNCTION:
bwb_strcpy (tbuf, "FUNCTION without END FUNCTION");
break;
case C_EXIT_FUNCTION:
bwb_strcpy (tbuf, "EXIT FUNCTION without FUNCTION");
break;
case C_END_FUNCTION:
bwb_strcpy (tbuf, "END FUNCTION without FUNCTION");
break;
case C_IF8THEN:
bwb_strcpy (tbuf, "IF THEN without END IF");
break;
case C_ELSEIF:
bwb_strcpy (tbuf, "ELSEIF without IF THEN");
break;
case C_ELSE:
bwb_strcpy (tbuf, "ELSE without IF THEN");
break;
case C_END_IF:
bwb_strcpy (tbuf, "END IF without IF THEN");
break;
case C_SELECT_CASE:
bwb_strcpy (tbuf, "SELECT CASE without END SELECT");
break;
case C_CASE:
bwb_strcpy (tbuf, "CASE without SELECT CASE");
break;
case C_CASE_ELSE:
bwb_strcpy (tbuf, "CASE ELSE without SELECT CASE");
break;
case C_END_SELECT:
bwb_strcpy (tbuf, "END SELECT without SELECT CASE");
break;
default:
bwb_strcpy (tbuf, "UNKNOWN COMMAND");
break;
}
fprintf (My->SYSOUT->cfp, "%s: %d %s\n", tbuf, current->number,
current->buffer);
ResetConsoleColumn ();
return FALSE;
}
static LineType *
find_BottomLineInCode (LineType * l)
{
if (l == NULL)
{
return NULL;
}
while (l->OtherLine != NULL)
{
switch (l->cmdnum)
{
case C_NEXT:
case C_LOOP:
case C_UNTIL:
case C_WEND:
case C_END_SUB:
case C_END_FUNCTION:
case C_END_IF:
case C_END_SELECT:
return l;
}
l = l->OtherLine;
}
/* l->OtherLine == NULL */
return l;
}
static int
MissingBottomLine (LineType * current, int cmdnum)
{
LineType *BottomLineInCode;
BottomLineInCode = find_BottomLineInCode (current);
if (BottomLineInCode == NULL)
{
return TRUE;
}
if (BottomLineInCode->cmdnum != cmdnum)
{
return TRUE;
}
return FALSE;
}
static LineType *
FindParentCommand (int cmdnum, unsigned int Indention,
LineType * Previous[ /* EXECLEVELS */ ])
{
assert (Previous != NULL);
if (Indention > 0)
{
unsigned int i;
for (i = Indention - 1; /* i >= 0 */ ; i--)
{
if (Previous[i]->cmdnum == cmdnum)
{
/* FOUND */
return Previous[i]; /* EXIT_FOR->OtherLine == FOR */
}
if (i == 0)
{
/* NOT FOUND */
}
}
}
/* NOT FOUND */
return NULL;
}
extern int
bwb_scan (void)
{
/*
STATIC ANALYSIS
Background.
This routine began as a way to record the line numbers associated with the cmdnum of FUNCTION, SUB, or LABEL.
Pretty-printing.
Indention was added for pretty-printing by LIST, based upon the cmdnum (Indetion++, Indention--).
Mismatched structured commands.
When reviewing a properly indented listing, mismatched structured commands are easier to visually indentify
(FOR without NEXT and so on), so Previous[] was added to record the previous cmdnum at a given Indention.
Comparing Current->cmdnum with Previous->cmdnum allows mismatched structured commands to be detected.
Reduce stack usage for structured loops.
OtherLine, which was previously determined at runtime for loops, could now be determined during the scan.
Previously all loops used the stack so the EXIT command could find the loop's bottom line.
The EXIT commands could now look in Previous[] to determine their loop's top line and follow that to the loop's bottom line.
As a result, now the FOR loops use the stack to hold the current iteration value, but all other loops do not.
Reduce stack usaage for structured IF/SELECT.
Previuosly the structured IF/SELECT command used the stack to hold the results of comparisons and intermediate values.
OtherLine is now used to link these commands to their next occurring command.
As a result, the path thru the structure is now chosen at the IF/SELECT command, and the stack is no longer used.
The RESUME command knows about this linkage, so a simple "RESUME" jumps to the "IF THEN" or "SELECT CASE"
and "RESUME NEXT" jumps to the "END IF" or "END SELECT".
Caching for unstructured commands.
OtherLine was not previously used for any purpose for the unstructured GOTO, GOSUB, IF and ON commands.
It is now used to cache the line last executed by these commands to reduce the time required to find the target line.
The cache reduces execution time because the target line is usually (but not always) the same.
For the simple commands "GOTO 100", "GOSUB 100" and "IF x THEN 100", the cache will always succeed.
For the command "IF x THEN 100 ELSE 200", the cache will succeed for the last taken path.
Because programs are typically written so one path is more likely, the cache usually succeeds.
For the "ON x GOTO ..." and "ON x GOSUB ...", the cache succeeds when the result of the test expression repeats, such as:
FOR I = 1 TO 100
ON INT(I/10) GOSUB ...
NEXT I
In this example, the cache will succeed 90 times and fail 10 times.
Checking FOR/NEXT variable names.
If a variable name is provided for a NEXT command, then it is compared against the variable name of the matching FOR command.
This detects the following kind of mismatch:
FOR I = ...
NEXT J
OtherLine is now used for different purposes depending upon the command.
For structured IF8THEN and SELECT_CASE, OtherLine is used to form a one-way list:
IF8THEN->OtherLine == next occuring ELSE_IF, ELSE, END_IF
ELSE_IF->Otherline == next occuring ELSE_IF, ELSE, END_IF
ELSE->OtherLine == END_IF
END_IF->OtherLine == NULL
For the structured loops, OtherLine is uses as a circular list:
WHILE->OtherLine == WEND
EXIT_WHILE->OtherLine == WHILE
WEND->OtherLine == WHILE
For unstructured flow-of-control commands, OtherLine is used as a one-entry cache.
It contains a pointer to the Most Recently Used line returned by the command:
GOTO->OtherLine == MRU target line
GOSUB->OtherLine == MRU target line
IF->OtherLine == MRU target line
ON->OtherLine == MRU target line
For DATA commands, OtherLine points to the next DATA line (if it exists), otherwise it points to EndMarker.
StartMarker->OtherLine points to the first DATA line (if it exists), otherwise it points to EndMarker.
RESTORE knows about this.
For other commands, OtherLine is not used.
Any command which _requires_ OtherLine is not allowed be executed from the console in immediate mode.
*/
LineType *current;
LineType *prev_DATA; /* previous DATA statement */
unsigned int Indention;
LineType *Previous[EXECLEVELS]; /* previous part of structured command */
assert( My != NULL );
assert( My->StartMarker != NULL );
assert( My->EndMarker != NULL );
prev_DATA = NULL;
if (My->IsScanRequired == FALSE)
{
/* program is clean, no scan required */
return TRUE;
}
/* program needs to be scanned again, because a line was added or deleted */
/* reset these whenever a SCAN occurs */
My->StartMarker->OtherLine = My->EndMarker; /* default when no DATA statements exist */
My->DataLine = My->EndMarker; /* default when no DATA statements exist */
My->DataPosition = My->DataLine->Startpos;
My->ERL = NULL;
My->ContinueLine = NULL;
/* first run through the FUNCTION - SUB loopkup table and free any existing memory */
UserFunction_clear ();
for (Indention = 0; Indention < EXECLEVELS; Indention++)
{
Previous[Indention] = NULL;
}
Indention = 0;
for (current = My->StartMarker->next; current != My->EndMarker;
current = current->next)
{
assert( current != NULL );
current->OtherLine = NULL;
if (current->cmdnum == C_DATA)
{
if (prev_DATA == NULL)
{
/* I am the first DATA statement */
My->StartMarker->OtherLine = current;
My->DataLine = current;
My->DataPosition = My->DataLine->Startpos;
}
else
{
/* link the previous DATA statement to me */
prev_DATA->OtherLine = current;
}
/* I am the last DATA statement so far */
current->OtherLine = My->EndMarker;
/* I should point at the next DATA statement */
prev_DATA = current;
}
switch (current->cmdnum)
{
case C_DEF:
case C_FUNCTION:
case C_SUB:
case C_DEF8LBL:
UserFunction_add (current);
}
/* verify the 'current' command is consistent with a 'previous' command at a lower indention */
switch (current->cmdnum)
{
case C_EXIT_FOR:
current->OtherLine = FindParentCommand (C_FOR, Indention, Previous); /* EXIT_FOR->OtherLine == FOR */
if (current->OtherLine == NULL)
{
return ErrorMessage (current);
}
break;
case C_EXIT_WHILE:
current->OtherLine = FindParentCommand (C_WHILE, Indention, Previous); /* EXIT_WHILE->OtherLine == WHILE */
if (current->OtherLine == NULL)
{
return ErrorMessage (current);
}
break;
case C_EXIT_REPEAT:
current->OtherLine = FindParentCommand (C_REPEAT, Indention, Previous); /* EXIT_REPEAT->OtherLine == REPEAT */
if (current->OtherLine == NULL)
{
return ErrorMessage (current);
}
break;
case C_EXIT_FUNCTION:
current->OtherLine = FindParentCommand (C_FUNCTION, Indention, Previous); /* EXIT_FUNCTION->OtherLine == FUNCTION */
if (current->OtherLine == NULL)
{
return ErrorMessage (current);
}
break;
case C_EXIT_SUB:
current->OtherLine = FindParentCommand (C_SUB, Indention, Previous); /* EXIT_SUB->OtherLine == SUB */
if (current->OtherLine == NULL)
{
return ErrorMessage (current);
}
break;
case C_EXIT_DO:
current->OtherLine = FindParentCommand (C_DO, Indention, Previous); /* EXIT_DO->OtherLine == DO */
if (current->OtherLine == NULL)
{
return ErrorMessage (current);
}
break;
}
/* verify the 'current' command is consistent with a 'previous' command at the same indention */
switch (current->cmdnum)
{
case C_NEXT:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_FOR:
/* if( TRUE ) */
{
/* compare the 'NEXT' variable with the 'FOR' variable */
current->position = current->Startpos;
Previous[Indention]->position = Previous[Indention]->Startpos;
if (line_is_eol (current))
{
/* NEXT */
/* there is no variable to compare */
}
else
{
/* NEXT variable */
char NextVarName[NameLengthMax + 1];
char ForVarName[NameLengthMax + 1];
if (line_read_varname (current, NextVarName) == FALSE)
{
return ErrorMessage (current);
}
if (line_read_varname (Previous[Indention], ForVarName) == FALSE)
{
return ErrorMessage (current);
}
if (bwb_stricmp (ForVarName, NextVarName) != 0)
{
return ErrorMessage (current);
}
}
/* MATCHED */
current->Startpos = current->position;
Previous[Indention]->position = Previous[Indention]->Startpos;
}
/* OK */
Previous[Indention]->OtherLine = current; /* FOR->OtherLine = NEXT */
current->OtherLine = Previous[Indention]; /* NEXT->OtherLine = FOR */
Previous[Indention] = current; /* last command at this level = NEXT */
break;
default:
return ErrorMessage (current);
}
break;
case C_LOOP:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_DO:
/* OK */
Previous[Indention]->OtherLine = current; /* DO->OtherLine = LOOP */
current->OtherLine = Previous[Indention]; /* LOOP->OtherLine = DO */
Previous[Indention] = current; /* last command at this level = LOOP */
break;
default:
return ErrorMessage (current);
}
break;
case C_UNTIL:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_REPEAT:
/* OK */
Previous[Indention]->OtherLine = current; /* REPEAT->OtherLine = UNTIL */
current->OtherLine = Previous[Indention]; /* UNTIL->OtherLine = REPEAT */
Previous[Indention] = current; /* last command at this level = UNTIL */
break;
default:
return ErrorMessage (current);
}
break;
case C_WEND:
if (Indention == 0)
{
fprintf (My->SYSOUT->cfp, "Unmatched command %d %s\n",
current->number, current->buffer);
ResetConsoleColumn ();
return FALSE;
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_WHILE:
/* OK */
Previous[Indention]->OtherLine = current; /* WHILE->OtherLine = WEND */
current->OtherLine = Previous[Indention]; /* WEND->OtherLine = WHILE */
Previous[Indention] = current; /* last command at this level = WEND */
break;
default:
return ErrorMessage (current);
}
break;
case C_END_SUB:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_SUB:
/* OK */
Previous[Indention]->OtherLine = current; /* SUB->OtherLine = END_SUB */
current->OtherLine = Previous[Indention]; /* END_SUB->OtherLine = SUB */
Previous[Indention] = current; /* last command at this level = END_SUB */
break;
default:
return ErrorMessage (current);
}
break;
case C_END_FUNCTION:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_FUNCTION:
/* OK */
Previous[Indention]->OtherLine = current; /* FUNCTION->OtherLine = END_FUNCTION */
current->OtherLine = Previous[Indention]; /* END_FUNCTION->OtherLine = FUNCTION */
Previous[Indention] = current; /* last command at this level = END_FUNCTION */
break;
default:
return ErrorMessage (current);
}
break;
case C_ELSEIF:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_IF8THEN:
/* OK */
Previous[Indention]->OtherLine = current; /* IF8THEN->OtherLine = ELSEIF */
Previous[Indention] = current; /* last command at this level = ELSEIF */
break;
case C_ELSEIF:
/* OK */
Previous[Indention]->OtherLine = current; /* ELSEIF->OtherLine = ELSEIF */
Previous[Indention] = current; /* last command at this level = ELSEIF */
break;
default:
return ErrorMessage (current);
}
break;
case C_ELSE:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_IF8THEN:
/* OK */
Previous[Indention]->OtherLine = current; /* IF8THEN->OtherLine = ELSE */
Previous[Indention] = current; /* last command at this level = ELSE */
break;
case C_ELSEIF:
/* OK */
Previous[Indention]->OtherLine = current; /* ELSEIF->OtherLine = ELSE */
Previous[Indention] = current; /* last command at this level = ELSE */
break;
default:
return ErrorMessage (current);
}
break;
case C_END_IF:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_IF8THEN:
/* OK */
Previous[Indention]->OtherLine = current; /* IF8THEN->OtherLine = END_IF */
Previous[Indention] = current; /* last command at this level = END_IF */
break;
case C_ELSEIF:
/* OK */
Previous[Indention]->OtherLine = current; /* ELSEIF->OtherLine = END_IF */
Previous[Indention] = current; /* last command at this level = END_IF */
break;
case C_ELSE:
/* OK */
Previous[Indention]->OtherLine = current; /* ELSE->OtherLine = END_IF */
Previous[Indention] = current; /* last command at this level = END_IF */
break;
default:
return ErrorMessage (current);
}
break;
case C_CASE:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_SELECT_CASE:
/* OK */
Previous[Indention]->OtherLine = current; /* C_SELECT_CASE->OtherLine = C_CASE */
Previous[Indention] = current; /* last command at this level = C_CASE */
break;
case C_CASE:
/* OK */
Previous[Indention]->OtherLine = current; /* CASE->OtherLine = C_CASE */
Previous[Indention] = current; /* last command at this level = C_CASE */
break;
default:
return ErrorMessage (current);
}
break;
case C_CASE_ELSE:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_CASE:
/* OK */
Previous[Indention]->OtherLine = current; /* CASE->OtherLine = C_CASE_ELSE */
Previous[Indention] = current; /* last command at this level = C_CASE_ELSE */
break;
default:
return ErrorMessage (current);
}
break;
case C_END_SELECT:
if (Indention == 0)
{
return ErrorMessage (current);
}
Indention--;
switch (Previous[Indention]->cmdnum)
{
case C_CASE:
/* OK */
Previous[Indention]->OtherLine = current; /* CASE->OtherLine = END_SELECT */
Previous[Indention] = current; /* last command at this level = END_SELECT */
break;
case C_CASE_ELSE:
/* OK */
Previous[Indention]->OtherLine = current; /* CASE_ELSE->OtherLine = END_SELECT */
Previous[Indention] = current; /* last command at this level = END_SELECT */
break;
default:
return ErrorMessage (current);
}
break;
}
/* OK */
current->Indention = Indention;
/* record the 'current' command as the 'previous' command at this indention */
switch (current->cmdnum)
{
case C_FUNCTION:
case C_SUB:
/* this 'command' can NOT be inside the structure of another 'command' */
if (Indention > 0)
{
return ErrorMessage (Previous[Indention - 1]);
}
case C_FOR:
case C_DO:
case C_REPEAT:
case C_WHILE:
case C_IF8THEN:
case C_SELECT_CASE:
if (Previous[Indention] != NULL)
{
/* we are NOT the first command at this indention level */
/* verify the 'previous' command at this level was properly closed */
switch (Previous[Indention]->cmdnum)
{
case C_FOR:
case C_DO:
case C_REPEAT:
case C_WHILE:
case C_FUNCTION:
case C_SUB:
case C_IF8THEN:
case C_ELSEIF:
case C_ELSE:
case C_SELECT_CASE:
case C_CASE:
case C_CASE_ELSE:
/* there is an existing unclosed structure */
return ErrorMessage (Previous[Indention]);
}
}
Previous[Indention] = current;
Indention++;
if (Indention == EXECLEVELS)
{
fprintf (My->SYSOUT->cfp, "Program is nested too deep\n");
ResetConsoleColumn ();
return FALSE;
}
Previous[Indention] = NULL;
break;
case C_ELSEIF:
case C_ELSE:
case C_CASE:
case C_CASE_ELSE:
/*
Previous[ Indention ] was already checked and assigned above, just indent.
*/
Indention++;
if (Indention == EXECLEVELS)
{
fprintf (My->SYSOUT->cfp, "Program is nested too deep\n");
ResetConsoleColumn ();
return FALSE;
}
Previous[Indention] = NULL;
break;
}
}
if (Indention > 0)
{
return ErrorMessage (Previous[Indention - 1]);
}
/* verify the OtherLine chain terminates correctly; we should find the bottom command */
for (current = My->StartMarker->next; current != My->EndMarker;
current = current->next)
{
assert( current != NULL );
switch (current->cmdnum)
{
case C_FOR:
case C_EXIT_FOR:
if (MissingBottomLine (current, C_NEXT))
{
return ErrorMessage (current);
}
break;
case C_DO:
case C_EXIT_DO:
if (MissingBottomLine (current, C_LOOP))
{
return ErrorMessage (current);
}
break;
case C_REPEAT:
case C_EXIT_REPEAT:
if (MissingBottomLine (current, C_UNTIL))
{
return ErrorMessage (current);
}
break;
case C_WHILE:
case C_EXIT_WHILE:
if (MissingBottomLine (current, C_WEND))
{
return ErrorMessage (current);
}
break;
case C_FUNCTION:
case C_EXIT_FUNCTION:
if (MissingBottomLine (current, C_END_FUNCTION))
{
return ErrorMessage (current);
}
break;
case C_SUB:
case C_EXIT_SUB:
if (MissingBottomLine (current, C_END_SUB))
{
return ErrorMessage (current);
}
break;
case C_IF8THEN:
if (MissingBottomLine (current, C_END_IF))
{
return ErrorMessage (current);
}
break;
case C_SELECT_CASE:
if (MissingBottomLine (current, C_END_SELECT))
{
return ErrorMessage (current);
}
break;
}
}
/* return */
My->IsScanRequired = FALSE;
return TRUE;
}
/***************************************************************
FUNCTION: UserFunction_clear()
DESCRIPTION: This C function clears all existing memory
in the FUNCTION-SUB lookup table.
***************************************************************/
static int
UserFunction_clear (void)
{
UserFunctionType *current;
assert( My != NULL );
/* run through table and clear memory */
for (current = My->UserFunctionHead; current != NULL;)
{
UserFunctionType *next;
assert( current != NULL );
next = current->next;
/* check for local variables and free them */
if (current->local_variable != NULL)
{
var_free (current->local_variable);
current->local_variable = NULL;
}
if (current->name != NULL)
{
free (current->name);
current->name = NULL;
}
free (current);
current = next;
}
My->UserFunctionHead = NULL;
return TRUE;
}
extern int
UserFunction_name (char *name)
{
/* search USER functions */
UserFunctionType *L;
assert (name != NULL);
assert( My != NULL );
assert( My->CurrentVersion != NULL );
for (L = My->UserFunctionHead; L != NULL; L = L->next)
{
if (My->CurrentVersion->OptionVersionValue & L->OptionVersionBitmask)
{
if (bwb_stricmp (L->name, name) == 0)
{
return TRUE;
}
}
}
return FALSE;
}
extern UserFunctionType *
UserFunction_find_exact (char *name, unsigned char ParameterCount,
ParamBitsType ParameterTypes)
{
/* search USER functions */
UserFunctionType *L;
assert (name != NULL);
assert( My != NULL );
assert( My->CurrentVersion != NULL );
for (L = My->UserFunctionHead; L != NULL; L = L->next)
{
if (My->CurrentVersion->OptionVersionValue & L->OptionVersionBitmask)
{
if (L->ParameterCount == ParameterCount
&& L->ParameterTypes == ParameterTypes)
{
if (bwb_stricmp (L->name, name) == 0)
{
/* FOUND */
return L;
}
}
}
}
/* NOT FOUND */
return NULL;
}
/***************************************************************
FUNCTION: UserFunction_add()
DESCRIPTION: This C function adds an entry to the
FUNCTION-SUB lookup table.
***************************************************************/
extern int
UserFunction_add (LineType * l /* , int *position , int code */ )
{
char *name;
UserFunctionType *f;
char TypeCode;
char varname[NameLengthMax + 1];
assert (l != NULL);
assert( My != NULL );
assert( My->CurrentVersion != NULL );
assert( My->DefaultVariableType != NULL );