xref: /freebsd/contrib/bc/src/bc_parse.c (revision 12e0d316644a4f80f5f1f78cf07bd93def43b1ca)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2024 Gavin D. Howard and contributors.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright notice,
15  *   this list of conditions and the following disclaimer in the documentation
16  *   and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * *****************************************************************************
31  *
32  * The parser for bc.
33  *
34  */
35 
36 #if BC_ENABLED
37 
38 #include <assert.h>
39 #include <stdbool.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <setjmp.h>
44 
45 #include <bc.h>
46 #include <num.h>
47 #include <vm.h>
48 
49 // Before you embark on trying to understand this code, have you read the
50 // Development manual (manuals/development.md) and the comment in include/bc.h
51 // yet? No? Do that first. I'm serious.
52 //
53 // The reason is because this file holds the most sensitive and finicky code in
54 // the entire codebase. Even getting history to work on Windows was nothing
55 // compared to this. This is where dreams go to die, where dragons live, and
56 // from which Ken Thompson himself would flee.
57 
58 static void
59 bc_parse_else(BcParse* p);
60 
61 static void
62 bc_parse_stmt(BcParse* p);
63 
64 static BcParseStatus
65 bc_parse_expr_err(BcParse* p, uint8_t flags, BcParseNext next);
66 
67 static void
68 bc_parse_expr_status(BcParse* p, uint8_t flags, BcParseNext next);
69 
70 /**
71  * Returns true if an instruction could only have come from a "leaf" expression.
72  * For more on what leaf expressions are, read the comment for BC_PARSE_LEAF().
73  * @param t  The instruction to test.
74  * @return   True if the instruction is a from a leaf expression.
75  */
76 static bool
bc_parse_inst_isLeaf(BcInst t)77 bc_parse_inst_isLeaf(BcInst t)
78 {
79 	return (t >= BC_INST_NUM && t <= BC_INST_LEADING_ZERO) ||
80 #if BC_ENABLE_EXTRA_MATH
81 	       t == BC_INST_TRUNC ||
82 #endif // BC_ENABLE_EXTRA_MATH
83 	       t <= BC_INST_DEC;
84 }
85 
86 /**
87  * Returns true if the *previous* token was a delimiter. A delimiter is anything
88  * that can legally end a statement. In bc's case, it could be a newline, a
89  * semicolon, and a brace in certain cases.
90  * @param p  The parser.
91  * @return   True if the token is a legal delimiter.
92  */
93 static bool
bc_parse_isDelimiter(const BcParse * p)94 bc_parse_isDelimiter(const BcParse* p)
95 {
96 	BcLexType t = p->l.t;
97 	bool good;
98 
99 	// If it's an obvious delimiter, say so.
100 	if (BC_PARSE_DELIMITER(t)) return true;
101 
102 	good = false;
103 
104 	// If the current token is a keyword, then...beware. That means that we need
105 	// to check for a "dangling" else, where there was no brace-delimited block
106 	// on the previous if.
107 	if (t == BC_LEX_KW_ELSE)
108 	{
109 		size_t i;
110 		uint16_t *fptr = NULL, flags = BC_PARSE_FLAG_ELSE;
111 
112 		// As long as going up the stack is valid for a dangling else, keep on.
113 		for (i = 0; i < p->flags.len && BC_PARSE_BLOCK_STMT(flags); ++i)
114 		{
115 			fptr = bc_vec_item_rev(&p->flags, i);
116 			flags = *fptr;
117 
118 			// If we need a brace and don't have one, then we don't have a
119 			// delimiter.
120 			if ((flags & BC_PARSE_FLAG_BRACE) && p->l.last != BC_LEX_RBRACE)
121 			{
122 				return false;
123 			}
124 		}
125 
126 		// Oh, and we had also better have an if statement somewhere.
127 		good = ((flags & BC_PARSE_FLAG_IF) != 0);
128 	}
129 	else if (t == BC_LEX_RBRACE)
130 	{
131 		size_t i;
132 
133 		// Since we have a brace, we need to just check if a brace was needed.
134 		for (i = 0; !good && i < p->flags.len; ++i)
135 		{
136 			uint16_t* fptr = bc_vec_item_rev(&p->flags, i);
137 			good = (((*fptr) & BC_PARSE_FLAG_BRACE) != 0);
138 		}
139 	}
140 
141 	return good;
142 }
143 
144 /**
145  * Returns true if we are in top level of a function body. The POSIX grammar
146  * is defined such that anything is allowed after a function body, so we must
147  * use this function to detect that case when ending a function body.
148  * @param p  The parser.
149  * @return   True if we are in the top level of parsing a function body.
150  */
151 static bool
bc_parse_TopFunc(const BcParse * p)152 bc_parse_TopFunc(const BcParse* p)
153 {
154 	bool good = p->flags.len == 2;
155 
156 	uint16_t val = BC_PARSE_FLAG_BRACE | BC_PARSE_FLAG_FUNC_INNER;
157 	val |= BC_PARSE_FLAG_FUNC;
158 
159 	return good && BC_PARSE_TOP_FLAG(p) == val;
160 }
161 
162 /**
163  * Sets a previously defined exit label. What are labels? See the bc Parsing
164  * section of the Development manual (manuals/development.md).
165  * @param p  The parser.
166  */
167 static void
bc_parse_setLabel(BcParse * p)168 bc_parse_setLabel(BcParse* p)
169 {
170 	BcFunc* func = p->func;
171 	BcInstPtr* ip = bc_vec_top(&p->exits);
172 	size_t* label;
173 
174 	assert(func == bc_vec_item(&p->prog->fns, p->fidx));
175 
176 	// Set the preallocated label to the correct index.
177 	label = bc_vec_item(&func->labels, ip->idx);
178 	*label = func->code.len;
179 
180 	// Now, we don't need the exit label; it is done.
181 	bc_vec_pop(&p->exits);
182 }
183 
184 /**
185  * Creates a label and sets it to idx. If this is an exit label, then idx is
186  * actually invalid, but it doesn't matter because it will be fixed by
187  * bc_parse_setLabel() later.
188  * @param p    The parser.
189  * @param idx  The index of the label.
190  */
191 static void
bc_parse_createLabel(BcParse * p,size_t idx)192 bc_parse_createLabel(BcParse* p, size_t idx)
193 {
194 	bc_vec_push(&p->func->labels, &idx);
195 }
196 
197 /**
198  * Creates a conditional label. Unlike an exit label, this label is set at
199  * creation time because it comes *before* the code that will target it.
200  * @param p    The parser.
201  * @param idx  The index of the label.
202  */
203 static void
bc_parse_createCondLabel(BcParse * p,size_t idx)204 bc_parse_createCondLabel(BcParse* p, size_t idx)
205 {
206 	bc_parse_createLabel(p, p->func->code.len);
207 	bc_vec_push(&p->conds, &idx);
208 }
209 
210 /**
211  * Creates an exit label to be filled in later by bc_parse_setLabel(). Also, why
212  * create a label to be filled in later? Because exit labels are meant to be
213  * targeted by code that comes *before* the label. Since we have to parse that
214  * code first, and don't know how long it will be, we need to just make sure to
215  * reserve a slot to be filled in later when we know.
216  *
217  * By the way, this uses BcInstPtr because it was convenient. The field idx
218  * holds the index, and the field func holds the loop boolean.
219  *
220  * @param p     The parser.
221  * @param idx   The index of the label's position.
222  * @param loop  True if the exit label is for a loop or not.
223  */
224 static void
bc_parse_createExitLabel(BcParse * p,size_t idx,bool loop)225 bc_parse_createExitLabel(BcParse* p, size_t idx, bool loop)
226 {
227 	BcInstPtr ip;
228 
229 	assert(p->func == bc_vec_item(&p->prog->fns, p->fidx));
230 
231 	ip.func = loop;
232 	ip.idx = idx;
233 	ip.len = 0;
234 
235 	bc_vec_push(&p->exits, &ip);
236 	bc_parse_createLabel(p, SIZE_MAX);
237 }
238 
239 /**
240  * Pops the correct operators off of the operator stack based on the current
241  * operator. This is because of the Shunting-Yard algorithm. Lower prec means
242  * higher precedence.
243  * @param p       The parser.
244  * @param type    The operator.
245  * @param start   The previous start of the operator stack. For more
246  *                information, see the bc Parsing section of the Development
247  *                manual (manuals/development.md).
248  * @param nexprs  A pointer to the current number of expressions that have not
249  *                been consumed yet. This is an IN and OUT parameter.
250  */
251 static void
bc_parse_operator(BcParse * p,BcLexType type,size_t start,size_t * nexprs)252 bc_parse_operator(BcParse* p, BcLexType type, size_t start, size_t* nexprs)
253 {
254 	BcLexType t;
255 	uchar l, r = BC_PARSE_OP_PREC(type);
256 	uchar left = BC_PARSE_OP_LEFT(type);
257 
258 	// While we haven't hit the stop point yet...
259 	while (p->ops.len > start)
260 	{
261 		// Get the top operator.
262 		t = BC_PARSE_TOP_OP(p);
263 
264 		// If it's a left paren, we have reached the end of whatever expression
265 		// this is no matter what. We also don't pop the left paren because it
266 		// will need to stay for the rest of the subexpression.
267 		if (t == BC_LEX_LPAREN) break;
268 
269 		// Break for precedence. Precedence operates differently on left and
270 		// right associativity, by the way. A left associative operator that
271 		// matches the current precedence should take priority, but a right
272 		// associative operator should not.
273 		//
274 		// Also, a lower precedence value means a higher precedence.
275 		l = BC_PARSE_OP_PREC(t);
276 		if (l >= r && (l != r || !left)) break;
277 
278 		// Do the housekeeping. In particular, make sure to note that one
279 		// expression was consumed (well, two were, but another was added) if
280 		// the operator was not a prefix operator. (Postfix operators are not
281 		// handled by this function at all.)
282 		bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
283 		bc_vec_pop(&p->ops);
284 		*nexprs -= !BC_PARSE_OP_PREFIX(t);
285 	}
286 
287 	bc_vec_push(&p->ops, &type);
288 }
289 
290 /**
291  * Parses a right paren. In the Shunting-Yard algorithm, it needs to be put on
292  * the operator stack. But before that, it needs to consume whatever operators
293  * there are until it hits a left paren.
294  * @param p       The parser.
295  * @param nexprs  A pointer to the current number of expressions that have not
296  *                been consumed yet. This is an IN and OUT parameter.
297  */
298 static void
bc_parse_rightParen(BcParse * p,size_t * nexprs)299 bc_parse_rightParen(BcParse* p, size_t* nexprs)
300 {
301 	BcLexType top;
302 
303 	// Consume operators until a left paren.
304 	while ((top = BC_PARSE_TOP_OP(p)) != BC_LEX_LPAREN)
305 	{
306 		bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
307 		bc_vec_pop(&p->ops);
308 		*nexprs -= !BC_PARSE_OP_PREFIX(top);
309 	}
310 
311 	// We need to pop the left paren as well.
312 	bc_vec_pop(&p->ops);
313 
314 	// Oh, and we also want the next token.
315 	bc_lex_next(&p->l);
316 }
317 
318 /**
319  * Parses function arguments.
320  * @param p      The parser.
321  * @param flags  Flags restricting what kind of expressions the arguments can
322  *               be.
323  */
324 static void
bc_parse_args(BcParse * p,uint8_t flags)325 bc_parse_args(BcParse* p, uint8_t flags)
326 {
327 	bool comma = false;
328 	size_t nargs;
329 
330 	bc_lex_next(&p->l);
331 
332 	// Print and comparison operators not allowed. Well, comparison operators
333 	// only for POSIX. But we do allow arrays, and we *must* get a value.
334 	flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
335 	flags |= (BC_PARSE_ARRAY | BC_PARSE_NEEDVAL);
336 
337 	// Count the arguments and parse them.
338 	for (nargs = 0; p->l.t != BC_LEX_RPAREN; ++nargs)
339 	{
340 		bc_parse_expr_status(p, flags, bc_parse_next_arg);
341 
342 		comma = (p->l.t == BC_LEX_COMMA);
343 		if (comma) bc_lex_next(&p->l);
344 	}
345 
346 	// An ending comma is FAIL.
347 	if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
348 
349 	// Now do the call with the number of arguments.
350 	bc_parse_push(p, BC_INST_CALL);
351 	bc_parse_pushIndex(p, nargs);
352 }
353 
354 /**
355  * Parses a function call.
356  * @param p      The parser.
357  * @param flags  Flags restricting what kind of expressions the arguments can
358  *               be.
359  */
360 static void
bc_parse_call(BcParse * p,const char * name,uint8_t flags)361 bc_parse_call(BcParse* p, const char* name, uint8_t flags)
362 {
363 	size_t idx;
364 
365 	bc_parse_args(p, flags);
366 
367 	// We just assert this because bc_parse_args() should
368 	// ensure that the next token is what it should be.
369 	assert(p->l.t == BC_LEX_RPAREN);
370 
371 	// We cannot use bc_program_insertFunc() here
372 	// because it will overwrite an existing function.
373 	idx = bc_map_index(&p->prog->fn_map, name);
374 
375 	// The function does not exist yet. Create a space for it. If the user does
376 	// not define it, it's a *runtime* error, not a parse error.
377 	if (idx == BC_VEC_INVALID_IDX)
378 	{
379 		idx = bc_program_insertFunc(p->prog, name);
380 
381 		assert(idx != BC_VEC_INVALID_IDX);
382 
383 		// Make sure that this pointer was not invalidated.
384 		p->func = bc_vec_item(&p->prog->fns, p->fidx);
385 	}
386 	// The function exists, so set the right function index.
387 	else idx = ((BcId*) bc_vec_item(&p->prog->fn_map, idx))->idx;
388 
389 	bc_parse_pushIndex(p, idx);
390 
391 	// Make sure to get the next token.
392 	bc_lex_next(&p->l);
393 }
394 
395 /**
396  * Parses a name/identifier-based expression. It could be a variable, an array
397  * element, an array itself (for function arguments), a function call, etc.
398  * @param p           The parser.
399  * @param type        A pointer to return the resulting instruction.
400  * @param can_assign  A pointer to return true if the name can be assigned to,
401  *                    false otherwise.
402  * @param flags       Flags restricting what kind of expression the name can be.
403  */
404 static void
bc_parse_name(BcParse * p,BcInst * type,bool * can_assign,uint8_t flags)405 bc_parse_name(BcParse* p, BcInst* type, bool* can_assign, uint8_t flags)
406 {
407 	char* name;
408 
409 	BC_SIG_ASSERT_LOCKED;
410 
411 	// We want a copy of the name since the lexer might overwrite its copy.
412 	name = bc_vm_strdup(p->l.str.v);
413 
414 	BC_SETJMP_LOCKED(vm, err);
415 
416 	// We need the next token to see if it's just a variable or something more.
417 	bc_lex_next(&p->l);
418 
419 	// Array element or array.
420 	if (p->l.t == BC_LEX_LBRACKET)
421 	{
422 		bc_lex_next(&p->l);
423 
424 		// Array only. This has to be a function parameter.
425 		if (p->l.t == BC_LEX_RBRACKET)
426 		{
427 			// Error if arrays are not allowed.
428 			if (BC_ERR(!(flags & BC_PARSE_ARRAY)))
429 			{
430 				bc_parse_err(p, BC_ERR_PARSE_EXPR);
431 			}
432 
433 			*type = BC_INST_ARRAY;
434 			*can_assign = false;
435 		}
436 		else
437 		{
438 			// If we are here, we have an array element. We need to set the
439 			// expression parsing flags.
440 			uint8_t flags2 = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) |
441 			                 BC_PARSE_NEEDVAL;
442 
443 			bc_parse_expr_status(p, flags2, bc_parse_next_elem);
444 
445 			// The next token *must* be a right bracket.
446 			if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
447 			{
448 				bc_parse_err(p, BC_ERR_PARSE_TOKEN);
449 			}
450 
451 			*type = BC_INST_ARRAY_ELEM;
452 			*can_assign = true;
453 		}
454 
455 		// Make sure to get the next token.
456 		bc_lex_next(&p->l);
457 
458 		// Push the instruction and the name of the identifier.
459 		bc_parse_push(p, *type);
460 		bc_parse_pushName(p, name, false);
461 	}
462 	else if (p->l.t == BC_LEX_LPAREN)
463 	{
464 		// We are parsing a function call; error if not allowed.
465 		if (BC_ERR(flags & BC_PARSE_NOCALL))
466 		{
467 			bc_parse_err(p, BC_ERR_PARSE_TOKEN);
468 		}
469 
470 		*type = BC_INST_CALL;
471 		*can_assign = false;
472 
473 		bc_parse_call(p, name, flags);
474 	}
475 	else
476 	{
477 		// Just a variable.
478 		*type = BC_INST_VAR;
479 		*can_assign = true;
480 		bc_parse_push(p, BC_INST_VAR);
481 		bc_parse_pushName(p, name, true);
482 	}
483 
484 err:
485 	// Need to make sure to unallocate the name.
486 	free(name);
487 	BC_LONGJMP_CONT(vm);
488 	BC_SIG_MAYLOCK;
489 }
490 
491 /**
492  * Parses a builtin function that takes no arguments. This includes read(),
493  * rand(), maxibase(), maxobase(), maxscale(), and maxrand().
494  * @param p     The parser.
495  * @param inst  The instruction corresponding to the builtin.
496  */
497 static void
bc_parse_noArgBuiltin(BcParse * p,BcInst inst)498 bc_parse_noArgBuiltin(BcParse* p, BcInst inst)
499 {
500 	// Must have a left paren.
501 	bc_lex_next(&p->l);
502 	if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
503 
504 	// Must have a right paren.
505 	bc_lex_next(&p->l);
506 	if ((p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
507 
508 	bc_parse_push(p, inst);
509 
510 	bc_lex_next(&p->l);
511 }
512 
513 /**
514  * Parses a builtin function that takes 1 argument. This includes length(),
515  * sqrt(), abs(), scale(), and irand().
516  * @param p      The parser.
517  * @param type   The lex token.
518  * @param flags  The expression parsing flags for parsing the argument.
519  * @param prev   An out parameter; the previous instruction pointer.
520  */
521 static void
bc_parse_builtin(BcParse * p,BcLexType type,uint8_t flags,BcInst * prev)522 bc_parse_builtin(BcParse* p, BcLexType type, uint8_t flags, BcInst* prev)
523 {
524 	// Must have a left paren.
525 	bc_lex_next(&p->l);
526 	if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
527 
528 	bc_lex_next(&p->l);
529 
530 	// Change the flags as needed for parsing the argument.
531 	flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
532 	flags |= BC_PARSE_NEEDVAL;
533 
534 	// Since length can take arrays, we need to specially add that flag.
535 	if (type == BC_LEX_KW_LENGTH || type == BC_LEX_KW_ASCIIFY)
536 	{
537 		flags |= BC_PARSE_ARRAY;
538 	}
539 
540 	// Otherwise, we need to clear it because it could be set.
541 	else flags &= ~(BC_PARSE_ARRAY);
542 
543 	bc_parse_expr_status(p, flags, bc_parse_next_rel);
544 
545 	// Must have a right paren.
546 	if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
547 
548 	// Adjust previous based on the token and push it.
549 	*prev = type - BC_LEX_KW_LENGTH + BC_INST_LENGTH;
550 	bc_parse_push(p, *prev);
551 
552 	bc_lex_next(&p->l);
553 }
554 
555 /**
556  * Parses a builtin function that takes 3 arguments. This includes modexp() and
557  * divmod().
558  * @param p      The parser.
559  * @param type   The lex token.
560  * @param flags  The expression parsing flags for parsing the argument.
561  * @param prev   An out parameter; the previous instruction pointer.
562  */
563 static void
bc_parse_builtin3(BcParse * p,BcLexType type,uint8_t flags,BcInst * prev)564 bc_parse_builtin3(BcParse* p, BcLexType type, uint8_t flags, BcInst* prev)
565 {
566 	assert(type == BC_LEX_KW_MODEXP || type == BC_LEX_KW_DIVMOD);
567 
568 	// Must have a left paren.
569 	bc_lex_next(&p->l);
570 	if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
571 
572 	bc_lex_next(&p->l);
573 
574 	// Change the flags as needed for parsing the argument.
575 	flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
576 	flags |= BC_PARSE_NEEDVAL;
577 
578 	bc_parse_expr_status(p, flags, bc_parse_next_builtin);
579 
580 	// Must have a comma.
581 	if (BC_ERR(p->l.t != BC_LEX_COMMA)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
582 
583 	bc_lex_next(&p->l);
584 
585 	bc_parse_expr_status(p, flags, bc_parse_next_builtin);
586 
587 	// Must have a comma.
588 	if (BC_ERR(p->l.t != BC_LEX_COMMA)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
589 
590 	bc_lex_next(&p->l);
591 
592 	// If it is a divmod, parse an array name. Otherwise, just parse another
593 	// expression.
594 	if (type == BC_LEX_KW_DIVMOD)
595 	{
596 		// Must have a name.
597 		if (BC_ERR(p->l.t != BC_LEX_NAME)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
598 
599 		// This is safe because the next token should not overwrite the name.
600 		bc_lex_next(&p->l);
601 
602 		// Must have a left bracket.
603 		if (BC_ERR(p->l.t != BC_LEX_LBRACKET))
604 		{
605 			bc_parse_err(p, BC_ERR_PARSE_TOKEN);
606 		}
607 
608 		// This is safe because the next token should not overwrite the name.
609 		bc_lex_next(&p->l);
610 
611 		// Must have a right bracket.
612 		if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
613 		{
614 			bc_parse_err(p, BC_ERR_PARSE_TOKEN);
615 		}
616 
617 		// This is safe because the next token should not overwrite the name.
618 		bc_lex_next(&p->l);
619 	}
620 	else bc_parse_expr_status(p, flags, bc_parse_next_rel);
621 
622 	// Must have a right paren.
623 	if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
624 
625 	// Adjust previous based on the token and push it.
626 	*prev = type - BC_LEX_KW_MODEXP + BC_INST_MODEXP;
627 	bc_parse_push(p, *prev);
628 
629 	// If we have divmod, we need to assign the modulus to the array element, so
630 	// we need to push the instructions for doing so.
631 	if (type == BC_LEX_KW_DIVMOD)
632 	{
633 		// The zeroth element.
634 		bc_parse_push(p, BC_INST_ZERO);
635 		bc_parse_push(p, BC_INST_ARRAY_ELEM);
636 
637 		// Push the array.
638 		bc_parse_pushName(p, p->l.str.v, false);
639 
640 		// Swap them and assign. After this, the top item on the stack should
641 		// be the quotient.
642 		bc_parse_push(p, BC_INST_SWAP);
643 		bc_parse_push(p, BC_INST_ASSIGN_NO_VAL);
644 	}
645 
646 	bc_lex_next(&p->l);
647 }
648 
649 /**
650  * Parses the scale keyword. This is special because scale can be a value or a
651  * builtin function.
652  * @param p           The parser.
653  * @param type        An out parameter; the instruction for the parse.
654  * @param can_assign  An out parameter; whether the expression can be assigned
655  *                    to.
656  * @param flags       The expression parsing flags for parsing a scale() arg.
657  */
658 static void
bc_parse_scale(BcParse * p,BcInst * type,bool * can_assign,uint8_t flags)659 bc_parse_scale(BcParse* p, BcInst* type, bool* can_assign, uint8_t flags)
660 {
661 	bc_lex_next(&p->l);
662 
663 	// Without the left paren, it's just the keyword.
664 	if (p->l.t != BC_LEX_LPAREN)
665 	{
666 		// Set, push, and return.
667 		*type = BC_INST_SCALE;
668 		*can_assign = true;
669 		bc_parse_push(p, BC_INST_SCALE);
670 		return;
671 	}
672 
673 	// Handle the scale function.
674 	*type = BC_INST_SCALE_FUNC;
675 	*can_assign = false;
676 
677 	// Once again, adjust the flags.
678 	flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
679 	flags |= BC_PARSE_NEEDVAL;
680 
681 	bc_lex_next(&p->l);
682 
683 	bc_parse_expr_status(p, flags, bc_parse_next_rel);
684 
685 	// Must have a right paren.
686 	if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
687 
688 	bc_parse_push(p, BC_INST_SCALE_FUNC);
689 
690 	bc_lex_next(&p->l);
691 }
692 
693 /**
694  * Parses and increment or decrement operator. This is a bit complex.
695  * @param p           The parser.
696  * @param prev        An out parameter; the previous instruction pointer.
697  * @param can_assign  An out parameter; whether the expression can be assigned
698  *                    to.
699  * @param nexs        An in/out parameter; the number of expressions in the
700  *                    parse tree that are not used.
701  * @param flags       The expression parsing flags for parsing a scale() arg.
702  */
703 static void
bc_parse_incdec(BcParse * p,BcInst * prev,bool * can_assign,size_t * nexs,uint8_t flags)704 bc_parse_incdec(BcParse* p, BcInst* prev, bool* can_assign, size_t* nexs,
705                 uint8_t flags)
706 {
707 	BcLexType type;
708 	uchar inst;
709 	BcInst etype = *prev;
710 	BcLexType last = p->l.last;
711 
712 	assert(prev != NULL && can_assign != NULL);
713 
714 	// If we can't assign to the previous token, then we have an error.
715 	if (BC_ERR(last == BC_LEX_OP_INC || last == BC_LEX_OP_DEC ||
716 	           last == BC_LEX_RPAREN))
717 	{
718 		bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
719 	}
720 
721 	// Is the previous instruction for a variable?
722 	if (BC_PARSE_INST_VAR(etype))
723 	{
724 		// If so, this is a postfix operator.
725 		if (!*can_assign) bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
726 
727 		// Only postfix uses BC_INST_INC and BC_INST_DEC.
728 		*prev = inst = BC_INST_INC + (p->l.t != BC_LEX_OP_INC);
729 		bc_parse_push(p, inst);
730 		bc_lex_next(&p->l);
731 		*can_assign = false;
732 	}
733 	else
734 	{
735 		// This is a prefix operator. In that case, we just convert it to
736 		// an assignment instruction.
737 		*prev = inst = BC_INST_ASSIGN_PLUS + (p->l.t != BC_LEX_OP_INC);
738 
739 		bc_lex_next(&p->l);
740 		type = p->l.t;
741 
742 		// Because we parse the next part of the expression
743 		// right here, we need to increment this.
744 		*nexs = *nexs + 1;
745 
746 		// Is the next token a normal identifier?
747 		if (type == BC_LEX_NAME)
748 		{
749 			// Parse the name.
750 			uint8_t flags2 = flags & ~(BC_PARSE_ARRAY);
751 			bc_parse_name(p, prev, can_assign, flags2 | BC_PARSE_NOCALL);
752 		}
753 		// Is the next token a global?
754 		else if (type >= BC_LEX_KW_LAST && type <= BC_LEX_KW_OBASE)
755 		{
756 			bc_parse_push(p, type - BC_LEX_KW_LAST + BC_INST_LAST);
757 			bc_lex_next(&p->l);
758 		}
759 		// Is the next token specifically scale, which needs special treatment?
760 		else if (BC_NO_ERR(type == BC_LEX_KW_SCALE))
761 		{
762 			bc_lex_next(&p->l);
763 
764 			// Check that scale() was not used.
765 			if (BC_ERR(p->l.t == BC_LEX_LPAREN))
766 			{
767 				bc_parse_err(p, BC_ERR_PARSE_TOKEN);
768 			}
769 			else bc_parse_push(p, BC_INST_SCALE);
770 		}
771 		// Now we know we have an error.
772 		else bc_parse_err(p, BC_ERR_PARSE_TOKEN);
773 
774 		*can_assign = false;
775 
776 		bc_parse_push(p, BC_INST_ONE);
777 		bc_parse_push(p, inst);
778 	}
779 }
780 
781 /**
782  * Parses the minus operator. This needs special treatment because it is either
783  * subtract or negation.
784  * @param p        The parser.
785  * @param prev     An in/out parameter; the previous instruction.
786  * @param ops_bgn  The size of the operator stack.
787  * @param rparen   True if the last token was a right paren.
788  * @param binlast  True if the last token was a binary operator.
789  * @param nexprs   An in/out parameter; the number of unused expressions.
790  */
791 static void
bc_parse_minus(BcParse * p,BcInst * prev,size_t ops_bgn,bool rparen,bool binlast,size_t * nexprs)792 bc_parse_minus(BcParse* p, BcInst* prev, size_t ops_bgn, bool rparen,
793                bool binlast, size_t* nexprs)
794 {
795 	BcLexType type;
796 
797 	bc_lex_next(&p->l);
798 
799 	// Figure out if it's a minus or a negation.
800 	type = BC_PARSE_LEAF(*prev, binlast, rparen) ? BC_LEX_OP_MINUS : BC_LEX_NEG;
801 	*prev = BC_PARSE_TOKEN_INST(type);
802 
803 	// We can just push onto the op stack because this is the largest
804 	// precedence operator that gets pushed. Inc/dec does not.
805 	if (type != BC_LEX_OP_MINUS) bc_vec_push(&p->ops, &type);
806 	else bc_parse_operator(p, type, ops_bgn, nexprs);
807 }
808 
809 /**
810  * Parses a string.
811  * @param p     The parser.
812  * @param inst  The instruction corresponding to how the string was found and
813  *              how it should be printed.
814  */
815 static void
bc_parse_str(BcParse * p,BcInst inst)816 bc_parse_str(BcParse* p, BcInst inst)
817 {
818 	bc_parse_addString(p);
819 	bc_parse_push(p, inst);
820 	bc_lex_next(&p->l);
821 }
822 
823 /**
824  * Parses a print statement.
825  * @param p  The parser.
826  */
827 static void
bc_parse_print(BcParse * p,BcLexType type)828 bc_parse_print(BcParse* p, BcLexType type)
829 {
830 	BcLexType t;
831 	bool comma = false;
832 	BcInst inst = type == BC_LEX_KW_STREAM ? BC_INST_PRINT_STREAM :
833 	                                         BC_INST_PRINT_POP;
834 
835 	bc_lex_next(&p->l);
836 
837 	t = p->l.t;
838 
839 	// A print or stream statement has to have *something*.
840 	if (bc_parse_isDelimiter(p)) bc_parse_err(p, BC_ERR_PARSE_PRINT);
841 
842 	do
843 	{
844 		// If the token is a string, then print it with escapes.
845 		// BC_INST_PRINT_POP plays that role for bc.
846 		if (t == BC_LEX_STR) bc_parse_str(p, inst);
847 		else
848 		{
849 			// We have an actual number; parse and add a print instruction.
850 			bc_parse_expr_status(p, BC_PARSE_NEEDVAL, bc_parse_next_print);
851 			bc_parse_push(p, inst);
852 		}
853 
854 		// Is the next token a comma?
855 		comma = (p->l.t == BC_LEX_COMMA);
856 
857 		// Get the next token if we have a comma.
858 		if (comma) bc_lex_next(&p->l);
859 		else
860 		{
861 			// If we don't have a comma, the statement needs to end.
862 			if (!bc_parse_isDelimiter(p)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
863 			else break;
864 		}
865 
866 		t = p->l.t;
867 	}
868 	while (true);
869 
870 	// If we have a comma but no token, that's bad.
871 	if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
872 }
873 
874 /**
875  * Parses a return statement.
876  * @param p  The parser.
877  */
878 static void
bc_parse_return(BcParse * p)879 bc_parse_return(BcParse* p)
880 {
881 	BcLexType t;
882 	bool paren;
883 	uchar inst = BC_INST_RET0;
884 
885 	// If we are not in a function, that's an error.
886 	if (BC_ERR(!BC_PARSE_FUNC(p))) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
887 
888 	// If we are in a void function, make sure to return void.
889 	if (p->func->voidfn) inst = BC_INST_RET_VOID;
890 
891 	bc_lex_next(&p->l);
892 
893 	t = p->l.t;
894 	paren = (t == BC_LEX_LPAREN);
895 
896 	// An empty return statement just needs to push the selected instruction.
897 	if (bc_parse_isDelimiter(p)) bc_parse_push(p, inst);
898 	else
899 	{
900 		BcParseStatus s;
901 
902 		// Need to parse the expression whose value will be returned.
903 		s = bc_parse_expr_err(p, BC_PARSE_NEEDVAL, bc_parse_next_expr);
904 
905 		// If the expression was empty, just push the selected instruction.
906 		if (s == BC_PARSE_STATUS_EMPTY_EXPR)
907 		{
908 			bc_parse_push(p, inst);
909 			bc_lex_next(&p->l);
910 		}
911 
912 		// POSIX requires parentheses.
913 		if (!paren || p->l.last != BC_LEX_RPAREN)
914 		{
915 			bc_parse_err(p, BC_ERR_POSIX_RET);
916 		}
917 
918 		// Void functions require an empty expression.
919 		if (BC_ERR(p->func->voidfn))
920 		{
921 			if (s != BC_PARSE_STATUS_EMPTY_EXPR)
922 			{
923 				bc_parse_verr(p, BC_ERR_PARSE_RET_VOID, p->func->name);
924 			}
925 		}
926 		// If we got here, we want to be sure to end the function with a real
927 		// return instruction, just in case.
928 		else bc_parse_push(p, BC_INST_RET);
929 	}
930 }
931 
932 /**
933  * Clears flags that indicate the end of an if statement and its block and sets
934  * the jump location.
935  * @param p  The parser.
936  */
937 static void
bc_parse_noElse(BcParse * p)938 bc_parse_noElse(BcParse* p)
939 {
940 	uint16_t* flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
941 	*flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
942 	bc_parse_setLabel(p);
943 }
944 
945 /**
946  * Ends (finishes parsing) the body of a control statement or a function.
947  * @param p      The parser.
948  * @param brace  True if the body was ended by a brace, false otherwise.
949  */
950 static void
bc_parse_endBody(BcParse * p,bool brace)951 bc_parse_endBody(BcParse* p, bool brace)
952 {
953 	bool has_brace, new_else = false;
954 
955 	// We cannot be ending a body if there are no bodies to end.
956 	if (BC_ERR(p->flags.len <= 1)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
957 
958 	if (brace)
959 	{
960 		// The brace was already gotten; make sure that the caller did not lie.
961 		// We check for the requirement of braces later.
962 		assert(p->l.t == BC_LEX_RBRACE);
963 
964 		bc_lex_next(&p->l);
965 
966 		// If the next token is not a delimiter, that is a problem.
967 		if (BC_ERR(!bc_parse_isDelimiter(p) && !bc_parse_TopFunc(p)))
968 		{
969 			bc_parse_err(p, BC_ERR_PARSE_TOKEN);
970 		}
971 	}
972 
973 	// Do we have a brace flag?
974 	has_brace = (BC_PARSE_BRACE(p) != 0);
975 
976 	do
977 	{
978 		size_t len = p->flags.len;
979 		bool loop;
980 
981 		// If we have a brace flag but not a brace, that's a problem.
982 		if (has_brace && !brace) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
983 
984 		// Are we inside a loop?
985 		loop = (BC_PARSE_LOOP_INNER(p) != 0);
986 
987 		// If we are ending a loop or an else...
988 		if (loop || BC_PARSE_ELSE(p))
989 		{
990 			// Loops have condition labels that we have to take care of as well.
991 			if (loop)
992 			{
993 				size_t* label = bc_vec_top(&p->conds);
994 
995 				bc_parse_push(p, BC_INST_JUMP);
996 				bc_parse_pushIndex(p, *label);
997 
998 				bc_vec_pop(&p->conds);
999 			}
1000 
1001 			bc_parse_setLabel(p);
1002 			bc_vec_pop(&p->flags);
1003 		}
1004 		// If we are ending a function...
1005 		else if (BC_PARSE_FUNC_INNER(p))
1006 		{
1007 			BcInst inst = (p->func->voidfn ? BC_INST_RET_VOID : BC_INST_RET0);
1008 			bc_parse_push(p, inst);
1009 			bc_parse_updateFunc(p, BC_PROG_MAIN);
1010 			bc_vec_pop(&p->flags);
1011 		}
1012 		// If we have a brace flag and not an if statement, we can pop the top
1013 		// of the flags stack because they have been taken care of above.
1014 		else if (has_brace && !BC_PARSE_IF(p)) bc_vec_pop(&p->flags);
1015 
1016 		// This needs to be last to parse nested if's properly.
1017 		if (BC_PARSE_IF(p) && (len == p->flags.len || !BC_PARSE_BRACE(p)))
1018 		{
1019 			// Eat newlines.
1020 			while (p->l.t == BC_LEX_NLINE)
1021 			{
1022 				bc_lex_next(&p->l);
1023 			}
1024 
1025 			// *Now* we can pop the flags.
1026 			bc_vec_pop(&p->flags);
1027 
1028 			// If we are allowed non-POSIX stuff...
1029 			if (!BC_S)
1030 			{
1031 				// Have we found yet another dangling else?
1032 				*(BC_PARSE_TOP_FLAG_PTR(p)) |= BC_PARSE_FLAG_IF_END;
1033 				new_else = (p->l.t == BC_LEX_KW_ELSE);
1034 
1035 				// Parse the else or end the if statement body.
1036 				if (new_else) bc_parse_else(p);
1037 				else if (!has_brace && (!BC_PARSE_IF_END(p) || brace))
1038 				{
1039 					bc_parse_noElse(p);
1040 				}
1041 			}
1042 			// POSIX requires us to do the bare minimum only.
1043 			else bc_parse_noElse(p);
1044 		}
1045 
1046 		// If these are both true, we have "used" the braces that we found.
1047 		if (brace && has_brace) brace = false;
1048 	}
1049 	// This condition was perhaps the hardest single part of the parser. If
1050 	// the flags stack does not have enough, we should stop. If we have a
1051 	// new else statement, we should stop. If we do have the end of an if
1052 	// statement and we have eaten the brace, we should stop. If we do have
1053 	// a brace flag, we should stop.
1054 	while (p->flags.len > 1 && !new_else && (!BC_PARSE_IF_END(p) || brace) &&
1055 	       !(has_brace = (BC_PARSE_BRACE(p) != 0)));
1056 
1057 	// If we have a brace, yet no body for it, that's a problem.
1058 	if (BC_ERR(p->flags.len == 1 && brace)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1059 	else if (brace && BC_PARSE_BRACE(p))
1060 	{
1061 		// If we make it here, we have a brace and a flag for it.
1062 		uint16_t flags = BC_PARSE_TOP_FLAG(p);
1063 
1064 		// This condition ensure that the *last* body is correctly finished by
1065 		// popping its flags.
1066 		if (!(flags & (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_LOOP_INNER)) &&
1067 		    !(flags & (BC_PARSE_FLAG_IF | BC_PARSE_FLAG_ELSE)) &&
1068 		    !(flags & (BC_PARSE_FLAG_IF_END)))
1069 		{
1070 			bc_vec_pop(&p->flags);
1071 		}
1072 	}
1073 }
1074 
1075 /**
1076  * Starts the body of a control statement or function.
1077  * @param p      The parser.
1078  * @param flags  The current flags (will be edited).
1079  */
1080 static void
bc_parse_startBody(BcParse * p,uint16_t flags)1081 bc_parse_startBody(BcParse* p, uint16_t flags)
1082 {
1083 	assert(flags);
1084 	flags |= (BC_PARSE_TOP_FLAG(p) & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
1085 	flags |= BC_PARSE_FLAG_BODY;
1086 	bc_vec_push(&p->flags, &flags);
1087 }
1088 
1089 void
bc_parse_endif(BcParse * p)1090 bc_parse_endif(BcParse* p)
1091 {
1092 	size_t i;
1093 	bool good;
1094 
1095 	// Not a problem if this is true.
1096 	if (BC_NO_ERR(!BC_PARSE_NO_EXEC(p))) return;
1097 
1098 	good = true;
1099 
1100 	// Find an instance of a body that needs closing, i.e., a statement that did
1101 	// not have a right brace when it should have.
1102 	for (i = 0; good && i < p->flags.len; ++i)
1103 	{
1104 		uint16_t flag = *((uint16_t*) bc_vec_item(&p->flags, i));
1105 		good = ((flag & BC_PARSE_FLAG_BRACE) != BC_PARSE_FLAG_BRACE);
1106 	}
1107 
1108 	// If we did not find such an instance...
1109 	if (good)
1110 	{
1111 		// We set this to restore it later. We don't want the parser thinking
1112 		// that we are on stdin for this one because it will want more.
1113 		BcMode mode = vm->mode;
1114 
1115 		vm->mode = BC_MODE_FILE;
1116 
1117 		// End all of the if statements and loops.
1118 		while (p->flags.len > 1 || BC_PARSE_IF_END(p))
1119 		{
1120 			if (BC_PARSE_IF_END(p)) bc_parse_noElse(p);
1121 			if (p->flags.len > 1) bc_parse_endBody(p, false);
1122 		}
1123 
1124 		vm->mode = (uchar) mode;
1125 	}
1126 	// If we reach here, a block was not properly closed, and we should error.
1127 	else bc_parse_err(&vm->prs, BC_ERR_PARSE_BLOCK);
1128 }
1129 
1130 /**
1131  * Parses an if statement.
1132  * @param p  The parser.
1133  */
1134 static void
bc_parse_if(BcParse * p)1135 bc_parse_if(BcParse* p)
1136 {
1137 	// We are allowed relational operators, and we must have a value.
1138 	size_t idx;
1139 	uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
1140 
1141 	// Get the left paren and barf if necessary.
1142 	bc_lex_next(&p->l);
1143 	if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1144 
1145 	// Parse the condition.
1146 	bc_lex_next(&p->l);
1147 	bc_parse_expr_status(p, flags, bc_parse_next_rel);
1148 
1149 	// Must have a right paren.
1150 	if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1151 
1152 	bc_lex_next(&p->l);
1153 
1154 	// Insert the conditional jump instruction.
1155 	bc_parse_push(p, BC_INST_JUMP_ZERO);
1156 
1157 	idx = p->func->labels.len;
1158 
1159 	// Push the index for the instruction and create an exit label for an else
1160 	// statement.
1161 	bc_parse_pushIndex(p, idx);
1162 	bc_parse_createExitLabel(p, idx, false);
1163 
1164 	bc_parse_startBody(p, BC_PARSE_FLAG_IF);
1165 }
1166 
1167 /**
1168  * Parses an else statement.
1169  * @param p  The parser.
1170  */
1171 static void
bc_parse_else(BcParse * p)1172 bc_parse_else(BcParse* p)
1173 {
1174 	size_t idx = p->func->labels.len;
1175 
1176 	// We must be at the end of an if statement.
1177 	if (BC_ERR(!BC_PARSE_IF_END(p))) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1178 
1179 	// Push an unconditional jump to make bc jump over the else statement if it
1180 	// executed the original if statement.
1181 	bc_parse_push(p, BC_INST_JUMP);
1182 	bc_parse_pushIndex(p, idx);
1183 
1184 	// Clear the else stuff. Yes, that function is misnamed for its use here,
1185 	// but deal with it.
1186 	bc_parse_noElse(p);
1187 
1188 	// Create the exit label and parse the body.
1189 	bc_parse_createExitLabel(p, idx, false);
1190 	bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
1191 
1192 	bc_lex_next(&p->l);
1193 }
1194 
1195 /**
1196  * Parse a while loop.
1197  * @param p  The parser.
1198  */
1199 static void
bc_parse_while(BcParse * p)1200 bc_parse_while(BcParse* p)
1201 {
1202 	// We are allowed relational operators, and we must have a value.
1203 	size_t idx;
1204 	uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
1205 
1206 	// Get the left paren and barf if necessary.
1207 	bc_lex_next(&p->l);
1208 	if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1209 	bc_lex_next(&p->l);
1210 
1211 	// Create the labels. Loops need both.
1212 	bc_parse_createCondLabel(p, p->func->labels.len);
1213 	idx = p->func->labels.len;
1214 	bc_parse_createExitLabel(p, idx, true);
1215 
1216 	// Parse the actual condition and barf on non-right paren.
1217 	bc_parse_expr_status(p, flags, bc_parse_next_rel);
1218 	if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1219 	bc_lex_next(&p->l);
1220 
1221 	// Now we can push the conditional jump and start the body.
1222 	bc_parse_push(p, BC_INST_JUMP_ZERO);
1223 	bc_parse_pushIndex(p, idx);
1224 	bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
1225 }
1226 
1227 /**
1228  * Parse a for loop.
1229  * @param p  The parser.
1230  */
1231 static void
bc_parse_for(BcParse * p)1232 bc_parse_for(BcParse* p)
1233 {
1234 	size_t cond_idx, exit_idx, body_idx, update_idx;
1235 
1236 	// Barf on the missing left paren.
1237 	bc_lex_next(&p->l);
1238 	if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1239 	bc_lex_next(&p->l);
1240 
1241 	// The first statement can be empty, but if it is, check for error in POSIX
1242 	// mode. Otherwise, parse it.
1243 	if (p->l.t != BC_LEX_SCOLON) bc_parse_expr_status(p, 0, bc_parse_next_for);
1244 	else bc_parse_err(p, BC_ERR_POSIX_FOR);
1245 
1246 	// Must have a semicolon.
1247 	if (BC_ERR(p->l.t != BC_LEX_SCOLON)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1248 	bc_lex_next(&p->l);
1249 
1250 	// These are indices for labels. There are so many of them because the end
1251 	// of the loop must unconditionally jump to the update code. Then the update
1252 	// code must unconditionally jump to the condition code. Then the condition
1253 	// code must *conditionally* jump to the exit.
1254 	cond_idx = p->func->labels.len;
1255 	update_idx = cond_idx + 1;
1256 	body_idx = update_idx + 1;
1257 	exit_idx = body_idx + 1;
1258 
1259 	// This creates the condition label.
1260 	bc_parse_createLabel(p, p->func->code.len);
1261 
1262 	// Parse an expression if it exists.
1263 	if (p->l.t != BC_LEX_SCOLON)
1264 	{
1265 		uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
1266 		bc_parse_expr_status(p, flags, bc_parse_next_for);
1267 	}
1268 	else
1269 	{
1270 		// Set this for the next call to bc_parse_number because an empty
1271 		// condition means that it is an infinite loop, so the condition must be
1272 		// non-zero. This is safe to set because the current token is a
1273 		// semicolon, which has no string requirement.
1274 		bc_vec_string(&p->l.str, sizeof(bc_parse_one) - 1, bc_parse_one);
1275 		bc_parse_number(p);
1276 
1277 		// An empty condition makes POSIX mad.
1278 		bc_parse_err(p, BC_ERR_POSIX_FOR);
1279 	}
1280 
1281 	// Must have a semicolon.
1282 	if (BC_ERR(p->l.t != BC_LEX_SCOLON)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1283 	bc_lex_next(&p->l);
1284 
1285 	// Now we can set up the conditional jump to the exit and an unconditional
1286 	// jump to the body right after. The unconditional jump to the body is
1287 	// because there is update code coming right after the condition, so we need
1288 	// to skip it to get to the body.
1289 	bc_parse_push(p, BC_INST_JUMP_ZERO);
1290 	bc_parse_pushIndex(p, exit_idx);
1291 	bc_parse_push(p, BC_INST_JUMP);
1292 	bc_parse_pushIndex(p, body_idx);
1293 
1294 	// Now create the label for the update code.
1295 	bc_parse_createCondLabel(p, update_idx);
1296 
1297 	// Parse if not empty, and if it is, let POSIX yell if necessary.
1298 	if (p->l.t != BC_LEX_RPAREN) bc_parse_expr_status(p, 0, bc_parse_next_rel);
1299 	else bc_parse_err(p, BC_ERR_POSIX_FOR);
1300 
1301 	// Must have a right paren.
1302 	if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1303 
1304 	// Set up a jump to the condition right after the update code.
1305 	bc_parse_push(p, BC_INST_JUMP);
1306 	bc_parse_pushIndex(p, cond_idx);
1307 	bc_parse_createLabel(p, p->func->code.len);
1308 
1309 	// Create an exit label for the body and start the body.
1310 	bc_parse_createExitLabel(p, exit_idx, true);
1311 	bc_lex_next(&p->l);
1312 	bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
1313 }
1314 
1315 /**
1316  * Parse a statement or token that indicates a loop exit. This includes an
1317  * actual loop exit, the break keyword, or the continue keyword.
1318  * @param p     The parser.
1319  * @param type  The type of exit.
1320  */
1321 static void
bc_parse_loopExit(BcParse * p,BcLexType type)1322 bc_parse_loopExit(BcParse* p, BcLexType type)
1323 {
1324 	size_t i;
1325 	BcInstPtr* ip;
1326 
1327 	// Must have a loop. If we don't, that's an error.
1328 	if (BC_ERR(!BC_PARSE_LOOP(p))) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1329 
1330 	// If we have a break statement...
1331 	if (type == BC_LEX_KW_BREAK)
1332 	{
1333 		// If there are no exits, something went wrong somewhere.
1334 		if (BC_ERR(!p->exits.len)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1335 
1336 		// Get the exit.
1337 		i = p->exits.len - 1;
1338 		ip = bc_vec_item(&p->exits, i);
1339 
1340 		// The condition !ip->func is true if the exit is not for a loop, so we
1341 		// need to find the first actual loop exit.
1342 		while (!ip->func && i < p->exits.len)
1343 		{
1344 			ip = bc_vec_item(&p->exits, i);
1345 			i -= 1;
1346 		}
1347 
1348 		// Make sure everything is hunky dory.
1349 		assert(ip != NULL && (i < p->exits.len || ip->func));
1350 
1351 		// Set the index for the exit.
1352 		i = ip->idx;
1353 	}
1354 	// If we have a continue statement or just the loop end, jump to the
1355 	// condition (or update for a foor loop).
1356 	else i = *((size_t*) bc_vec_top(&p->conds));
1357 
1358 	// Add the unconditional jump.
1359 	bc_parse_push(p, BC_INST_JUMP);
1360 	bc_parse_pushIndex(p, i);
1361 
1362 	bc_lex_next(&p->l);
1363 }
1364 
1365 /**
1366  * Parse a function (header).
1367  * @param p  The parser.
1368  */
1369 static void
bc_parse_func(BcParse * p)1370 bc_parse_func(BcParse* p)
1371 {
1372 	bool comma = false, voidfn;
1373 	uint16_t flags;
1374 	size_t idx;
1375 
1376 	bc_lex_next(&p->l);
1377 
1378 	// Must have a name.
1379 	if (BC_ERR(p->l.t != BC_LEX_NAME)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
1380 
1381 	// If the name is "void", and POSIX is not on, mark as void.
1382 	voidfn = (!BC_IS_POSIX && p->l.t == BC_LEX_NAME &&
1383 	          !strcmp(p->l.str.v, "void"));
1384 
1385 	// We can safely do this because the expected token should not overwrite the
1386 	// function name.
1387 	bc_lex_next(&p->l);
1388 
1389 	// If we *don't* have another name, then void is the name of the function.
1390 	voidfn = (voidfn && p->l.t == BC_LEX_NAME);
1391 
1392 	// With a void function, allow POSIX to complain and get a new token.
1393 	if (voidfn)
1394 	{
1395 		bc_parse_err(p, BC_ERR_POSIX_VOID);
1396 
1397 		// We can safely do this because the expected token should not overwrite
1398 		// the function name.
1399 		bc_lex_next(&p->l);
1400 	}
1401 
1402 	// Must have a left paren.
1403 	if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
1404 
1405 	// Make sure the functions map and vector are synchronized.
1406 	assert(p->prog->fns.len == p->prog->fn_map.len);
1407 
1408 	// Insert the function by name into the map and vector.
1409 	idx = bc_program_insertFunc(p->prog, p->l.str.v);
1410 
1411 	// Make sure the insert worked.
1412 	assert(idx);
1413 
1414 	// Update the function pointer and stuff in the parser and set its void.
1415 	bc_parse_updateFunc(p, idx);
1416 	p->func->voidfn = voidfn;
1417 
1418 	bc_lex_next(&p->l);
1419 
1420 	// While we do not have a right paren, we are still parsing arguments.
1421 	while (p->l.t != BC_LEX_RPAREN)
1422 	{
1423 		BcType t = BC_TYPE_VAR;
1424 
1425 		// If we have an asterisk, we are parsing a reference argument.
1426 		if (p->l.t == BC_LEX_OP_MULTIPLY)
1427 		{
1428 			t = BC_TYPE_REF;
1429 			bc_lex_next(&p->l);
1430 
1431 			// Let POSIX complain if necessary.
1432 			bc_parse_err(p, BC_ERR_POSIX_REF);
1433 		}
1434 
1435 		// If we don't have a name, the argument will not have a name. Barf.
1436 		if (BC_ERR(p->l.t != BC_LEX_NAME)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
1437 
1438 		// Increment the number of parameters.
1439 		p->func->nparams += 1;
1440 
1441 		// Copy the string in the lexer so that we can use the lexer again.
1442 		bc_vec_string(&p->buf, p->l.str.len, p->l.str.v);
1443 
1444 		bc_lex_next(&p->l);
1445 
1446 		// We are parsing an array parameter if this is true.
1447 		if (p->l.t == BC_LEX_LBRACKET)
1448 		{
1449 			// Set the array type, unless we are already parsing a reference.
1450 			if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY;
1451 
1452 			bc_lex_next(&p->l);
1453 
1454 			// The brackets *must* be empty.
1455 			if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
1456 			{
1457 				bc_parse_err(p, BC_ERR_PARSE_FUNC);
1458 			}
1459 
1460 			bc_lex_next(&p->l);
1461 		}
1462 		// If we did *not* get a bracket, but we are expecting a reference, we
1463 		// have a problem.
1464 		else if (BC_ERR(t == BC_TYPE_REF))
1465 		{
1466 			bc_parse_verr(p, BC_ERR_PARSE_REF_VAR, p->buf.v);
1467 		}
1468 
1469 		// Test for comma and get the next token if it exists.
1470 		comma = (p->l.t == BC_LEX_COMMA);
1471 		if (comma) bc_lex_next(&p->l);
1472 
1473 		// Insert the parameter into the function.
1474 		bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line);
1475 	}
1476 
1477 	// If we have a comma, but no parameter, barf.
1478 	if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
1479 
1480 	// Start the body.
1481 	flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER;
1482 	bc_parse_startBody(p, flags);
1483 
1484 	bc_lex_next(&p->l);
1485 
1486 	// POSIX requires that a brace be on the same line as the function header.
1487 	// If we don't have a brace, let POSIX throw an error.
1488 	if (p->l.t != BC_LEX_LBRACE) bc_parse_err(p, BC_ERR_POSIX_BRACE);
1489 }
1490 
1491 /**
1492  * Parse an auto list.
1493  * @param p  The parser.
1494  */
1495 static void
bc_parse_auto(BcParse * p)1496 bc_parse_auto(BcParse* p)
1497 {
1498 	bool comma, one;
1499 
1500 	// Error if the auto keyword appeared in the wrong place.
1501 	if (BC_ERR(!p->auto_part)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1502 	bc_lex_next(&p->l);
1503 
1504 	p->auto_part = comma = false;
1505 
1506 	// We need at least one variable or array.
1507 	one = (p->l.t == BC_LEX_NAME);
1508 
1509 	// While we have a variable or array.
1510 	while (p->l.t == BC_LEX_NAME)
1511 	{
1512 		BcType t;
1513 
1514 		// Copy the name from the lexer, so we can use it again.
1515 		bc_vec_string(&p->buf, p->l.str.len - 1, p->l.str.v);
1516 
1517 		bc_lex_next(&p->l);
1518 
1519 		// If we are parsing an array...
1520 		if (p->l.t == BC_LEX_LBRACKET)
1521 		{
1522 			t = BC_TYPE_ARRAY;
1523 
1524 			bc_lex_next(&p->l);
1525 
1526 			// The brackets *must* be empty.
1527 			if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
1528 			{
1529 				bc_parse_err(p, BC_ERR_PARSE_FUNC);
1530 			}
1531 
1532 			bc_lex_next(&p->l);
1533 		}
1534 		else t = BC_TYPE_VAR;
1535 
1536 		// Test for comma and get the next token if it exists.
1537 		comma = (p->l.t == BC_LEX_COMMA);
1538 		if (comma) bc_lex_next(&p->l);
1539 
1540 		// Insert the auto into the function.
1541 		bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line);
1542 	}
1543 
1544 	// If we have a comma, but no auto, barf.
1545 	if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
1546 
1547 	// If we don't have any variables or arrays, barf.
1548 	if (BC_ERR(!one)) bc_parse_err(p, BC_ERR_PARSE_NO_AUTO);
1549 
1550 	// The auto statement should be all that's in the statement.
1551 	if (BC_ERR(!bc_parse_isDelimiter(p))) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1552 }
1553 
1554 /**
1555  * Parses a body.
1556  * @param p      The parser.
1557  * @param brace  True if a brace was encountered, false otherwise.
1558  */
1559 static void
bc_parse_body(BcParse * p,bool brace)1560 bc_parse_body(BcParse* p, bool brace)
1561 {
1562 	uint16_t* flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
1563 
1564 	assert(flag_ptr != NULL);
1565 	assert(p->flags.len >= 2);
1566 
1567 	// The body flag is for when we expect a body. We got a body, so clear the
1568 	// flag.
1569 	*flag_ptr &= ~(BC_PARSE_FLAG_BODY);
1570 
1571 	// If we are inside a function, that means we just barely entered it, and
1572 	// we can expect an auto list.
1573 	if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER)
1574 	{
1575 		// We *must* have a brace in this case.
1576 		if (BC_ERR(!brace)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1577 
1578 		p->auto_part = (p->l.t != BC_LEX_KW_AUTO);
1579 
1580 		if (!p->auto_part)
1581 		{
1582 			// Make sure this is true to not get a parse error.
1583 			p->auto_part = true;
1584 
1585 			// Since we already have the auto keyword, parse.
1586 			bc_parse_auto(p);
1587 		}
1588 
1589 		// Eat a newline.
1590 		if (p->l.t == BC_LEX_NLINE) bc_lex_next(&p->l);
1591 	}
1592 	else
1593 	{
1594 		// This is the easy part.
1595 		size_t len = p->flags.len;
1596 
1597 		assert(*flag_ptr);
1598 
1599 		// Parse a statement.
1600 		bc_parse_stmt(p);
1601 
1602 		// This is a very important condition to get right. If there is no
1603 		// brace, and no body flag, and the flags len hasn't shrunk, then we
1604 		// have a body that was not delimited by braces, so we need to end it
1605 		// now, after just one statement.
1606 		if (!brace && !BC_PARSE_BODY(p) && len <= p->flags.len)
1607 		{
1608 			bc_parse_endBody(p, false);
1609 		}
1610 	}
1611 }
1612 
1613 /**
1614  * Parses a statement. This is the entry point for just about everything, except
1615  * function definitions.
1616  * @param p  The parser.
1617  */
1618 static void
bc_parse_stmt(BcParse * p)1619 bc_parse_stmt(BcParse* p)
1620 {
1621 	size_t len;
1622 	uint16_t flags;
1623 	BcLexType type = p->l.t;
1624 
1625 	// Eat newline.
1626 	if (type == BC_LEX_NLINE)
1627 	{
1628 		bc_lex_next(&p->l);
1629 		return;
1630 	}
1631 
1632 	// Eat auto list.
1633 	if (type == BC_LEX_KW_AUTO)
1634 	{
1635 		bc_parse_auto(p);
1636 		return;
1637 	}
1638 
1639 	// If we reach this point, no auto list is allowed.
1640 	p->auto_part = false;
1641 
1642 	// Everything but an else needs to be taken care of here, but else is
1643 	// special.
1644 	if (type != BC_LEX_KW_ELSE)
1645 	{
1646 		// After an if, no else found.
1647 		if (BC_PARSE_IF_END(p))
1648 		{
1649 			// Clear the expectation for else, end body, and return. Returning
1650 			// gives us a clean slate for parsing again.
1651 			bc_parse_noElse(p);
1652 			if (p->flags.len > 1 && !BC_PARSE_BRACE(p))
1653 			{
1654 				bc_parse_endBody(p, false);
1655 			}
1656 
1657 			return;
1658 		}
1659 		// With a left brace, we are parsing a body.
1660 		else if (type == BC_LEX_LBRACE)
1661 		{
1662 			// We need to start a body if we are not expecting one yet.
1663 			if (!BC_PARSE_BODY(p))
1664 			{
1665 				bc_parse_startBody(p, BC_PARSE_FLAG_BRACE);
1666 				bc_lex_next(&p->l);
1667 			}
1668 			// If we *are* expecting a body, that body should get a brace. This
1669 			// takes care of braces being on a different line than if and loop
1670 			// headers.
1671 			else
1672 			{
1673 				*(BC_PARSE_TOP_FLAG_PTR(p)) |= BC_PARSE_FLAG_BRACE;
1674 				bc_lex_next(&p->l);
1675 				bc_parse_body(p, true);
1676 			}
1677 
1678 			// If we have reached this point, we need to return for a clean
1679 			// slate.
1680 			return;
1681 		}
1682 		// This happens when we are expecting a body and get a single statement,
1683 		// i.e., a body with no braces surrounding it. Returns after for a clean
1684 		// slate.
1685 		else if (BC_PARSE_BODY(p) && !BC_PARSE_BRACE(p))
1686 		{
1687 			bc_parse_body(p, false);
1688 			return;
1689 		}
1690 	}
1691 
1692 	len = p->flags.len;
1693 	flags = BC_PARSE_TOP_FLAG(p);
1694 
1695 	switch (type)
1696 	{
1697 		// All of these are valid for expressions.
1698 		case BC_LEX_OP_INC:
1699 		case BC_LEX_OP_DEC:
1700 		case BC_LEX_OP_MINUS:
1701 		case BC_LEX_OP_BOOL_NOT:
1702 		case BC_LEX_LPAREN:
1703 		case BC_LEX_NAME:
1704 		case BC_LEX_NUMBER:
1705 		case BC_LEX_KW_IBASE:
1706 		case BC_LEX_KW_LAST:
1707 		case BC_LEX_KW_LENGTH:
1708 		case BC_LEX_KW_OBASE:
1709 		case BC_LEX_KW_SCALE:
1710 #if BC_ENABLE_EXTRA_MATH
1711 		case BC_LEX_KW_SEED:
1712 #endif // BC_ENABLE_EXTRA_MATH
1713 		case BC_LEX_KW_SQRT:
1714 		case BC_LEX_KW_ABS:
1715 		case BC_LEX_KW_IS_NUMBER:
1716 		case BC_LEX_KW_IS_STRING:
1717 #if BC_ENABLE_EXTRA_MATH
1718 		case BC_LEX_KW_IRAND:
1719 #endif // BC_ENABLE_EXTRA_MATH
1720 		case BC_LEX_KW_ASCIIFY:
1721 		case BC_LEX_KW_MODEXP:
1722 		case BC_LEX_KW_DIVMOD:
1723 		case BC_LEX_KW_READ:
1724 #if BC_ENABLE_EXTRA_MATH
1725 		case BC_LEX_KW_RAND:
1726 #endif // BC_ENABLE_EXTRA_MATH
1727 		case BC_LEX_KW_MAXIBASE:
1728 		case BC_LEX_KW_MAXOBASE:
1729 		case BC_LEX_KW_MAXSCALE:
1730 #if BC_ENABLE_EXTRA_MATH
1731 		case BC_LEX_KW_MAXRAND:
1732 #endif // BC_ENABLE_EXTRA_MATH
1733 		case BC_LEX_KW_LINE_LENGTH:
1734 		case BC_LEX_KW_GLOBAL_STACKS:
1735 		case BC_LEX_KW_LEADING_ZERO:
1736 		{
1737 			bc_parse_expr_status(p, BC_PARSE_PRINT, bc_parse_next_expr);
1738 			break;
1739 		}
1740 
1741 		case BC_LEX_KW_ELSE:
1742 		{
1743 			bc_parse_else(p);
1744 			break;
1745 		}
1746 
1747 		// Just eat.
1748 		case BC_LEX_SCOLON:
1749 		{
1750 			// Do nothing.
1751 			break;
1752 		}
1753 
1754 		case BC_LEX_RBRACE:
1755 		{
1756 			bc_parse_endBody(p, true);
1757 			break;
1758 		}
1759 
1760 		case BC_LEX_STR:
1761 		{
1762 			bc_parse_str(p, BC_INST_PRINT_STR);
1763 			break;
1764 		}
1765 
1766 		case BC_LEX_KW_BREAK:
1767 		case BC_LEX_KW_CONTINUE:
1768 		{
1769 			bc_parse_loopExit(p, p->l.t);
1770 			break;
1771 		}
1772 
1773 		case BC_LEX_KW_FOR:
1774 		{
1775 			bc_parse_for(p);
1776 			break;
1777 		}
1778 
1779 		case BC_LEX_KW_HALT:
1780 		{
1781 			bc_parse_push(p, BC_INST_HALT);
1782 			bc_lex_next(&p->l);
1783 			break;
1784 		}
1785 
1786 		case BC_LEX_KW_IF:
1787 		{
1788 			bc_parse_if(p);
1789 			break;
1790 		}
1791 
1792 		case BC_LEX_KW_LIMITS:
1793 		{
1794 			// `limits` is a compile-time command, so execute it right away.
1795 			bc_vm_printf("BC_LONG_BIT      = %lu\n", (ulong) BC_LONG_BIT);
1796 			bc_vm_printf("BC_BASE_DIGS     = %lu\n", (ulong) BC_BASE_DIGS);
1797 			bc_vm_printf("BC_BASE_POW      = %lu\n", (ulong) BC_BASE_POW);
1798 			bc_vm_printf("BC_OVERFLOW_MAX  = %lu\n", (ulong) BC_NUM_BIGDIG_MAX);
1799 			bc_vm_printf("\n");
1800 			bc_vm_printf("BC_BASE_MAX      = %lu\n", BC_MAX_OBASE);
1801 			bc_vm_printf("BC_DIM_MAX       = %lu\n", BC_MAX_DIM);
1802 			bc_vm_printf("BC_SCALE_MAX     = %lu\n", BC_MAX_SCALE);
1803 			bc_vm_printf("BC_STRING_MAX    = %lu\n", BC_MAX_STRING);
1804 			bc_vm_printf("BC_NAME_MAX      = %lu\n", BC_MAX_NAME);
1805 			bc_vm_printf("BC_NUM_MAX       = %lu\n", BC_MAX_NUM);
1806 #if BC_ENABLE_EXTRA_MATH
1807 			bc_vm_printf("BC_RAND_MAX      = %lu\n", BC_MAX_RAND);
1808 #endif // BC_ENABLE_EXTRA_MATH
1809 			bc_vm_printf("MAX Exponent     = %lu\n", BC_MAX_EXP);
1810 			bc_vm_printf("Number of vars   = %lu\n", BC_MAX_VARS);
1811 
1812 			bc_lex_next(&p->l);
1813 
1814 			break;
1815 		}
1816 
1817 		case BC_LEX_KW_STREAM:
1818 		case BC_LEX_KW_PRINT:
1819 		{
1820 			bc_parse_print(p, type);
1821 			break;
1822 		}
1823 
1824 		case BC_LEX_KW_QUIT:
1825 		{
1826 			// Quit is a compile-time command. We don't exit directly, so the vm
1827 			// can clean up.
1828 			vm->status = BC_STATUS_QUIT;
1829 			BC_JMP;
1830 			break;
1831 		}
1832 
1833 		case BC_LEX_KW_RETURN:
1834 		{
1835 			bc_parse_return(p);
1836 			break;
1837 		}
1838 
1839 		case BC_LEX_KW_WHILE:
1840 		{
1841 			bc_parse_while(p);
1842 			break;
1843 		}
1844 
1845 		case BC_LEX_EOF:
1846 		case BC_LEX_INVALID:
1847 		case BC_LEX_NEG:
1848 #if BC_ENABLE_EXTRA_MATH
1849 		case BC_LEX_OP_TRUNC:
1850 #endif // BC_ENABLE_EXTRA_MATH
1851 		case BC_LEX_OP_POWER:
1852 		case BC_LEX_OP_MULTIPLY:
1853 		case BC_LEX_OP_DIVIDE:
1854 		case BC_LEX_OP_MODULUS:
1855 		case BC_LEX_OP_PLUS:
1856 #if BC_ENABLE_EXTRA_MATH
1857 		case BC_LEX_OP_PLACES:
1858 		case BC_LEX_OP_LSHIFT:
1859 		case BC_LEX_OP_RSHIFT:
1860 #endif // BC_ENABLE_EXTRA_MATH
1861 		case BC_LEX_OP_REL_EQ:
1862 		case BC_LEX_OP_REL_LE:
1863 		case BC_LEX_OP_REL_GE:
1864 		case BC_LEX_OP_REL_NE:
1865 		case BC_LEX_OP_REL_LT:
1866 		case BC_LEX_OP_REL_GT:
1867 		case BC_LEX_OP_BOOL_OR:
1868 		case BC_LEX_OP_BOOL_AND:
1869 		case BC_LEX_OP_ASSIGN_POWER:
1870 		case BC_LEX_OP_ASSIGN_MULTIPLY:
1871 		case BC_LEX_OP_ASSIGN_DIVIDE:
1872 		case BC_LEX_OP_ASSIGN_MODULUS:
1873 		case BC_LEX_OP_ASSIGN_PLUS:
1874 		case BC_LEX_OP_ASSIGN_MINUS:
1875 #if BC_ENABLE_EXTRA_MATH
1876 		case BC_LEX_OP_ASSIGN_PLACES:
1877 		case BC_LEX_OP_ASSIGN_LSHIFT:
1878 		case BC_LEX_OP_ASSIGN_RSHIFT:
1879 #endif // BC_ENABLE_EXTRA_MATH
1880 		case BC_LEX_OP_ASSIGN:
1881 		case BC_LEX_NLINE:
1882 		case BC_LEX_WHITESPACE:
1883 		case BC_LEX_RPAREN:
1884 		case BC_LEX_LBRACKET:
1885 		case BC_LEX_COMMA:
1886 		case BC_LEX_RBRACKET:
1887 		case BC_LEX_LBRACE:
1888 		case BC_LEX_KW_AUTO:
1889 		case BC_LEX_KW_DEFINE:
1890 #if DC_ENABLED
1891 		case BC_LEX_EXTENDED_REGISTERS:
1892 		case BC_LEX_EQ_NO_REG:
1893 		case BC_LEX_COLON:
1894 		case BC_LEX_EXECUTE:
1895 		case BC_LEX_PRINT_STACK:
1896 		case BC_LEX_CLEAR_STACK:
1897 		case BC_LEX_REG_STACK_LEVEL:
1898 		case BC_LEX_STACK_LEVEL:
1899 		case BC_LEX_DUPLICATE:
1900 		case BC_LEX_SWAP:
1901 		case BC_LEX_POP:
1902 		case BC_LEX_STORE_IBASE:
1903 		case BC_LEX_STORE_OBASE:
1904 		case BC_LEX_STORE_SCALE:
1905 #if BC_ENABLE_EXTRA_MATH
1906 		case BC_LEX_STORE_SEED:
1907 #endif // BC_ENABLE_EXTRA_MATH
1908 		case BC_LEX_LOAD:
1909 		case BC_LEX_LOAD_POP:
1910 		case BC_LEX_STORE_PUSH:
1911 		case BC_LEX_PRINT_POP:
1912 		case BC_LEX_NQUIT:
1913 		case BC_LEX_EXEC_STACK_LENGTH:
1914 		case BC_LEX_SCALE_FACTOR:
1915 		case BC_LEX_ARRAY_LENGTH:
1916 #endif // DC_ENABLED
1917 		{
1918 			bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1919 		}
1920 	}
1921 
1922 	// If the flags did not change, we expect a delimiter.
1923 	if (len == p->flags.len && flags == BC_PARSE_TOP_FLAG(p))
1924 	{
1925 		if (BC_ERR(!bc_parse_isDelimiter(p)))
1926 		{
1927 			bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1928 		}
1929 	}
1930 
1931 	// Make sure semicolons are eaten.
1932 	while (p->l.t == BC_LEX_SCOLON || p->l.t == BC_LEX_NLINE)
1933 	{
1934 		bc_lex_next(&p->l);
1935 	}
1936 
1937 	// POSIX's grammar does not allow a function definition after a semicolon
1938 	// without a newline, so check specifically for that case and error if
1939 	// the POSIX standard flag is set.
1940 	if (p->l.last == BC_LEX_SCOLON && p->l.t == BC_LEX_KW_DEFINE && BC_IS_POSIX)
1941 	{
1942 		bc_parse_err(p, BC_ERR_POSIX_FUNC_AFTER_SEMICOLON);
1943 	}
1944 }
1945 
1946 void
bc_parse_parse(BcParse * p)1947 bc_parse_parse(BcParse* p)
1948 {
1949 	assert(p);
1950 
1951 	BC_SETJMP_LOCKED(vm, exit);
1952 
1953 	// We should not let an EOF get here unless some partial parse was not
1954 	// completed, in which case, it's the user's fault.
1955 	if (BC_ERR(p->l.t == BC_LEX_EOF)) bc_parse_err(p, BC_ERR_PARSE_EOF);
1956 
1957 	// Functions need special parsing.
1958 	else if (p->l.t == BC_LEX_KW_DEFINE)
1959 	{
1960 		if (BC_ERR(BC_PARSE_NO_EXEC(p)))
1961 		{
1962 			bc_parse_endif(p);
1963 			if (BC_ERR(BC_PARSE_NO_EXEC(p)))
1964 			{
1965 				bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1966 			}
1967 		}
1968 		bc_parse_func(p);
1969 	}
1970 
1971 	// Otherwise, parse a normal statement.
1972 	else bc_parse_stmt(p);
1973 
1974 exit:
1975 
1976 	// We need to reset on error.
1977 	if (BC_ERR(((vm->status && vm->status != BC_STATUS_QUIT) || vm->sig != 0)))
1978 	{
1979 		bc_parse_reset(p);
1980 	}
1981 
1982 	BC_LONGJMP_CONT(vm);
1983 	BC_SIG_MAYLOCK;
1984 }
1985 
1986 /**
1987  * Parse an expression. This is the actual implementation of the Shunting-Yard
1988  * Algorithm.
1989  * @param p      The parser.
1990  * @param flags  The flags for what is valid in the expression.
1991  * @param next   A set of tokens for what is valid *after* the expression.
1992  * @return       A parse status. In some places, an empty expression is an
1993  *               error, and sometimes, it is required. This allows this function
1994  *               to tell the caller if the expression was empty and let the
1995  *               caller handle it.
1996  */
1997 static BcParseStatus
bc_parse_expr_err(BcParse * p,uint8_t flags,BcParseNext next)1998 bc_parse_expr_err(BcParse* p, uint8_t flags, BcParseNext next)
1999 {
2000 	BcInst prev = BC_INST_PRINT;
2001 	uchar inst = BC_INST_INVALID;
2002 	BcLexType top, t;
2003 	size_t nexprs, ops_bgn;
2004 	uint32_t i, nparens, nrelops;
2005 	bool pfirst, rprn, array_last, done, get_token, assign;
2006 	bool bin_last, incdec, can_assign;
2007 
2008 	// One of these *must* be true.
2009 	assert(!(flags & BC_PARSE_PRINT) || !(flags & BC_PARSE_NEEDVAL));
2010 
2011 	// These are set very carefully. In fact, controlling the values of these
2012 	// locals is the biggest part of making this work. ops_bgn especially is
2013 	// important because it marks where the operator stack begins for *this*
2014 	// invocation of this function. That's because bc_parse_expr_err() is
2015 	// recursive (the Shunting-Yard Algorithm is most easily expressed
2016 	// recursively when parsing subexpressions), and each invocation needs to
2017 	// know where to stop.
2018 	//
2019 	// - nparens is the number of left parens without matches.
2020 	// - nrelops is the number of relational operators that appear in the expr.
2021 	// - nexprs is the number of unused expressions.
2022 	// - rprn is a right paren encountered last.
2023 	// - array_last is an array item encountered last.
2024 	// - done means the expression has been fully parsed.
2025 	// - get_token is true when a token is needed at the end of an iteration.
2026 	// - assign is true when an assignment statement was parsed last.
2027 	// - incdec is true when the previous operator was an inc or dec operator.
2028 	// - can_assign is true when an assignemnt is valid.
2029 	// - bin_last is true when the previous instruction was a binary operator.
2030 	t = p->l.t;
2031 	pfirst = (p->l.t == BC_LEX_LPAREN);
2032 	nparens = nrelops = 0;
2033 	nexprs = 0;
2034 	ops_bgn = p->ops.len;
2035 	rprn = array_last = done = get_token = assign = incdec = can_assign = false;
2036 	bin_last = true;
2037 
2038 	// We want to eat newlines if newlines are not a valid ending token.
2039 	// This is for spacing in things like for loop headers.
2040 	if (!(flags & BC_PARSE_NOREAD))
2041 	{
2042 		while ((t = p->l.t) == BC_LEX_NLINE)
2043 		{
2044 			bc_lex_next(&p->l);
2045 		}
2046 	}
2047 
2048 	// This is the Shunting-Yard algorithm loop.
2049 	for (; !done && BC_PARSE_EXPR(t); t = p->l.t)
2050 	{
2051 		// Make sure an array expression is not mixed with any others. However,
2052 		// a right parenthesis may end the expression, so we will need to take
2053 		// care of that right there.
2054 		if (BC_ERR(array_last && t != BC_LEX_RPAREN))
2055 		{
2056 			bc_parse_err(p, BC_ERR_PARSE_EXPR);
2057 		}
2058 
2059 		switch (t)
2060 		{
2061 			case BC_LEX_OP_INC:
2062 			case BC_LEX_OP_DEC:
2063 			{
2064 				// These operators can only be used with items that can be
2065 				// assigned to.
2066 				if (BC_ERR(incdec)) bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
2067 
2068 				bc_parse_incdec(p, &prev, &can_assign, &nexprs, flags);
2069 
2070 				rprn = get_token = bin_last = false;
2071 				incdec = true;
2072 				flags &= ~(BC_PARSE_ARRAY);
2073 
2074 				break;
2075 			}
2076 
2077 #if BC_ENABLE_EXTRA_MATH
2078 			case BC_LEX_OP_TRUNC:
2079 			{
2080 				// The previous token must have been a leaf expression, or the
2081 				// operator is in the wrong place.
2082 				if (BC_ERR(!BC_PARSE_LEAF(prev, bin_last, rprn)))
2083 				{
2084 					bc_parse_err(p, BC_ERR_PARSE_TOKEN);
2085 				}
2086 
2087 				// I can just add the instruction because
2088 				// negative will already be taken care of.
2089 				bc_parse_push(p, BC_INST_TRUNC);
2090 
2091 				rprn = can_assign = incdec = false;
2092 				get_token = true;
2093 				flags &= ~(BC_PARSE_ARRAY);
2094 
2095 				break;
2096 			}
2097 #endif // BC_ENABLE_EXTRA_MATH
2098 
2099 			case BC_LEX_OP_MINUS:
2100 			{
2101 				bc_parse_minus(p, &prev, ops_bgn, rprn, bin_last, &nexprs);
2102 
2103 				rprn = get_token = can_assign = false;
2104 
2105 				// This is true if it was a binary operator last.
2106 				bin_last = (prev == BC_INST_MINUS);
2107 				if (bin_last) incdec = false;
2108 
2109 				flags &= ~(BC_PARSE_ARRAY);
2110 
2111 				break;
2112 			}
2113 
2114 			// All of this group, including the fallthrough, is to parse binary
2115 			// operators.
2116 			case BC_LEX_OP_ASSIGN_POWER:
2117 			case BC_LEX_OP_ASSIGN_MULTIPLY:
2118 			case BC_LEX_OP_ASSIGN_DIVIDE:
2119 			case BC_LEX_OP_ASSIGN_MODULUS:
2120 			case BC_LEX_OP_ASSIGN_PLUS:
2121 			case BC_LEX_OP_ASSIGN_MINUS:
2122 #if BC_ENABLE_EXTRA_MATH
2123 			case BC_LEX_OP_ASSIGN_PLACES:
2124 			case BC_LEX_OP_ASSIGN_LSHIFT:
2125 			case BC_LEX_OP_ASSIGN_RSHIFT:
2126 #endif // BC_ENABLE_EXTRA_MATH
2127 			case BC_LEX_OP_ASSIGN:
2128 			{
2129 				// We need to make sure the assignment is valid.
2130 				if (!BC_PARSE_INST_VAR(prev))
2131 				{
2132 					bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
2133 				}
2134 
2135 				// Fallthrough.
2136 				BC_FALLTHROUGH
2137 			}
2138 
2139 			case BC_LEX_OP_POWER:
2140 			case BC_LEX_OP_MULTIPLY:
2141 			case BC_LEX_OP_DIVIDE:
2142 			case BC_LEX_OP_MODULUS:
2143 			case BC_LEX_OP_PLUS:
2144 #if BC_ENABLE_EXTRA_MATH
2145 			case BC_LEX_OP_PLACES:
2146 			case BC_LEX_OP_LSHIFT:
2147 			case BC_LEX_OP_RSHIFT:
2148 #endif // BC_ENABLE_EXTRA_MATH
2149 			case BC_LEX_OP_REL_EQ:
2150 			case BC_LEX_OP_REL_LE:
2151 			case BC_LEX_OP_REL_GE:
2152 			case BC_LEX_OP_REL_NE:
2153 			case BC_LEX_OP_REL_LT:
2154 			case BC_LEX_OP_REL_GT:
2155 			case BC_LEX_OP_BOOL_NOT:
2156 			case BC_LEX_OP_BOOL_OR:
2157 			case BC_LEX_OP_BOOL_AND:
2158 			{
2159 				// This is true if the operator if the token is a prefix
2160 				// operator. This is only for boolean not.
2161 				if (BC_PARSE_OP_PREFIX(t))
2162 				{
2163 					// Prefix operators are only allowed after binary operators
2164 					// or prefix operators.
2165 					if (BC_ERR(!bin_last && !BC_PARSE_OP_PREFIX(p->l.last)))
2166 					{
2167 						bc_parse_err(p, BC_ERR_PARSE_EXPR);
2168 					}
2169 				}
2170 				// If we execute the else, that means we have a binary operator.
2171 				// If the previous operator was a prefix or a binary operator,
2172 				// then a binary operator is not allowed.
2173 				else if (BC_ERR(BC_PARSE_PREV_PREFIX(prev) || bin_last))
2174 				{
2175 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2176 				}
2177 
2178 				nrelops += (t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT);
2179 				prev = BC_PARSE_TOKEN_INST(t);
2180 
2181 				bc_parse_operator(p, t, ops_bgn, &nexprs);
2182 
2183 				rprn = incdec = can_assign = false;
2184 				get_token = true;
2185 				bin_last = !BC_PARSE_OP_PREFIX(t);
2186 				flags &= ~(BC_PARSE_ARRAY);
2187 
2188 				break;
2189 			}
2190 
2191 			case BC_LEX_LPAREN:
2192 			{
2193 				// A left paren is *not* allowed right after a leaf expr.
2194 				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2195 				{
2196 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2197 				}
2198 
2199 				nparens += 1;
2200 				rprn = incdec = can_assign = false;
2201 				get_token = true;
2202 
2203 				// Push the paren onto the operator stack.
2204 				bc_vec_push(&p->ops, &t);
2205 
2206 				break;
2207 			}
2208 
2209 			case BC_LEX_RPAREN:
2210 			{
2211 				// This needs to be a status. The error is handled in
2212 				// bc_parse_expr_status().
2213 				if (BC_ERR(p->l.last == BC_LEX_LPAREN))
2214 				{
2215 					return BC_PARSE_STATUS_EMPTY_EXPR;
2216 				}
2217 
2218 				// The right paren must not come after a prefix or binary
2219 				// operator.
2220 				if (BC_ERR(bin_last || BC_PARSE_PREV_PREFIX(prev)))
2221 				{
2222 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2223 				}
2224 
2225 				// If there are no parens left, we are done, but we need another
2226 				// token.
2227 				if (!nparens)
2228 				{
2229 					done = true;
2230 					get_token = false;
2231 					break;
2232 				}
2233 
2234 				// Now that we know the right paren has not ended the
2235 				// expression, make sure an array expression is not mixed with
2236 				// any others.
2237 				if (BC_ERR(array_last))
2238 				{
2239 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2240 				}
2241 
2242 				nparens -= 1;
2243 				rprn = true;
2244 				get_token = bin_last = incdec = false;
2245 
2246 				bc_parse_rightParen(p, &nexprs);
2247 
2248 				break;
2249 			}
2250 
2251 			case BC_LEX_STR:
2252 			{
2253 				// POSIX only allows strings alone.
2254 				if (BC_IS_POSIX) bc_parse_err(p, BC_ERR_POSIX_EXPR_STRING);
2255 
2256 				// A string is a leaf and cannot come right after a leaf.
2257 				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2258 				{
2259 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2260 				}
2261 
2262 				bc_parse_addString(p);
2263 
2264 				get_token = true;
2265 				bin_last = rprn = false;
2266 				nexprs += 1;
2267 
2268 				break;
2269 			}
2270 
2271 			case BC_LEX_NAME:
2272 			{
2273 				// A name is a leaf and cannot come right after a leaf.
2274 				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2275 				{
2276 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2277 				}
2278 
2279 				get_token = bin_last = false;
2280 
2281 				bc_parse_name(p, &prev, &can_assign, flags & ~BC_PARSE_NOCALL);
2282 
2283 				rprn = (prev == BC_INST_CALL);
2284 				array_last = (prev == BC_INST_ARRAY);
2285 				nexprs += 1;
2286 				flags &= ~(BC_PARSE_ARRAY);
2287 
2288 				break;
2289 			}
2290 
2291 			case BC_LEX_NUMBER:
2292 			{
2293 				// A number is a leaf and cannot come right after a leaf.
2294 				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2295 				{
2296 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2297 				}
2298 
2299 				// The number instruction is pushed in here.
2300 				bc_parse_number(p);
2301 
2302 				nexprs += 1;
2303 				prev = BC_INST_NUM;
2304 				get_token = true;
2305 				rprn = bin_last = can_assign = false;
2306 				flags &= ~(BC_PARSE_ARRAY);
2307 
2308 				break;
2309 			}
2310 
2311 			case BC_LEX_KW_IBASE:
2312 			case BC_LEX_KW_LAST:
2313 			case BC_LEX_KW_OBASE:
2314 #if BC_ENABLE_EXTRA_MATH
2315 			case BC_LEX_KW_SEED:
2316 #endif // BC_ENABLE_EXTRA_MATH
2317 			{
2318 				// All of these are leaves and cannot come right after a leaf.
2319 				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2320 				{
2321 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2322 				}
2323 
2324 				prev = t - BC_LEX_KW_LAST + BC_INST_LAST;
2325 				bc_parse_push(p, prev);
2326 
2327 				get_token = can_assign = true;
2328 				rprn = bin_last = false;
2329 				nexprs += 1;
2330 				flags &= ~(BC_PARSE_ARRAY);
2331 
2332 				break;
2333 			}
2334 
2335 			case BC_LEX_KW_LENGTH:
2336 			case BC_LEX_KW_SQRT:
2337 			case BC_LEX_KW_ABS:
2338 			case BC_LEX_KW_IS_NUMBER:
2339 			case BC_LEX_KW_IS_STRING:
2340 #if BC_ENABLE_EXTRA_MATH
2341 			case BC_LEX_KW_IRAND:
2342 #endif // BC_ENABLE_EXTRA_MATH
2343 			case BC_LEX_KW_ASCIIFY:
2344 			{
2345 				// All of these are leaves and cannot come right after a leaf.
2346 				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2347 				{
2348 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2349 				}
2350 
2351 				bc_parse_builtin(p, t, flags, &prev);
2352 
2353 				rprn = get_token = bin_last = incdec = can_assign = false;
2354 				nexprs += 1;
2355 				flags &= ~(BC_PARSE_ARRAY);
2356 
2357 				break;
2358 			}
2359 
2360 			case BC_LEX_KW_READ:
2361 #if BC_ENABLE_EXTRA_MATH
2362 			case BC_LEX_KW_RAND:
2363 #endif // BC_ENABLE_EXTRA_MATH
2364 			case BC_LEX_KW_MAXIBASE:
2365 			case BC_LEX_KW_MAXOBASE:
2366 			case BC_LEX_KW_MAXSCALE:
2367 #if BC_ENABLE_EXTRA_MATH
2368 			case BC_LEX_KW_MAXRAND:
2369 #endif // BC_ENABLE_EXTRA_MATH
2370 			case BC_LEX_KW_LINE_LENGTH:
2371 			case BC_LEX_KW_GLOBAL_STACKS:
2372 			case BC_LEX_KW_LEADING_ZERO:
2373 			{
2374 				// All of these are leaves and cannot come right after a leaf.
2375 				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2376 				{
2377 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2378 				}
2379 
2380 				// Error if we have read and it's not allowed.
2381 				else if (t == BC_LEX_KW_READ && BC_ERR(flags & BC_PARSE_NOREAD))
2382 				{
2383 					bc_parse_err(p, BC_ERR_EXEC_REC_READ);
2384 				}
2385 
2386 				prev = t - BC_LEX_KW_READ + BC_INST_READ;
2387 				bc_parse_noArgBuiltin(p, prev);
2388 
2389 				rprn = get_token = bin_last = incdec = can_assign = false;
2390 				nexprs += 1;
2391 				flags &= ~(BC_PARSE_ARRAY);
2392 
2393 				break;
2394 			}
2395 
2396 			case BC_LEX_KW_SCALE:
2397 			{
2398 				// This is a leaf and cannot come right after a leaf.
2399 				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2400 				{
2401 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2402 				}
2403 
2404 				// Scale needs special work because it can be a variable *or* a
2405 				// function.
2406 				bc_parse_scale(p, &prev, &can_assign, flags);
2407 
2408 				rprn = get_token = bin_last = false;
2409 				nexprs += 1;
2410 				flags &= ~(BC_PARSE_ARRAY);
2411 
2412 				break;
2413 			}
2414 
2415 			case BC_LEX_KW_MODEXP:
2416 			case BC_LEX_KW_DIVMOD:
2417 			{
2418 				// This is a leaf and cannot come right after a leaf.
2419 				if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2420 				{
2421 					bc_parse_err(p, BC_ERR_PARSE_EXPR);
2422 				}
2423 
2424 				bc_parse_builtin3(p, t, flags, &prev);
2425 
2426 				rprn = get_token = bin_last = incdec = can_assign = false;
2427 				nexprs += 1;
2428 				flags &= ~(BC_PARSE_ARRAY);
2429 
2430 				break;
2431 			}
2432 
2433 			case BC_LEX_EOF:
2434 			case BC_LEX_INVALID:
2435 			case BC_LEX_NEG:
2436 			case BC_LEX_NLINE:
2437 			case BC_LEX_WHITESPACE:
2438 			case BC_LEX_LBRACKET:
2439 			case BC_LEX_COMMA:
2440 			case BC_LEX_RBRACKET:
2441 			case BC_LEX_LBRACE:
2442 			case BC_LEX_SCOLON:
2443 			case BC_LEX_RBRACE:
2444 			case BC_LEX_KW_AUTO:
2445 			case BC_LEX_KW_BREAK:
2446 			case BC_LEX_KW_CONTINUE:
2447 			case BC_LEX_KW_DEFINE:
2448 			case BC_LEX_KW_FOR:
2449 			case BC_LEX_KW_IF:
2450 			case BC_LEX_KW_LIMITS:
2451 			case BC_LEX_KW_RETURN:
2452 			case BC_LEX_KW_WHILE:
2453 			case BC_LEX_KW_HALT:
2454 			case BC_LEX_KW_PRINT:
2455 			case BC_LEX_KW_QUIT:
2456 			case BC_LEX_KW_STREAM:
2457 			case BC_LEX_KW_ELSE:
2458 #if DC_ENABLED
2459 			case BC_LEX_EXTENDED_REGISTERS:
2460 			case BC_LEX_EQ_NO_REG:
2461 			case BC_LEX_COLON:
2462 			case BC_LEX_EXECUTE:
2463 			case BC_LEX_PRINT_STACK:
2464 			case BC_LEX_CLEAR_STACK:
2465 			case BC_LEX_REG_STACK_LEVEL:
2466 			case BC_LEX_STACK_LEVEL:
2467 			case BC_LEX_DUPLICATE:
2468 			case BC_LEX_SWAP:
2469 			case BC_LEX_POP:
2470 			case BC_LEX_STORE_IBASE:
2471 			case BC_LEX_STORE_OBASE:
2472 			case BC_LEX_STORE_SCALE:
2473 #if BC_ENABLE_EXTRA_MATH
2474 			case BC_LEX_STORE_SEED:
2475 #endif // BC_ENABLE_EXTRA_MATH
2476 			case BC_LEX_LOAD:
2477 			case BC_LEX_LOAD_POP:
2478 			case BC_LEX_STORE_PUSH:
2479 			case BC_LEX_PRINT_POP:
2480 			case BC_LEX_NQUIT:
2481 			case BC_LEX_EXEC_STACK_LENGTH:
2482 			case BC_LEX_SCALE_FACTOR:
2483 			case BC_LEX_ARRAY_LENGTH:
2484 #endif // DC_ENABLED
2485 			{
2486 #if BC_DEBUG
2487 				// We should never get here, even in debug builds.
2488 				bc_parse_err(p, BC_ERR_PARSE_TOKEN);
2489 				break;
2490 #endif // BC_DEBUG
2491 			}
2492 		}
2493 
2494 		if (get_token) bc_lex_next(&p->l);
2495 	}
2496 
2497 	// Now that we have parsed the expression, we need to empty the operator
2498 	// stack.
2499 	while (p->ops.len > ops_bgn)
2500 	{
2501 		top = BC_PARSE_TOP_OP(p);
2502 		assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
2503 
2504 		// There should not be *any* parens on the stack anymore.
2505 		if (BC_ERR(top == BC_LEX_LPAREN || top == BC_LEX_RPAREN))
2506 		{
2507 			bc_parse_err(p, BC_ERR_PARSE_EXPR);
2508 		}
2509 
2510 		bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
2511 
2512 		// Adjust the number of unused expressions.
2513 		nexprs -= !BC_PARSE_OP_PREFIX(top);
2514 		bc_vec_pop(&p->ops);
2515 
2516 		incdec = false;
2517 	}
2518 
2519 	// There must be only one expression at the top.
2520 	if (BC_ERR(nexprs != 1)) bc_parse_err(p, BC_ERR_PARSE_EXPR);
2521 
2522 	// Check that the next token is correct.
2523 	for (i = 0; i < next.len && t != next.tokens[i]; ++i)
2524 	{
2525 		continue;
2526 	}
2527 	if (BC_ERR(i == next.len && !bc_parse_isDelimiter(p)))
2528 	{
2529 		bc_parse_err(p, BC_ERR_PARSE_EXPR);
2530 	}
2531 
2532 	// Check that POSIX would be happy with the number of relational operators.
2533 	if (!(flags & BC_PARSE_REL) && nrelops)
2534 	{
2535 		bc_parse_err(p, BC_ERR_POSIX_REL_POS);
2536 	}
2537 	else if ((flags & BC_PARSE_REL) && nrelops > 1)
2538 	{
2539 		bc_parse_err(p, BC_ERR_POSIX_MULTIREL);
2540 	}
2541 
2542 	// If this is true, then we might be in a situation where we don't print.
2543 	// We would want to have the increment/decrement operator not make an extra
2544 	// copy if it's not necessary.
2545 	if (!(flags & BC_PARSE_NEEDVAL) && !pfirst)
2546 	{
2547 		// We have the easy case if the last operator was an assignment
2548 		// operator.
2549 		if (assign)
2550 		{
2551 			inst = *((uchar*) bc_vec_top(&p->func->code));
2552 			inst += (BC_INST_ASSIGN_POWER_NO_VAL - BC_INST_ASSIGN_POWER);
2553 			incdec = false;
2554 		}
2555 		// If we have an inc/dec operator and we are *not* printing, implement
2556 		// the optimization to get rid of the extra copy.
2557 		else if (incdec && !(flags & BC_PARSE_PRINT))
2558 		{
2559 			inst = *((uchar*) bc_vec_top(&p->func->code));
2560 			incdec = (inst <= BC_INST_DEC);
2561 			inst = BC_INST_ASSIGN_PLUS_NO_VAL +
2562 			       (inst != BC_INST_INC && inst != BC_INST_ASSIGN_PLUS);
2563 		}
2564 
2565 		// This condition allows us to change the previous assignment
2566 		// instruction (which does a copy) for a NO_VAL version, which does not.
2567 		// This condition is set if either of the above if statements ends up
2568 		// being true.
2569 		if (inst >= BC_INST_ASSIGN_POWER_NO_VAL &&
2570 		    inst <= BC_INST_ASSIGN_NO_VAL)
2571 		{
2572 			// Pop the previous assignment instruction and push a new one.
2573 			// Inc/dec needs the extra instruction because it is now a binary
2574 			// operator and needs a second operand.
2575 			bc_vec_pop(&p->func->code);
2576 			if (incdec) bc_parse_push(p, BC_INST_ONE);
2577 			bc_parse_push(p, inst);
2578 		}
2579 	}
2580 
2581 	// If we might have to print...
2582 	if ((flags & BC_PARSE_PRINT))
2583 	{
2584 		// With a paren first or the last operator not being an assignment, we
2585 		// *do* want to print.
2586 		if (pfirst || !assign) bc_parse_push(p, BC_INST_PRINT);
2587 	}
2588 	// We need to make sure to push a pop instruction for assignment statements
2589 	// that will not print. The print will pop, but without it, we need to pop.
2590 	else if (!(flags & BC_PARSE_NEEDVAL) &&
2591 	         (inst < BC_INST_ASSIGN_POWER_NO_VAL ||
2592 	          inst > BC_INST_ASSIGN_NO_VAL))
2593 	{
2594 		bc_parse_push(p, BC_INST_POP);
2595 	}
2596 
2597 	// We want to eat newlines if newlines are not a valid ending token.
2598 	// This is for spacing in things like for loop headers.
2599 	//
2600 	// Yes, this is one case where I reuse a variable for a different purpose;
2601 	// in this case, incdec being true now means that newlines are not valid.
2602 	for (incdec = true, i = 0; i < next.len && incdec; ++i)
2603 	{
2604 		incdec = (next.tokens[i] != BC_LEX_NLINE);
2605 	}
2606 	if (incdec)
2607 	{
2608 		while (p->l.t == BC_LEX_NLINE)
2609 		{
2610 			bc_lex_next(&p->l);
2611 		}
2612 	}
2613 
2614 	return BC_PARSE_STATUS_SUCCESS;
2615 }
2616 
2617 /**
2618  * Parses an expression with bc_parse_expr_err(), but throws an error if it gets
2619  * an empty expression.
2620  * @param p      The parser.
2621  * @param flags  The flags for what is valid in the expression.
2622  * @param next   A set of tokens for what is valid *after* the expression.
2623  */
2624 static void
bc_parse_expr_status(BcParse * p,uint8_t flags,BcParseNext next)2625 bc_parse_expr_status(BcParse* p, uint8_t flags, BcParseNext next)
2626 {
2627 	BcParseStatus s = bc_parse_expr_err(p, flags, next);
2628 
2629 	if (BC_ERR(s == BC_PARSE_STATUS_EMPTY_EXPR))
2630 	{
2631 		bc_parse_err(p, BC_ERR_PARSE_EMPTY_EXPR);
2632 	}
2633 }
2634 
2635 void
bc_parse_expr(BcParse * p,uint8_t flags)2636 bc_parse_expr(BcParse* p, uint8_t flags)
2637 {
2638 	assert(p);
2639 	bc_parse_expr_status(p, flags, bc_parse_next_read);
2640 }
2641 #endif // BC_ENABLED
2642