2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 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 static void bc_parse_else(BcParse *p);
50 static void bc_parse_stmt(BcParse *p);
51 static BcParseStatus bc_parse_expr_err(BcParse *p, uint8_t flags,
54 static bool bc_parse_inst_isLeaf(BcInst t) {
55 return (t >= BC_INST_NUM && t <= BC_INST_MAXSCALE) ||
56 #if BC_ENABLE_EXTRA_MATH
58 #endif // BC_ENABLE_EXTRA_MATH
62 static bool bc_parse_isDelimiter(const BcParse *p) {
67 if (BC_PARSE_DELIMITER(t)) return true;
69 if (t == BC_LEX_KW_ELSE) {
72 uint16_t *fptr = NULL, flags = BC_PARSE_FLAG_ELSE;
74 for (i = 0; i < p->flags.len && BC_PARSE_BLOCK_STMT(flags); ++i) {
76 fptr = bc_vec_item_rev(&p->flags, i);
79 if ((flags & BC_PARSE_FLAG_BRACE) && p->l.last != BC_LEX_RBRACE)
83 good = ((flags & BC_PARSE_FLAG_IF) != 0);
85 else if (t == BC_LEX_RBRACE) {
89 for (i = 0; !good && i < p->flags.len; ++i) {
90 uint16_t *fptr = bc_vec_item_rev(&p->flags, i);
91 good = (((*fptr) & BC_PARSE_FLAG_BRACE) != 0);
98 static void bc_parse_setLabel(BcParse *p) {
100 BcFunc *func = p->func;
101 BcInstPtr *ip = bc_vec_top(&p->exits);
104 assert(func == bc_vec_item(&p->prog->fns, p->fidx));
106 label = bc_vec_item(&func->labels, ip->idx);
107 *label = func->code.len;
109 bc_vec_pop(&p->exits);
112 static void bc_parse_createLabel(BcParse *p, size_t idx) {
113 bc_vec_push(&p->func->labels, &idx);
116 static void bc_parse_createCondLabel(BcParse *p, size_t idx) {
117 bc_parse_createLabel(p, p->func->code.len);
118 bc_vec_push(&p->conds, &idx);
121 static void bc_parse_createExitLabel(BcParse *p, size_t idx, bool loop) {
125 assert(p->func == bc_vec_item(&p->prog->fns, p->fidx));
131 bc_vec_push(&p->exits, &ip);
132 bc_parse_createLabel(p, SIZE_MAX);
135 static void bc_parse_operator(BcParse *p, BcLexType type,
136 size_t start, size_t *nexprs)
139 uchar l, r = BC_PARSE_OP_PREC(type);
140 uchar left = BC_PARSE_OP_LEFT(type);
142 while (p->ops.len > start) {
144 t = BC_PARSE_TOP_OP(p);
145 if (t == BC_LEX_LPAREN) break;
147 l = BC_PARSE_OP_PREC(t);
148 if (l >= r && (l != r || !left)) break;
150 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
152 *nexprs -= !BC_PARSE_OP_PREFIX(t);
155 bc_vec_push(&p->ops, &type);
158 static void bc_parse_rightParen(BcParse *p, size_t *nexs) {
162 while ((top = BC_PARSE_TOP_OP(p)) != BC_LEX_LPAREN) {
163 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
165 *nexs -= !BC_PARSE_OP_PREFIX(top);
173 static void bc_parse_params(BcParse *p, uint8_t flags) {
180 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
181 flags |= (BC_PARSE_ARRAY | BC_PARSE_NEEDVAL);
183 for (nparams = 0; p->l.t != BC_LEX_RPAREN; ++nparams) {
185 bc_parse_expr_status(p, flags, bc_parse_next_param);
187 comma = (p->l.t == BC_LEX_COMMA);
188 if (comma) bc_lex_next(&p->l);
191 if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
192 bc_parse_push(p, BC_INST_CALL);
193 bc_parse_pushIndex(p, nparams);
196 static void bc_parse_call(BcParse *p, const char *name, uint8_t flags) {
200 bc_parse_params(p, flags);
202 // We just assert this because bc_parse_params() should
203 // ensure that the next token is what it should be.
204 assert(p->l.t == BC_LEX_RPAREN);
206 // We cannot use bc_program_insertFunc() here
207 // because it will overwrite an existing function.
208 idx = bc_map_index(&p->prog->fn_map, name);
210 if (idx == BC_VEC_INVALID_IDX) {
214 idx = bc_program_insertFunc(p->prog, name);
218 assert(idx != BC_VEC_INVALID_IDX);
220 // Make sure that this pointer was not invalidated.
221 p->func = bc_vec_item(&p->prog->fns, p->fidx);
223 else idx = ((BcId*) bc_vec_item(&p->prog->fn_map, idx))->idx;
225 bc_parse_pushIndex(p, idx);
230 static void bc_parse_name(BcParse *p, BcInst *type,
231 bool *can_assign, uint8_t flags)
237 name = bc_vm_strdup(p->l.str.v);
239 BC_SETJMP_LOCKED(err);
245 if (p->l.t == BC_LEX_LBRACKET) {
249 if (p->l.t == BC_LEX_RBRACKET) {
251 if (BC_ERR(!(flags & BC_PARSE_ARRAY)))
252 bc_parse_err(p, BC_ERR_PARSE_EXPR);
254 *type = BC_INST_ARRAY;
259 uint8_t flags2 = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) |
262 bc_parse_expr_status(p, flags2, bc_parse_next_elem);
264 if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
265 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
267 *type = BC_INST_ARRAY_ELEM;
273 bc_parse_push(p, *type);
274 bc_parse_pushName(p, name, false);
276 else if (p->l.t == BC_LEX_LPAREN) {
278 if (BC_ERR(flags & BC_PARSE_NOCALL))
279 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
281 *type = BC_INST_CALL;
284 bc_parse_call(p, name, flags);
289 bc_parse_push(p, BC_INST_VAR);
290 bc_parse_pushName(p, name, true);
299 static void bc_parse_noArgBuiltin(BcParse *p, BcInst inst) {
302 if (BC_ERR(p->l.t != BC_LEX_LPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
305 if ((p->l.t != BC_LEX_RPAREN)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
307 bc_parse_push(p, inst);
312 static void bc_parse_builtin(BcParse *p, BcLexType type,
313 uint8_t flags, BcInst *prev)
316 if (BC_ERR(p->l.t != BC_LEX_LPAREN))
317 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
321 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
322 flags |= BC_PARSE_NEEDVAL;
323 if (type == BC_LEX_KW_LENGTH) flags |= BC_PARSE_ARRAY;
325 bc_parse_expr_status(p, flags, bc_parse_next_rel);
327 if (BC_ERR(p->l.t != BC_LEX_RPAREN))
328 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
330 *prev = type - BC_LEX_KW_LENGTH + BC_INST_LENGTH;
331 bc_parse_push(p, *prev);
336 static void bc_parse_scale(BcParse *p, BcInst *type,
337 bool *can_assign, uint8_t flags)
341 if (p->l.t != BC_LEX_LPAREN) {
342 *type = BC_INST_SCALE;
344 bc_parse_push(p, BC_INST_SCALE);
348 *type = BC_INST_SCALE_FUNC;
350 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
351 flags |= BC_PARSE_NEEDVAL;
355 bc_parse_expr_status(p, flags, bc_parse_next_rel);
356 if (BC_ERR(p->l.t != BC_LEX_RPAREN))
357 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
359 bc_parse_push(p, BC_INST_SCALE_FUNC);
364 static void bc_parse_incdec(BcParse *p, BcInst *prev, bool *can_assign,
365 size_t *nexs, uint8_t flags)
369 BcInst etype = *prev;
370 BcLexType last = p->l.last;
372 assert(prev != NULL && can_assign != NULL);
374 if (BC_ERR(last == BC_LEX_OP_INC || last == BC_LEX_OP_DEC ||
375 last == BC_LEX_RPAREN))
377 bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
380 if (BC_PARSE_INST_VAR(etype)) {
382 if (!*can_assign) bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
384 *prev = inst = BC_INST_INC + (p->l.t != BC_LEX_OP_INC);
385 bc_parse_push(p, inst);
391 *prev = inst = BC_INST_ASSIGN_PLUS + (p->l.t != BC_LEX_OP_INC);
396 // Because we parse the next part of the expression
397 // right here, we need to increment this.
400 if (type == BC_LEX_NAME) {
401 uint8_t flags2 = flags & ~BC_PARSE_ARRAY;
402 bc_parse_name(p, prev, can_assign, flags2 | BC_PARSE_NOCALL);
404 else if (type >= BC_LEX_KW_LAST && type <= BC_LEX_KW_OBASE) {
405 bc_parse_push(p, type - BC_LEX_KW_LAST + BC_INST_LAST);
408 else if (BC_NO_ERR(type == BC_LEX_KW_SCALE)) {
412 if (BC_ERR(p->l.t == BC_LEX_LPAREN))
413 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
414 else bc_parse_push(p, BC_INST_SCALE);
416 else bc_parse_err(p, BC_ERR_PARSE_TOKEN);
420 bc_parse_push(p, BC_INST_ONE);
421 bc_parse_push(p, inst);
425 static void bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
426 bool rparen, bool binlast, size_t *nexprs)
432 type = BC_PARSE_LEAF(*prev, binlast, rparen) ? BC_LEX_OP_MINUS : BC_LEX_NEG;
433 *prev = BC_PARSE_TOKEN_INST(type);
435 // We can just push onto the op stack because this is the largest
436 // precedence operator that gets pushed. Inc/dec does not.
437 if (type != BC_LEX_OP_MINUS) bc_vec_push(&p->ops, &type);
438 else bc_parse_operator(p, type, ops_bgn, nexprs);
441 static void bc_parse_str(BcParse *p, char inst) {
442 bc_parse_addString(p);
443 bc_parse_push(p, inst);
447 static void bc_parse_print(BcParse *p) {
456 if (bc_parse_isDelimiter(p)) bc_parse_err(p, BC_ERR_PARSE_PRINT);
459 if (t == BC_LEX_STR) bc_parse_str(p, BC_INST_PRINT_POP);
461 bc_parse_expr_status(p, BC_PARSE_NEEDVAL, bc_parse_next_print);
462 bc_parse_push(p, BC_INST_PRINT_POP);
465 comma = (p->l.t == BC_LEX_COMMA);
467 if (comma) bc_lex_next(&p->l);
469 if (!bc_parse_isDelimiter(p))
470 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
477 if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
480 static void bc_parse_return(BcParse *p) {
484 uchar inst = BC_INST_RET0;
486 if (BC_ERR(!BC_PARSE_FUNC(p))) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
488 if (p->func->voidfn) inst = BC_INST_RET_VOID;
493 paren = t == BC_LEX_LPAREN;
495 if (bc_parse_isDelimiter(p)) bc_parse_push(p, inst);
500 s = bc_parse_expr_err(p, BC_PARSE_NEEDVAL, bc_parse_next_expr);
502 if (s == BC_PARSE_STATUS_EMPTY_EXPR) {
503 bc_parse_push(p, inst);
507 if (!paren || p->l.last != BC_LEX_RPAREN) {
508 bc_parse_err(p, BC_ERR_POSIX_RET);
510 else if (BC_ERR(p->func->voidfn))
511 bc_parse_verr(p, BC_ERR_PARSE_RET_VOID, p->func->name);
513 bc_parse_push(p, BC_INST_RET);
517 static void bc_parse_noElse(BcParse *p) {
518 uint16_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
519 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
520 bc_parse_setLabel(p);
523 static void bc_parse_endBody(BcParse *p, bool brace) {
525 bool has_brace, new_else = false;
527 if (BC_ERR(p->flags.len <= 1)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
531 assert(p->l.t == BC_LEX_RBRACE);
534 if (BC_ERR(!bc_parse_isDelimiter(p)))
535 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
538 has_brace = (BC_PARSE_BRACE(p) != 0);
541 size_t len = p->flags.len;
544 if (has_brace && !brace) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
546 loop = (BC_PARSE_LOOP_INNER(p) != 0);
548 if (loop || BC_PARSE_ELSE(p)) {
552 size_t *label = bc_vec_top(&p->conds);
554 bc_parse_push(p, BC_INST_JUMP);
555 bc_parse_pushIndex(p, *label);
557 bc_vec_pop(&p->conds);
560 bc_parse_setLabel(p);
561 bc_vec_pop(&p->flags);
563 else if (BC_PARSE_FUNC_INNER(p)) {
564 BcInst inst = (p->func->voidfn ? BC_INST_RET_VOID : BC_INST_RET0);
565 bc_parse_push(p, inst);
566 bc_parse_updateFunc(p, BC_PROG_MAIN);
567 bc_vec_pop(&p->flags);
569 else if (BC_PARSE_BRACE(p) && !BC_PARSE_IF(p)) bc_vec_pop(&p->flags);
571 // This needs to be last to parse nested if's properly.
572 if (BC_PARSE_IF(p) && (len == p->flags.len || !BC_PARSE_BRACE(p))) {
574 while (p->l.t == BC_LEX_NLINE) bc_lex_next(&p->l);
576 bc_vec_pop(&p->flags);
580 *(BC_PARSE_TOP_FLAG_PTR(p)) |= BC_PARSE_FLAG_IF_END;
581 new_else = (p->l.t == BC_LEX_KW_ELSE);
583 if (new_else) bc_parse_else(p);
584 else if (!has_brace && (!BC_PARSE_IF_END(p) || brace))
587 else bc_parse_noElse(p);
590 if (brace && has_brace) brace = false;
592 } while (p->flags.len > 1 && !new_else && (!BC_PARSE_IF_END(p) || brace) &&
593 !(has_brace = (BC_PARSE_BRACE(p) != 0)));
595 if (BC_ERR(p->flags.len == 1 && brace))
596 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
597 else if (brace && BC_PARSE_BRACE(p)) {
599 uint16_t flags = BC_PARSE_TOP_FLAG(p);
601 if (!(flags & (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_LOOP_INNER)) &&
602 !(flags & (BC_PARSE_FLAG_IF | BC_PARSE_FLAG_ELSE)) &&
603 !(flags & (BC_PARSE_FLAG_IF_END)))
605 bc_vec_pop(&p->flags);
610 static void bc_parse_startBody(BcParse *p, uint16_t flags) {
612 flags |= (BC_PARSE_TOP_FLAG(p) & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
613 flags |= BC_PARSE_FLAG_BODY;
614 bc_vec_push(&p->flags, &flags);
617 static void bc_parse_if(BcParse *p) {
620 uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
623 if (BC_ERR(p->l.t != BC_LEX_LPAREN))
624 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
627 bc_parse_expr_status(p, flags, bc_parse_next_rel);
628 if (BC_ERR(p->l.t != BC_LEX_RPAREN))
629 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
632 bc_parse_push(p, BC_INST_JUMP_ZERO);
634 idx = p->func->labels.len;
636 bc_parse_pushIndex(p, idx);
637 bc_parse_createExitLabel(p, idx, false);
638 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
641 static void bc_parse_else(BcParse *p) {
643 size_t idx = p->func->labels.len;
645 if (BC_ERR(!BC_PARSE_IF_END(p)))
646 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
648 bc_parse_push(p, BC_INST_JUMP);
649 bc_parse_pushIndex(p, idx);
653 bc_parse_createExitLabel(p, idx, false);
654 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
659 static void bc_parse_while(BcParse *p) {
662 uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
665 if (BC_ERR(p->l.t != BC_LEX_LPAREN))
666 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
669 bc_parse_createCondLabel(p, p->func->labels.len);
670 idx = p->func->labels.len;
671 bc_parse_createExitLabel(p, idx, true);
673 bc_parse_expr_status(p, flags, bc_parse_next_rel);
674 if (BC_ERR(p->l.t != BC_LEX_RPAREN))
675 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
678 bc_parse_push(p, BC_INST_JUMP_ZERO);
679 bc_parse_pushIndex(p, idx);
680 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
683 static void bc_parse_for(BcParse *p) {
685 size_t cond_idx, exit_idx, body_idx, update_idx;
688 if (BC_ERR(p->l.t != BC_LEX_LPAREN))
689 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
692 if (p->l.t != BC_LEX_SCOLON)
693 bc_parse_expr_status(p, 0, bc_parse_next_for);
694 else bc_parse_err(p, BC_ERR_POSIX_FOR);
696 if (BC_ERR(p->l.t != BC_LEX_SCOLON))
697 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
700 cond_idx = p->func->labels.len;
701 update_idx = cond_idx + 1;
702 body_idx = update_idx + 1;
703 exit_idx = body_idx + 1;
705 bc_parse_createLabel(p, p->func->code.len);
707 if (p->l.t != BC_LEX_SCOLON) {
708 uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL);
709 bc_parse_expr_status(p, flags, bc_parse_next_for);
713 // Set this for the next call to bc_parse_number.
714 // This is safe to set because the current token
715 // is a semicolon, which has no string requirement.
716 bc_vec_string(&p->l.str, sizeof(bc_parse_one) - 1, bc_parse_one);
719 bc_parse_err(p, BC_ERR_POSIX_FOR);
722 if (BC_ERR(p->l.t != BC_LEX_SCOLON))
723 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
727 bc_parse_push(p, BC_INST_JUMP_ZERO);
728 bc_parse_pushIndex(p, exit_idx);
729 bc_parse_push(p, BC_INST_JUMP);
730 bc_parse_pushIndex(p, body_idx);
732 bc_parse_createCondLabel(p, update_idx);
734 if (p->l.t != BC_LEX_RPAREN)
735 bc_parse_expr_status(p, 0, bc_parse_next_rel);
736 else bc_parse_err(p, BC_ERR_POSIX_FOR);
738 if (BC_ERR(p->l.t != BC_LEX_RPAREN))
739 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
740 bc_parse_push(p, BC_INST_JUMP);
741 bc_parse_pushIndex(p, cond_idx);
742 bc_parse_createLabel(p, p->func->code.len);
744 bc_parse_createExitLabel(p, exit_idx, true);
746 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
749 static void bc_parse_loopExit(BcParse *p, BcLexType type) {
754 if (BC_ERR(!BC_PARSE_LOOP(p))) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
756 if (type == BC_LEX_KW_BREAK) {
758 if (BC_ERR(!p->exits.len)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
760 i = p->exits.len - 1;
761 ip = bc_vec_item(&p->exits, i);
763 while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
764 assert(ip != NULL && (i < p->exits.len || ip->func));
767 else i = *((size_t*) bc_vec_top(&p->conds));
769 bc_parse_push(p, BC_INST_JUMP);
770 bc_parse_pushIndex(p, i);
775 static void bc_parse_func(BcParse *p) {
777 bool comma = false, voidfn;
783 if (BC_ERR(p->l.t != BC_LEX_NAME))
784 bc_parse_err(p, BC_ERR_PARSE_FUNC);
786 voidfn = (!BC_IS_POSIX && p->l.t == BC_LEX_NAME &&
787 !strcmp(p->l.str.v, "void"));
791 voidfn = (voidfn && p->l.t == BC_LEX_NAME);
794 bc_parse_err(p, BC_ERR_POSIX_VOID);
798 if (BC_ERR(p->l.t != BC_LEX_LPAREN))
799 bc_parse_err(p, BC_ERR_PARSE_FUNC);
801 assert(p->prog->fns.len == p->prog->fn_map.len);
805 idx = bc_program_insertFunc(p->prog, p->l.str.v);
810 bc_parse_updateFunc(p, idx);
811 p->func->voidfn = voidfn;
815 while (p->l.t != BC_LEX_RPAREN) {
817 BcType t = BC_TYPE_VAR;
819 if (p->l.t == BC_LEX_OP_MULTIPLY) {
822 bc_parse_err(p, BC_ERR_POSIX_REF);
825 if (BC_ERR(p->l.t != BC_LEX_NAME))
826 bc_parse_err(p, BC_ERR_PARSE_FUNC);
828 p->func->nparams += 1;
830 bc_vec_string(&p->buf, p->l.str.len, p->l.str.v);
834 if (p->l.t == BC_LEX_LBRACKET) {
836 if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY;
840 if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
841 bc_parse_err(p, BC_ERR_PARSE_FUNC);
845 else if (BC_ERR(t == BC_TYPE_REF))
846 bc_parse_verr(p, BC_ERR_PARSE_REF_VAR, p->buf.v);
848 comma = (p->l.t == BC_LEX_COMMA);
853 bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line);
856 if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
858 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER;
859 bc_parse_startBody(p, flags);
863 if (p->l.t != BC_LEX_LBRACE) bc_parse_err(p, BC_ERR_POSIX_BRACE);
866 static void bc_parse_auto(BcParse *p) {
870 if (BC_ERR(!p->auto_part)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
873 p->auto_part = comma = false;
874 one = p->l.t == BC_LEX_NAME;
876 while (p->l.t == BC_LEX_NAME) {
880 bc_vec_string(&p->buf, p->l.str.len - 1, p->l.str.v);
884 if (p->l.t == BC_LEX_LBRACKET) {
890 if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
891 bc_parse_err(p, BC_ERR_PARSE_FUNC);
895 else t = BC_TYPE_VAR;
897 comma = (p->l.t == BC_LEX_COMMA);
898 if (comma) bc_lex_next(&p->l);
900 bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line);
903 if (BC_ERR(comma)) bc_parse_err(p, BC_ERR_PARSE_FUNC);
904 if (BC_ERR(!one)) bc_parse_err(p, BC_ERR_PARSE_NO_AUTO);
905 if (BC_ERR(!bc_parse_isDelimiter(p)))
906 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
909 static void bc_parse_body(BcParse *p, bool brace) {
911 uint16_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
913 assert(flag_ptr != NULL);
914 assert(p->flags.len >= 2);
916 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
918 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
920 if (BC_ERR(!brace)) bc_parse_err(p, BC_ERR_PARSE_TOKEN);
922 p->auto_part = (p->l.t != BC_LEX_KW_AUTO);
926 // Make sure this is true to not get a parse error.
932 if (p->l.t == BC_LEX_NLINE) bc_lex_next(&p->l);
936 size_t len = p->flags.len;
942 if (!brace && !BC_PARSE_BODY(p) && len <= p->flags.len)
943 bc_parse_endBody(p, false);
947 static void bc_parse_stmt(BcParse *p) {
951 BcLexType type = p->l.t;
953 if (type == BC_LEX_NLINE) {
957 if (type == BC_LEX_KW_AUTO) {
962 p->auto_part = false;
964 if (type != BC_LEX_KW_ELSE) {
966 if (BC_PARSE_IF_END(p)) {
968 if (p->flags.len > 1 && !BC_PARSE_BRACE(p))
969 bc_parse_endBody(p, false);
972 else if (type == BC_LEX_LBRACE) {
974 if (!BC_PARSE_BODY(p)) {
975 bc_parse_startBody(p, BC_PARSE_FLAG_BRACE);
979 *(BC_PARSE_TOP_FLAG_PTR(p)) |= BC_PARSE_FLAG_BRACE;
981 bc_parse_body(p, true);
986 else if (BC_PARSE_BODY(p) && !BC_PARSE_BRACE(p)) {
987 bc_parse_body(p, false);
993 flags = BC_PARSE_TOP_FLAG(p);
999 case BC_LEX_OP_MINUS:
1000 case BC_LEX_OP_BOOL_NOT:
1004 case BC_LEX_KW_IBASE:
1005 case BC_LEX_KW_LAST:
1006 case BC_LEX_KW_LENGTH:
1007 case BC_LEX_KW_OBASE:
1008 case BC_LEX_KW_SCALE:
1009 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1010 case BC_LEX_KW_SEED:
1011 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1012 case BC_LEX_KW_SQRT:
1014 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1015 case BC_LEX_KW_IRAND:
1016 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1017 case BC_LEX_KW_READ:
1018 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1019 case BC_LEX_KW_RAND:
1020 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1021 case BC_LEX_KW_MAXIBASE:
1022 case BC_LEX_KW_MAXOBASE:
1023 case BC_LEX_KW_MAXSCALE:
1024 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1025 case BC_LEX_KW_MAXRAND:
1026 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1028 bc_parse_expr_status(p, BC_PARSE_PRINT, bc_parse_next_expr);
1032 case BC_LEX_KW_ELSE:
1046 bc_parse_endBody(p, true);
1052 bc_parse_str(p, BC_INST_PRINT_STR);
1056 case BC_LEX_KW_BREAK:
1057 case BC_LEX_KW_CONTINUE:
1059 bc_parse_loopExit(p, p->l.t);
1069 case BC_LEX_KW_HALT:
1071 bc_parse_push(p, BC_INST_HALT);
1082 case BC_LEX_KW_LIMITS:
1084 bc_vm_printf("BC_LONG_BIT = %lu\n", (ulong) BC_LONG_BIT);
1085 bc_vm_printf("BC_BASE_DIGS = %lu\n", (ulong) BC_BASE_DIGS);
1086 bc_vm_printf("BC_BASE_POW = %lu\n", (ulong) BC_BASE_POW);
1087 bc_vm_printf("BC_OVERFLOW_MAX = %lu\n", (ulong) BC_NUM_BIGDIG_MAX);
1089 bc_vm_printf("BC_BASE_MAX = %lu\n", BC_MAX_OBASE);
1090 bc_vm_printf("BC_DIM_MAX = %lu\n", BC_MAX_DIM);
1091 bc_vm_printf("BC_SCALE_MAX = %lu\n", BC_MAX_SCALE);
1092 bc_vm_printf("BC_STRING_MAX = %lu\n", BC_MAX_STRING);
1093 bc_vm_printf("BC_NAME_MAX = %lu\n", BC_MAX_NAME);
1094 bc_vm_printf("BC_NUM_MAX = %lu\n", BC_MAX_NUM);
1095 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1096 bc_vm_printf("BC_RAND_MAX = %lu\n", BC_MAX_RAND);
1097 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1098 bc_vm_printf("MAX Exponent = %lu\n", BC_MAX_EXP);
1099 bc_vm_printf("Number of vars = %lu\n", BC_MAX_VARS);
1106 case BC_LEX_KW_PRINT:
1112 case BC_LEX_KW_QUIT:
1114 // Quit is a compile-time command. We don't exit directly,
1115 // so the vm can clean up. Limits do the same thing.
1116 vm.status = BC_STATUS_QUIT;
1121 case BC_LEX_KW_RETURN:
1127 case BC_LEX_KW_WHILE:
1135 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1139 if (len == p->flags.len && flags == BC_PARSE_TOP_FLAG(p)) {
1140 if (BC_ERR(!bc_parse_isDelimiter(p)))
1141 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1144 // Make sure semicolons are eaten.
1145 while (p->l.t == BC_LEX_SCOLON) bc_lex_next(&p->l);
1148 void bc_parse_parse(BcParse *p) {
1154 if (BC_ERR(p->l.t == BC_LEX_EOF)) bc_parse_err(p, BC_ERR_PARSE_EOF);
1155 else if (p->l.t == BC_LEX_KW_DEFINE) {
1156 if (BC_ERR(BC_PARSE_NO_EXEC(p)))
1157 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1160 else bc_parse_stmt(p);
1164 if (BC_ERR(((vm.status && vm.status != BC_STATUS_QUIT) || vm.sig)))
1169 static BcParseStatus bc_parse_expr_err(BcParse *p, uint8_t flags,
1172 BcInst prev = BC_INST_PRINT;
1173 uchar inst = BC_INST_INVALID;
1174 BcLexType top, t = p->l.t;
1175 size_t nexprs = 0, ops_bgn = p->ops.len;
1176 uint32_t i, nparens, nrelops;
1177 bool pfirst, rprn, done, get_token, assign, bin_last, incdec, can_assign;
1179 assert(!(flags & BC_PARSE_PRINT) || !(flags & BC_PARSE_NEEDVAL));
1181 pfirst = (p->l.t == BC_LEX_LPAREN);
1182 nparens = nrelops = 0;
1183 rprn = done = get_token = assign = incdec = can_assign = false;
1186 // We want to eat newlines if newlines are not a valid ending token.
1187 // This is for spacing in things like for loop headers.
1188 if (!(flags & BC_PARSE_NOREAD)) {
1189 while ((t = p->l.t) == BC_LEX_NLINE) bc_lex_next(&p->l);
1192 for (; !done && BC_PARSE_EXPR(t); t = p->l.t)
1199 if (BC_ERR(incdec)) bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
1200 bc_parse_incdec(p, &prev, &can_assign, &nexprs, flags);
1201 rprn = get_token = bin_last = false;
1203 flags &= ~(BC_PARSE_ARRAY);
1207 #if BC_ENABLE_EXTRA_MATH
1208 case BC_LEX_OP_TRUNC:
1210 if (BC_ERR(!BC_PARSE_LEAF(prev, bin_last, rprn)))
1211 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1213 // I can just add the instruction because
1214 // negative will already be taken care of.
1215 bc_parse_push(p, BC_INST_TRUNC);
1216 rprn = can_assign = incdec = false;
1218 flags &= ~(BC_PARSE_ARRAY);
1221 #endif // BC_ENABLE_EXTRA_MATH
1223 case BC_LEX_OP_MINUS:
1225 bc_parse_minus(p, &prev, ops_bgn, rprn, bin_last, &nexprs);
1226 rprn = get_token = can_assign = false;
1227 bin_last = (prev == BC_INST_MINUS);
1228 if (bin_last) incdec = false;
1229 flags &= ~(BC_PARSE_ARRAY);
1233 case BC_LEX_OP_ASSIGN_POWER:
1234 case BC_LEX_OP_ASSIGN_MULTIPLY:
1235 case BC_LEX_OP_ASSIGN_DIVIDE:
1236 case BC_LEX_OP_ASSIGN_MODULUS:
1237 case BC_LEX_OP_ASSIGN_PLUS:
1238 case BC_LEX_OP_ASSIGN_MINUS:
1239 #if BC_ENABLE_EXTRA_MATH
1240 case BC_LEX_OP_ASSIGN_PLACES:
1241 case BC_LEX_OP_ASSIGN_LSHIFT:
1242 case BC_LEX_OP_ASSIGN_RSHIFT:
1243 #endif // BC_ENABLE_EXTRA_MATH
1244 case BC_LEX_OP_ASSIGN:
1246 if (!BC_PARSE_INST_VAR(prev))
1247 bc_parse_err(p, BC_ERR_PARSE_ASSIGN);
1252 case BC_LEX_OP_POWER:
1253 case BC_LEX_OP_MULTIPLY:
1254 case BC_LEX_OP_DIVIDE:
1255 case BC_LEX_OP_MODULUS:
1256 case BC_LEX_OP_PLUS:
1257 #if BC_ENABLE_EXTRA_MATH
1258 case BC_LEX_OP_PLACES:
1259 case BC_LEX_OP_LSHIFT:
1260 case BC_LEX_OP_RSHIFT:
1261 #endif // BC_ENABLE_EXTRA_MATH
1262 case BC_LEX_OP_REL_EQ:
1263 case BC_LEX_OP_REL_LE:
1264 case BC_LEX_OP_REL_GE:
1265 case BC_LEX_OP_REL_NE:
1266 case BC_LEX_OP_REL_LT:
1267 case BC_LEX_OP_REL_GT:
1268 case BC_LEX_OP_BOOL_NOT:
1269 case BC_LEX_OP_BOOL_OR:
1270 case BC_LEX_OP_BOOL_AND:
1272 if (BC_PARSE_OP_PREFIX(t)) {
1273 if (BC_ERR(!bin_last && !BC_PARSE_OP_PREFIX(p->l.last)))
1274 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1276 else if (BC_ERR(BC_PARSE_PREV_PREFIX(prev) || bin_last))
1277 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1279 nrelops += (t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT);
1280 prev = BC_PARSE_TOKEN_INST(t);
1281 bc_parse_operator(p, t, ops_bgn, &nexprs);
1282 rprn = incdec = can_assign = false;
1284 bin_last = !BC_PARSE_OP_PREFIX(t);
1285 flags &= ~(BC_PARSE_ARRAY);
1292 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
1293 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1296 rprn = incdec = can_assign = false;
1298 bc_vec_push(&p->ops, &t);
1305 // This needs to be a status. The error
1306 // is handled in bc_parse_expr_status().
1307 if (BC_ERR(p->l.last == BC_LEX_LPAREN))
1308 return BC_PARSE_STATUS_EMPTY_EXPR;
1310 if (BC_ERR(bin_last || BC_PARSE_PREV_PREFIX(prev)))
1311 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1321 get_token = bin_last = incdec = false;
1323 bc_parse_rightParen(p, &nexprs);
1330 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
1331 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1333 get_token = bin_last = false;
1334 bc_parse_name(p, &prev, &can_assign,
1335 flags & ~BC_PARSE_NOCALL);
1336 rprn = (prev == BC_INST_CALL);
1338 flags &= ~(BC_PARSE_ARRAY);
1345 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
1346 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1352 rprn = bin_last = can_assign = false;
1353 flags &= ~(BC_PARSE_ARRAY);
1358 case BC_LEX_KW_IBASE:
1359 case BC_LEX_KW_LAST:
1360 case BC_LEX_KW_OBASE:
1361 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1362 case BC_LEX_KW_SEED:
1363 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1365 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
1366 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1368 prev = t - BC_LEX_KW_LAST + BC_INST_LAST;
1369 bc_parse_push(p, prev);
1371 get_token = can_assign = true;
1372 rprn = bin_last = false;
1374 flags &= ~(BC_PARSE_ARRAY);
1379 case BC_LEX_KW_LENGTH:
1380 case BC_LEX_KW_SQRT:
1382 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1383 case BC_LEX_KW_IRAND:
1384 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1386 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
1387 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1389 bc_parse_builtin(p, t, flags, &prev);
1390 rprn = get_token = bin_last = incdec = can_assign = false;
1392 flags &= ~(BC_PARSE_ARRAY);
1397 case BC_LEX_KW_READ:
1398 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1399 case BC_LEX_KW_RAND:
1400 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1401 case BC_LEX_KW_MAXIBASE:
1402 case BC_LEX_KW_MAXOBASE:
1403 case BC_LEX_KW_MAXSCALE:
1404 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1405 case BC_LEX_KW_MAXRAND:
1406 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
1408 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
1409 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1410 else if (t == BC_LEX_KW_READ && BC_ERR(flags & BC_PARSE_NOREAD))
1411 bc_parse_err(p, BC_ERR_EXEC_REC_READ);
1413 prev = t - BC_LEX_KW_READ + BC_INST_READ;
1414 bc_parse_noArgBuiltin(p, prev);
1417 rprn = get_token = bin_last = incdec = can_assign = false;
1419 flags &= ~(BC_PARSE_ARRAY);
1424 case BC_LEX_KW_SCALE:
1426 if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn)))
1427 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1429 bc_parse_scale(p, &prev, &can_assign, flags);
1430 rprn = get_token = bin_last = false;
1432 flags &= ~(BC_PARSE_ARRAY);
1440 bc_parse_err(p, BC_ERR_PARSE_TOKEN);
1446 if (get_token) bc_lex_next(&p->l);
1449 while (p->ops.len > ops_bgn) {
1451 top = BC_PARSE_TOP_OP(p);
1452 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
1454 if (BC_ERR(top == BC_LEX_LPAREN || top == BC_LEX_RPAREN))
1455 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1457 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
1459 nexprs -= !BC_PARSE_OP_PREFIX(top);
1460 bc_vec_pop(&p->ops);
1465 if (BC_ERR(nexprs != 1)) bc_parse_err(p, BC_ERR_PARSE_EXPR);
1467 for (i = 0; i < next.len && t != next.tokens[i]; ++i);
1468 if (BC_ERR(i == next.len && !bc_parse_isDelimiter(p)))
1469 bc_parse_err(p, BC_ERR_PARSE_EXPR);
1471 if (!(flags & BC_PARSE_REL) && nrelops)
1472 bc_parse_err(p, BC_ERR_POSIX_REL_POS);
1473 else if ((flags & BC_PARSE_REL) && nrelops > 1)
1474 bc_parse_err(p, BC_ERR_POSIX_MULTIREL);
1476 if (!(flags & BC_PARSE_NEEDVAL) && !pfirst) {
1479 inst = *((uchar*) bc_vec_top(&p->func->code));
1480 inst += (BC_INST_ASSIGN_POWER_NO_VAL - BC_INST_ASSIGN_POWER);
1483 else if (incdec && !(flags & BC_PARSE_PRINT)) {
1484 inst = *((uchar*) bc_vec_top(&p->func->code));
1485 incdec = (inst <= BC_INST_DEC);
1486 inst = BC_INST_ASSIGN_PLUS_NO_VAL + (inst != BC_INST_INC &&
1487 inst != BC_INST_ASSIGN_PLUS);
1490 if (inst >= BC_INST_ASSIGN_POWER_NO_VAL &&
1491 inst <= BC_INST_ASSIGN_NO_VAL)
1493 bc_vec_pop(&p->func->code);
1494 if (incdec) bc_parse_push(p, BC_INST_ONE);
1495 bc_parse_push(p, inst);
1499 if ((flags & BC_PARSE_PRINT)) {
1500 if (pfirst || !assign) bc_parse_push(p, BC_INST_PRINT);
1502 else if (!(flags & BC_PARSE_NEEDVAL) &&
1503 (inst < BC_INST_ASSIGN_POWER_NO_VAL ||
1504 inst > BC_INST_ASSIGN_NO_VAL))
1506 bc_parse_push(p, BC_INST_POP);
1509 // We want to eat newlines if newlines are not a valid ending token.
1510 // This is for spacing in things like for loop headers.
1511 for (incdec = true, i = 0; i < next.len && incdec; ++i)
1512 incdec = (next.tokens[i] != BC_LEX_NLINE);
1514 while (p->l.t == BC_LEX_NLINE) bc_lex_next(&p->l);
1517 return BC_PARSE_STATUS_SUCCESS;
1520 void bc_parse_expr_status(BcParse *p, uint8_t flags, BcParseNext next) {
1522 BcParseStatus s = bc_parse_expr_err(p, flags, next);
1524 if (BC_ERR(s == BC_PARSE_STATUS_EMPTY_EXPR))
1525 bc_parse_err(p, BC_ERR_PARSE_EMPTY_EXPR);
1528 void bc_parse_expr(BcParse *p, uint8_t flags) {
1530 bc_parse_expr_status(p, flags, bc_parse_next_read);
1532 #endif // BC_ENABLED