2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
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.
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.
30 * *****************************************************************************
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.
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.
59 bc_parse_else(BcParse* p);
62 bc_parse_stmt(BcParse* p);
65 bc_parse_expr_err(BcParse* p, uint8_t flags, BcParseNext next);
68 bc_parse_expr_status(BcParse* p, uint8_t flags, BcParseNext next);
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.
77 bc_parse_inst_isLeaf(BcInst t)
79 return (t >= BC_INST_NUM && t <= BC_INST_LEADING_ZERO) ||
80 #if BC_ENABLE_EXTRA_MATH
82 #endif // BC_ENABLE_EXTRA_MATH
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.
94 bc_parse_isDelimiter(const BcParse* p)
99 // If it's an obvious delimiter, say so.
100 if (BC_PARSE_DELIMITER(t)) return true;
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)
110 uint16_t *fptr = NULL, flags = BC_PARSE_FLAG_ELSE;
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)
115 fptr = bc_vec_item_rev(&p->flags, i);
118 // If we need a brace and don't have one, then we don't have a
120 if ((flags & BC_PARSE_FLAG_BRACE) && p->l.last != BC_LEX_RBRACE)
126 // Oh, and we had also better have an if statement somewhere.
127 good = ((flags & BC_PARSE_FLAG_IF) != 0);
129 else if (t == BC_LEX_RBRACE)
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)
136 uint16_t* fptr = bc_vec_item_rev(&p->flags, i);
137 good = (((*fptr) & BC_PARSE_FLAG_BRACE) != 0);
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.
152 bc_parse_TopFunc(const BcParse* p)
154 bool good = p->flags.len == 2;
156 uint16_t val = BC_PARSE_FLAG_BRACE | BC_PARSE_FLAG_FUNC_INNER;
157 val |= BC_PARSE_FLAG_FUNC;
159 return good && BC_PARSE_TOP_FLAG(p) == val;
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.
168 bc_parse_setLabel(BcParse* p)
170 BcFunc* func = p->func;
171 BcInstPtr* ip = bc_vec_top(&p->exits);
174 assert(func == bc_vec_item(&p->prog->fns, p->fidx));
176 // Set the preallocated label to the correct index.
177 label = bc_vec_item(&func->labels, ip->idx);
178 *label = func->code.len;
180 // Now, we don't need the exit label; it is done.
181 bc_vec_pop(&p->exits);
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.
192 bc_parse_createLabel(BcParse* p, size_t idx)
194 bc_vec_push(&p->func->labels, &idx);
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.
204 bc_parse_createCondLabel(BcParse* p, size_t idx)
206 bc_parse_createLabel(p, p->func->code.len);
207 bc_vec_push(&p->conds, &idx);
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.
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.
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.
225 bc_parse_createExitLabel(BcParse* p, size_t idx, bool loop)
229 assert(p->func == bc_vec_item(&p->prog->fns, p->fidx));
235 bc_vec_push(&p->exits, &ip);
236 bc_parse_createLabel(p, SIZE_MAX);
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
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.
252 bc_parse_operator(BcParse* p, BcLexType type, size_t start, size_t* nexprs)
255 uchar l, r = BC_PARSE_OP_PREC(type);
256 uchar left = BC_PARSE_OP_LEFT(type);
258 // While we haven't hit the stop point yet...
259 while (p->ops.len > start)
261 // Get the top operator.
262 t = BC_PARSE_TOP_OP(p);
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;
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.
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;
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));
284 *nexprs -= !BC_PARSE_OP_PREFIX(t);
287 bc_vec_push(&p->ops, &type);
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.
299 bc_parse_rightParen(BcParse* p, size_t* nexprs)
303 // Consume operators until a left paren.
304 while ((top = BC_PARSE_TOP_OP(p)) != BC_LEX_LPAREN)
306 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
308 *nexprs -= !BC_PARSE_OP_PREFIX(top);
311 // We need to pop the left paren as well.
314 // Oh, and we also want the next token.
319 * Parses function arguments.
320 * @param p The parser.
321 * @param flags Flags restricting what kind of expressions the arguments can
325 bc_parse_args(BcParse* p, uint8_t flags)
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);
337 // Count the arguments and parse them.
338 for (nargs = 0; p->l.t != BC_LEX_RPAREN; ++nargs)
340 bc_parse_expr_status(p, flags, bc_parse_next_arg);
342 comma = (p->l.t == BC_LEX_COMMA);
343 if (comma) bc_lex_next(&p->l);
346 // An ending comma is FAIL.
347 if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
349 // Now do the call with the number of arguments.
350 bc_parse_push(p, BC_INST_CALL);
351 bc_parse_pushIndex(p, nargs);
355 * Parses a function call.
356 * @param p The parser.
357 * @param flags Flags restricting what kind of expressions the arguments can
361 bc_parse_call(BcParse* p, const char* name, uint8_t flags)
365 bc_parse_args(p, flags);
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);
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);
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)
379 idx = bc_program_insertFunc(p->prog, name);
381 assert(idx != BC_VEC_INVALID_IDX);
383 // Make sure that this pointer was not invalidated.
384 p->func = bc_vec_item(&p->prog->fns, p->fidx);
386 // The function exists, so set the right function index.
387 else idx = ((BcId*) bc_vec_item(&p->prog->fn_map, idx))->idx;
389 bc_parse_pushIndex(p, idx);
391 // Make sure to get the next token.
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,
402 * @param flags Flags restricting what kind of expression the name can be.
405 bc_parse_name(BcParse* p, BcInst* type, bool* can_assign, uint8_t flags)
409 BC_SIG_ASSERT_LOCKED;
411 // We want a copy of the name since the lexer might overwrite its copy.
412 name = bc_vm_strdup(p->l.str.v);
414 BC_SETJMP_LOCKED(vm, err);
416 // We need the next token to see if it's just a variable or something more.
419 // Array element or array.
420 if (p->l.t == BC_LEX_LBRACKET)
424 // Array only. This has to be a function parameter.
425 if (p->l.t == BC_LEX_RBRACKET)
427 // Error if arrays are not allowed.
428 if (BC_ERR(!(flags & BC_PARSE_ARRAY)))
430 bc_parse_err(p, BC_ERR_PARSE_EXPR);
433 *type = BC_INST_ARRAY;
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)) |
443 bc_parse_expr_status(p, flags2, bc_parse_next_elem);
445 // The next token *must* be a right bracket.
446 if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
448 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
451 *type = BC_INST_ARRAY_ELEM;
455 // Make sure to get the next token.
458 // Push the instruction and the name of the identifier.
459 bc_parse_push(p, *type);
460 bc_parse_pushName(p, name, false);
462 else if (p->l.t == BC_LEX_LPAREN)
464 // We are parsing a function call; error if not allowed.
465 if (BC_ERR(flags & BC_PARSE_NOCALL))
467 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
470 *type = BC_INST_CALL;
473 bc_parse_call(p, name, flags);
480 bc_parse_push(p, BC_INST_VAR);
481 bc_parse_pushName(p, name, true);
485 // Need to make sure to unallocate the name.
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.
498 bc_parse_noArgBuiltin(BcParse* p, BcInst inst)
500 // Must have a left paren.
502 if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
504 // Must have a right paren.
506 if ((p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
508 bc_parse_push(p, inst);
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.
522 bc_parse_builtin(BcParse* p, BcLexType type, uint8_t flags, BcInst* prev)
524 // Must have a left paren.
526 if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
530 // Change the flags as needed for parsing the argument.
531 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
532 flags |= BC_PARSE_NEEDVAL;
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)
537 flags |= BC_PARSE_ARRAY;
540 // Otherwise, we need to clear it because it could be set.
541 else flags &= ~(BC_PARSE_ARRAY);
543 bc_parse_expr_status(p, flags, bc_parse_next_rel);
545 // Must have a right paren.
546 if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
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);
556 * Parses a builtin function that takes 3 arguments. This includes modexp() and
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.
564 bc_parse_builtin3(BcParse* p, BcLexType type, uint8_t flags, BcInst* prev)
566 assert(type == BC_LEX_KW_MODEXP || type == BC_LEX_KW_DIVMOD);
568 // Must have a left paren.
570 if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
574 // Change the flags as needed for parsing the argument.
575 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
576 flags |= BC_PARSE_NEEDVAL;
578 bc_parse_expr_status(p, flags, bc_parse_next_builtin);
580 // Must have a comma.
581 if (BC_ERR(p->l.t != BC_LEX_COMMA)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
585 bc_parse_expr_status(p, flags, bc_parse_next_builtin);
587 // Must have a comma.
588 if (BC_ERR(p->l.t != BC_LEX_COMMA)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
592 // If it is a divmod, parse an array name. Otherwise, just parse another
594 if (type == BC_LEX_KW_DIVMOD)
597 if (BC_ERR(p->l.t != BC_LEX_NAME)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
599 // This is safe because the next token should not overwrite the name.
602 // Must have a left bracket.
603 if (BC_ERR(p->l.t != BC_LEX_LBRACKET))
605 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
608 // This is safe because the next token should not overwrite the name.
611 // Must have a right bracket.
612 if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
614 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
617 // This is safe because the next token should not overwrite the name.
620 else bc_parse_expr_status(p, flags, bc_parse_next_rel);
622 // Must have a right paren.
623 if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
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);
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)
633 // The zeroth element.
634 bc_parse_push(p, BC_INST_ZERO);
635 bc_parse_push(p, BC_INST_ARRAY_ELEM);
638 bc_parse_pushName(p, p->l.str.v, false);
640 // Swap them and assign. After this, the top item on the stack should
642 bc_parse_push(p, BC_INST_SWAP);
643 bc_parse_push(p, BC_INST_ASSIGN_NO_VAL);
650 * Parses the scale keyword. This is special because scale can be a value or a
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
656 * @param flags The expression parsing flags for parsing a scale() arg.
659 bc_parse_scale(BcParse* p, BcInst* type, bool* can_assign, uint8_t flags)
663 // Without the left paren, it's just the keyword.
664 if (p->l.t != BC_LEX_LPAREN)
666 // Set, push, and return.
667 *type = BC_INST_SCALE;
669 bc_parse_push(p, BC_INST_SCALE);
673 // Handle the scale function.
674 *type = BC_INST_SCALE_FUNC;
677 // Once again, adjust the flags.
678 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
679 flags |= BC_PARSE_NEEDVAL;
683 bc_parse_expr_status(p, flags, bc_parse_next_rel);
685 // Must have a right paren.
686 if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
688 bc_parse_push(p, BC_INST_SCALE_FUNC);
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
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.
704 bc_parse_incdec(BcParse* p, BcInst* prev, bool* can_assign, size_t* nexs,
709 BcInst etype = *prev;
710 BcLexType last = p->l.last;
712 assert(prev != NULL && can_assign != NULL);
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))
718 bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
721 // Is the previous instruction for a variable?
722 if (BC_PARSE_INST_VAR(etype))
724 // If so, this is a postfix operator.
725 if (!*can_assign) bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
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);
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);
742 // Because we parse the next part of the expression
743 // right here, we need to increment this.
746 // Is the next token a normal identifier?
747 if (type == BC_LEX_NAME)
750 uint8_t flags2 = flags & ~(BC_PARSE_ARRAY);
751 bc_parse_name(p, prev, can_assign, flags2 | BC_PARSE_NOCALL);
753 // Is the next token a global?
754 else if (type >= BC_LEX_KW_LAST && type <= BC_LEX_KW_OBASE)
756 bc_parse_push(p, type - BC_LEX_KW_LAST + BC_INST_LAST);
759 // Is the next token specifically scale, which needs special treatment?
760 else if (BC_NO_ERR(type == BC_LEX_KW_SCALE))
764 // Check that scale() was not used.
765 if (BC_ERR(p->l.t == BC_LEX_LPAREN))
767 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
769 else bc_parse_push(p, BC_INST_SCALE);
771 // Now we know we have an error.
772 else bc_parse_err(p, BC_ERR_PARSE_TOKEN);
776 bc_parse_push(p, BC_INST_ONE);
777 bc_parse_push(p, inst);
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.
792 bc_parse_minus(BcParse* p, BcInst* prev, size_t ops_bgn, bool rparen,
793 bool binlast, size_t* nexprs)
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);
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);
811 * @param p The parser.
812 * @param inst The instruction corresponding to how the string was found and
813 * how it should be printed.
816 bc_parse_str(BcParse* p, BcInst inst)
818 bc_parse_addString(p);
819 bc_parse_push(p, inst);
824 * Parses a print statement.
825 * @param p The parser.
828 bc_parse_print(BcParse* p, BcLexType type)
832 BcInst inst = type == BC_LEX_KW_STREAM ? BC_INST_PRINT_STREAM :
839 // A print or stream statement has to have *something*.
840 if (bc_parse_isDelimiter(p)) bc_parse_err(p, BC_ERR_PARSE_PRINT);
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);
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);
854 // Is the next token a comma?
855 comma = (p->l.t == BC_LEX_COMMA);
857 // Get the next token if we have a comma.
858 if (comma) bc_lex_next(&p->l);
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);
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);
875 * Parses a return statement.
876 * @param p The parser.
879 bc_parse_return(BcParse* p)
883 uchar inst = BC_INST_RET0;
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);
888 // If we are in a void function, make sure to return void.
889 if (p->func->voidfn) inst = BC_INST_RET_VOID;
894 paren = (t == BC_LEX_LPAREN);
896 // An empty return statement just needs to push the selected instruction.
897 if (bc_parse_isDelimiter(p)) bc_parse_push(p, inst);
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);
905 // If the expression was empty, just push the selected instruction.
906 if (s == BC_PARSE_STATUS_EMPTY_EXPR)
908 bc_parse_push(p, inst);
912 // POSIX requires parentheses.
913 if (!paren || p->l.last != BC_LEX_RPAREN)
915 bc_parse_err(p, BC_ERR_POSIX_RET);
918 // Void functions require an empty expression.
919 if (BC_ERR(p->func->voidfn))
921 if (s != BC_PARSE_STATUS_EMPTY_EXPR)
923 bc_parse_verr(p, BC_ERR_PARSE_RET_VOID, p->func->name);
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);
933 * Clears flags that indicate the end of an if statement and its block and sets
935 * @param p The parser.
938 bc_parse_noElse(BcParse* p)
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);
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.
951 bc_parse_endBody(BcParse* p, bool brace)
953 bool has_brace, new_else = false;
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);
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);
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)))
969 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
973 // Do we have a brace flag?
974 has_brace = (BC_PARSE_BRACE(p) != 0);
978 size_t len = p->flags.len;
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);
984 // Are we inside a loop?
985 loop = (BC_PARSE_LOOP_INNER(p) != 0);
987 // If we are ending a loop or an else...
988 if (loop || BC_PARSE_ELSE(p))
990 // Loops have condition labels that we have to take care of as well.
993 size_t* label = bc_vec_top(&p->conds);
995 bc_parse_push(p, BC_INST_JUMP);
996 bc_parse_pushIndex(p, *label);
998 bc_vec_pop(&p->conds);
1001 bc_parse_setLabel(p);
1002 bc_vec_pop(&p->flags);
1004 // If we are ending a function...
1005 else if (BC_PARSE_FUNC_INNER(p))
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);
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);
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)))
1020 while (p->l.t == BC_LEX_NLINE)
1025 // *Now* we can pop the flags.
1026 bc_vec_pop(&p->flags);
1028 // If we are allowed non-POSIX stuff...
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);
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))
1042 // POSIX requires us to do the bare minimum only.
1043 else bc_parse_noElse(p);
1046 // If these are both true, we have "used" the braces that we found.
1047 if (brace && has_brace) brace = false;
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)));
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))
1061 // If we make it here, we have a brace and a flag for it.
1062 uint16_t flags = BC_PARSE_TOP_FLAG(p);
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)))
1070 bc_vec_pop(&p->flags);
1076 * Starts the body of a control statement or function.
1077 * @param p The parser.
1078 * @param flags The current flags (will be edited).
1081 bc_parse_startBody(BcParse* p, uint16_t 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);
1090 bc_parse_endif(BcParse* p)
1095 // Not a problem if this is true.
1096 if (BC_NO_ERR(!BC_PARSE_NO_EXEC(p))) return;
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)
1104 uint16_t flag = *((uint16_t*) bc_vec_item(&p->flags, i));
1105 good = ((flag & BC_PARSE_FLAG_BRACE) != BC_PARSE_FLAG_BRACE);
1108 // If we did not find such an instance...
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;
1115 vm->mode = BC_MODE_FILE;
1117 // End all of the if statements and loops.
1118 while (p->flags.len > 1 || BC_PARSE_IF_END(p))
1120 if (BC_PARSE_IF_END(p)) bc_parse_noElse(p);
1121 if (p->flags.len > 1) bc_parse_endBody(p, false);
1124 vm->mode = (uchar) mode;
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);
1131 * Parses an if statement.
1132 * @param p The parser.
1135 bc_parse_if(BcParse* p)
1137 // We are allowed relational operators, and we must have a value.
1139 uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
1141 // Get the left paren and barf if necessary.
1143 if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1145 // Parse the condition.
1147 bc_parse_expr_status(p, flags, bc_parse_next_rel);
1149 // Must have a right paren.
1150 if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1154 // Insert the conditional jump instruction.
1155 bc_parse_push(p, BC_INST_JUMP_ZERO);
1157 idx = p->func->labels.len;
1159 // Push the index for the instruction and create an exit label for an else
1161 bc_parse_pushIndex(p, idx);
1162 bc_parse_createExitLabel(p, idx, false);
1164 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
1168 * Parses an else statement.
1169 * @param p The parser.
1172 bc_parse_else(BcParse* p)
1174 size_t idx = p->func->labels.len;
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);
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);
1184 // Clear the else stuff. Yes, that function is misnamed for its use here,
1185 // but deal with it.
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);
1196 * Parse a while loop.
1197 * @param p The parser.
1200 bc_parse_while(BcParse* p)
1202 // We are allowed relational operators, and we must have a value.
1204 uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
1206 // Get the left paren and barf if necessary.
1208 if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
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);
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);
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);
1229 * @param p The parser.
1232 bc_parse_for(BcParse* p)
1234 size_t cond_idx, exit_idx, body_idx, update_idx;
1236 // Barf on the missing left paren.
1238 if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
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);
1246 // Must have a semicolon.
1247 if (BC_ERR(p->l.t != BC_LEX_SCOLON)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
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;
1259 // This creates the condition label.
1260 bc_parse_createLabel(p, p->func->code.len);
1262 // Parse an expression if it exists.
1263 if (p->l.t != BC_LEX_SCOLON)
1265 uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
1266 bc_parse_expr_status(p, flags, bc_parse_next_for);
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);
1277 // An empty condition makes POSIX mad.
1278 bc_parse_err(p, BC_ERR_POSIX_FOR);
1281 // Must have a semicolon.
1282 if (BC_ERR(p->l.t != BC_LEX_SCOLON)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
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);
1294 // Now create the label for the update code.
1295 bc_parse_createCondLabel(p, update_idx);
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);
1301 // Must have a right paren.
1302 if (BC_ERR(p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
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);
1309 // Create an exit label for the body and start the body.
1310 bc_parse_createExitLabel(p, exit_idx, true);
1312 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
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.
1322 bc_parse_loopExit(BcParse* p, BcLexType type)
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);
1330 // If we have a break statement...
1331 if (type == BC_LEX_KW_BREAK)
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);
1337 i = p->exits.len - 1;
1338 ip = bc_vec_item(&p->exits, i);
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)
1344 ip = bc_vec_item(&p->exits, i);
1348 // Make sure everything is hunky dory.
1349 assert(ip != NULL && (i < p->exits.len || ip->func));
1351 // Set the index for the exit.
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));
1358 // Add the unconditional jump.
1359 bc_parse_push(p, BC_INST_JUMP);
1360 bc_parse_pushIndex(p, i);
1366 * Parse a function (header).
1367 * @param p The parser.
1370 bc_parse_func(BcParse* p)
1372 bool comma = false, voidfn;
1378 // Must have a name.
1379 if (BC_ERR(p->l.t != BC_LEX_NAME)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
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"));
1385 // We can safely do this because the expected token should not overwrite the
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);
1392 // With a void function, allow POSIX to complain and get a new token.
1395 bc_parse_err(p, BC_ERR_POSIX_VOID);
1397 // We can safely do this because the expected token should not overwrite
1398 // the function name.
1402 // Must have a left paren.
1403 if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
1405 // Make sure the functions map and vector are synchronized.
1406 assert(p->prog->fns.len == p->prog->fn_map.len);
1408 // Insert the function by name into the map and vector.
1409 idx = bc_program_insertFunc(p->prog, p->l.str.v);
1411 // Make sure the insert worked.
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;
1420 // While we do not have a right paren, we are still parsing arguments.
1421 while (p->l.t != BC_LEX_RPAREN)
1423 BcType t = BC_TYPE_VAR;
1425 // If we have an asterisk, we are parsing a reference argument.
1426 if (p->l.t == BC_LEX_OP_MULTIPLY)
1431 // Let POSIX complain if necessary.
1432 bc_parse_err(p, BC_ERR_POSIX_REF);
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);
1438 // Increment the number of parameters.
1439 p->func->nparams += 1;
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);
1446 // We are parsing an array parameter if this is true.
1447 if (p->l.t == BC_LEX_LBRACKET)
1449 // Set the array type, unless we are already parsing a reference.
1450 if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY;
1454 // The brackets *must* be empty.
1455 if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
1457 bc_parse_err(p, BC_ERR_PARSE_FUNC);
1462 // If we did *not* get a bracket, but we are expecting a reference, we
1464 else if (BC_ERR(t == BC_TYPE_REF))
1466 bc_parse_verr(p, BC_ERR_PARSE_REF_VAR, p->buf.v);
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);
1473 // Insert the parameter into the function.
1474 bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line);
1477 // If we have a comma, but no parameter, barf.
1478 if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
1481 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER;
1482 bc_parse_startBody(p, flags);
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);
1492 * Parse an auto list.
1493 * @param p The parser.
1496 bc_parse_auto(BcParse* p)
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);
1504 p->auto_part = comma = false;
1506 // We need at least one variable or array.
1507 one = (p->l.t == BC_LEX_NAME);
1509 // While we have a variable or array.
1510 while (p->l.t == BC_LEX_NAME)
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);
1519 // If we are parsing an array...
1520 if (p->l.t == BC_LEX_LBRACKET)
1526 // The brackets *must* be empty.
1527 if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
1529 bc_parse_err(p, BC_ERR_PARSE_FUNC);
1534 else t = BC_TYPE_VAR;
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);
1540 // Insert the auto into the function.
1541 bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line);
1544 // If we have a comma, but no auto, barf.
1545 if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
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);
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);
1556 * @param p The parser.
1557 * @param brace True if a brace was encountered, false otherwise.
1560 bc_parse_body(BcParse* p, bool brace)
1562 uint16_t* flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
1564 assert(flag_ptr != NULL);
1565 assert(p->flags.len >= 2);
1567 // The body flag is for when we expect a body. We got a body, so clear the
1569 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
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)
1575 // We *must* have a brace in this case.
1576 if (BC_ERR(!brace)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1578 p->auto_part = (p->l.t != BC_LEX_KW_AUTO);
1582 // Make sure this is true to not get a parse error.
1583 p->auto_part = true;
1585 // Since we already have the auto keyword, parse.
1590 if (p->l.t == BC_LEX_NLINE) bc_lex_next(&p->l);
1594 // This is the easy part.
1595 size_t len = p->flags.len;
1599 // Parse a statement.
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)
1608 bc_parse_endBody(p, false);
1614 * Parses a statement. This is the entry point for just about everything, except
1615 * function definitions.
1616 * @param p The parser.
1619 bc_parse_stmt(BcParse* p)
1623 BcLexType type = p->l.t;
1626 if (type == BC_LEX_NLINE)
1633 if (type == BC_LEX_KW_AUTO)
1639 // If we reach this point, no auto list is allowed.
1640 p->auto_part = false;
1642 // Everything but an else needs to be taken care of here, but else is
1644 if (type != BC_LEX_KW_ELSE)
1646 // After an if, no else found.
1647 if (BC_PARSE_IF_END(p))
1649 // Clear the expectation for else, end body, and return. Returning
1650 // gives us a clean slate for parsing again.
1652 if (p->flags.len > 1 && !BC_PARSE_BRACE(p))
1654 bc_parse_endBody(p, false);
1659 // With a left brace, we are parsing a body.
1660 else if (type == BC_LEX_LBRACE)
1662 // We need to start a body if we are not expecting one yet.
1663 if (!BC_PARSE_BODY(p))
1665 bc_parse_startBody(p, BC_PARSE_FLAG_BRACE);
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
1673 *(BC_PARSE_TOP_FLAG_PTR(p)) |= BC_PARSE_FLAG_BRACE;
1675 bc_parse_body(p, true);
1678 // If we have reached this point, we need to return for a clean
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
1685 else if (BC_PARSE_BODY(p) && !BC_PARSE_BRACE(p))
1687 bc_parse_body(p, false);
1693 flags = BC_PARSE_TOP_FLAG(p);
1697 // All of these are valid for expressions.
1700 case BC_LEX_OP_MINUS:
1701 case BC_LEX_OP_BOOL_NOT:
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:
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:
1737 bc_parse_expr_status(p, BC_PARSE_PRINT, bc_parse_next_expr);
1741 case BC_LEX_KW_ELSE:
1756 bc_parse_endBody(p, true);
1762 bc_parse_str(p, BC_INST_PRINT_STR);
1766 case BC_LEX_KW_BREAK:
1767 case BC_LEX_KW_CONTINUE:
1769 bc_parse_loopExit(p, p->l.t);
1779 case BC_LEX_KW_HALT:
1781 bc_parse_push(p, BC_INST_HALT);
1792 case BC_LEX_KW_LIMITS:
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);
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);
1817 case BC_LEX_KW_STREAM:
1818 case BC_LEX_KW_PRINT:
1820 bc_parse_print(p, type);
1824 case BC_LEX_KW_QUIT:
1826 // Quit is a compile-time command. We don't exit directly, so the vm
1828 vm->status = BC_STATUS_QUIT;
1833 case BC_LEX_KW_RETURN:
1839 case BC_LEX_KW_WHILE:
1846 case BC_LEX_INVALID:
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:
1882 case BC_LEX_WHITESPACE:
1884 case BC_LEX_LBRACKET:
1886 case BC_LEX_RBRACKET:
1888 case BC_LEX_KW_AUTO:
1889 case BC_LEX_KW_DEFINE:
1891 case BC_LEX_EXTENDED_REGISTERS:
1892 case BC_LEX_EQ_NO_REG:
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:
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
1909 case BC_LEX_LOAD_POP:
1910 case BC_LEX_STORE_PUSH:
1911 case BC_LEX_PRINT_POP:
1913 case BC_LEX_EXEC_STACK_LENGTH:
1914 case BC_LEX_SCALE_FACTOR:
1915 case BC_LEX_ARRAY_LENGTH:
1916 #endif // DC_ENABLED
1918 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1922 // If the flags did not change, we expect a delimiter.
1923 if (len == p->flags.len && flags == BC_PARSE_TOP_FLAG(p))
1925 if (BC_ERR(!bc_parse_isDelimiter(p)))
1927 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1931 // Make sure semicolons are eaten.
1932 while (p->l.t == BC_LEX_SCOLON || p->l.t == BC_LEX_NLINE)
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)
1942 bc_parse_err(p, BC_ERR_POSIX_FUNC_AFTER_SEMICOLON);
1947 bc_parse_parse(BcParse* p)
1951 BC_SETJMP_LOCKED(vm, exit);
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);
1957 // Functions need special parsing.
1958 else if (p->l.t == BC_LEX_KW_DEFINE)
1960 if (BC_ERR(BC_PARSE_NO_EXEC(p)))
1963 if (BC_ERR(BC_PARSE_NO_EXEC(p)))
1965 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1971 // Otherwise, parse a normal statement.
1972 else bc_parse_stmt(p);
1976 // We need to reset on error.
1977 if (BC_ERR(((vm->status && vm->status != BC_STATUS_QUIT) || vm->sig != 0)))
1982 BC_LONGJMP_CONT(vm);
1987 * Parse an expression. This is the actual implementation of the Shunting-Yard
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
1997 static BcParseStatus
1998 bc_parse_expr_err(BcParse* p, uint8_t flags, BcParseNext next)
2000 BcInst prev = BC_INST_PRINT;
2001 uchar inst = BC_INST_INVALID;
2003 size_t nexprs, ops_bgn;
2004 uint32_t i, nparens, nrelops;
2005 bool pfirst, rprn, done, get_token, assign, bin_last, incdec, can_assign;
2007 // One of these *must* be true.
2008 assert(!(flags & BC_PARSE_PRINT) || !(flags & BC_PARSE_NEEDVAL));
2010 // These are set very carefully. In fact, controlling the values of these
2011 // locals is the biggest part of making this work. ops_bgn especially is
2012 // important because it marks where the operator stack begins for *this*
2013 // invocation of this function. That's because bc_parse_expr_err() is
2014 // recursive (the Shunting-Yard Algorithm is most easily expressed
2015 // recursively when parsing subexpressions), and each invocation needs to
2016 // know where to stop.
2018 // - nparens is the number of left parens without matches.
2019 // - nrelops is the number of relational operators that appear in the expr.
2020 // - nexprs is the number of unused expressions.
2021 // - rprn is a right paren encountered last.
2022 // - done means the expression has been fully parsed.
2023 // - get_token is true when a token is needed at the end of an iteration.
2024 // - assign is true when an assignment statement was parsed last.
2025 // - incdec is true when the previous operator was an inc or dec operator.
2026 // - can_assign is true when an assignemnt is valid.
2027 // - bin_last is true when the previous instruction was a binary operator.
2029 pfirst = (p->l.t == BC_LEX_LPAREN);
2030 nparens = nrelops = 0;
2032 ops_bgn = p->ops.len;
2033 rprn = done = get_token = assign = incdec = can_assign = false;
2036 // We want to eat newlines if newlines are not a valid ending token.
2037 // This is for spacing in things like for loop headers.
2038 if (!(flags & BC_PARSE_NOREAD))
2040 while ((t = p->l.t) == BC_LEX_NLINE)
2046 // This is the Shunting-Yard algorithm loop.
2047 for (; !done && BC_PARSE_EXPR(t); t = p->l.t)
2054 // These operators can only be used with items that can be
2056 if (BC_ERR(incdec)) bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
2058 bc_parse_incdec(p, &prev, &can_assign, &nexprs, flags);
2060 rprn = get_token = bin_last = false;
2062 flags &= ~(BC_PARSE_ARRAY);
2067 #if BC_ENABLE_EXTRA_MATH
2068 case BC_LEX_OP_TRUNC:
2070 // The previous token must have been a leaf expression, or the
2071 // operator is in the wrong place.
2072 if (BC_ERR(!BC_PARSE_LEAF(prev, bin_last, rprn)))
2074 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
2077 // I can just add the instruction because
2078 // negative will already be taken care of.
2079 bc_parse_push(p, BC_INST_TRUNC);
2081 rprn = can_assign = incdec = false;
2083 flags &= ~(BC_PARSE_ARRAY);
2087 #endif // BC_ENABLE_EXTRA_MATH
2089 case BC_LEX_OP_MINUS:
2091 bc_parse_minus(p, &prev, ops_bgn, rprn, bin_last, &nexprs);
2093 rprn = get_token = can_assign = false;
2095 // This is true if it was a binary operator last.
2096 bin_last = (prev == BC_INST_MINUS);
2097 if (bin_last) incdec = false;
2099 flags &= ~(BC_PARSE_ARRAY);
2104 // All of this group, including the fallthrough, is to parse binary
2106 case BC_LEX_OP_ASSIGN_POWER:
2107 case BC_LEX_OP_ASSIGN_MULTIPLY:
2108 case BC_LEX_OP_ASSIGN_DIVIDE:
2109 case BC_LEX_OP_ASSIGN_MODULUS:
2110 case BC_LEX_OP_ASSIGN_PLUS:
2111 case BC_LEX_OP_ASSIGN_MINUS:
2112 #if BC_ENABLE_EXTRA_MATH
2113 case BC_LEX_OP_ASSIGN_PLACES:
2114 case BC_LEX_OP_ASSIGN_LSHIFT:
2115 case BC_LEX_OP_ASSIGN_RSHIFT:
2116 #endif // BC_ENABLE_EXTRA_MATH
2117 case BC_LEX_OP_ASSIGN:
2119 // We need to make sure the assignment is valid.
2120 if (!BC_PARSE_INST_VAR(prev))
2122 bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
2129 case BC_LEX_OP_POWER:
2130 case BC_LEX_OP_MULTIPLY:
2131 case BC_LEX_OP_DIVIDE:
2132 case BC_LEX_OP_MODULUS:
2133 case BC_LEX_OP_PLUS:
2134 #if BC_ENABLE_EXTRA_MATH
2135 case BC_LEX_OP_PLACES:
2136 case BC_LEX_OP_LSHIFT:
2137 case BC_LEX_OP_RSHIFT:
2138 #endif // BC_ENABLE_EXTRA_MATH
2139 case BC_LEX_OP_REL_EQ:
2140 case BC_LEX_OP_REL_LE:
2141 case BC_LEX_OP_REL_GE:
2142 case BC_LEX_OP_REL_NE:
2143 case BC_LEX_OP_REL_LT:
2144 case BC_LEX_OP_REL_GT:
2145 case BC_LEX_OP_BOOL_NOT:
2146 case BC_LEX_OP_BOOL_OR:
2147 case BC_LEX_OP_BOOL_AND:
2149 // This is true if the operator if the token is a prefix
2150 // operator. This is only for boolean not.
2151 if (BC_PARSE_OP_PREFIX(t))
2153 // Prefix operators are only allowed after binary operators
2154 // or prefix operators.
2155 if (BC_ERR(!bin_last && !BC_PARSE_OP_PREFIX(p->l.last)))
2157 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2160 // If we execute the else, that means we have a binary operator.
2161 // If the previous operator was a prefix or a binary operator,
2162 // then a binary operator is not allowed.
2163 else if (BC_ERR(BC_PARSE_PREV_PREFIX(prev) || bin_last))
2165 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2168 nrelops += (t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT);
2169 prev = BC_PARSE_TOKEN_INST(t);
2171 bc_parse_operator(p, t, ops_bgn, &nexprs);
2173 rprn = incdec = can_assign = false;
2175 bin_last = !BC_PARSE_OP_PREFIX(t);
2176 flags &= ~(BC_PARSE_ARRAY);
2183 // A left paren is *not* allowed right after a leaf expr.
2184 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2186 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2190 rprn = incdec = can_assign = false;
2193 // Push the paren onto the operator stack.
2194 bc_vec_push(&p->ops, &t);
2201 // This needs to be a status. The error is handled in
2202 // bc_parse_expr_status().
2203 if (BC_ERR(p->l.last == BC_LEX_LPAREN))
2205 return BC_PARSE_STATUS_EMPTY_EXPR;
2208 // The right paren must not come after a prefix or binary
2210 if (BC_ERR(bin_last || BC_PARSE_PREV_PREFIX(prev)))
2212 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2215 // If there are no parens left, we are done, but we need another
2226 get_token = bin_last = incdec = false;
2228 bc_parse_rightParen(p, &nexprs);
2235 // POSIX only allows strings alone.
2236 if (BC_IS_POSIX) bc_parse_err(p, BC_ERR_POSIX_EXPR_STRING);
2238 // A string is a leaf and cannot come right after a leaf.
2239 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2241 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2244 bc_parse_addString(p);
2247 bin_last = rprn = false;
2255 // A name is a leaf and cannot come right after a leaf.
2256 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2258 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2261 get_token = bin_last = false;
2263 bc_parse_name(p, &prev, &can_assign, flags & ~BC_PARSE_NOCALL);
2265 rprn = (prev == BC_INST_CALL);
2267 flags &= ~(BC_PARSE_ARRAY);
2274 // A number is a leaf and cannot come right after a leaf.
2275 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2277 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2280 // The number instruction is pushed in here.
2286 rprn = bin_last = can_assign = false;
2287 flags &= ~(BC_PARSE_ARRAY);
2292 case BC_LEX_KW_IBASE:
2293 case BC_LEX_KW_LAST:
2294 case BC_LEX_KW_OBASE:
2295 #if BC_ENABLE_EXTRA_MATH
2296 case BC_LEX_KW_SEED:
2297 #endif // BC_ENABLE_EXTRA_MATH
2299 // All of these are leaves and cannot come right after a leaf.
2300 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2302 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2305 prev = t - BC_LEX_KW_LAST + BC_INST_LAST;
2306 bc_parse_push(p, prev);
2308 get_token = can_assign = true;
2309 rprn = bin_last = false;
2311 flags &= ~(BC_PARSE_ARRAY);
2316 case BC_LEX_KW_LENGTH:
2317 case BC_LEX_KW_SQRT:
2319 case BC_LEX_KW_IS_NUMBER:
2320 case BC_LEX_KW_IS_STRING:
2321 #if BC_ENABLE_EXTRA_MATH
2322 case BC_LEX_KW_IRAND:
2323 #endif // BC_ENABLE_EXTRA_MATH
2324 case BC_LEX_KW_ASCIIFY:
2326 // All of these are leaves and cannot come right after a leaf.
2327 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2329 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2332 bc_parse_builtin(p, t, flags, &prev);
2334 rprn = get_token = bin_last = incdec = can_assign = false;
2336 flags &= ~(BC_PARSE_ARRAY);
2341 case BC_LEX_KW_READ:
2342 #if BC_ENABLE_EXTRA_MATH
2343 case BC_LEX_KW_RAND:
2344 #endif // BC_ENABLE_EXTRA_MATH
2345 case BC_LEX_KW_MAXIBASE:
2346 case BC_LEX_KW_MAXOBASE:
2347 case BC_LEX_KW_MAXSCALE:
2348 #if BC_ENABLE_EXTRA_MATH
2349 case BC_LEX_KW_MAXRAND:
2350 #endif // BC_ENABLE_EXTRA_MATH
2351 case BC_LEX_KW_LINE_LENGTH:
2352 case BC_LEX_KW_GLOBAL_STACKS:
2353 case BC_LEX_KW_LEADING_ZERO:
2355 // All of these are leaves and cannot come right after a leaf.
2356 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2358 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2361 // Error if we have read and it's not allowed.
2362 else if (t == BC_LEX_KW_READ && BC_ERR(flags & BC_PARSE_NOREAD))
2364 bc_parse_err(p, BC_ERR_EXEC_REC_READ);
2367 prev = t - BC_LEX_KW_READ + BC_INST_READ;
2368 bc_parse_noArgBuiltin(p, prev);
2370 rprn = get_token = bin_last = incdec = can_assign = false;
2372 flags &= ~(BC_PARSE_ARRAY);
2377 case BC_LEX_KW_SCALE:
2379 // This is a leaf and cannot come right after a leaf.
2380 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2382 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2385 // Scale needs special work because it can be a variable *or* a
2387 bc_parse_scale(p, &prev, &can_assign, flags);
2389 rprn = get_token = bin_last = false;
2391 flags &= ~(BC_PARSE_ARRAY);
2396 case BC_LEX_KW_MODEXP:
2397 case BC_LEX_KW_DIVMOD:
2399 // This is a leaf and cannot come right after a leaf.
2400 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
2402 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2405 bc_parse_builtin3(p, t, flags, &prev);
2407 rprn = get_token = bin_last = incdec = can_assign = false;
2409 flags &= ~(BC_PARSE_ARRAY);
2415 case BC_LEX_INVALID:
2418 case BC_LEX_WHITESPACE:
2419 case BC_LEX_LBRACKET:
2421 case BC_LEX_RBRACKET:
2425 case BC_LEX_KW_AUTO:
2426 case BC_LEX_KW_BREAK:
2427 case BC_LEX_KW_CONTINUE:
2428 case BC_LEX_KW_DEFINE:
2431 case BC_LEX_KW_LIMITS:
2432 case BC_LEX_KW_RETURN:
2433 case BC_LEX_KW_WHILE:
2434 case BC_LEX_KW_HALT:
2435 case BC_LEX_KW_PRINT:
2436 case BC_LEX_KW_QUIT:
2437 case BC_LEX_KW_STREAM:
2438 case BC_LEX_KW_ELSE:
2440 case BC_LEX_EXTENDED_REGISTERS:
2441 case BC_LEX_EQ_NO_REG:
2443 case BC_LEX_EXECUTE:
2444 case BC_LEX_PRINT_STACK:
2445 case BC_LEX_CLEAR_STACK:
2446 case BC_LEX_REG_STACK_LEVEL:
2447 case BC_LEX_STACK_LEVEL:
2448 case BC_LEX_DUPLICATE:
2451 case BC_LEX_STORE_IBASE:
2452 case BC_LEX_STORE_OBASE:
2453 case BC_LEX_STORE_SCALE:
2454 #if BC_ENABLE_EXTRA_MATH
2455 case BC_LEX_STORE_SEED:
2456 #endif // BC_ENABLE_EXTRA_MATH
2458 case BC_LEX_LOAD_POP:
2459 case BC_LEX_STORE_PUSH:
2460 case BC_LEX_PRINT_POP:
2462 case BC_LEX_EXEC_STACK_LENGTH:
2463 case BC_LEX_SCALE_FACTOR:
2464 case BC_LEX_ARRAY_LENGTH:
2465 #endif // DC_ENABLED
2468 // We should never get here, even in debug builds.
2469 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
2475 if (get_token) bc_lex_next(&p->l);
2478 // Now that we have parsed the expression, we need to empty the operator
2480 while (p->ops.len > ops_bgn)
2482 top = BC_PARSE_TOP_OP(p);
2483 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
2485 // There should not be *any* parens on the stack anymore.
2486 if (BC_ERR(top == BC_LEX_LPAREN || top == BC_LEX_RPAREN))
2488 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2491 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
2493 // Adjust the number of unused expressions.
2494 nexprs -= !BC_PARSE_OP_PREFIX(top);
2495 bc_vec_pop(&p->ops);
2500 // There must be only one expression at the top.
2501 if (BC_ERR(nexprs != 1)) bc_parse_err(p, BC_ERR_PARSE_EXPR);
2503 // Check that the next token is correct.
2504 for (i = 0; i < next.len && t != next.tokens[i]; ++i)
2508 if (BC_ERR(i == next.len && !bc_parse_isDelimiter(p)))
2510 bc_parse_err(p, BC_ERR_PARSE_EXPR);
2513 // Check that POSIX would be happy with the number of relational operators.
2514 if (!(flags & BC_PARSE_REL) && nrelops)
2516 bc_parse_err(p, BC_ERR_POSIX_REL_POS);
2518 else if ((flags & BC_PARSE_REL) && nrelops > 1)
2520 bc_parse_err(p, BC_ERR_POSIX_MULTIREL);
2523 // If this is true, then we might be in a situation where we don't print.
2524 // We would want to have the increment/decrement operator not make an extra
2525 // copy if it's not necessary.
2526 if (!(flags & BC_PARSE_NEEDVAL) && !pfirst)
2528 // We have the easy case if the last operator was an assignment
2532 inst = *((uchar*) bc_vec_top(&p->func->code));
2533 inst += (BC_INST_ASSIGN_POWER_NO_VAL - BC_INST_ASSIGN_POWER);
2536 // If we have an inc/dec operator and we are *not* printing, implement
2537 // the optimization to get rid of the extra copy.
2538 else if (incdec && !(flags & BC_PARSE_PRINT))
2540 inst = *((uchar*) bc_vec_top(&p->func->code));
2541 incdec = (inst <= BC_INST_DEC);
2542 inst = BC_INST_ASSIGN_PLUS_NO_VAL +
2543 (inst != BC_INST_INC && inst != BC_INST_ASSIGN_PLUS);
2546 // This condition allows us to change the previous assignment
2547 // instruction (which does a copy) for a NO_VAL version, which does not.
2548 // This condition is set if either of the above if statements ends up
2550 if (inst >= BC_INST_ASSIGN_POWER_NO_VAL &&
2551 inst <= BC_INST_ASSIGN_NO_VAL)
2553 // Pop the previous assignment instruction and push a new one.
2554 // Inc/dec needs the extra instruction because it is now a binary
2555 // operator and needs a second operand.
2556 bc_vec_pop(&p->func->code);
2557 if (incdec) bc_parse_push(p, BC_INST_ONE);
2558 bc_parse_push(p, inst);
2562 // If we might have to print...
2563 if ((flags & BC_PARSE_PRINT))
2565 // With a paren first or the last operator not being an assignment, we
2566 // *do* want to print.
2567 if (pfirst || !assign) bc_parse_push(p, BC_INST_PRINT);
2569 // We need to make sure to push a pop instruction for assignment statements
2570 // that will not print. The print will pop, but without it, we need to pop.
2571 else if (!(flags & BC_PARSE_NEEDVAL) &&
2572 (inst < BC_INST_ASSIGN_POWER_NO_VAL ||
2573 inst > BC_INST_ASSIGN_NO_VAL))
2575 bc_parse_push(p, BC_INST_POP);
2578 // We want to eat newlines if newlines are not a valid ending token.
2579 // This is for spacing in things like for loop headers.
2581 // Yes, this is one case where I reuse a variable for a different purpose;
2582 // in this case, incdec being true now means that newlines are not valid.
2583 for (incdec = true, i = 0; i < next.len && incdec; ++i)
2585 incdec = (next.tokens[i] != BC_LEX_NLINE);
2589 while (p->l.t == BC_LEX_NLINE)
2595 return BC_PARSE_STATUS_SUCCESS;
2599 * Parses an expression with bc_parse_expr_err(), but throws an error if it gets
2600 * an empty expression.
2601 * @param p The parser.
2602 * @param flags The flags for what is valid in the expression.
2603 * @param next A set of tokens for what is valid *after* the expression.
2606 bc_parse_expr_status(BcParse* p, uint8_t flags, BcParseNext next)
2608 BcParseStatus s = bc_parse_expr_err(p, flags, next);
2610 if (BC_ERR(s == BC_PARSE_STATUS_EMPTY_EXPR))
2612 bc_parse_err(p, BC_ERR_PARSE_EMPTY_EXPR);
2617 bc_parse_expr(BcParse* p, uint8_t flags)
2620 bc_parse_expr_status(p, flags, bc_parse_next_read);
2622 #endif // BC_ENABLED