aboutsummaryrefslogtreecommitdiff
path: root/src/bc_parse.c
blob: c2fc2186a065ccdd14442e05d97e5d1d97776733 (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
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
2307
2308
2309
2310
2311
2312
2313
/*
 * *****************************************************************************
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * *****************************************************************************
 *
 * The parser for bc.
 *
 */

#if BC_ENABLED

#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include <setjmp.h>

#include <bc.h>
#include <num.h>
#include <vm.h>

// Before you embark on trying to understand this code, have you read the
// Development manual (manuals/development.md) and the comment in include/bc.h
// yet? No? Do that first. I'm serious.
//
// The reason is because this file holds the most sensitive and finicky code in
// the entire codebase. Even getting history to work on Windows was nothing
// compared to this. This is where dreams go to die, where dragons live, and
// from which Ken Thompson himself would flee.

static void bc_parse_else(BcParse *p);
static void bc_parse_stmt(BcParse *p);
static BcParseStatus bc_parse_expr_err(BcParse *p, uint8_t flags,
                                       BcParseNext next);
static void bc_parse_expr_status(BcParse *p, uint8_t flags, BcParseNext next);

/**
 * Returns true if an instruction could only have come from a "leaf" expression.
 * For more on what leaf expressions are, read the comment for BC_PARSE_LEAF().
 * @param t  The instruction to test.
 */
static bool bc_parse_inst_isLeaf(BcInst t) {
	return (t >= BC_INST_NUM && t <= BC_INST_MAXSCALE) ||
#if BC_ENABLE_EXTRA_MATH
	        t == BC_INST_TRUNC ||
#endif // BC_ENABLE_EXTRA_MATH
	        t <= BC_INST_DEC;
}

/**
 * Returns true if the *previous* token was a delimiter. A delimiter is anything
 * that can legally end a statement. In bc's case, it could be a newline, a
 * semicolon, and a brace in certain cases.
 * @param p  The parser.
 */
static bool bc_parse_isDelimiter(const BcParse *p) {

	BcLexType t = p->l.t;
	bool good;

	// If it's an obvious delimiter, say so.
	if (BC_PARSE_DELIMITER(t)) return true;

	good = false;

	// If the current token is a keyword, then...beware. That means that we need
	// to check for a "dangling" else, where there was no brace-delimited block
	// on the previous if.
	if (t == BC_LEX_KW_ELSE) {

		size_t i;
		uint16_t *fptr = NULL, flags = BC_PARSE_FLAG_ELSE;

		// As long as going up the stack is valid for a dangling else, keep on.
		for (i = 0; i < p->flags.len && BC_PARSE_BLOCK_STMT(flags); ++i) {

			fptr = bc_vec_item_rev(&p->flags, i);
			flags = *fptr;

			// If we need a brace and don't have one, then we don't have a
			// delimiter.
			if ((flags & BC_PARSE_FLAG_BRACE) && p->l.last != BC_LEX_RBRACE)
				return false;
		}

		// Oh, and we had also better have an if statement somewhere.
		good = ((flags & BC_PARSE_FLAG_IF) != 0);
	}
	else if (t == BC_LEX_RBRACE) {

		size_t i;

		// Since we have a brace, we need to just check if a brace was needed.
		for (i = 0; !good && i < p->flags.len; ++i) {
			uint16_t *fptr = bc_vec_item_rev(&p->flags, i);
			good = (((*fptr) & BC_PARSE_FLAG_BRACE) != 0);
		}
	}

	return good;
}

/**
 * Sets a previously defined exit label. What are labels? See the bc Parsing
 * section of the Development manual (manuals/development.md).
 * @param p  The parser.
 */
static void bc_parse_setLabel(BcParse *p) {

	BcFunc *func = p->func;
	BcInstPtr *ip = bc_vec_top(&p->exits);
	size_t *label;

	assert(func == bc_vec_item(&p->prog->fns, p->fidx));

	// Set the preallocated label to the correct index.
	label = bc_vec_item(&func->labels, ip->idx);
	*label = func->code.len;

	// Now, we don't need the exit label; it is done.
	bc_vec_pop(&p->exits);
}

/**
 * Creates a label and sets it to idx. If this is an exit label, then idx is
 * actually invalid, but it doesn't matter because it will be fixed by
 * bc_parse_setLabel() later.
 * @param p    The parser.
 * @param idx  The index of the label.
 */
static void bc_parse_createLabel(BcParse *p, size_t idx) {
	bc_vec_push(&p->func->labels, &idx);
}

/**
 * Creates a conditional label. Unlike an exit label, this label is set at
 * creation time because it comes *before* the code that will target it.
 * @param p    The parser.
 * @param idx  The index of the label.
 */
static void bc_parse_createCondLabel(BcParse *p, size_t idx) {
	bc_parse_createLabel(p, p->func->code.len);
	bc_vec_push(&p->conds, &idx);
}

/*
 * Creates an exit label to be filled in later by bc_parse_setLabel(). Also, why
 * create a label to be filled in later? Because exit labels are meant to be
 * targeted by code that comes *before* the label. Since we have to parse that
 * code first, and don't know how long it will be, we need to just make sure to
 * reserve a slot to be filled in later when we know.
 *
 * By the way, this uses BcInstPtr because it was convenient. The field idx
 * holds the index, and the field func holds the loop boolean.
 *
 * @param p     The parser.
 * @param idx   The index of the label's position.
 * @param loop  True if the exit label is for a loop or not.
 */
static void bc_parse_createExitLabel(BcParse *p, size_t idx, bool loop) {

	BcInstPtr ip;

	assert(p->func == bc_vec_item(&p->prog->fns, p->fidx));

	ip.func = loop;
	ip.idx = idx;
	ip.len = 0;

	bc_vec_push(&p->exits, &ip);
	bc_parse_createLabel(p, SIZE_MAX);
}

/**
 * Pops the correct operators off of the operator stack based on the current
 * operator. This is because of the Shunting-Yard algorithm. Lower prec means
 * higher precedence.
 * @param p       The parser.
 * @param type    The operator.
 * @param start   The previous start of the operator stack. For more
 *                information, see the bc Parsing section of the Development
 *                manual (manuals/development.md).
 * @param nexprs  A pointer to the current number of expressions that have not
 *                been consumed yet. This is an IN and OUT parameter.
 */
static void bc_parse_operator(BcParse *p, BcLexType type,
                              size_t start, size_t *nexprs)
{
	BcLexType t;
	uchar l, r = BC_PARSE_OP_PREC(type);
	uchar left = BC_PARSE_OP_LEFT(type);

	// While we haven't hit the stop point yet.
	while (p->ops.len > start) {

		// Get the top operator.
		t = BC_PARSE_TOP_OP(p);

		// If it's a right paren, we have reached the end of whatever expression
		// this is no matter what.
		if (t == BC_LEX_LPAREN) break;

		// Break for precedence. Precedence operates differently on left and
		// right associativity, by the way. A left associative operator that
		// matches the current precedence should take priority, but a right
		// associative operator should not.
		l = BC_PARSE_OP_PREC(t);
		if (l >= r && (l != r || !left)) break;

		// Do the housekeeping. In particular, make sure to note that one
		// expression was consumed. (Two were, but another was added.)
		bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
		bc_vec_pop(&p->ops);
		*nexprs -= !BC_PARSE_OP_PREFIX(t);
	}

	bc_vec_push(&p->ops, &type);
}

/**
 * Parses a right paren. In the Shunting-Yard algorithm, it needs to be put on
 * the operator stack. But before that, it needs to consume whatever operators
 * there are until it hits a left paren.
 * @param p       The parser.
 * @param nexprs  A pointer to the current number of expressions that have not
 *                been consumed yet. This is an IN and OUT parameter.
 */
static void bc_parse_rightParen(BcParse *p, size_t *nexprs) {

	BcLexType top;

	// Consume operators until a left paren.
	while ((top = BC_PARSE_TOP_OP(p)) != BC_LEX_LPAREN) {
		bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
		bc_vec_pop(&p->ops);
		*nexprs -= !BC_PARSE_OP_PREFIX(top);
	}

	// We need to pop the left paren as well.
	bc_vec_pop(&p->ops);

	// Oh, and we also want the next token.
	bc_lex_next(&p->l);
}

/**
 * Parses function arguments.
 * @param p      The parser.
 * @param flags  Flags restricting what kind of expressions the arguments can
 *               be.
 */
static void bc_parse_args(BcParse *p, uint8_t flags) {

	bool comma = false;
	size_t nargs;

	bc_lex_next(&p->l);

	// Print and comparison operators not allowed. Well, comparison operators
	// only for POSIX. But we do allow arrays, and we *must* get a value.
	flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
	flags |= (BC_PARSE_ARRAY | BC_PARSE_NEEDVAL);

	// Count the arguments and parse them.
	for (nargs = 0; p->l.t != BC_LEX_RPAREN; ++nargs) {

		bc_parse_expr_status(p, flags, bc_parse_next_arg);

		comma = (p->l.t == BC_LEX_COMMA);
		if (comma) bc_lex_next(&p->l);
	}

	// An ending comma is FAIL.
	if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	// Now do the call with the number of arguments.
	bc_parse_push(p, BC_INST_CALL);
	bc_parse_pushIndex(p, nargs);
}

/**
 * Parses a function call.
 * @param p      The parser.
 * @param flags  Flags restricting what kind of expressions the arguments can
 *               be.
 */
static void bc_parse_call(BcParse *p, const char *name, uint8_t flags) {

	size_t idx;

	bc_parse_args(p, flags);

	// We just assert this because bc_parse_args() should
	// ensure that the next token is what it should be.
	assert(p->l.t == BC_LEX_RPAREN);

	// We cannot use bc_program_insertFunc() here
	// because it will overwrite an existing function.
	idx = bc_map_index(&p->prog->fn_map, name);

	// The function does not exist yet. Create a space for it. If the user does
	// not define it, it's a *runtime* error, not a parse error.
	if (idx == BC_VEC_INVALID_IDX) {

		BC_SIG_LOCK;

		idx = bc_program_insertFunc(p->prog, name);

		BC_SIG_UNLOCK;

		assert(idx != BC_VEC_INVALID_IDX);

		// Make sure that this pointer was not invalidated.
		p->func = bc_vec_item(&p->prog->fns, p->fidx);
	}
	// The function exists, so set the right function index.
	else idx = ((BcId*) bc_vec_item(&p->prog->fn_map, idx))->idx;

	bc_parse_pushIndex(p, idx);

	// Make sure to get the next token.
	bc_lex_next(&p->l);
}

/**
 * Parses a name/identifier-based expression. It could be a variable, an array
 * element, an array itself (for function arguments), a function call, etc.
 *
 */
static void bc_parse_name(BcParse *p, BcInst *type,
                          bool *can_assign, uint8_t flags)
{
	char *name;

	BC_SIG_LOCK;

	// We want a copy of the name since the lexer might overwrite its copy.
	name = bc_vm_strdup(p->l.str.v);

	BC_SETJMP_LOCKED(err);

	BC_SIG_UNLOCK;

	// We need the next token to see if it's just a variable or something more.
	bc_lex_next(&p->l);

	// Array element or array.
	if (p->l.t == BC_LEX_LBRACKET) {

		bc_lex_next(&p->l);

		// Array only. This has to be a function parameter.
		if (p->l.t == BC_LEX_RBRACKET) {

			// Error if arrays are not allowed.
			if (BC_ERR(!(flags & BC_PARSE_ARRAY)))
				bc_parse_err(p, BC_ERR_PARSE_EXPR);

			*type = BC_INST_ARRAY;
			*can_assign = false;
		}
		else {

			// If we are here, we have an array element. We need to set the
			// expression parsing flags.
			uint8_t flags2 = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) |
			                 BC_PARSE_NEEDVAL;

			bc_parse_expr_status(p, flags2, bc_parse_next_elem);

			// The next token *must* be a right bracket.
			if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
				bc_parse_err(p, BC_ERR_PARSE_TOKEN);

			*type = BC_INST_ARRAY_ELEM;
			*can_assign = true;
		}

		// Make sure to get the next token.
		bc_lex_next(&p->l);

		// Push the instruction and the name of the identifier.
		bc_parse_push(p, *type);
		bc_parse_pushName(p, name, false);
	}
	else if (p->l.t == BC_LEX_LPAREN) {

		// We are parsing a function call; error if not allowed.
		if (BC_ERR(flags & BC_PARSE_NOCALL))
			bc_parse_err(p, BC_ERR_PARSE_TOKEN);

		*type = BC_INST_CALL;
		*can_assign = false;

		bc_parse_call(p, name, flags);
	}
	else {
		// Just a variable.
		*type = BC_INST_VAR;
		*can_assign = true;
		bc_parse_push(p, BC_INST_VAR);
		bc_parse_pushName(p, name, true);
	}

err:
	// Need to make sure to unallocate the name.
	BC_SIG_MAYLOCK;
	free(name);
	BC_LONGJMP_CONT;
}

/**
 * Parses a builtin function that takes no arguments. This includes read(),
 * rand(), maxibase(), maxobase(), maxscale(), and maxrand().
 * @param p     The parser.
 * @param inst  The instruction corresponding to the builtin.
 */
static void bc_parse_noArgBuiltin(BcParse *p, BcInst inst) {

	// Must have a left paren.
	bc_lex_next(&p->l);
	if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	// Must have a right paren.
	bc_lex_next(&p->l);
	if ((p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	bc_parse_push(p, inst);

	bc_lex_next(&p->l);
}

/**
 * Parses a builtin function that takes 1 argument. This includes length(),
 * sqrt(), abs(), scale(), and irand().
 * @param p      The parser.
 * @param type   The lex token.
 * @param flags  The expression parsing flags for parsing the argument.
 * @param prev   An out parameter; the previous instruction pointer.
 */
static void bc_parse_builtin(BcParse *p, BcLexType type,
                             uint8_t flags, BcInst *prev)
{
	// Must have a left paren.
	bc_lex_next(&p->l);
	if (BC_ERR(p->l.t != BC_LEX_LPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	bc_lex_next(&p->l);

	// Change the flags as needed for parsing the argument.
	flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
	flags |= BC_PARSE_NEEDVAL;

	// Since length can take arrays, we need to specially add that flag.
	if (type == BC_LEX_KW_LENGTH) flags |= BC_PARSE_ARRAY;

	bc_parse_expr_status(p, flags, bc_parse_next_rel);

	// Must have a right paren.
	if (BC_ERR(p->l.t != BC_LEX_RPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	// Adjust previous based on the token and push it.
	*prev = type - BC_LEX_KW_LENGTH + BC_INST_LENGTH;
	bc_parse_push(p, *prev);

	bc_lex_next(&p->l);
}

/**
 * Parses a builtin function that takes 3 arguments. This includes modexp() and
 * divmod().
 */
static void bc_parse_builtin3(BcParse *p, BcLexType type,
                              uint8_t flags, BcInst *prev)
{
	assert(type == BC_LEX_KW_MODEXP || type == BC_LEX_KW_DIVMOD);

	// Must have a left paren.
	bc_lex_next(&p->l);
	if (BC_ERR(p->l.t != BC_LEX_LPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	bc_lex_next(&p->l);

	// Change the flags as needed for parsing the argument.
	flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
	flags |= BC_PARSE_NEEDVAL;

	bc_parse_expr_status(p, flags, bc_parse_next_builtin);

	// Must have a comma.
	if (BC_ERR(p->l.t != BC_LEX_COMMA))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	bc_lex_next(&p->l);

	bc_parse_expr_status(p, flags, bc_parse_next_builtin);

	// Must have a comma.
	if (BC_ERR(p->l.t != BC_LEX_COMMA))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	bc_lex_next(&p->l);

	// If it is a divmod, parse an array name. Otherwise, just parse another
	// expression.
	if (type == BC_LEX_KW_DIVMOD) {

		// Must have a name.
		if (BC_ERR(p->l.t != BC_LEX_NAME)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

		// This is safe because the next token should not overwrite the name.
		bc_lex_next(&p->l);

		// Must have a left bracket.
		if (BC_ERR(p->l.t != BC_LEX_LBRACKET))
			bc_parse_err(p, BC_ERR_PARSE_TOKEN);

		// This is safe because the next token should not overwrite the name.
		bc_lex_next(&p->l);

		// Must have a right bracket.
		if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
			bc_parse_err(p, BC_ERR_PARSE_TOKEN);

		// This is safe because the next token should not overwrite the name.
		bc_lex_next(&p->l);
	}
	else bc_parse_expr_status(p, flags, bc_parse_next_rel);

	// Must have a right paren.
	if (BC_ERR(p->l.t != BC_LEX_RPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	// Adjust previous based on the token and push it.
	*prev = type - BC_LEX_KW_MODEXP + BC_INST_MODEXP;
	bc_parse_push(p, *prev);

	// If we have divmod, we need to assign the modulus to the array element, so
	// we need to push the instructions for doing so.
	if (type == BC_LEX_KW_DIVMOD) {

		// The zeroth element.
		bc_parse_push(p, BC_INST_ZERO);
		bc_parse_push(p, BC_INST_ARRAY_ELEM);

		// Push the array.
		bc_parse_pushName(p, p->l.str.v, false);

		// Swap them and assign. After this, the top item on the stack should
		// be the quotient.
		bc_parse_push(p, BC_INST_SWAP);
		bc_parse_push(p, BC_INST_ASSIGN_NO_VAL);
	}

	bc_lex_next(&p->l);
}

/**
 * Parses the scale keyword. This is special because scale can be a value or a
 * builtin function.
 * @param p           The parser.
 * @param type        An out parameter; the instruction for the parse.
 * @param can_assign  An out parameter; whether the expression can be assigned
 *                    to.
 * @param flags       The expression parsing flags for parsing a scale() arg.
 */
static void bc_parse_scale(BcParse *p, BcInst *type,
                           bool *can_assign, uint8_t flags)
{
	bc_lex_next(&p->l);

	// Without the left paren, it's just the keyword.
	if (p->l.t != BC_LEX_LPAREN) {

		// Set, push, and return.
		*type = BC_INST_SCALE;
		*can_assign = true;
		bc_parse_push(p, BC_INST_SCALE);
		return;
	}

	// Handle the scale function.
	*type = BC_INST_SCALE_FUNC;
	*can_assign = false;

	// Once again, adjust the flags.
	flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
	flags |= BC_PARSE_NEEDVAL;

	bc_lex_next(&p->l);

	bc_parse_expr_status(p, flags, bc_parse_next_rel);

	// Must have a right paren.
	if (BC_ERR(p->l.t != BC_LEX_RPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	bc_parse_push(p, BC_INST_SCALE_FUNC);

	bc_lex_next(&p->l);
}

/**
 * Parses and increment or decrement operator. This is a bit complex.
 * @param p           The parser.
 * @param prev        An out parameter; the previous instruction pointer.
 * @param can_assign  An out parameter; whether the expression can be assigned
 *                    to.
 * @param nexs        An in/out parameter; the number of expressions in the
 *                    parse tree that are not used.
 * @param flags       The expression parsing flags for parsing a scale() arg.
 */
static void bc_parse_incdec(BcParse *p, BcInst *prev, bool *can_assign,
                            size_t *nexs, uint8_t flags)
{
	BcLexType type;
	uchar inst;
	BcInst etype = *prev;
	BcLexType last = p->l.last;

	assert(prev != NULL && can_assign != NULL);

	// If we can't assign to the previous token, then we have an error.
	if (BC_ERR(last == BC_LEX_OP_INC || last == BC_LEX_OP_DEC ||
	           last == BC_LEX_RPAREN))
	{
		bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
	}

	// Is the previous instruction for a variable?
	if (BC_PARSE_INST_VAR(etype)) {

		// If so, this is a postfix operator.
		if (!*can_assign) bc_parse_err(p, BC_ERR_PARSE_ASSIGN);

		// Only postfix uses BC_INST_INC and BC_INST_DEC.
		*prev = inst = BC_INST_INC + (p->l.t != BC_LEX_OP_INC);
		bc_parse_push(p, inst);
		bc_lex_next(&p->l);
		*can_assign = false;
	}
	else {

		// This is a prefix operator. In that case, we just convert it to
		// an assignment instruction.
		*prev = inst = BC_INST_ASSIGN_PLUS + (p->l.t != BC_LEX_OP_INC);

		bc_lex_next(&p->l);
		type = p->l.t;

		// Because we parse the next part of the expression
		// right here, we need to increment this.
		*nexs = *nexs + 1;

		// Is the next token a normal identifier?
		if (type == BC_LEX_NAME) {

			// Parse the name.
			uint8_t flags2 = flags & ~BC_PARSE_ARRAY;
			bc_parse_name(p, prev, can_assign, flags2 | BC_PARSE_NOCALL);
		}
		// Is the next token a global?
		else if (type >= BC_LEX_KW_LAST && type <= BC_LEX_KW_OBASE) {
			bc_parse_push(p, type - BC_LEX_KW_LAST + BC_INST_LAST);
			bc_lex_next(&p->l);
		}
		// Is the next token specifically scale, which needs special treatment?
		else if (BC_NO_ERR(type == BC_LEX_KW_SCALE)) {

			bc_lex_next(&p->l);

			// Check that scale() was not used.
			if (BC_ERR(p->l.t == BC_LEX_LPAREN))
				bc_parse_err(p, BC_ERR_PARSE_TOKEN);
			else bc_parse_push(p, BC_INST_SCALE);
		}
		// Now we know we have an error.
		else bc_parse_err(p, BC_ERR_PARSE_TOKEN);

		*can_assign = false;

		bc_parse_push(p, BC_INST_ONE);
		bc_parse_push(p, inst);
	}
}

/**
 * Parses the minus operator. This needs special treatment because it is either
 * subtract or negation.
 * @param p        The parser.
 * @param prev     An in/out parameter; the previous instruction.
 * @param ops_bgn  The size of the operator stack.
 * @param rparen   True if the last token was a right paren.
 * @param binlast  True if the last token was a binary operator.
 * @param nexprs   An in/out parameter; the number of unused expressions.
 */
static void bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
                           bool rparen, bool binlast, size_t *nexprs)
{
	BcLexType type;

	bc_lex_next(&p->l);

	// Figure out if it's a minus or a negation.
	type = BC_PARSE_LEAF(*prev, binlast, rparen) ? BC_LEX_OP_MINUS : BC_LEX_NEG;
	*prev = BC_PARSE_TOKEN_INST(type);

	// We can just push onto the op stack because this is the largest
	// precedence operator that gets pushed. Inc/dec does not.
	if (type != BC_LEX_OP_MINUS) bc_vec_push(&p->ops, &type);
	else bc_parse_operator(p, type, ops_bgn, nexprs);
}

/**
 * Parses a string.
 * @param p     The parser.
 * @param inst  The instruction corresponding to how the string was found and
 *              how it should be printed.
 */
static void bc_parse_str(BcParse *p, BcInst inst) {
	bc_parse_addString(p);
	bc_parse_push(p, inst);
	bc_lex_next(&p->l);
}

/**
 * Parses a print statement.
 * @param p  The parser.
 */
static void bc_parse_print(BcParse *p, BcLexType type) {

	BcLexType t;
	bool comma = false;
	BcInst inst = type == BC_LEX_KW_STREAM ?
	              BC_INST_PRINT_STREAM : BC_INST_PRINT_POP;

	bc_lex_next(&p->l);

	t = p->l.t;

	// A print or stream statement has to have *something*.
	if (bc_parse_isDelimiter(p)) bc_parse_err(p, BC_ERR_PARSE_PRINT);

	do {

		// If the token is a string, then print it with escapes.
		// BC_INST_PRINT_POP plays that role for bc.
		if (t == BC_LEX_STR) bc_parse_str(p, inst);
		else {
			// We have an actual number; parse and add a print instruction.
			bc_parse_expr_status(p, BC_PARSE_NEEDVAL, bc_parse_next_print);
			bc_parse_push(p, inst);
		}

		// Is the next token a comma?
		comma = (p->l.t == BC_LEX_COMMA);

		// Get the next token if we have a comma.
		if (comma) bc_lex_next(&p->l);
		else {

			// If we don't have a comma, the statement needs to end.
			if (!bc_parse_isDelimiter(p))
				bc_parse_err(p, BC_ERR_PARSE_TOKEN);
			else break;
		}

		t = p->l.t;

	} while (true);

	// If we have a comma but no token, that's bad.
	if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
}

/**
 * Parses a return statement.
 * @param p  The parser.
 */
static void bc_parse_return(BcParse *p) {

	BcLexType t;
	bool paren;
	uchar inst = BC_INST_RET0;

	// If we are not in a function, that's an error.
	if (BC_ERR(!BC_PARSE_FUNC(p))) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	// If we are in a void function, make sure to return void.
	if (p->func->voidfn) inst = BC_INST_RET_VOID;

	bc_lex_next(&p->l);

	t = p->l.t;
	paren = (t == BC_LEX_LPAREN);

	// An empty return statement just needs to push the selected instruction.
	if (bc_parse_isDelimiter(p)) bc_parse_push(p, inst);
	else {

		BcParseStatus s;

		// Need to parse the expression whose value will be returned.
		s = bc_parse_expr_err(p, BC_PARSE_NEEDVAL, bc_parse_next_expr);

		// If the expression was empty, just push the selected instruction.
		if (s == BC_PARSE_STATUS_EMPTY_EXPR) {
			bc_parse_push(p, inst);
			bc_lex_next(&p->l);
		}

		// POSIX requires parentheses.
		if (!paren || p->l.last != BC_LEX_RPAREN) {
			bc_parse_err(p, BC_ERR_POSIX_RET);
		}

		// Void functions require an empty expression.
		if (BC_ERR(p->func->voidfn)) {
			if (s != BC_PARSE_STATUS_EMPTY_EXPR)
				bc_parse_verr(p, BC_ERR_PARSE_RET_VOID, p->func->name);
		}
		// If we got here, we want to be sure to end the function with a real
		// return instruction, just in case.
		else bc_parse_push(p, BC_INST_RET);
	}
}

/**
 * Clears flags that indicate the end of an if statement and its block and sets
 * the jump location.
 * @param p  The parser.
 */
static void bc_parse_noElse(BcParse *p) {
	uint16_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
	*flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
	bc_parse_setLabel(p);
}

/**
 * Ends (finishes parsing) the body of a control statement or a function.
 * @param p      The parser.
 * @param brace  True if the body was ended by a brace, false otherwise.
 */
static void bc_parse_endBody(BcParse *p, bool brace) {

	bool has_brace, new_else = false;

	// We cannot be ending a body if there are no bodies to end.
	if (BC_ERR(p->flags.len <= 1)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	if (brace) {

		// The brace was already gotten; make sure that the caller did not lie.
		// We check for the requirement of braces later.
		assert(p->l.t == BC_LEX_RBRACE);

		bc_lex_next(&p->l);

		// If the next token is not a delimiter, that is a problem.
		if (BC_ERR(!bc_parse_isDelimiter(p)))
			bc_parse_err(p, BC_ERR_PARSE_TOKEN);
	}

	// Do we have a brace flag?
	has_brace = (BC_PARSE_BRACE(p) != 0);

	do {
		size_t len = p->flags.len;
		bool loop;

		// If we have a brace flag but not a brace, that's a problem.
		if (has_brace && !brace) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

		// Are we inside a loop?
		loop = (BC_PARSE_LOOP_INNER(p) != 0);

		// If we are ending a loop or an else...
		if (loop || BC_PARSE_ELSE(p)) {

			// Loops have condition labels that we have to take care of as well.
			if (loop) {

				size_t *label = bc_vec_top(&p->conds);

				bc_parse_push(p, BC_INST_JUMP);
				bc_parse_pushIndex(p, *label);

				bc_vec_pop(&p->conds);
			}

			bc_parse_setLabel(p);
			bc_vec_pop(&p->flags);
		}
		// If we are ending a function...
		else if (BC_PARSE_FUNC_INNER(p)) {
			BcInst inst = (p->func->voidfn ? BC_INST_RET_VOID : BC_INST_RET0);
			bc_parse_push(p, inst);
			bc_parse_updateFunc(p, BC_PROG_MAIN);
			bc_vec_pop(&p->flags);
		}
		// If we have a brace flag and not an if statement, we can pop the top
		// of the flags stack because they have been taken care of above.
		else if (has_brace && !BC_PARSE_IF(p)) bc_vec_pop(&p->flags);

		// This needs to be last to parse nested if's properly.
		if (BC_PARSE_IF(p) && (len == p->flags.len || !BC_PARSE_BRACE(p))) {

			// Eat newlines.
			while (p->l.t == BC_LEX_NLINE) bc_lex_next(&p->l);

			// *Now* we can pop the flags.
			bc_vec_pop(&p->flags);

			// If we are allowed non-POSIX stuff...
			if (!BC_S) {

				// Have we found yet another dangling else?
				*(BC_PARSE_TOP_FLAG_PTR(p)) |= BC_PARSE_FLAG_IF_END;
				new_else = (p->l.t == BC_LEX_KW_ELSE);

				// Parse the else or end the if statement body.
				if (new_else) bc_parse_else(p);
				else if (!has_brace && (!BC_PARSE_IF_END(p) || brace))
					bc_parse_noElse(p);
			}
			// POSIX requires us to do the bare minimum only.
			else bc_parse_noElse(p);
		}

		// If these are both true, we have "used" the braces that we found.
		if (brace && has_brace) brace = false;

	// This condition was perhaps the hardest single part of the parser. If the
	// flags stack does not have enough, we should stop. If we have a new else
	// statement, we should stop. If we do have the end of an if statement and
	// we have eaten the brace, we should stop. If we do have a brace flag, we
	// should stop.
	} while (p->flags.len > 1 && !new_else && (!BC_PARSE_IF_END(p) || brace) &&
	         !(has_brace = (BC_PARSE_BRACE(p) != 0)));

	// If we have a brace, yet no body for it, that's a problem.
	if (BC_ERR(p->flags.len == 1 && brace))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);
	else if (brace && BC_PARSE_BRACE(p)) {

		// If we make it here, we have a brace and a flag for it.
		uint16_t flags = BC_PARSE_TOP_FLAG(p);

		// This condition ensure that the *last* body is correctly finished by
		// popping its flags.
		if (!(flags & (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_LOOP_INNER)) &&
		    !(flags & (BC_PARSE_FLAG_IF | BC_PARSE_FLAG_ELSE)) &&
		    !(flags & (BC_PARSE_FLAG_IF_END)))
		{
			bc_vec_pop(&p->flags);
		}
	}
}

/**
 * Starts the body of a control statement or function.
 * @param p      The parser.
 * @param flags  The current flags (will be edited).
 */
static void bc_parse_startBody(BcParse *p, uint16_t flags) {
	assert(flags);
	flags |= (BC_PARSE_TOP_FLAG(p) & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
	flags |= BC_PARSE_FLAG_BODY;
	bc_vec_push(&p->flags, &flags);
}

void bc_parse_endif(BcParse *p) {

	size_t i;
	bool good;

	// Not a problem if this is true.
	if (BC_NO_ERR(!BC_PARSE_NO_EXEC(p))) return;

	good = true;

	// Find an instance of a body that needs closing, i.e., a statement that did
	// not have a right brace when it should have.
	for (i = 0; good && i < p->flags.len; ++i) {
		uint16_t flag = *((uint16_t*) bc_vec_item(&p->flags, i));
		good = ((flag & BC_PARSE_FLAG_BRACE) != BC_PARSE_FLAG_BRACE);
	}

	// If we did not find such an instance...
	if (good) {

		// We set this to restore it later. We don't want the parser thinking
		// that we are on stdin for this one because it will want more.
		bool is_stdin = vm.is_stdin;

		vm.is_stdin = false;

		// End all of the if statements and loops.
		while (p->flags.len > 1 || BC_PARSE_IF_END(p)) {
			if (BC_PARSE_IF_END(p)) bc_parse_noElse(p);
			if (p->flags.len > 1) bc_parse_endBody(p, false);
		}

		vm.is_stdin = is_stdin;
	}
	// If we reach here, a block was not properly closed, and we should error.
	else bc_parse_err(&vm.prs, BC_ERR_PARSE_BLOCK);
}

/**
 * Parses an if statement.
 * @param p  The parser.
 */
static void bc_parse_if(BcParse *p) {

	// We are allowed relational operators, and we must have a value.
	size_t idx;
	uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);

	// Get the left paren and barf if necessary.
	bc_lex_next(&p->l);
	if (BC_ERR(p->l.t != BC_LEX_LPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	// Parse the condition.
	bc_lex_next(&p->l);
	bc_parse_expr_status(p, flags, bc_parse_next_rel);

	// Must have a right paren.
	if (BC_ERR(p->l.t != BC_LEX_RPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	bc_lex_next(&p->l);

	// Insert the conditional jump instruction.
	bc_parse_push(p, BC_INST_JUMP_ZERO);

	idx = p->func->labels.len;

	// Push the index for the instruction and create an exit label for an else
	// statement.
	bc_parse_pushIndex(p, idx);
	bc_parse_createExitLabel(p, idx, false);

	bc_parse_startBody(p, BC_PARSE_FLAG_IF);
}

/**
 * Parses an else statement.
 * @param p  The parser.
 */
static void bc_parse_else(BcParse *p) {

	size_t idx = p->func->labels.len;

	// We must be at the end of an if statement.
	if (BC_ERR(!BC_PARSE_IF_END(p)))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	// Push an unconditional jump to make bc jump over the else statement if it
	// executed the original if statement.
	bc_parse_push(p, BC_INST_JUMP);
	bc_parse_pushIndex(p, idx);

	// Clear the else stuff. Yes, that function is misnamed for its use here,
	// but deal with it.
	bc_parse_noElse(p);

	// Create the exit label and parse the body.
	bc_parse_createExitLabel(p, idx, false);
	bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);

	bc_lex_next(&p->l);
}

/**
 * Parse a while loop.
 * @param p  The parser.
 */
static void bc_parse_while(BcParse *p) {

	// We are allowed relational operators, and we must have a value.
	size_t idx;
	uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);

	// Get the left paren and barf if necessary.
	bc_lex_next(&p->l);
	if (BC_ERR(p->l.t != BC_LEX_LPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);
	bc_lex_next(&p->l);

	// Create the labels. Loops need both.
	bc_parse_createCondLabel(p, p->func->labels.len);
	idx = p->func->labels.len;
	bc_parse_createExitLabel(p, idx, true);

	// Parse the actual condition and barf on non-right paren.
	bc_parse_expr_status(p, flags, bc_parse_next_rel);
	if (BC_ERR(p->l.t != BC_LEX_RPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);
	bc_lex_next(&p->l);

	// Now we can push the conditional jump and start the body.
	bc_parse_push(p, BC_INST_JUMP_ZERO);
	bc_parse_pushIndex(p, idx);
	bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
}

/**
 * Parse a for loop.
 * @param p  The parser.
 */
static void bc_parse_for(BcParse *p) {

	size_t cond_idx, exit_idx, body_idx, update_idx;

	// Barf on the missing left paren.
	bc_lex_next(&p->l);
	if (BC_ERR(p->l.t != BC_LEX_LPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);
	bc_lex_next(&p->l);

	// The first statement can be empty, but if it is, check for error in POSIX
	// mode. Otherwise, parse it.
	if (p->l.t != BC_LEX_SCOLON)
		bc_parse_expr_status(p, 0, bc_parse_next_for);
	else bc_parse_err(p, BC_ERR_POSIX_FOR);

	// Must have a semicolon.
	if (BC_ERR(p->l.t != BC_LEX_SCOLON)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
	bc_lex_next(&p->l);

	// These are indices for labels. There are so many of them because the end
	// of the loop must unconditionally jump to the update code. Then the update
	// code must unconditionally jump to the condition code. Then the condition
	// code must *conditionally* jump to the exit.
	cond_idx = p->func->labels.len;
	update_idx = cond_idx + 1;
	body_idx = update_idx + 1;
	exit_idx = body_idx + 1;

	// This creates the condition label.
	bc_parse_createLabel(p, p->func->code.len);

	// Parse an expression if it exists.
	if (p->l.t != BC_LEX_SCOLON) {
		uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
		bc_parse_expr_status(p, flags, bc_parse_next_for);
	}
	else {

		// Set this for the next call to bc_parse_number because an empty
		// condition means that it is an infinite loop, so the condition must be
		// non-zero. This is safe to set because the current token is a
		// semicolon, which has no string requirement.
		bc_vec_string(&p->l.str, sizeof(bc_parse_one) - 1, bc_parse_one);
		bc_parse_number(p);

		// An empty condition makes POSIX mad.
		bc_parse_err(p, BC_ERR_POSIX_FOR);
	}

	// Must have a semicolon.
	if (BC_ERR(p->l.t != BC_LEX_SCOLON))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);
	bc_lex_next(&p->l);

	// Now we can set up the conditional jump to the exit and an unconditional
	// jump to the body right after. The unconditional jump to the body is
	// because there is update code coming right after the condition, so we need
	// to skip it to get to the body.
	bc_parse_push(p, BC_INST_JUMP_ZERO);
	bc_parse_pushIndex(p, exit_idx);
	bc_parse_push(p, BC_INST_JUMP);
	bc_parse_pushIndex(p, body_idx);

	// Now create the label for the update code.
	bc_parse_createCondLabel(p, update_idx);

	// Parse if not empty, and if it is, let POSIX yell if necessary.
	if (p->l.t != BC_LEX_RPAREN)
		bc_parse_expr_status(p, 0, bc_parse_next_rel);
	else bc_parse_err(p, BC_ERR_POSIX_FOR);

	// Must have a right paren.
	if (BC_ERR(p->l.t != BC_LEX_RPAREN))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	// Set up a jump to the condition right after the update code.
	bc_parse_push(p, BC_INST_JUMP);
	bc_parse_pushIndex(p, cond_idx);
	bc_parse_createLabel(p, p->func->code.len);

	// Create an exit label for the body and start the body.
	bc_parse_createExitLabel(p, exit_idx, true);
	bc_lex_next(&p->l);
	bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
}

/**
 * Parse a statement or token that indicates a loop exit. This includes an
 * actual loop exit, the break keyword, or the continue keyword.
 * @param p     The parser.
 * @param type  The type of exit.
 */
static void bc_parse_loopExit(BcParse *p, BcLexType type) {

	size_t i;
	BcInstPtr *ip;

	// Must have a loop. If we don't, that's an error.
	if (BC_ERR(!BC_PARSE_LOOP(p))) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

	// If we have a break statement...
	if (type == BC_LEX_KW_BREAK) {

		// If there are no exits, something went wrong somewhere.
		if (BC_ERR(!p->exits.len)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

		// Get the exit.
		i = p->exits.len - 1;
		ip = bc_vec_item(&p->exits, i);

		// The condition !ip->func is true if the exit is not for a loop, so we
		// need to find the first actual loop exit.
		while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);

		// Make sure everything is hunky dory.
		assert(ip != NULL && (i < p->exits.len || ip->func));

		// Set the index for the exit.
		i = ip->idx;
	}
	// If we have a continue statement or just the loop end, jump to the
	// condition (or update for a foor loop).
	else i = *((size_t*) bc_vec_top(&p->conds));

	// Add the unconditional jump.
	bc_parse_push(p, BC_INST_JUMP);
	bc_parse_pushIndex(p, i);

	bc_lex_next(&p->l);
}

/**
 * Parse a function (header).
 * @param p  The parser.
 */
static void bc_parse_func(BcParse *p) {

	bool comma = false, voidfn;
	uint16_t flags;
	size_t idx;

	bc_lex_next(&p->l);

	// Must have a name.
	if (BC_ERR(p->l.t != BC_LEX_NAME)) bc_parse_err(p, BC_ERR_PARSE_FUNC);

	// If the name is "void", and POSIX is not on, mark as void.
	voidfn = (!BC_IS_POSIX && p->l.t == BC_LEX_NAME &&
	          !strcmp(p->l.str.v, "void"));

	// We can safely do this because the expected token should not overwrite the
	// function name.
	bc_lex_next(&p->l);

	// If we *don't* have another name, then void is the name of the function.
	voidfn = (voidfn && p->l.t == BC_LEX_NAME);

	// With a void function, allow POSIX to complain and get a new token.
	if (voidfn) {

		bc_parse_err(p, BC_ERR_POSIX_VOID);

		// We can safely do this because the expected token should not overwrite
		// the function name.
		bc_lex_next(&p->l);
	}

	// Must have a left paren.
	if (BC_ERR(p->l.t != BC_LEX_LPAREN))
		bc_parse_err(p, BC_ERR_PARSE_FUNC);

	// Make sure the functions map and vector are synchronized.
	assert(p->prog->fns.len == p->prog->fn_map.len);

	// Must lock signals because vectors are changed, and the vector functions
	// expect signals to be locked.
	BC_SIG_LOCK;

	// Insert the function by name into the map and vector.
	idx = bc_program_insertFunc(p->prog, p->l.str.v);

	BC_SIG_UNLOCK;

	// Make sure the insert worked.
	assert(idx);

	// Update the function pointer and stuff in the parser and set its void.
	bc_parse_updateFunc(p, idx);
	p->func->voidfn = voidfn;

	bc_lex_next(&p->l);

	// While we do not have a right paren, we are still parsing arguments.
	while (p->l.t != BC_LEX_RPAREN) {

		BcType t = BC_TYPE_VAR;

		// If we have an asterisk, we are parsing a reference argument.
		if (p->l.t == BC_LEX_OP_MULTIPLY) {

			t = BC_TYPE_REF;
			bc_lex_next(&p->l);

			// Let POSIX complain if necessary.
			bc_parse_err(p, BC_ERR_POSIX_REF);
		}

		// If we don't have a name, the argument will not have a name. Barf.
		if (BC_ERR(p->l.t != BC_LEX_NAME))
			bc_parse_err(p, BC_ERR_PARSE_FUNC);

		// Increment the number of parameters.
		p->func->nparams += 1;

		// Copy the string in the lexer so that we can use the lexer again.
		bc_vec_string(&p->buf, p->l.str.len, p->l.str.v);

		bc_lex_next(&p->l);

		// We are parsing an array parameter if this is true.
		if (p->l.t == BC_LEX_LBRACKET) {

			// Set the array type, unless we are already parsing a reference.
			if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY;

			bc_lex_next(&p->l);

			// The brackets *must* be empty.
			if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
				bc_parse_err(p, BC_ERR_PARSE_FUNC);

			bc_lex_next(&p->l);
		}
		// If we did *not* get a bracket, but we are expecting a reference, we
		// have a problem.
		else if (BC_ERR(t == BC_TYPE_REF))
			bc_parse_verr(p, BC_ERR_PARSE_REF_VAR, p->buf.v);

		// Test for comma and get the next token if it exists.
		comma = (p->l.t == BC_LEX_COMMA);
		if (comma) bc_lex_next(&p->l);

		// Insert the parameter into the function.
		bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line);
	}

	// If we have a comma, but no parameter, barf.
	if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_FUNC);

	// Start the body.
	flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER;
	bc_parse_startBody(p, flags);

	bc_lex_next(&p->l);

	// POSIX requires that a brace be on the same line as the function header.
	// If we don't have a brace, let POSIX throw an error.
	if (p->l.t != BC_LEX_LBRACE) bc_parse_err(p, BC_ERR_POSIX_BRACE);
}

/**
 * Parse an auto list.
 * @param p  The parser.
 */
static void bc_parse_auto(BcParse *p) {

	bool comma, one;

	// Error if the auto keyword appeared in the wrong place.
	if (BC_ERR(!p->auto_part)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
	bc_lex_next(&p->l);

	p->auto_part = comma = false;

	// We need at least one variable or array.
	one = (p->l.t == BC_LEX_NAME);

	// While we have a variable or array.
	while (p->l.t == BC_LEX_NAME) {

		BcType t;

		// Copy the name from the lexer, so we can use it again.
		bc_vec_string(&p->buf, p->l.str.len - 1, p->l.str.v);

		bc_lex_next(&p->l);

		// If we are parsing an array...
		if (p->l.t == BC_LEX_LBRACKET) {

			t = BC_TYPE_ARRAY;

			bc_lex_next(&p->l);

			// The brackets *must* be empty.
			if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
				bc_parse_err(p, BC_ERR_PARSE_FUNC);

			bc_lex_next(&p->l);
		}
		else t = BC_TYPE_VAR;

		// Test for comma and get the next token if it exists.
		comma = (p->l.t == BC_LEX_COMMA);
		if (comma) bc_lex_next(&p->l);

		// Insert the auto into the function.
		bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line);
	}

	// If we have a comma, but no auto, barf.
	if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_FUNC);

	// If we don't have any variables or arrays, barf.
	if (BC_ERR(!one)) bc_parse_err(p, BC_ERR_PARSE_NO_AUTO);

	// The auto statement should be all that's in the statement.
	if (BC_ERR(!bc_parse_isDelimiter(p)))
		bc_parse_err(p, BC_ERR_PARSE_TOKEN);
}

/**
 * Parses a body.
 * @param p      The parser.
 * @param brace  True if a brace was encountered, false otherwise.
 */
static void bc_parse_body(BcParse *p, bool brace) {

	uint16_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);

	assert(flag_ptr != NULL);
	assert(p->flags.len >= 2);

	// The body flag is for when we expect a body. We got a body, so clear the
	// flag.
	*flag_ptr &= ~(BC_PARSE_FLAG_BODY);

	// If we are inside a function, that means we just barely entered it, and
	// we can expect an auto list.
	if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {

		// We *must* have a brace in this case.
		if (BC_ERR(!brace)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);

		p->auto_part = (p->l.t != BC_LEX_KW_AUTO);

		if (!p->auto_part) {

			// Make sure this is true to not get a parse error.
			p->auto_part = true;

			// Since we already have the auto keyword, parse.
			bc_parse_auto(p);
		}

		// Eat a newline.
		if (p->l.t == BC_LEX_NLINE) bc_lex_next(&p->l);
	}
	else {

		// This is the easy part.
		size_t len = p->flags.len;

		assert(*flag_ptr);

		// Parse a statement.
		bc_parse_stmt(p);

		// This is a very important condition to get right. If there is no
		// brace, and no body flag, and the flags len hasn't shrunk, then we
		// have a body that was not delimited by braces, so we need to end it
		// now, after just one statement.
		if (!brace && !BC_PARSE_BODY(p) && len <= p->flags.len)
			bc_parse_endBody(p, false);
	}
}

/**
 * Parses a statement. This is the entry point for just about everything, except
 * function definitions.
 * @param p  The parser.
 */
static void bc_parse_stmt(BcParse *p) {

	size_t len;
	uint16_t flags;
	BcLexType type = p->l.t;

	// Eat newline.
	if (type == BC_LEX_NLINE) {
		bc_lex_next(&p->l);
		return;
	}

	// Eat auto list.
	if (type == BC_LEX_KW_AUTO) {
		bc_parse_auto(p);
		return;
	}

	// If we reach this point, no auto list is allowed.
	p->auto_part = false;

	// Everything but an else needs to be taken care of here, but else is
	// special.
	if (type != BC_LEX_KW_ELSE) {

		// After an if, no else found.
		if (BC_PARSE_IF_END(p)) {

			// Clear the expectation for else, end body, and return. Returning
			// gives us a clean slate for parsing again.
			bc_parse_noElse(p);
			if (p->flags.len > 1 && !BC_PARSE_BRACE(p))
				bc_parse_endBody(p, false);
			return;
		}
		// With a left brace, we are parsing a body.
		else if (type == BC_LEX_LBRACE) {

			// We need to start a body if we are not expecting one yet.
			if (!BC_PARSE_BODY(p)) {
				bc_parse_startBody(p, BC_PARSE_FLAG_BRACE);
				bc_lex_next(&p->l);
			}
			// If we *are* expecting a body, that body should get a brace. This
			// takes care of braces being on a different line than if and loop
			// headers.
			else {
				*(BC_PARSE_TOP_FLAG_PTR(p)) |= BC_PARSE_FLAG_BRACE;
				bc_lex_next(&p->l);
				bc_parse_body(p, true);
			}

			// If we have reached this point, we need to return for a clean
			// slate.
			return;
		}
		// This happens when we are expecting a body and get a single statement,
		// i.e., a body with no braces surrounding it. Returns after for a clean
		// slate.
		else if (BC_PARSE_BODY(p) && !BC_PARSE_BRACE(p)) {
			bc_parse_body(p, false);
			return;
		}
	}

	len = p->flags.len;
	flags = BC_PARSE_TOP_FLAG(p);

	switch (type) {

		// All of these are valid for expressions.
		case BC_LEX_OP_INC:
		case BC_LEX_OP_DEC:
		case BC_LEX_OP_MINUS:
		case BC_LEX_OP_BOOL_NOT:
		case BC_LEX_LPAREN:
		case BC_LEX_NAME:
		case BC_LEX_NUMBER:
		case BC_LEX_KW_IBASE:
		case BC_LEX_KW_LAST:
		case BC_LEX_KW_LENGTH:
		case BC_LEX_KW_OBASE:
		case BC_LEX_KW_SCALE:
#if BC_ENABLE_EXTRA_MATH
		case BC_LEX_KW_SEED:
#endif // BC_ENABLE_EXTRA_MATH
		case BC_LEX_KW_SQRT:
		case BC_LEX_KW_ABS:
#if BC_ENABLE_EXTRA_MATH
		case BC_LEX_KW_IRAND:
#endif // BC_ENABLE_EXTRA_MATH
		case BC_LEX_KW_ASCIIFY:
		case BC_LEX_KW_MODEXP:
		case BC_LEX_KW_DIVMOD:
		case BC_LEX_KW_READ:
#if BC_ENABLE_EXTRA_MATH
		case BC_LEX_KW_RAND:
#endif // BC_ENABLE_EXTRA_MATH
		case BC_LEX_KW_MAXIBASE:
		case BC_LEX_KW_MAXOBASE:
		case BC_LEX_KW_MAXSCALE:
#if BC_ENABLE_EXTRA_MATH
		case BC_LEX_KW_MAXRAND:
#endif // BC_ENABLE_EXTRA_MATH
		case BC_LEX_KW_LINE_LENGTH:
		case BC_LEX_KW_GLOBAL_STACKS:
		case BC_LEX_KW_LEADING_ZERO:
		{
			bc_parse_expr_status(p, BC_PARSE_PRINT, bc_parse_next_expr);
			break;
		}

		case BC_LEX_KW_ELSE:
		{
			bc_parse_else(p);
			break;
		}

		// Just eat.
		case BC_LEX_SCOLON:
		{
			// Do nothing.
			break;
		}

		case BC_LEX_RBRACE:
		{
			bc_parse_endBody(p, true);
			break;
		}

		case BC_LEX_STR:
		{
			bc_parse_str(p, BC_INST_PRINT_STR);
			break;
		}

		case BC_LEX_KW_BREAK:
		case BC_LEX_KW_CONTINUE:
		{
			bc_parse_loopExit(p, p->l.t);
			break;
		}

		case BC_LEX_KW_FOR:
		{
			bc_parse_for(p);
			break;
		}

		case BC_LEX_KW_HALT:
		{
			bc_parse_push(p, BC_INST_HALT);
			bc_lex_next(&p->l);
			break;
		}

		case BC_LEX_KW_IF:
		{
			bc_parse_if(p);
			break;
		}

		case BC_LEX_KW_LIMITS:
		{
			// `limits` is a compile-time command, so execute it right away.
			bc_vm_printf("BC_LONG_BIT      = %lu\n", (ulong) BC_LONG_BIT);
			bc_vm_printf("BC_BASE_DIGS     = %lu\n", (ulong) BC_BASE_DIGS);
			bc_vm_printf("BC_BASE_POW      = %lu\n", (ulong) BC_BASE_POW);
			bc_vm_printf("BC_OVERFLOW_MAX  = %lu\n", (ulong) BC_NUM_BIGDIG_MAX);
			bc_vm_printf("\n");
			bc_vm_printf("BC_BASE_MAX      = %lu\n", BC_MAX_OBASE);
			bc_vm_printf("BC_DIM_MAX       = %lu\n", BC_MAX_DIM);
			bc_vm_printf("BC_SCALE_MAX     = %lu\n", BC_MAX_SCALE);
			bc_vm_printf("BC_STRING_MAX    = %lu\n", BC_MAX_STRING);
			bc_vm_printf("BC_NAME_MAX      = %lu\n", BC_MAX_NAME);
			bc_vm_printf("BC_NUM_MAX       = %lu\n", BC_MAX_NUM);
#if BC_ENABLE_EXTRA_MATH
			bc_vm_printf("BC_RAND_MAX      = %lu\n", BC_MAX_RAND);
#endif // BC_ENABLE_EXTRA_MATH
			bc_vm_printf("MAX Exponent     = %lu\n", BC_MAX_EXP);
			bc_vm_printf("Number of vars   = %lu\n", BC_MAX_VARS);

			bc_lex_next(&p->l);

			break;
		}

		case BC_LEX_KW_STREAM:
		case BC_LEX_KW_PRINT:
		{
			bc_parse_print(p, type);
			break;
		}

		case BC_LEX_KW_QUIT:
		{
			// Quit is a compile-time command. We don't exit directly, so the vm
			// can clean up.
			vm.status = BC_STATUS_QUIT;
			BC_JMP;
			break;
		}

		case BC_LEX_KW_RETURN:
		{
			bc_parse_return(p);
			break;
		}

		case BC_LEX_KW_WHILE:
		{
			bc_parse_while(p);
			break;
		}

		default:
		{
			bc_parse_err(p, BC_ERR_PARSE_TOKEN);
		}
	}

	// If the flags did not change, we expect a delimiter.
	if (len == p->flags.len && flags == BC_PARSE_TOP_FLAG(p)) {
		if (BC_ERR(!bc_parse_isDelimiter(p)))
			bc_parse_err(p, BC_ERR_PARSE_TOKEN);
	}

	// Make sure semicolons are eaten.
	while (p->l.t == BC_LEX_SCOLON) bc_lex_next(&p->l);
}

void bc_parse_parse(BcParse *p) {

	assert(p);

	BC_SETJMP(exit);

	// We should not let an EOF get here unless some partial parse was not
	// completed, in which case, it's the user's fault.
	if (BC_ERR(p->l.t == BC_LEX_EOF)) bc_parse_err(p, BC_ERR_PARSE_EOF);

	// Functions need special parsing.
	else if (p->l.t == BC_LEX_KW_DEFINE) {
		if (BC_ERR(BC_PARSE_NO_EXEC(p))) {
			bc_parse_endif(p);
			if (BC_ERR(BC_PARSE_NO_EXEC(p)))
				bc_parse_err(p, BC_ERR_PARSE_TOKEN);
		}
		bc_parse_func(p);
	}

	// Otherwise, parse a normal statement.
	else bc_parse_stmt(p);

exit:

	BC_SIG_MAYLOCK;

	// We need to reset on error.
	if (BC_ERR(((vm.status && vm.status != BC_STATUS_QUIT) || vm.sig)))
		bc_parse_reset(p);

	BC_LONGJMP_CONT;
}

/**
 * Parse an expression. This is the actual implementation of the Shunting-Yard
 * Algorithm.
 * @param p      The parser.
 * @param flags  The flags for what is valid in the expression.
 * @param next   A set of tokens for what is valid *after* the expression.
 * @return       A parse status. In some places, an empty expression is an
 *               error, and sometimes, it is required. This allows this function
 *               to tell the caller if the expression was empty and let the
 *               caller handle it.
 */
static BcParseStatus bc_parse_expr_err(BcParse *p, uint8_t flags,
                                       BcParseNext next)
{
	BcInst prev = BC_INST_PRINT;
	uchar inst = BC_INST_INVALID;
	BcLexType top, t;
	size_t nexprs, ops_bgn;
	uint32_t i, nparens, nrelops;
	bool pfirst, rprn, done, get_token, assign, bin_last, incdec, can_assign;

	// One of these *must* be true.
	assert(!(flags & BC_PARSE_PRINT) || !(flags & BC_PARSE_NEEDVAL));

	// These are set very carefully. In fact, controlling the values of these
	// locals is the biggest part of making this work. ops_bgn especially is
	// important because it marks where the operator stack begins for *this*
	// invocation of this function. That's because bc_parse_expr_err() is
	// recursive (the Shunting-Yard Algorithm is most easily expressed
	// recursively when parsing subexpressions), and each invocation needs to
	// know where to stop.
	//
	// - nparens is the number of left parens without matches.
	// - nrelops is the number of relational operators that appear in the expr.
	// - nexprs is the number of unused expressions.
	// - rprn is a right paren encountered last.
	// - done means the expression has been fully parsed.
	// - get_token is true when a token is needed at the end of an iteration.
	// - assign is true when an assignment statement was parsed last.
	// - incdec is true when the previous operator was an inc or dec operator.
	// - can_assign is true when an assignemnt is valid.
	// - bin_last is true when the previous instruction was a binary operator.
	t = p->l.t;
	pfirst = (p->l.t == BC_LEX_LPAREN);
	nparens = nrelops = 0;
	nexprs = 0;
	ops_bgn = p->ops.len;
	rprn = done = get_token = assign = incdec = can_assign = false;
	bin_last = true;

	// We want to eat newlines if newlines are not a valid ending token.
	// This is for spacing in things like for loop headers.
	if (!(flags & BC_PARSE_NOREAD)) {
		while ((t = p->l.t) == BC_LEX_NLINE) bc_lex_next(&p->l);
	}

	// This is the Shunting-Yard algorithm loop.
	for (; !done && BC_PARSE_EXPR(t); t = p->l.t)
	{
		switch (t) {

			case BC_LEX_OP_INC:
			case BC_LEX_OP_DEC:
			{
				// These operators can only be used with items that can be
				// assigned to.
				if (BC_ERR(incdec)) bc_parse_err(p, BC_ERR_PARSE_ASSIGN);

				bc_parse_incdec(p, &prev, &can_assign, &nexprs, flags);

				rprn = get_token = bin_last = false;
				incdec = true;
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

#if BC_ENABLE_EXTRA_MATH
			case BC_LEX_OP_TRUNC:
			{
				// The previous token must have been a leaf expression, or the
				// operator is in the wrong place.
				if (BC_ERR(!BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_TOKEN);

				// I can just add the instruction because
				// negative will already be taken care of.
				bc_parse_push(p, BC_INST_TRUNC);

				rprn = can_assign = incdec = false;
				get_token = true;
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}
#endif // BC_ENABLE_EXTRA_MATH

			case BC_LEX_OP_MINUS:
			{
				bc_parse_minus(p, &prev, ops_bgn, rprn, bin_last, &nexprs);

				rprn = get_token = can_assign = false;

				// This is true if it was a binary operator last.
				bin_last = (prev == BC_INST_MINUS);
				if (bin_last) incdec = false;

				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

			// All of this group, including the fallthrough, is to parse binary
			// operators.
			case BC_LEX_OP_ASSIGN_POWER:
			case BC_LEX_OP_ASSIGN_MULTIPLY:
			case BC_LEX_OP_ASSIGN_DIVIDE:
			case BC_LEX_OP_ASSIGN_MODULUS:
			case BC_LEX_OP_ASSIGN_PLUS:
			case BC_LEX_OP_ASSIGN_MINUS:
#if BC_ENABLE_EXTRA_MATH
			case BC_LEX_OP_ASSIGN_PLACES:
			case BC_LEX_OP_ASSIGN_LSHIFT:
			case BC_LEX_OP_ASSIGN_RSHIFT:
#endif // BC_ENABLE_EXTRA_MATH
			case BC_LEX_OP_ASSIGN:
			{
				// We need to make sure the assignment is valid.
				if (!BC_PARSE_INST_VAR(prev))
					bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
			}
			// Fallthrough.
			BC_FALLTHROUGH

			case BC_LEX_OP_POWER:
			case BC_LEX_OP_MULTIPLY:
			case BC_LEX_OP_DIVIDE:
			case BC_LEX_OP_MODULUS:
			case BC_LEX_OP_PLUS:
#if BC_ENABLE_EXTRA_MATH
			case BC_LEX_OP_PLACES:
			case BC_LEX_OP_LSHIFT:
			case BC_LEX_OP_RSHIFT:
#endif // BC_ENABLE_EXTRA_MATH
			case BC_LEX_OP_REL_EQ:
			case BC_LEX_OP_REL_LE:
			case BC_LEX_OP_REL_GE:
			case BC_LEX_OP_REL_NE:
			case BC_LEX_OP_REL_LT:
			case BC_LEX_OP_REL_GT:
			case BC_LEX_OP_BOOL_NOT:
			case BC_LEX_OP_BOOL_OR:
			case BC_LEX_OP_BOOL_AND:
			{
				// This is true if the operator if the token is a prefix
				// operator. This is only for boolean not.
				if (BC_PARSE_OP_PREFIX(t)) {

					// Prefix operators are only allowed after binary operators
					// or prefix operators.
					if (BC_ERR(!bin_last && !BC_PARSE_OP_PREFIX(p->l.last)))
						bc_parse_err(p, BC_ERR_PARSE_EXPR);
				}
				// If we execute the else, that means we have a binary operator.
				// If the previous operator was a prefix or a binary operator,
				// then a binary operator is not allowed.
				else if (BC_ERR(BC_PARSE_PREV_PREFIX(prev) || bin_last))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				nrelops += (t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT);
				prev = BC_PARSE_TOKEN_INST(t);

				bc_parse_operator(p, t, ops_bgn, &nexprs);

				rprn = incdec = can_assign = false;
				get_token = true;
				bin_last = !BC_PARSE_OP_PREFIX(t);
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

			case BC_LEX_LPAREN:
			{
				// A left paren is *not* allowed right after a leaf expr.
				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				nparens += 1;
				rprn = incdec = can_assign = false;
				get_token = true;

				// Push the paren onto the operator stack.
				bc_vec_push(&p->ops, &t);

				break;
			}

			case BC_LEX_RPAREN:
			{
				// This needs to be a status. The error is handled in
				// bc_parse_expr_status().
				if (BC_ERR(p->l.last == BC_LEX_LPAREN))
					return BC_PARSE_STATUS_EMPTY_EXPR;

				// The right paren must not come after a prefix or binary
				// operator.
				if (BC_ERR(bin_last || BC_PARSE_PREV_PREFIX(prev)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				// If there are no parens left, we are done, but we need another
				// token.
				if (!nparens) {
					done = true;
					get_token = false;
					break;
				}

				nparens -= 1;
				rprn = true;
				get_token = bin_last = incdec = false;

				bc_parse_rightParen(p, &nexprs);

				break;
			}

			case BC_LEX_STR:
			{
				// POSIX only allows strings alone.
				if (BC_IS_POSIX) bc_parse_err(p, BC_ERR_POSIX_EXPR_STRING);

				// A string is a leaf and cannot come right after a leaf.
				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				bc_parse_addString(p);

				get_token = true;
				bin_last = rprn = false;
				nexprs += 1;

				break;
			}

			case BC_LEX_NAME:
			{
				// A name is a leaf and cannot come right after a leaf.
				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				get_token = bin_last = false;

				bc_parse_name(p, &prev, &can_assign, flags & ~BC_PARSE_NOCALL);

				rprn = (prev == BC_INST_CALL);
				nexprs += 1;
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

			case BC_LEX_NUMBER:
			{
				// A number is a leaf and cannot come right after a leaf.
				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				// The number instruction is pushed in here.
				bc_parse_number(p);

				nexprs += 1;
				prev = BC_INST_NUM;
				get_token = true;
				rprn = bin_last = can_assign = false;
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

			case BC_LEX_KW_IBASE:
			case BC_LEX_KW_LAST:
			case BC_LEX_KW_OBASE:
#if BC_ENABLE_EXTRA_MATH
			case BC_LEX_KW_SEED:
#endif // BC_ENABLE_EXTRA_MATH
			{
				// All of these are leaves and cannot come right after a leaf.
				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				prev = t - BC_LEX_KW_LAST + BC_INST_LAST;
				bc_parse_push(p, prev);

				get_token = can_assign = true;
				rprn = bin_last = false;
				nexprs += 1;
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

			case BC_LEX_KW_LENGTH:
			case BC_LEX_KW_SQRT:
			case BC_LEX_KW_ABS:
#if BC_ENABLE_EXTRA_MATH
			case BC_LEX_KW_IRAND:
#endif // BC_ENABLE_EXTRA_MATH
			case BC_LEX_KW_ASCIIFY:
			{
				// All of these are leaves and cannot come right after a leaf.
				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				bc_parse_builtin(p, t, flags, &prev);

				rprn = get_token = bin_last = incdec = can_assign = false;
				nexprs += 1;
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

			case BC_LEX_KW_READ:
#if BC_ENABLE_EXTRA_MATH
			case BC_LEX_KW_RAND:
#endif // BC_ENABLE_EXTRA_MATH
			case BC_LEX_KW_MAXIBASE:
			case BC_LEX_KW_MAXOBASE:
			case BC_LEX_KW_MAXSCALE:
#if BC_ENABLE_EXTRA_MATH
			case BC_LEX_KW_MAXRAND:
#endif // BC_ENABLE_EXTRA_MATH
			case BC_LEX_KW_LINE_LENGTH:
			case BC_LEX_KW_GLOBAL_STACKS:
			case BC_LEX_KW_LEADING_ZERO:
			{
				// All of these are leaves and cannot come right after a leaf.
				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				// Error if we have read and it's not allowed.
				else if (t == BC_LEX_KW_READ && BC_ERR(flags & BC_PARSE_NOREAD))
					bc_parse_err(p, BC_ERR_EXEC_REC_READ);

				prev = t - BC_LEX_KW_READ + BC_INST_READ;
				bc_parse_noArgBuiltin(p, prev);

				rprn = get_token = bin_last = incdec = can_assign = false;
				nexprs += 1;
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

			case BC_LEX_KW_SCALE:
			{
				// This is a leaf and cannot come right after a leaf.
				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				// Scale needs special work because it can be a variable *or* a
				// function.
				bc_parse_scale(p, &prev, &can_assign, flags);

				rprn = get_token = bin_last = false;
				nexprs += 1;
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

			case BC_LEX_KW_MODEXP:
			case BC_LEX_KW_DIVMOD:
			{
				// This is a leaf and cannot come right after a leaf.
				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
					bc_parse_err(p, BC_ERR_PARSE_EXPR);

				bc_parse_builtin3(p, t, flags, &prev);

				rprn = get_token = bin_last = incdec = can_assign = false;
				nexprs += 1;
				flags &= ~(BC_PARSE_ARRAY);

				break;
			}

			default:
			{
#ifndef NDEBUG
				// We should never get here, even in debug builds.
				bc_parse_err(p, BC_ERR_PARSE_TOKEN);
				break;
#endif // NDEBUG
			}
		}

		if (get_token) bc_lex_next(&p->l);
	}

	// Now that we have parsed the expression, we need to empty the operator
	// stack.
	while (p->ops.len > ops_bgn) {

		top = BC_PARSE_TOP_OP(p);
		assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;

		// There should not be *any* parens on the stack anymore.
		if (BC_ERR(top == BC_LEX_LPAREN || top == BC_LEX_RPAREN))
			bc_parse_err(p, BC_ERR_PARSE_EXPR);

		bc_parse_push(p, BC_PARSE_TOKEN_INST(top));

		// Adjust the number of unused expressions.
		nexprs -= !BC_PARSE_OP_PREFIX(top);
		bc_vec_pop(&p->ops);

		incdec = false;
	}

	// There must be only one expression at the top.
	if (BC_ERR(nexprs != 1)) bc_parse_err(p, BC_ERR_PARSE_EXPR);

	// Check that the next token is correct.
	for (i = 0; i < next.len && t != next.tokens[i]; ++i);
	if (BC_ERR(i == next.len && !bc_parse_isDelimiter(p)))
		bc_parse_err(p, BC_ERR_PARSE_EXPR);

	// Check that POSIX would be happy with the number of relational operators.
	if (!(flags & BC_PARSE_REL) && nrelops)
		bc_parse_err(p, BC_ERR_POSIX_REL_POS);
	else if ((flags & BC_PARSE_REL) && nrelops > 1)
		bc_parse_err(p, BC_ERR_POSIX_MULTIREL);

	// If this is true, then we might be in a situation where we don't print.
	// We would want to have the increment/decrement operator not make an extra
	// copy if it's not necessary.
	if (!(flags & BC_PARSE_NEEDVAL) && !pfirst) {

		// We have the easy case if the last operator was an assignment
		// operator.
		if (assign) {
			inst = *((uchar*) bc_vec_top(&p->func->code));
			inst += (BC_INST_ASSIGN_POWER_NO_VAL - BC_INST_ASSIGN_POWER);
			incdec = false;
		}
		// If we have an inc/dec operator and we are *not* printing, implement
		// the optimization to get rid of the extra copy.
		else if (incdec && !(flags & BC_PARSE_PRINT)) {
			inst = *((uchar*) bc_vec_top(&p->func->code));
			incdec = (inst <= BC_INST_DEC);
			inst = BC_INST_ASSIGN_PLUS_NO_VAL + (inst != BC_INST_INC &&
			                                     inst != BC_INST_ASSIGN_PLUS);
		}

		// This condition allows us to change the previous assignment
		// instruction (which does a copy) for a NO_VAL version, which does not.
		// This condition is set if either of the above if statements ends up
		// being true.
		if (inst >= BC_INST_ASSIGN_POWER_NO_VAL &&
		    inst <= BC_INST_ASSIGN_NO_VAL)
		{
			// Pop the previous assignment instruction and push a new one.
			// Inc/dec needs the extra instruction because it is now a binary
			// operator and needs a second operand.
			bc_vec_pop(&p->func->code);
			if (incdec) bc_parse_push(p, BC_INST_ONE);
			bc_parse_push(p, inst);
		}
	}

	// If we might have to print...
	if ((flags & BC_PARSE_PRINT)) {

		// With a paren first or the last operator not being an assignment, we
		// *do* want to print.
		if (pfirst || !assign) bc_parse_push(p, BC_INST_PRINT);
	}
	// We need to make sure to push a pop instruction for assignment statements
	// that will not print. The print will pop, but without it, we need to pop.
	else if (!(flags & BC_PARSE_NEEDVAL) &&
	         (inst < BC_INST_ASSIGN_POWER_NO_VAL ||
	          inst > BC_INST_ASSIGN_NO_VAL))
	{
		bc_parse_push(p, BC_INST_POP);
	}

	// We want to eat newlines if newlines are not a valid ending token.
	// This is for spacing in things like for loop headers.
	//
	// Yes, this is one case where I reuse a variable for a different purpose;
	// in this case, incdec being true now means that newlines are not valid.
	for (incdec = true, i = 0; i < next.len && incdec; ++i)
		incdec = (next.tokens[i] != BC_LEX_NLINE);
	if (incdec) {
		while (p->l.t == BC_LEX_NLINE) bc_lex_next(&p->l);
	}

	return BC_PARSE_STATUS_SUCCESS;
}

/**
 * Parses an expression with bc_parse_expr_err(), but throws an error if it gets
 * an empty expression.
 * @param p      The parser.
 * @param flags  The flags for what is valid in the expression.
 * @param next   A set of tokens for what is valid *after* the expression.
 */
static void bc_parse_expr_status(BcParse *p, uint8_t flags, BcParseNext next) {

	BcParseStatus s = bc_parse_expr_err(p, flags, next);

	if (BC_ERR(s == BC_PARSE_STATUS_EMPTY_EXPR))
		bc_parse_err(p, BC_ERR_PARSE_EMPTY_EXPR);
}

void bc_parse_expr(BcParse *p, uint8_t flags) {
	assert(p);
	bc_parse_expr_status(p, flags, bc_parse_next_read);
}
#endif // BC_ENABLED