aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--miscutils/bc.c92
1 files changed, 48 insertions, 44 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index fa24f0c9f..7fcf00aa9 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -2903,6 +2903,7 @@ static BC_STATUS zbc_lex_next(BcLex *l)
2903} 2903}
2904#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS) 2904#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
2905 2905
2906#if ENABLE_BC
2906static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l) 2907static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
2907{ 2908{
2908 if (l->t.t == BC_LEX_NLINE) 2909 if (l->t.t == BC_LEX_NLINE)
@@ -2921,6 +2922,7 @@ static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
2921 RETURN_STATUS(s); 2922 RETURN_STATUS(s);
2922} 2923}
2923#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS) 2924#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
2925#endif
2924 2926
2925static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text) 2927static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
2926{ 2928{
@@ -3481,6 +3483,7 @@ static void bc_parse_pushIndex(BcParse *p, size_t idx)
3481 } 3483 }
3482} 3484}
3483 3485
3486#if ENABLE_BC
3484static void bc_parse_pushJUMP(BcParse *p, size_t idx) 3487static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3485{ 3488{
3486 bc_parse_push(p, BC_INST_JUMP); 3489 bc_parse_push(p, BC_INST_JUMP);
@@ -3493,17 +3496,6 @@ static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3493 bc_parse_pushIndex(p, idx); 3496 bc_parse_pushIndex(p, idx);
3494} 3497}
3495 3498
3496static void bc_parse_pushNUM(BcParse *p)
3497{
3498 char *num = xstrdup(p->l.t.v.v);
3499 size_t idx = G.prog.consts.len;
3500
3501 bc_vec_push(&G.prog.consts, &num);
3502
3503 bc_parse_push(p, BC_INST_NUM);
3504 bc_parse_pushIndex(p, idx);
3505}
3506
3507static BC_STATUS bc_parse_pushSTR(BcParse *p) 3499static BC_STATUS bc_parse_pushSTR(BcParse *p)
3508{ 3500{
3509 char *str = xstrdup(p->l.t.v.v); 3501 char *str = xstrdup(p->l.t.v.v);
@@ -3515,6 +3507,18 @@ static BC_STATUS bc_parse_pushSTR(BcParse *p)
3515 RETURN_STATUS(zbc_lex_next(&p->l)); 3507 RETURN_STATUS(zbc_lex_next(&p->l));
3516} 3508}
3517#define bc_parse_pushSTR(...) (bc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) 3509#define bc_parse_pushSTR(...) (bc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
3510#endif
3511
3512static void bc_parse_pushNUM(BcParse *p)
3513{
3514 char *num = xstrdup(p->l.t.v.v);
3515 size_t idx = G.prog.consts.len;
3516
3517 bc_vec_push(&G.prog.consts, &num);
3518
3519 bc_parse_push(p, BC_INST_NUM);
3520 bc_parse_pushIndex(p, idx);
3521}
3518 3522
3519IF_BC(static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p);) 3523IF_BC(static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p);)
3520IF_DC(static BC_STATUS zdc_parse_parse(BcParse *p);) 3524IF_DC(static BC_STATUS zdc_parse_parse(BcParse *p);)
@@ -3602,6 +3606,39 @@ static void bc_parse_create(BcParse *p, size_t fidx)
3602 p->func = bc_program_func(fidx); 3606 p->func = bc_program_func(fidx);
3603} 3607}
3604 3608
3609// Note: takes ownership of 'name' (must be malloced)
3610static size_t bc_program_addFunc(char *name)
3611{
3612 size_t idx;
3613 BcId entry, *entry_ptr;
3614 BcFunc f;
3615 int inserted;
3616
3617 entry.name = name;
3618 entry.idx = G.prog.fns.len;
3619
3620 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3621 if (!inserted) free(name);
3622
3623 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3624 idx = entry_ptr->idx;
3625
3626 if (!inserted) {
3627 BcFunc *func = bc_program_func(entry_ptr->idx);
3628
3629 // We need to reset these, so the function can be repopulated.
3630 func->nparams = 0;
3631 bc_vec_pop_all(&func->autos);
3632 bc_vec_pop_all(&func->code);
3633 bc_vec_pop_all(&func->labels);
3634 } else {
3635 bc_func_init(&f);
3636 bc_vec_push(&G.prog.fns, &f);
3637 }
3638
3639 return idx;
3640}
3641
3605#if ENABLE_BC 3642#if ENABLE_BC
3606 3643
3607#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) 3644#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
@@ -3730,39 +3767,6 @@ static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
3730#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) 3767#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
3731 3768
3732// Note: takes ownership of 'name' (must be malloced) 3769// Note: takes ownership of 'name' (must be malloced)
3733static size_t bc_program_addFunc(char *name)
3734{
3735 size_t idx;
3736 BcId entry, *entry_ptr;
3737 BcFunc f;
3738 int inserted;
3739
3740 entry.name = name;
3741 entry.idx = G.prog.fns.len;
3742
3743 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3744 if (!inserted) free(name);
3745
3746 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3747 idx = entry_ptr->idx;
3748
3749 if (!inserted) {
3750 BcFunc *func = bc_program_func(entry_ptr->idx);
3751
3752 // We need to reset these, so the function can be repopulated.
3753 func->nparams = 0;
3754 bc_vec_pop_all(&func->autos);
3755 bc_vec_pop_all(&func->code);
3756 bc_vec_pop_all(&func->labels);
3757 } else {
3758 bc_func_init(&f);
3759 bc_vec_push(&G.prog.fns, &f);
3760 }
3761
3762 return idx;
3763}
3764
3765// Note: takes ownership of 'name' (must be malloced)
3766static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags) 3770static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
3767{ 3771{
3768 BcStatus s; 3772 BcStatus s;