File: bwb_stc.c

package info (click to toggle)
bwbasic 2.20pl2-11
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 1,236 kB
  • sloc: ansic: 21,001; makefile: 80
file content (2306 lines) | stat: -rw-r--r-- 53,345 bytes parent folder | download | duplicates (8)
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
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
/***************************************************************

        bwb_stc.c       Commands Related to Structured Programming
                        for Bywater BASIC Interpreter

                        Commands:	CALL
					SUB
					FUNCTION
					END SUB
					END FUNCTION

                        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).                                  */
/*---------------------------------------------------------------*/

#include <stdio.h>
#include <ctype.h>

#include "bwbasic.h"
#include "bwb_mes.h"

/* prototypes */

#if ANSI_C
static int fslt_clear( void );
static int fslt_add( struct bwb_line *line, int *position, int code );
static struct bwb_line *fslt_findl( char *buffer );
static struct fslte    *fslt_findf( char *buffer );
static int scan_getcmd( struct bwb_line *line, int *position );
static int scan_readargs( struct fslte *f,
   struct bwb_line *line, int *position );
static int call_readargs( struct fslte *f,
   char *expression, int *position );
static int is_endsub( struct bwb_line *l );
static struct bwb_line *find_endsub( struct bwb_line *l );
static struct bwb_line *bwb_loopuntil( struct bwb_line *l );
struct bwb_variable *bwb_vtov( struct bwb_variable *dst, struct bwb_variable *src );
struct bwb_variable *bwb_etov( struct bwb_variable *dst, struct exp_ese *src );
struct bwb_variable *var_pos( struct bwb_variable *firstvar, int p );
int fslt_addcallvar( struct bwb_variable *v );
int fslt_addlocalvar( struct fslte *f, struct bwb_variable *v );
#else
static int fslt_clear();
static int fslt_add();
static struct bwb_line *fslt_findl();
static struct fslte    *fslt_findf();
static int scan_getcmd();
static int scan_readargs();
static int call_readargs();
static int is_endsub();
static struct bwb_line *find_endsub();
static struct bwb_line *bwb_loopuntil();
struct bwb_variable *bwb_vtov();
struct bwb_variable *bwb_etov();
struct bwb_variable *var_pos();
int fslt_addcallvar();
int fslt_addlocalvar();
#endif				/* ANSI_C for prototypes */

/***************************************************************

        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.

***************************************************************/

#if ANSI_C
int
bwb_scan( void )
#else
int
bwb_scan()
#endif
   {
   struct bwb_line *current;
   int position;
   int c;

#if PROG_ERRORS
   if ( CURTASK rescan != TRUE )
      {
      bwb_error( "in bwb_scan(): call to scan while CURTASK rescan != TRUE" );
      return FALSE;
      }
#endif

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_scan(): beginning scan..." );
   bwb_debug( bwb_ebuf );
#endif

   /* first run through the FUNCTION - SUB loopkup table
      and free any existing memory */

   fslt_clear();

   /* run through the list of lines and identify SUB and FUNCTION statements */

   for ( current = CURTASK bwb_start.next; current != &CURTASK bwb_end; current = current->next )
      {

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in bwb_scan(): scanning line <%d>", current->number );
      bwb_debug( bwb_ebuf );
#endif

      c = scan_getcmd( current, &position );
      if ( c == getcmdnum( CMD_SUB ))
         {
         fslt_add( current, &position, EXEC_CALLSUB );
         }
      else if ( c == getcmdnum( CMD_FUNCTION ))
         {
         fslt_add( current, &position, EXEC_FUNCTION );
         }
      else if ( c == getcmdnum( CMD_DEF ))
         {
         fslt_add( current, &position, EXEC_FUNCTION );
         }
#if STRUCT_CMDS
      else if ( c == getcmdnum( CMD_LABEL ))
         {
         fslt_add( current, &position, EXEC_LABEL );
         }
#endif
      }

   /* return */

   CURTASK rescan = FALSE;
   return TRUE;

   }

/***************************************************************

        FUNCTION:       fslt_clear()

        DESCRIPTION:	This C function clears all existing memory
        		in the FUNCTION-SUB lookup table.

***************************************************************/

#if ANSI_C
static int
fslt_clear( void )
#else
static int
fslt_clear()
#endif
   {
   struct fslte *current, *next;
   struct bwb_variable *c, *n;

   /* run through table and clear memory */

   next = CURTASK fslt_start.next;
   for ( current = CURTASK fslt_start.next; current != &CURTASK fslt_end;
      current = next )
      {

      /* check for local variables and free them */

      c = current->local_variable;
      while ( c != NULL )
         {
         n = c->next;

         /* Revised to FREE pass-thru call by JBV */
         FREE( c, "fslt_clear" );
         c = n;
         }

      next = current->next;

      /* Revised to FREE pass-thru calls by JBV */
      if (current->name != NULL)
      {
          FREE( current->name, "fslt_clear" ); /* JBV */
          current->name = NULL; /* JBV */
      }
      FREE( current, "fslt_clear" );
      current = NULL; /* JBV */
      }

   /* reset linkage */

   CURTASK fslt_start.next = &CURTASK fslt_end;

   return TRUE;
   }

/***************************************************************

        FUNCTION:       scan_getcmd()

        DESCRIPTION:	This command returns the command number
        		for the first BASIC command word encountered
        		in a line.

***************************************************************/

#if ANSI_C
static int
scan_getcmd( struct bwb_line *line, int *position )
#else
static int
scan_getcmd( line, position )
   struct bwb_line *line;
   int *position;
#endif
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];

   *position = 0;
   adv_ws( line->buffer, position );

   /* check for NULL line */

   if ( line->buffer[ *position ] == '\0' )
      {
      return -1;
      }

   /* check for line number and advance beyond it */

   if ( isdigit( line->buffer[ *position ] ))
      {
      scan_element( line->buffer, position, tbuf );
      }

   /* get the command element in the buffer */

   scan_element( line->buffer, position, tbuf );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in scan_getcmd(): scanning element <%s>", tbuf );
   bwb_debug( bwb_ebuf );
#endif

#if STRUCT_CMDS

   if ( is_label( tbuf ) == TRUE )
      {
#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in scan_getcmd(): found label <%s>", tbuf );
      bwb_debug( bwb_ebuf );
#endif

      return getcmdnum( CMD_LABEL );
      }

#endif

   bwb_strtoupper( tbuf );

   /* return command number */

   return getcmdnum( tbuf );

   }

/***************************************************************

	FUNCTION:       scan_element()

        DESCRIPTION:    This function reads characters in <buffer>
                        beginning at <pos> and advances past a
                        line element, incrementing <pos> appropri-
                        ately and returning the line element in
			<element>.

			This function is almost identical to adv_element(),
			but it will not stop at a full colon.  This is
			necessary to detect a label in the first element
			position.  If MULTISEG_LINES is defined as TRUE,
			adv_element() will stop at the colon, interpreting
			it as the end-of-segment marker.

***************************************************************/

#if ANSI_C
extern int
scan_element( char *buffer, int *pos, char *element )
#else
int
scan_element( buffer, pos, element )
   char *buffer;
   int *pos;
   char *element;
#endif
   {
   int loop;                                    /* control loop */
   int e_pos;                                   /* position in element buffer */
   int str_const;                               /* boolean: building a string constant */

   /* advance beyond any initial whitespace */

   adv_ws( buffer, pos );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in adv_element(): receieved <%s>.", &( buffer[ *pos ] ));
   bwb_debug( bwb_ebuf );
#endif

   /* now loop while building an element and looking for an
      element terminator */

   loop = TRUE;
   e_pos = 0;
   element[ e_pos ] = '\0';
   str_const = FALSE;

   while ( loop == TRUE )
      {
      switch( buffer[ *pos ] )
         {
         case ',':                      /* element terminators */
         case ';':
	 case '=':
	 case ' ':
         case '\t':
         case '\0':
         case '\n':
         case '\r':
            if ( str_const == TRUE )
               {
               element[ e_pos ] = buffer[ *pos ];
               ++e_pos;
               ++( *pos );
               element[ e_pos ] = '\0';
               }
            else
               {
               return TRUE;
               }
            break;

         case '\"':                     /* string constant */
            element[ e_pos ] = buffer[ *pos ];
            ++e_pos;
            ++( *pos );
            element[ e_pos ] = '\0';
            if ( str_const == TRUE )    /* termination of string constant */
               {
               return TRUE;
               }
            else                        /* beginning of string constant */
               {
               str_const = TRUE;
               }
            break;

         case '(':                     /* MID$ command termination (JBV) */
            /* If MID$ is here, get out */
            if (strcmp(element, CMD_MID) == 0)
               return TRUE;

            /* else add it to the accumulation element */
            element[ e_pos ] = buffer[ *pos ];
            ++e_pos;
            ++( *pos );
            element[ e_pos ] = '\0';
            break;

         default:
            element[ e_pos ] = buffer[ *pos ];
            ++e_pos;
            ++( *pos );
            element[ e_pos ] = '\0';
            break;
         }
      }

   /* This should not happen */

   return FALSE;

   }

/***************************************************************

        FUNCTION:       fslt_add()

        DESCRIPTION:	This C function adds an entry to the
        		FUNCTION-SUB lookup table.

***************************************************************/

#if ANSI_C
static int
fslt_add( struct bwb_line *line, int *position, int code )
#else
static int
fslt_add( line, position, code )
   struct bwb_line *line;
   int *position;
   int code;
#endif
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];
   char *name;
   struct bwb_variable *v;
   struct fslte *f, *n;
   int p;

   /* get the element for name */

   if ( code == EXEC_LABEL )
      {
      p = 0;
      scan_element( line->buffer, &p, tbuf );
      if ( isdigit( tbuf[ 0 ] ))
         {
	 scan_element( line->buffer, &p, tbuf );
         }
      tbuf[ strlen( tbuf ) - 1 ] = '\0';
      }
   else
      {
      adv_ws( line->buffer, position );
      exp_getvfname( &( line->buffer[ *position ] ), tbuf );
      *position += strlen( tbuf );
      }

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in fslt_add(): adding SUB/FUNCTION/LABEL code <%d> name <%s>",
      code, tbuf );
   bwb_debug( bwb_ebuf );
#endif

   /* get memory for name buffer */

   /* Revised to CALLOC pass-thru call by JBV */
   if ( ( name = CALLOC( 1, strlen( tbuf ) + 1, "fslt_add" ) ) == NULL )
      {
#if PROG_ERRORS
      bwb_error( "in fslt_add(): failed to get memory for name buffer" );
#else
      bwb_error( err_getmem );
#endif
      return FALSE;
      }

   strcpy( name, tbuf );

   /* get memory for fslt structure */

   if ( ( f = CALLOC( 1, sizeof( struct fslte ), "fslt_add" ) ) == NULL )
      {
#if PROG_ERRORS
      bwb_error( "in fslt_add(): failed to get memory for fslt structure" );
#else
      bwb_error( err_getmem );
#endif
      return FALSE;
      }

   /* fill in structure */

   f->line = line;
   f->name = name;
   f->code = code;
   f->local_variable = NULL;

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in fslt_add(): current buffer <%s>",
      &( line->buffer[ *position ] ) );
   bwb_debug( bwb_ebuf );
#endif

   /* read arguments */

   adv_ws( line->buffer, position );
   if ( line->buffer[ *position ] == '(' )
      {
      scan_readargs( f, line, position );
      }

   /* if function, add one more local variable expressing the name
      of the function */

   if ( code == EXEC_FUNCTION )
      {

      v = var_new( tbuf );
      fslt_addlocalvar( f, v );

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in fslt_add(): added function-name variable <%s>",
         v->name );
      bwb_debug( bwb_ebuf );
      getchar();
#endif

      }

   /* establish linkages */

   n = CURTASK fslt_start.next;
   CURTASK fslt_start.next = f;
   f->next = n;

   return TRUE;

   }

/***************************************************************

        FUNCTION:       scan_readargs()

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

***************************************************************/

#if ANSI_C
static int
scan_readargs( struct fslte *f, struct bwb_line *line, int *position )
#else
static int
scan_readargs( f, line, position )
   struct fslte *f;
   struct bwb_line *line;
   int *position;
#endif
   {
   int control_loop;
   struct bwb_variable *v;
   char tbuf[ MAXSTRINGSIZE + 1 ];

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in scan_readargs(): reading arguments, buffer <%s>",
      &( line->buffer[ *position ] ) );
   bwb_debug( bwb_ebuf );
#endif

   /* if we are at begin paren, advance */

   if ( line->buffer[ *position ] == '(' )
      {
      ++( *position );
      }

   /* loop through looking for arguments */

   control_loop = TRUE;
   adv_ws( line->buffer, position );
   while ( control_loop == TRUE )
      {

      switch( line->buffer[ *position ] )
         {
         case '\n':		/* premature end of line */
         case '\r':
         case '\0':
            control_loop = FALSE;
            f->startpos = *position;
            bwb_error( err_syntax );
            return FALSE;
         case ')':		/* end of argument list */
            ++( *position );
            control_loop = FALSE;
            f->startpos = *position;
            return TRUE;

         default:		/* presume beginning of argument == variable name */

            exp_getvfname( &( line->buffer[ *position ] ), tbuf );
            *position += strlen( tbuf );

#if INTENSIVE_DEBUG
            sprintf( bwb_ebuf, "in scan_readargs(): read argument <%s>",
               tbuf );
            bwb_debug( bwb_ebuf );
#endif

            /* initialize the variable and add it to local chain */

            v = var_new( tbuf );
            fslt_addlocalvar( f, v );

            /* advance past the comma */

            if ( line->buffer[ *position ] == ',' )
               {
               ++( *position );
               }

            break;
         }

      adv_ws( line->buffer, position );
      }

   return TRUE;

   }

/***************************************************************

        FUNCTION:       call_readargs()

        DESCRIPTION:	This C function reads arguments (variable
        		names for a subroutine CALL or function
			call.

***************************************************************/

#if ANSI_C
static int
call_readargs( struct fslte *f, char *expression, int *position )
#else
static int
call_readargs( f, expression, position )
   struct fslte *f;
   char *expression;
   int *position;
#endif
   {
   int control_loop;
   struct bwb_variable *v, *c;
   char tbuf[ MAXSTRINGSIZE + 1 ];
   int argument_counter;
   int local_pos, single_var;
   struct exp_ese *e;

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in call_readargs(): reading arguments, buffer <%s>",
      &( expression[ *position ] ) );
   bwb_debug( bwb_ebuf );
#endif

   /* if we are at begin paren, advance */

   if ( expression[ *position ] == '(' )
      {
      ++( *position );
      }

   /* loop through looking for arguments */

   control_loop = TRUE;
   argument_counter = 0;

   while ( control_loop == TRUE )
      {

      adv_ws( expression, position );

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in call_readargs(): in loop, buffer <%s>",
         &( expression[ *position ] ) );
      bwb_debug( bwb_ebuf );
#endif

      switch( expression[ *position ] )
         {
         case '\n':		/* end of line */
         case '\r':
	 case '\0':
#if MULTISEG_LINES
	 case ':':              /* end of segment */
#endif
            control_loop = FALSE;
            return FALSE;

         case ')':		/* end of argument list */
            ++( *position );
            control_loop = FALSE;
            return TRUE;

         default:		/* presume beginning of argument */

            /* read the first word to see if it is a single variable name */

            single_var = FALSE;
            exp_getvfname( &( expression[ *position ] ), tbuf );
            local_pos = *position + strlen( tbuf );

            adv_ws( expression, &local_pos );

#if INTENSIVE_DEBUG
            sprintf( bwb_ebuf, "in call_readargs(): in loop, tbuf <%s>",
               tbuf );
            bwb_debug( bwb_ebuf );
#endif

            /* check now for the single variable name */

            if ( strlen( tbuf ) == 0 )
               {
               single_var = FALSE;
               }

            else
               {
               switch ( expression[ local_pos ] )
                  {
                  case ')':		/* end of argument list */

#if INTENSIVE_DEBUG
		     sprintf( bwb_ebuf, "in call_readargs(): detected end of argument list" );
		     bwb_debug( bwb_ebuf );
#endif

		     ++local_pos;       /* and fall through */
                  case '\n':		/* end of line */
                  case '\r':
		  case '\0':
#if MULTISEG_LINES
		  case ':':             /* end of segment */
#endif
		     control_loop = FALSE;      /* and fall through */
						/* added 1993-06-16 */
		  case ',':             /* end of argument */

		     single_var = TRUE;

		     /* look for variable from previous (calling) level */

		     -- CURTASK exsc;
		     v = var_find( tbuf );      /* find variable there */
		     ++ CURTASK exsc;

		     c = var_pos( CURTASK excs[ CURTASK exsc ].local_variable,
                        argument_counter );	/* find local equivalent */
                     bwb_vtov( c, v );		/* assign calling value to local variable */

#if INTENSIVE_DEBUG
		     sprintf( bwb_ebuf, "in call_readargs(): variable name is <%s>, local name <%s>",
			v->name, c->name );
		     bwb_debug( bwb_ebuf );
#endif

		     *position = local_pos;
                     break;
                  default:
                     single_var = FALSE;
                     break;
                  }
               }

            if ( single_var == FALSE )
               {
#if INTENSIVE_DEBUG
               sprintf( bwb_ebuf, "in call_readargs(): in loop, parse expression <%s>",
                  &( expression[ *position ] ) );
               bwb_debug( bwb_ebuf );
#endif

               e = bwb_exp( expression, FALSE, position );	/* parse */

#if INTENSIVE_DEBUG
	       sprintf( bwb_ebuf, "in call_readargs(): in loop, parsed expression, buffer <%s>",
		  &( expression[ *position ] ) );
               bwb_debug( bwb_ebuf );
#endif

               v = var_pos( CURTASK excs[ CURTASK exsc ].local_variable,
                  argument_counter );		/* assign to variable */
               bwb_etov( v, e );		/* assign value */
               }

            /* add the variable to the calling variable chain */

            fslt_addcallvar( v );

#if INTENSIVE_DEBUG
            str_btoc( tbuf, var_getsval( v ));
            if ( single_var == TRUE )
               {
               sprintf( bwb_ebuf, "in call_readargs(): added arg <%d> (single) name <%s> value <%s>",
                  argument_counter, v->name, tbuf );
               }
            else
               {
               sprintf( bwb_ebuf, "in call_readargs(): added arg <%d> (expression) name <%s> value <%s>",
                  argument_counter, v->name, tbuf );
               }
            bwb_debug( bwb_ebuf );
            getchar();
#endif

            /* advance past comma if present */

            adv_ws( expression, position );
            if ( expression[ *position ] == ',' )
               {
               ++( *position );
               }

            break;
         }

      ++argument_counter;

      }

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in call_readargs(): exiting function" );
   bwb_debug( bwb_ebuf );
#endif

   return TRUE;

   }

/***************************************************************

        FUNCTION:       fslt_findl()

        DESCRIPTION:	This C function finds a line corresponding
        		to a name in the FUNCTION-SUB lookup
			table.

***************************************************************/

#if ANSI_C
static struct bwb_line *
fslt_findl( char *buffer )
#else
static struct bwb_line *
fslt_findl( buffer )
   char *buffer;
#endif
   {
   struct fslte *r;

   r = fslt_findf( buffer );

   return r->line;

   }

/***************************************************************

        FUNCTION:       fslt_findf()

        DESCRIPTION:	This C function finds an fslte structure
			corresponding to a name in the FUNCTION-
			SUB lookup table.

***************************************************************/

#if ANSI_C
static struct fslte *
fslt_findf( char *buffer )
#else
static struct fslte *
fslt_findf( buffer )
   char *buffer;
#endif
   {
   struct fslte *f;
   register int c;

   /* remove open-paren from string */

   for ( c = 0; buffer[ c ] != '\0'; ++c )
      {
      if ( buffer[ c ] == '(' )
         {
         buffer[ c ] = '\0';
         }
      }

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in fslt_findf(): search for name <%s>", buffer );
   bwb_debug( bwb_ebuf );
#endif

   /* run through the table */

   for ( f = CURTASK fslt_start.next; f != &CURTASK fslt_end; f = f->next )
      {
      if ( strcmp( f->name, buffer ) == 0 )
         {
         return f;
         }
      }

   /* search has failed */

#if PROG_ERRORS
   sprintf( bwb_ebuf, "in fslt_findf(): failed to find Function/Subroutine <%s>",
      buffer );
   bwb_error( bwb_ebuf );
#else
   bwb_error( err_lnnotfound );
#endif

   return NULL;

   }

/***************************************************************

        FUNCTION:       bwb_def()

        DESCRIPTION:    This C function implements the BASIC
                        DEF statement. Since DEF and FUNCTION
			are equivalent, it simply passes execution
			to bwb_function().

	SYNTAX:		DEF FNname(arg...)] = expression

	NOTE:		It is not a strict requirement that the
			function name should begin with "FN".

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_def( struct bwb_line *l )
#else
struct bwb_line *
bwb_def( l )
   struct bwb_line *l;
#endif
   {

#if MULTISEG_LINES
   adv_eos( l->buffer, &( l->position ));
#endif

   return bwb_zline( l );
   }

#if STRUCT_CMDS

/***************************************************************

        FUNCTION:       bwb_function()

        DESCRIPTION:	This C function implements the BASIC
			FUNCTION and DEF commands.

	SYNTAX:		FUNCTION function-definition

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_function( struct bwb_line *l )
#else
struct bwb_line *
bwb_function( l )
   struct bwb_line *l;
#endif
   {

   return bwb_def( l );

   }

/***************************************************************

        FUNCTION:       bwb_endfnc()

        DESCRIPTION:	This C function implements the BASIC
			END FUNCTION command, ending a subroutine
			definition.  Because the command END
			can have multiple meanings, this function
			should be called from the bwb_xend()
			function, which should be able to identify
			an END FUNCTION command.

	SYNTAX:		END FUNCTION

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_endfnc( struct bwb_line *l )
#else
struct bwb_line *
bwb_endfnc( l )
   struct bwb_line *l;
#endif
   {
   struct bwb_variable *local;
   register int c;

   /* assign local variable values to calling variables */

   local = CURTASK excs[ CURTASK exsc ].local_variable;
   for ( c = 0; c < CURTASK excs[ CURTASK exsc ].n_cvs; ++c )
      {
      bwb_vtov( CURTASK excs[ CURTASK exsc ].calling_variable[ c ], local );
      local = local->next;
      }

   /* decrement the EXEC stack counter */

   bwb_decexec();

   /* and return next from old line */

   CURTASK excs[ CURTASK exsc ].line->next->position = 0;
   return CURTASK excs[ CURTASK exsc ].line->next;

   }

/***************************************************************

        FUNCTION:       bwb_call()

        DESCRIPTION:	This C function implements the BASIC
        		CALL subroutine command.

	SYNTAX:		CALL subroutine-name

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_call( struct bwb_line *l )
#else
struct bwb_line *
bwb_call( l )
   struct bwb_line *l;
#endif
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];
   struct bwb_line *call_line;
   struct fslte *f;

   adv_element( l->buffer, &( l->position ), tbuf );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_call(): call to subroutine <%s>", tbuf );
   bwb_debug( bwb_ebuf );
#endif

   /* find the line to call */

   call_line = fslt_findl( tbuf );
   f = fslt_findf( tbuf );

   if ( call_line == NULL )
      {
      return bwb_zline( l );
      }

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_call(): found line <%s>",
      call_line->buffer );
   bwb_debug( bwb_ebuf );
#endif

   /* save the old position on the EXEC stack */

   bwb_setexec( l, l->position, CURTASK excs[ CURTASK exsc ].code );

   /* increment and set new EXEC stack */

   bwb_incexec();
   call_line->position = 0;
   bwb_setexec( call_line, 0, EXEC_CALLSUB );

   /* attach local variables */

   CURTASK excs[ CURTASK exsc ].local_variable = f->local_variable;

   /* read calling variables for this call */

   call_readargs( f, l->buffer, &( l->position ) );

   return call_line;

   }

/***************************************************************

        FUNCTION:       bwb_sub()

	DESCRIPTION:    This function implements the BASIC
			SUB command, introducing a named
			subroutine.

	SYNTAX:		SUB subroutine-name
			(followed by subroutine definition ending
			with END SUB).

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_sub( struct bwb_line *l )
#else
struct bwb_line *
bwb_sub( l )
   struct bwb_line *l;
#endif
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];
   struct bwb_line *rline;
#if MULTISEG_LINES
   struct fslte *f;
#endif

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_sub(): entered function at exec level <%d>",
      CURTASK exsc );
   bwb_debug( bwb_ebuf );
#endif

   /* check current exec level: if 1 then only MAIN should be executed */

   if ( CURTASK exsc == 0 )
      {
      adv_element( l->buffer, &( l->position ), tbuf );
      bwb_strtoupper( tbuf );
      if ( strcmp( tbuf, "MAIN" ) == 0 )
         {
#if INTENSIVE_DEBUG
         sprintf( bwb_ebuf, "in bwb_sub(): found MAIN function at level 0" );
         bwb_debug( bwb_ebuf );
#endif

         bwb_incexec();

         bwb_setexec( l->next, 0, EXEC_MAIN );

         return bwb_zline( l );

         }

      /* if a MAIN function was not found at level 0, then skip the subroutine */

      else
         {

#if INTENSIVE_DEBUG
         sprintf( bwb_ebuf, "in bwb_sub(): found non-MAIN function at level 0" );
         bwb_debug( bwb_ebuf );
#endif

         rline = find_endsub( l );
         bwb_setexec( rline->next, 0, EXEC_CALLSUB );
         rline->next->position = 0;
         return rline->next;
         }
      }

   /* check for integrity of CALL-SUB sequence if above level 0 */

   else if ( CURTASK excs[ CURTASK exsc ].code != EXEC_CALLSUB )
      {
#if PROG_ERRORS
      sprintf( bwb_ebuf, "in bwb_sub(): SUB without CALL" );
      bwb_error( bwb_ebuf );
#else
      bwb_error( err_retnogosub );
#endif
      }

   /* advance position */

#if MULTISEG_LINES
   adv_ws( l->buffer, &( l->position ));
   adv_element( l->buffer, &( l->position ), tbuf );
   f = fslt_findf( tbuf );

   l->position = f->startpos;

   return bwb_zline( l );
#else
   return bwb_zline( l );
#endif
   }

/***************************************************************

        FUNCTION:       find_endsub()

        DESCRIPTION:    This function searches for a line containing
                        an END SUB statement corresponding to a previous
                        SUB statement.

***************************************************************/

#if ANSI_C
static struct bwb_line *
find_endsub( struct bwb_line *l )
#else
static struct bwb_line *
find_endsub( l )
   struct bwb_line *l;
#endif
   {
   struct bwb_line *current;
   register int s_level;
   int position;

   s_level = 1;
   for ( current = l->next; current != &CURTASK bwb_end; current = current->next )
      {
      position = 0;
      if ( current->marked != TRUE )
         {
         line_start( current->buffer, &position, &( current->lnpos ),
            &( current->lnum ),
            &( current->cmdpos ),
            &( current->cmdnum ),
            &( current->startpos ) );
         }
      current->position = current->startpos;

      if ( current->cmdnum > -1 )
         {

         if ( bwb_cmdtable[ current->cmdnum ].vector == bwb_sub )
            {
            ++s_level;

#if INTENSIVE_DEBUG
            sprintf( bwb_ebuf, "in find_endsub(): found SUB at line %d, level %d",
               current->number, s_level );
            bwb_debug( bwb_ebuf );
#endif

            }
         else if ( is_endsub( current ) == TRUE )
            {
            --s_level;

#if INTENSIVE_DEBUG
            sprintf( bwb_ebuf, "in find_endsub(): found END SUB at line %d, level %d",
               current->number, s_level );
            bwb_debug( bwb_ebuf );
#endif

            if ( s_level == 0 )
               {
               return current;
               }
            }

         }
      }

#if PROG_ERRORS
   sprintf( bwb_ebuf, "SUB without END SUB" );
   bwb_error( bwb_ebuf );
#else
   bwb_error( err_syntax  );
#endif

   return NULL;

   }

/***************************************************************

	FUNCTION:       is_endsub()

	DESCRIPTION:    This function determines whether the
			line buffer for line 'l' is positioned
			at an END SUB statement.

***************************************************************/

#if ANSI_C
static int
is_endsub( struct bwb_line *l )
#else
static int
is_endsub( l )
   struct bwb_line *l;
#endif
   {
   int position;
   char tbuf[ MAXVARNAMESIZE + 1];

   if ( bwb_cmdtable[ l->cmdnum ].vector != bwb_xend )
      {
      return FALSE;
      }

   position = l->startpos;
   adv_ws( l->buffer, &position );
   adv_element( l->buffer, &position, tbuf );
   bwb_strtoupper( tbuf );

   if ( strcmp( tbuf, "SUB" ) == 0 )
      {
      return TRUE;
      }

   return FALSE;

   }

/***************************************************************

        FUNCTION:       bwb_endsub()

        DESCRIPTION:	This C function implements the BASIC
			END SUB command, ending a subroutine
			definition.  Because the command END
			can have multiple meanings, this function
			should be called from the bwb_xend()
			function, which should be able to identify
			an END SUB command.

	SYNTAX:		END SUB

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_endsub( struct bwb_line *line )
#else
struct bwb_line *
bwb_endsub( line )
   struct bwb_line *line;
#endif
   {
   struct bwb_variable *l;
   register int c;

   /* assign local variable values to calling variables */

   l = CURTASK excs[ CURTASK exsc ].local_variable;
   for ( c = 0; c < CURTASK excs[ CURTASK exsc ].n_cvs; ++c )
      {
      bwb_vtov( CURTASK excs[ CURTASK exsc ].calling_variable[ c ], l );
      l = l->next;
      }

   /* decrement the EXEC stack counter */

   bwb_decexec();

   /* if the previous level was EXEC_MAIN,
      then execution continues from this point */

   if ( CURTASK excs[ CURTASK exsc + 1 ].code == EXEC_MAIN )
      {
      return bwb_zline( line );
      }

   /* else return next from old line */

   CURTASK excs[ CURTASK exsc ].line->next->position = 0;
   return CURTASK excs[ CURTASK exsc ].line->next;

   }

/***************************************************************

	FUNCTION:	find_label()

	DESCRIPTION:	This C function finds a program line that
			begins with the label included in <buffer>.

***************************************************************/

#if ANSI_C
extern struct bwb_line *
find_label( char *buffer )
#else
extern struct bwb_line *
find_label( buffer )
   char *buffer;
#endif
   {
   struct fslte *f;

   for ( f = CURTASK fslt_start.next; f != & ( CURTASK fslt_end ); f = f->next )
      {
      if ( strcmp( buffer, f->name ) == 0 )
         {
#if INTENSIVE_DEBUG
         sprintf( bwb_ebuf, "in find_label(): found label <%s>", buffer );
         bwb_debug( bwb_ebuf );
#endif
         return f->line;
         }
      }

#if PROG_ERRORS
   sprintf( bwb_ebuf, "in find_label(): failed to find label <%s>", buffer );
   bwb_error( bwb_ebuf );
#else
   bwb_error( err_lnnotfound );
#endif

   return NULL;

   }

/***************************************************************

	FUNCTION:       bwb_doloop()

	DESCRIPTION:    This C function implements the ANSI BASIC
			DO statement, when DO is not followed by
			an argument.  It is called by bwb_do() in
			bwb_cmd.c.

	SYNTAX:		DO

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_doloop( struct bwb_line *l )
#else
struct bwb_line *
bwb_doloop( l )
   struct bwb_line *l;
#endif
   {

   /* if this is the first time at this DO statement, note it */

   if ( CURTASK excs[ CURTASK exsc ].while_line != l )
      {

      bwb_incexec();
      CURTASK excs[ CURTASK exsc ].while_line = l;

      /* find the LOOP statement */

      CURTASK excs[ CURTASK exsc ].wend_line = find_loop( l );

      if ( CURTASK excs[ CURTASK exsc ].wend_line == NULL )
	 {
	 return bwb_zline( l );
	 }

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in bwb_doloop(): initialize DO loop, line <%d>",
	 l->number );
      bwb_debug( bwb_ebuf );
#endif

      }
#if INTENSIVE_DEBUG
   else
      {
      sprintf( bwb_ebuf, "in bwb_doloop(): return to DO loop, line <%d>",
	 l->number );
      bwb_debug( bwb_ebuf );
      }
#endif

   bwb_setexec( l, l->position, EXEC_DO );
   return bwb_zline( l );
   }

/***************************************************************

        FUNCTION:       bwb_loop()

	DESCRIPTION:    This C function implements the ANSI BASIC
			LOOP statement.

	SYNTAX:		LOOP [UNTIL expression]
			
***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_loop( struct bwb_line *l )
#else
struct bwb_line *
bwb_loop( l )
   struct bwb_line *l;
#endif
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_loop(): entered subroutine" );
   bwb_debug( bwb_ebuf );
#endif

   /* If the current exec stack is set for EXEC_WHILE, then we
      presume that this is a LOOP statement ending a DO WHILE
      loop */

   if ( CURTASK excs[ CURTASK exsc ].code == EXEC_WHILE )
      {
      return bwb_wend( l );
      }

   /* check integrity of DO loop */

   if ( CURTASK excs[ CURTASK exsc ].code != EXEC_DO )
      {
#if PROG_ERRORS
      sprintf( bwb_ebuf, "in bwb_loop(): exec stack code != EXEC_DO" );
      bwb_error( bwb_ebuf );
#else
      bwb_error( err_syntax );
#endif
      }

   if ( CURTASK excs[ CURTASK exsc ].while_line == NULL )
      {
#if PROG_ERRORS
      sprintf( bwb_ebuf, "in bwb_loop(): exec stack while_line == NULL" );
      bwb_error( bwb_ebuf );
#else
      bwb_error( err_syntax );
#endif
      }

   /* advance to find the first argument */

   adv_element( l->buffer, &( l->position ), tbuf );
   bwb_strtoupper( tbuf );

   /* detect a LOOP UNTIL structure */

   if ( strcmp( tbuf, CMD_XUNTIL ) == 0 )
      {

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in bwb_loop(): detected LOOP UNTIL" );
      bwb_debug( bwb_ebuf );
#endif

      return bwb_loopuntil( l );

      }

   /* LOOP does not have UNTIL */

   else
      {

      /* reset to the top of the current DO loop */

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in bwb_loop() return to line <%d>",
	 CURTASK excs[ CURTASK exsc ].while_line->number );
      bwb_debug( bwb_ebuf );
#endif

      CURTASK excs[ CURTASK exsc ].while_line->position = 0;
      bwb_setexec( CURTASK excs[ CURTASK exsc ].while_line, 0, EXEC_DO );

      return CURTASK excs[ CURTASK exsc ].while_line;

      }

   }

/***************************************************************

	FUNCTION:       bwb_loopuntil()

	DESCRIPTION:    This C function implements the ANSI BASIC
			LOOP UNTIL statement and is called by
			bwb_loop().

***************************************************************/

#if ANSI_C
static struct bwb_line *
bwb_loopuntil( struct bwb_line *l )
#else
static struct bwb_line *
bwb_loopuntil( l )
   struct bwb_line *l;
#endif
   {
   struct exp_ese *e;
   struct bwb_line *r;

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_loopuntil(): entered subroutine" );
   bwb_debug( bwb_ebuf );
#endif

   /* call bwb_exp() to interpret the expression */

   e = bwb_exp( l->buffer, FALSE, &( l->position ) );

   if ( (int) exp_getnval( e ) != FALSE ) /* Was == TRUE (JBV 10/1996) */
      {
      CURTASK excs[ CURTASK exsc ].while_line = NULL;
      r = CURTASK excs[ CURTASK exsc ].wend_line;
      bwb_setexec( r, 0, CURTASK excs[ CURTASK exsc - 1 ].code );
      r->position = 0;
      bwb_decexec();
      return r;
      }

   /* condition is false: loop around to DO again */

   else
      {

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in bwb_loopuntil() return to line <%d>",
	 CURTASK excs[ CURTASK exsc ].while_line->number );
      bwb_debug( bwb_ebuf );
#endif

      CURTASK excs[ CURTASK exsc ].while_line->position = 0;
      bwb_setexec( CURTASK excs[ CURTASK exsc ].while_line, 0, EXEC_DO );

      return CURTASK excs[ CURTASK exsc ].while_line;

      }

   }

/***************************************************************

        FUNCTION:       bwb_exit()

	DESCRIPTION:    This C function implements the BASIC EXIT
			statement, calling subroutines for either
			EXIT FOR or EXIT DO.

	SYNTAX:		EXIT FOR|DO

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_exit( struct bwb_line *l )
#else
struct bwb_line *
bwb_exit( l )
   struct bwb_line *l;
#endif
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_exit(): entered subroutine" );
   bwb_debug( bwb_ebuf );
#endif

   adv_element( l->buffer, &( l->position ), tbuf );
   bwb_strtoupper( tbuf );

   if ( strcmp( tbuf, CMD_XFOR ) == 0 )
      {

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in bwb_exit(): detected EXIT FOR" );
      bwb_debug( bwb_ebuf );
#endif

      return bwb_exitfor( l );
      }

   if ( strcmp( tbuf, CMD_XDO ) == 0 )
      {
      return bwb_exitdo( l );
      }

#if PROG_ERRORS
   sprintf( bwb_ebuf, "in bwb_exit(): Nonsense or nothing following EXIT" );
   bwb_error( bwb_ebuf );
#else
   bwb_error( err_syntax );
#endif

   return bwb_zline( l );
   }

/***************************************************************

	FUNCTION:       bwb_exitdo()

	DESCRIPTION:    This function handles the BASIC EXIT
			DO statement.  This is a structured
			programming command compatible with ANSI
			BASIC. It is called from the bwb_exit()
			subroutine.

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_exitdo( struct bwb_line *l )
#else
struct bwb_line *
bwb_exitdo( l )
   struct bwb_line *l;
#endif
   {
   struct bwb_line *next_line;
   int found;
   register int level;

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_exitdo(): entered subroutine" );
   bwb_debug( bwb_ebuf );
#endif

   /* Check the integrity of the DO statement */

   found = FALSE;
   level = CURTASK exsc;
   do
      {
      if ( CURTASK excs[ level ].code == EXEC_DO )
	 {
	 next_line = CURTASK excs[ CURTASK level ].wend_line;
	 found = TRUE;
	 }
      else
	 {
	 --level;
	 }
      }
   while ( ( level >= 0 ) && ( found == FALSE ) );

   if ( found != TRUE )
      {

#if PROG_ERRORS
      /* JBV 1/97 (was "bwb_exitfor") */
      sprintf( bwb_ebuf, "in bwb_exitdo(): EXIT DO without DO" );
      bwb_error( bwb_ebuf );
#else
      bwb_error( err_syntax );
#endif

      return bwb_zline( l );

      }

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_exitdo(): level found is <%d>, current <%d>",
      level, CURTASK exsc );
   bwb_debug( bwb_ebuf );
#endif

   /* decrement below the level of the NEXT statement */

   while( CURTASK exsc >= level )
      {
      bwb_decexec();
      }

   /* set the next line in the exec stack */

   next_line->position = 0;
   bwb_setexec( next_line, 0, EXEC_NORM );

   return next_line;

   }

#endif					/* STRUCT_CMDS */

/***************************************************************

	FUNCTION:       bwb_vtov()

	DESCRIPTION:    This function assigns the value of one
			bwBASIC variable (src) to the value of another
			bwBASIC variable (dst).

***************************************************************/

#if ANSI_C
struct bwb_variable *
bwb_vtov( struct bwb_variable *dst,
   struct bwb_variable *src )
#else
struct bwb_variable *
bwb_vtov( dst, src )
   struct bwb_variable *dst;
   struct bwb_variable *src;
#endif
   {

   if ( dst == src )
      {
      return dst;
      }

   if ( src->type != dst->type )
      {
#if PROG_ERRORS
      sprintf( bwb_ebuf, "in bwb_vtov(): mismatch src <%s> type <%d> dst <%s> type <%d>",
	 src->name, src->type, dst->name, dst->type );
      bwb_error( bwb_ebuf );
#else
      bwb_error( err_mismatch );
#endif
      return NULL;
      }

   if ( dst->type == NUMBER )
      {

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in bwb_vtov(): assigning var <%s> val <%lf> to var <%s>",
	 src->name, var_getnval( src ), dst->name );
      bwb_debug( bwb_ebuf );
#endif

      * var_findnval( dst, dst->array_pos ) = var_getnval( src );
      }
   else
      {
      str_btob( var_getsval( dst ), var_getsval( src ) );
      }

   return dst;
   }

/***************************************************************

	FUNCTION:       bwb_etov()

	DESCRIPTION:    This function assigns the value of a
			bwBASIC expression stack element (src)
			to the value of a bwBASIC variable (dst).

***************************************************************/

#if ANSI_C
struct bwb_variable *
bwb_etov( struct bwb_variable *dst, struct exp_ese *src )
#else
struct bwb_variable *
bwb_etov( dst, src )
   struct bwb_variable *dst;
   struct exp_ese *src;
#endif
   {

   if ( (int) src->type != dst->type )
      {
#if PROG_ERRORS
      sprintf( bwb_ebuf, "in bwb_etov(): mismatch src <%d> dst <%d>",
         src->type, dst->type );
      bwb_error( bwb_ebuf );
#else
      bwb_error( err_mismatch );
#endif
      return NULL;
      }

   if ( dst->type == NUMBER )
      {
      * var_findnval( dst, dst->array_pos ) = exp_getnval( src );
      }
   else
      {
      str_btob( var_getsval( dst ), exp_getsval( src ) );
      }

   return dst;
   }

/***************************************************************

	FUNCTION:       var_pos()

	DESCRIPTION:    This function returns the name of a
			local variable at a specified position
			in the local variable list.

***************************************************************/

#if ANSI_C
struct bwb_variable *
var_pos( struct bwb_variable *firstvar, int p )
#else
struct bwb_variable *
var_pos( firstvar, p )
   struct bwb_variable *firstvar;
   int p;
#endif
   {
   register int c;
   struct bwb_variable *v;

   v = firstvar;
   for ( c = 0; c != p; ++c )
      {
      v = v->next;
      }

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in var_pos(): returning pos <%d> variable <%s>",
      p, v->name );
   bwb_debug( bwb_ebuf );
#endif

   return v;
   }

/***************************************************************

	FUNCTION:       fslt_addcallvar()

	DESCRIPTION:    This function adds a calling variable
			to the FUNCTION-SUB lookup table at
			a specific level.

***************************************************************/

#if ANSI_C
int
fslt_addcallvar( struct bwb_variable *v )
#else
int
fslt_addcallvar( v )
   struct bwb_variable *v;
#endif
   {

   if ( CURTASK excs[ CURTASK exsc ].n_cvs >= MAX_FARGS )
      {
#if PROG_ERRORS
      sprintf( bwb_ebuf, "in fslt_addcallvar(): Maximum number of Function Args Exceeded" );
      bwb_error( bwb_ebuf );
#else
      bwb_error( err_overflow );
#endif
      }

   CURTASK excs[ CURTASK exsc ].calling_variable[ CURTASK excs[ CURTASK exsc ].n_cvs ] = v;
   ++CURTASK excs[ CURTASK exsc ].n_cvs;

   return TRUE;

   }

/***************************************************************

	FUNCTION:       exp_ufnc()

        DESCRIPTION:	This C function interprets a user-defined
			function, returning its value at the current
			level of the expression stack.

***************************************************************/

#if ANSI_C
int
exp_ufnc( char *expression )
#else
int
exp_ufnc( expression )
   char *expression;
#endif
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];
   struct bwb_line *call_line;
   struct fslte *f, *c;
   struct bwb_variable *v, *r;
   struct exp_ese *e;
   int save_elevel;
   int position, epos;
#if INTENSIVE_DEBUG
   register int i;
#endif

   position = 0;

   /* get the function name in tbuf */

   exp_getvfname( expression, tbuf );

   /* find the function name in the function-subroutine lookup table */

   for ( f = CURTASK fslt_start.next; f != &CURTASK fslt_end; f = f->next )
      {
      if ( strcmp( f->name, tbuf ) == 0 )
	 {
#if INTENSIVE_DEBUG
	 sprintf( bwb_ebuf, "in exp_ufnc(): found user function <%s>",
	    tbuf );
	 bwb_debug( bwb_ebuf );
#endif
	 c = f; 		/* current function-subroutine lookup table element */
	 call_line = f->line;	/* line to call for function */
	 }
      }

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in exp_ufnc(): call to function <%s>", tbuf );
   bwb_debug( bwb_ebuf );
#endif

   position += strlen( tbuf );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in exp_ufnc(): found line <%s>",
      call_line->buffer );
   bwb_debug( bwb_ebuf );
#endif

   /* save the old position on the EXEC stack */

   bwb_setexec( CURTASK excs[ CURTASK exsc ].line,
      position, CURTASK excs[ CURTASK exsc ].code );
   save_elevel = CURTASK exsc;

   /* increment and set new EXEC stack */

   bwb_incexec();
   call_line->position = 0;
   bwb_setexec( call_line, 0, EXEC_FUNCTION );

   /* attach local variables */

   CURTASK excs[ CURTASK exsc ].local_variable = c->local_variable;

#if INTENSIVE_DEBUG
   i = 0;
   sprintf( bwb_ebuf, "in exp_ufnc(): <%s> attached local variables EXEC level <%d>",
      tbuf, CURTASK exsc );
   bwb_debug( bwb_ebuf );
   for ( v = CURTASK excs[ CURTASK exsc ].local_variable; v != NULL; v = v->next )
      {
      sprintf( bwb_ebuf, "in exp_ufnc():   <%s> level <%d> variable <%d> name <%s>",
	 tbuf, CURTASK exsc, i, v->name );
      bwb_debug( bwb_ebuf );
      ++i;
      }
   getchar();
#endif

   /* read calling variables for this call */

   call_readargs( c, expression, &position );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in exp_ufnc(): current buffer <%s>",
      &( call_line->buffer[ c->startpos ] ) );
   bwb_debug( bwb_ebuf );
#endif

   /* determine if single-line function */

   epos = c->startpos;
   adv_ws( call_line->buffer, &epos );
   if ( call_line->buffer[ epos ] == '=' )
      {

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in exp_ufnc(): found SINGLE-LINE function" );
      bwb_debug( bwb_ebuf );
#endif

      ++epos;
      call_line->position = epos;
      bwb_setexec( call_line, epos, EXEC_FUNCTION );

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in exp_ufnc(): single line: parse <%s>",
	 &( call_line->buffer[ epos ] ) );
      bwb_debug( bwb_ebuf );
#endif

      e = bwb_exp( call_line->buffer, FALSE, &epos );
      v = var_find( tbuf );

#if INTENSIVE_DEBUG
      if ( e->type == STRING )
	 {
	 sprintf( bwb_ebuf, "in exp_ufnc(): expression returns <%d>-byte string",
	    exp_getsval( e )->length );
	 bwb_debug( bwb_ebuf );
	 }
      else
	 {
	 sprintf( bwb_ebuf, "in exp_ufnc(): expression returns number <%lf>",
	    (double) exp_getnval( e ) );
	 bwb_debug( bwb_ebuf );
	 }
#endif

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in exp_ufnc(): single line after parsing, <%s>",
	 &( call_line->buffer[ epos ] ) );
      bwb_debug( bwb_ebuf );
#endif

      bwb_etov( v, e );
      bwb_decexec();
      }

   /* multi-line function must be executed now */

   else
      {

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in exp_ufnc(): found MULTI-LINE function" );
      bwb_debug( bwb_ebuf );
#endif

      /* now execute until function is resolved */

      bwb_execline();
      while( CURTASK exsc > save_elevel )
	 {
	 bwb_execline();
	 }

      /* find the return value */

      for ( r = c->local_variable; r != NULL; r = r->next )
	 {
	 if ( strcmp( r->name, c->name ) == 0 )
	    {
	    v = r;
	    }
	 }

      }

   /* now place value in expression stack */

   CURTASK exps[ CURTASK expsc ].type = (char) v->type;
   CURTASK exps[ CURTASK expsc ].pos_adv = position;

   switch( v->type )
      {
      case STRING:
	 CURTASK exps[ CURTASK expsc ].operation = CONST_STRING;

#if INTENSIVE_DEBUG
	 sprintf( bwb_ebuf, "in exp_ufnc(): ready to assign <%d>-byte STRING",
	    var_getsval( v )->length );
	 bwb_debug( bwb_ebuf );
#endif

	 str_btob( exp_getsval( &( CURTASK exps[ CURTASK expsc ] )),
	    var_getsval( v ) );

#if INTENSIVE_DEBUG
	 str_btoc( tbuf, var_getsval( v ) );
	 sprintf( bwb_ebuf, "in exp_ufnc(): string assigned <%s>", tbuf );
	 bwb_debug( bwb_ebuf );
#endif

	 break;

      default:
	 CURTASK exps[ CURTASK expsc ].operation = NUMBER;
	 CURTASK exps[ CURTASK expsc ].nval = var_getnval( v );
	 break;
      }

   return TRUE;

   }

/***************************************************************

	FUNCTION:       fslt_addlocalvar()

	DESCRIPTION:    This function adds a local variable
			to the FUNCTION-SUB lookup table at
			a specific level.

***************************************************************/

#if ANSI_C
int
fslt_addlocalvar( struct fslte *f, struct bwb_variable *v )
#else
int
fslt_addlocalvar( f, v )
   struct fslte *f;
   struct bwb_variable *v;
#endif
   {
   struct bwb_variable *c, *p;
#if INTENSIVE_DEBUG
   register int i;
#endif

   /* find end of local chain */

   if ( f->local_variable == NULL )
      {
#if INTENSIVE_DEBUG
      i = 0;
#endif
      f->local_variable = v;
      }
   else
      {
#if INTENSIVE_DEBUG
      i = 1;
#endif
      p = f->local_variable;
      for ( c = f->local_variable->next; c != NULL; c = c->next )
         {
         p = c;
#if INTENSIVE_DEBUG
         ++i;
#endif
         }
      p->next = v;
      }

   v->next = NULL;

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in fslt_addlocalvar(): added local variable variable <%s> arg number <%d>",
      v->name, i );
   bwb_debug( bwb_ebuf );
   getchar();
#endif

   return TRUE;

   }

/***************************************************************

	FUNCTION:       fslt_init()

	DESCRIPTION:    This function initializes the FUNCTION-SUB
			lookup table.

***************************************************************/

#if ANSI_C
int
fslt_init( int task )
#else
int
fslt_init( task )
   int task;
#endif
   {
   LOCALTASK fslt_start.next = &(LOCALTASK fslt_end);
   return TRUE;
   }

/***************************************************************

	FUNCTION:       is_label()

	DESCRIPTION:    This function determines whether the string
			pointed to by 'buffer' is a label (i.e.,
			ends with colon).

***************************************************************/

#if ANSI_C
extern int
is_label( char *buffer )
#else
int
is_label( buffer )
   char *buffer;
#endif
   {

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in is_label(): check element <%s>", buffer );
   bwb_debug( bwb_ebuf );
#endif

   if ( buffer[ strlen( buffer ) - 1 ] == ':' )
      {
      return TRUE;
      }
   else
      {
      return FALSE;
      }

   }