aboutsummaryrefslogtreecommitdiffstats
path: root/bwb_stc.c
blob: bc310af9bb4c025751982d711e8147ad668f1f00 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
/***************************************************************
  
        bwb_stc.c       Commands Related to Structured Programming
                        for Bywater BASIC Interpreter
  
                        Copyright (c) 1993, Ted A. Campbell
                        Bywater Software
  
                        email: tcamp@delphi.com
  
        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 (eidetics@cerf.net).                                  */
/*                                                               */
/* Those additionally marked with "DD" were at the suggestion of */
/* Dale DePriest (daled@cadence.com).                            */
/*                                                               */
/* 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 );

  
  /* get the element for name */
  switch (l->cmdnum)
  {
  case C_DEF:
  case C_FUNCTION:
  case C_SUB:
    l->position = l->Startpos;
    if (line_read_varname (l, varname) == FALSE)
    {
      WARN_SYNTAX_ERROR;
      return FALSE;
    }
    break;
  case C_DEF8LBL:
    l->position = 0;
    if (line_read_label (l, varname) == FALSE)
    {
      WARN_SYNTAX_ERROR;
      return FALSE;
    }
    l->position = l->Startpos;
    break;
  default:
    WARN_SYNTAX_ERROR;
    return FALSE;
  }

  /* get memory for name buffer */
  if ((name =
       (char *) calloc (bwb_strlen (varname) + 1 /* NulChar */ ,
                        sizeof (char))) == NULL)
  {
    WARN_OUT_OF_MEMORY;
    return FALSE;
  }
  bwb_strcpy (name, varname);

  /* get memory for fslt structure */
  if ((f =
       (UserFunctionType *) calloc (1, sizeof (UserFunctionType))) == NULL)
  {
    WARN_OUT_OF_MEMORY;
    return FALSE;
  }
  /* fill in structure */

  f->line = l;
  f->name = name;
  f->local_variable = NULL;
  f->ParameterCount = 0;        /* 0..32, 255 == (...) */
  f->ParameterTypes = 0;        /* bit 0 is first parameter */
  f->startpos = l->position;
  f->OptionVersionBitmask = My->CurrentVersion->OptionVersionValue;




  /* read arguments */
  switch (l->cmdnum)
  {
  case C_DEF:
  case C_FUNCTION:
  case C_SUB:
    TypeCode = var_nametype (varname);
    if (line_peek_LparenChar (l))
    {
      if (scan_readargs (f, l))
      {
        f->startpos = l->position;
      }
    }
    /* determine function type */
    if (TypeCode == NulChar)
    {
      /* function has no explicit type char */
      TypeCode = line_read_type_declaration (l);
      if (TypeCode == NulChar)
      {
        /* function has no declared type */
        int i;
        i = VarTypeIndex (varname[0]);
        if (i < 0)
        {
          TypeCode = DoubleTypeCode;        /* default */
        }
        else
        {
          TypeCode = My->DefaultVariableType[i];
        }
      }
    }
    break;
  case C_DEF8LBL:
    TypeCode = LongTypeCode;
    break;
  default:
    WARN_SYNTAX_ERROR;
    return FALSE;
  }
  f->ReturnTypeCode = TypeCode;
  /* establish linkages */
  f->next = My->UserFunctionHead;
  My->UserFunctionHead = f;

  return TRUE;
}

/***************************************************************
  
        FUNCTION:       scan_readargs()
  
        DESCRIPTION: This C function reads arguments (variable
            names for an entry added to the FUNCTION-
         SUB lookup table.
  
***************************************************************/

static int
scan_readargs (UserFunctionType * f, LineType * l)
{
   
  assert (f != NULL);
  assert (l != NULL);
  assert( My != NULL );
  assert( My->CurrentVersion != NULL );
  assert( My->DefaultVariableType != NULL );

  f->ParameterCount = 0;        /* 0..32, 255 == (...) */
  f->ParameterTypes = 0;        /* bit 0 is first parameter */

  /* we should be at begin paren */
  if (line_skip_LparenChar (l) == FALSE)
  {
    WARN_SYNTAX_ERROR;
    return FALSE;
  }
  if (line_skip_RparenChar (l))
  {
    /* end of NO argument list */
    /* FUNCTION ABC() */
    return TRUE;
  }
  if (line_skip_word (l, "..."))
  {
    /* FUNCTION FRED( ... ) */
    if (line_skip_RparenChar (l))
    {
      f->ParameterCount = 0xFF;        /* VARIANT */
      f->ParameterTypes = 0;
      return TRUE;
    }
    WARN_SYNTAX_ERROR;
    return FALSE;
  }

  /* loop through looking for arguments */
  do
  {
    VariableType *v;
    char TypeCode;
    char varname[NameLengthMax + 1];

    /* presume beginning of argument == variable name */
    if (line_read_varname (l, varname) == FALSE)
    {
      WARN_SYNTAX_ERROR;
      return FALSE;
    }
    /* determine variable type */
    TypeCode = var_nametype (varname);
    if (TypeCode == NulChar)
    {
      /* variable has no explicit type char */
      TypeCode = line_read_type_declaration (l);
      if (TypeCode == NulChar)
      {
        /* variable has no declared type */
        int i;
        i = VarTypeIndex (varname[0]);
        if (i < 0)
        {
          TypeCode = DoubleTypeCode;        /* default */
        }
        else
        {
          TypeCode = My->DefaultVariableType[i];
        }
      }
    }

    /* initialize the variable and add it to local chain */
    v = var_new (varname, TypeCode);
    UserFunction_addlocalvar (f, v);
    if (VAR_IS_STRING (v))
    {
      f->ParameterTypes |= (1 << f->ParameterCount);
    }
    f->ParameterCount++;        /* 0..32, 255 == (...) */
    if (f->ParameterCount > MAX_FARGS)
    {
      /* should have been declared VARIANT */
      WARN_SYNTAX_ERROR;
      return FALSE;
    }
  }
  while (line_skip_seperator (l));

  if (line_skip_RparenChar (l))
  {
    /* end of argument list */
    return TRUE;
  }

  /* FUNCTION ABC( A$, B$, */
  WARN_SYNTAX_ERROR;
  return FALSE;
}



/***************************************************************
  
   FUNCTION:       UserFunction_addlocalvar()
  
   DESCRIPTION:    This function adds a local variable
         to the FUNCTION-SUB lookup table at
         a specific level.
  
***************************************************************/

extern int
UserFunction_addlocalvar (UserFunctionType * f, VariableType * v)
{
   
  assert (f != NULL);
  assert (v != NULL);

  /* find end of local chain */
  if (f->local_variable == NULL)
  {
    f->local_variable = v;
  }
  else
  {
    VariableType *p;
    VariableType *c;

    p = f->local_variable;
    for (c = f->local_variable->next; c != NULL; c = c->next)
    {
      p = c;
    }
    p->next = v;
  }
  v->next = NULL;
  return TRUE;
}

LineType *
bwb_DEF8LBL (LineType * l)
{
  /*
   **
   ** this command is used for a line that is a user label
   **
   */
   
  assert (l != NULL);

  if (l->LineFlags & (LINE_USER))
  {
    WARN_ILLEGAL_DIRECT;
    return (l);
  }

  line_skip_eol (l);
  return (l);
}

/***************************************************************
  
        FUNCTION:       bwb_def()
  
        DESCRIPTION:    This C function implements the BASIC
                        DEF statement. 
  
   SYNTAX:     DEF FNname(arg...)] = expression
  
   NOTE:    It is not a strict requirement that the
         function name should begin with "FN".
  
***************************************************************/

LineType *
bwb_DEF (LineType * l)
{
   
  assert (l != NULL);

  if (l->LineFlags & (LINE_USER))
  {
    WARN_ILLEGAL_DIRECT;
    return (l);
  }

  /* this line will be executed by IntrinsicFunction_deffn() in bwb_fnc.c */
  line_skip_eol (l);

  return (l);
}



/***************************************************************
  
        FUNCTION:       bwb_call()
  
        DESCRIPTION: This C function implements the BASIC
            CALL subroutine command.
  
   SYNTAX:     CALL subroutine-name( param1, param2 )
  
***************************************************************/

LineType *
bwb_CALL (LineType * l)
{
  VariantType x;
  VariantType *X;
   
  assert (l != NULL);

  X = &x;
  CLEAR_VARIANT (X);
  /* Call the expression interpreter to evaluate the function */
  if (line_read_expression (l, X) == FALSE)        /* bwb_CALL */
  {
    WARN_SYNTAX_ERROR;
    goto EXIT;
  }
EXIT:
  RELEASE_VARIANT (X);
  return (l);
}


/***************************************************************
  
   FUNCTION:   find_label()
  
   DESCRIPTION:   This C function finds a program line that
         begins with the label included in <buffer>.
  
***************************************************************/
LineType *
find_line_number (int number)
{
  /* 
   ** 
   ** LABELS are resolved to their line number by the expresson parser.
   ** However, LABELS usually do not have the LINE_NUMBERED flag set.
   **
   */
  assert( My != NULL );
  assert( My->StartMarker != NULL );
  assert( My->EndMarker != NULL );
   

  if (MINLIN <= number && number <= MAXLIN)
  {
    /* 
     **
     ** brute force search 
     **
     */
    LineType *x;

    for (x = My->StartMarker->next; x != NULL && x != My->EndMarker && x->number < number;
         x = x->next);
    assert( x != NULL );
    if (x->number == number)
    {
      /* FOUND */
      return x;
    }
  }
  /* NOT FOUND */
  WARN_UNDEFINED_LINE;
  return NULL;
}

extern VariableType *
var_chain (VariableType * argv)
{
  /* create a variable chain */
  VariableType *argn;
   

  if (argv == NULL)
  {
    /* we are the first variable in the chain */
    if ((argn = (VariableType *) calloc (1, sizeof (VariableType))) == NULL)
    {
      WARN_OUT_OF_MEMORY;
      return NULL;
    }
  }
  else
  {
    /* find the end of the chain */
    assert( argv != NULL );
    for (argn = argv; argn->next != NULL; argn = argn->next);

    /* add ourself to the end */
    assert( argn != NULL );
    if ((argn->next =
         (VariableType *) calloc (1, sizeof (VariableType))) == NULL)
    {
      WARN_OUT_OF_MEMORY;
      return NULL;
    }
    argn = argn->next;
  }
  assert( argn != NULL );
  argn->next = NULL;

  /* return pointer to the variable just created */
  return argn;
}


/* EOF */
Un proyecto texto-plano.xyz