diff options
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/bc.c | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 450c21c60..3f57f4df8 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
| @@ -1182,6 +1182,16 @@ static void *bc_vec_item(const BcVec *v, size_t idx) | |||
| 1182 | return v->v + v->size * idx; | 1182 | return v->v + v->size * idx; |
| 1183 | } | 1183 | } |
| 1184 | 1184 | ||
| 1185 | static char** bc_program_str(size_t idx) | ||
| 1186 | { | ||
| 1187 | return bc_vec_item(&G.prog.strs, idx); | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | static BcFunc* bc_program_func(size_t idx) | ||
| 1191 | { | ||
| 1192 | return bc_vec_item(&G.prog.fns, idx); | ||
| 1193 | } | ||
| 1194 | |||
| 1185 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) | 1195 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1186 | { | 1196 | { |
| 1187 | return v->v + v->size * (v->len - idx - 1); | 1197 | return v->v + v->size * (v->len - idx - 1); |
| @@ -3593,7 +3603,7 @@ static void bc_program_addFunc(char *name, size_t *idx); | |||
| 3593 | static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) | 3603 | static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) |
| 3594 | { | 3604 | { |
| 3595 | bc_program_addFunc(name, idx); | 3605 | bc_program_addFunc(name, idx); |
| 3596 | p->func = bc_vec_item(&G.prog.fns, p->fidx); | 3606 | p->func = bc_program_func(p->fidx); |
| 3597 | } | 3607 | } |
| 3598 | 3608 | ||
| 3599 | #define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i)) | 3609 | #define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i)) |
| @@ -3639,7 +3649,7 @@ static BcStatus bc_parse_text(BcParse *p, const char *text) | |||
| 3639 | { | 3649 | { |
| 3640 | BcStatus s; | 3650 | BcStatus s; |
| 3641 | 3651 | ||
| 3642 | p->func = bc_vec_item(&G.prog.fns, p->fidx); | 3652 | p->func = bc_program_func(p->fidx); |
| 3643 | 3653 | ||
| 3644 | if (!text[0] && !BC_PARSE_CAN_EXEC(p)) { | 3654 | if (!text[0] && !BC_PARSE_CAN_EXEC(p)) { |
| 3645 | p->l.t.t = BC_LEX_INVALID; | 3655 | p->l.t.t = BC_LEX_INVALID; |
| @@ -3662,13 +3672,13 @@ static void bc_program_reset(void) | |||
| 3662 | bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1); | 3672 | bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1); |
| 3663 | bc_vec_pop_all(&G.prog.results); | 3673 | bc_vec_pop_all(&G.prog.results); |
| 3664 | 3674 | ||
| 3665 | f = bc_vec_item(&G.prog.fns, 0); | 3675 | f = bc_program_func(0); |
| 3666 | ip = bc_vec_top(&G.prog.stack); | 3676 | ip = bc_vec_top(&G.prog.stack); |
| 3667 | ip->idx = f->code.len; | 3677 | ip->idx = f->code.len; |
| 3668 | } | 3678 | } |
| 3669 | 3679 | ||
| 3670 | #define bc_parse_updateFunc(p, f) \ | 3680 | #define bc_parse_updateFunc(p, f) \ |
| 3671 | ((p)->func = bc_vec_item(&G.prog.fns, ((p)->fidx = (f)))) | 3681 | ((p)->func = bc_program_func((p)->fidx = (f))) |
| 3672 | 3682 | ||
| 3673 | // Called when bc/dc_parse_parse() detects a failure, | 3683 | // Called when bc/dc_parse_parse() detects a failure, |
| 3674 | // resets parsing structures. | 3684 | // resets parsing structures. |
| @@ -5541,7 +5551,7 @@ static BcStatus bc_program_read(void) | |||
| 5541 | BcVec buf; | 5551 | BcVec buf; |
| 5542 | BcInstPtr ip; | 5552 | BcInstPtr ip; |
| 5543 | size_t i; | 5553 | size_t i; |
| 5544 | BcFunc *f = bc_vec_item(&G.prog.fns, BC_PROG_READ); | 5554 | BcFunc *f = bc_program_func(BC_PROG_READ); |
| 5545 | 5555 | ||
| 5546 | for (i = 0; i < G.prog.stack.len; ++i) { | 5556 | for (i = 0; i < G.prog.stack.len; ++i) { |
| 5547 | BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i); | 5557 | BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i); |
| @@ -5576,7 +5586,7 @@ static BcStatus bc_program_read(void) | |||
| 5576 | ip.len = G.prog.results.len; | 5586 | ip.len = G.prog.results.len; |
| 5577 | 5587 | ||
| 5578 | // Update this pointer, just in case. | 5588 | // Update this pointer, just in case. |
| 5579 | f = bc_vec_item(&G.prog.fns, BC_PROG_READ); | 5589 | f = bc_program_func(BC_PROG_READ); |
| 5580 | 5590 | ||
| 5581 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); | 5591 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); |
| 5582 | bc_vec_push(&G.prog.stack, &ip); | 5592 | bc_vec_push(&G.prog.stack, &ip); |
| @@ -5725,7 +5735,7 @@ static BcStatus bc_program_print(char inst, size_t idx) | |||
| 5725 | else { | 5735 | else { |
| 5726 | 5736 | ||
| 5727 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; | 5737 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
| 5728 | str = *((char **) bc_vec_item(&G.prog.strs, idx)); | 5738 | str = *bc_program_str(idx); |
| 5729 | 5739 | ||
| 5730 | if (inst == BC_INST_PRINT_STR) { | 5740 | if (inst == BC_INST_PRINT_STR) { |
| 5731 | for (i = 0, len = strlen(str); i < len; ++i) { | 5741 | for (i = 0, len = strlen(str); i < len; ++i) { |
| @@ -6125,7 +6135,7 @@ static BcStatus bc_program_call(char *code, size_t *idx) | |||
| 6125 | 6135 | ||
| 6126 | ip.idx = 0; | 6136 | ip.idx = 0; |
| 6127 | ip.func = bc_program_index(code, idx); | 6137 | ip.func = bc_program_index(code, idx); |
| 6128 | func = bc_vec_item(&G.prog.fns, ip.func); | 6138 | func = bc_program_func(ip.func); |
| 6129 | 6139 | ||
| 6130 | if (func->code.len == 0) { | 6140 | if (func->code.len == 0) { |
| 6131 | return bc_error("undefined function"); | 6141 | return bc_error("undefined function"); |
| @@ -6179,7 +6189,7 @@ static BcStatus bc_program_return(char inst) | |||
| 6179 | if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET)) | 6189 | if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET)) |
| 6180 | return bc_error_stack_has_too_few_elements(); | 6190 | return bc_error_stack_has_too_few_elements(); |
| 6181 | 6191 | ||
| 6182 | f = bc_vec_item(&G.prog.fns, ip->func); | 6192 | f = bc_program_func(ip->func); |
| 6183 | res.t = BC_RESULT_TEMP; | 6193 | res.t = BC_RESULT_TEMP; |
| 6184 | 6194 | ||
| 6185 | if (inst == BC_INST_RET) { | 6195 | if (inst == BC_INST_RET) { |
| @@ -6265,7 +6275,7 @@ static BcStatus bc_program_builtin(char inst) | |||
| 6265 | char **str; | 6275 | char **str; |
| 6266 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; | 6276 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
| 6267 | 6277 | ||
| 6268 | str = bc_vec_item(&G.prog.strs, idx); | 6278 | str = bc_program_str(idx); |
| 6269 | bc_num_ulong2num(&res.d.n, strlen(*str)); | 6279 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
| 6270 | } | 6280 | } |
| 6271 | #endif | 6281 | #endif |
| @@ -6397,7 +6407,7 @@ static BcStatus bc_program_asciify(void) | |||
| 6397 | } | 6407 | } |
| 6398 | else { | 6408 | else { |
| 6399 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; | 6409 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
| 6400 | str2 = *((char **) bc_vec_item(&G.prog.strs, idx)); | 6410 | str2 = *bc_program_str(idx); |
| 6401 | c = str2[0]; | 6411 | c = str2[0]; |
| 6402 | } | 6412 | } |
| 6403 | 6413 | ||
| @@ -6411,7 +6421,7 @@ static BcStatus bc_program_asciify(void) | |||
| 6411 | if (idx != len + BC_PROG_REQ_FUNCS) { | 6421 | if (idx != len + BC_PROG_REQ_FUNCS) { |
| 6412 | 6422 | ||
| 6413 | for (idx = 0; idx < G.prog.strs.len; ++idx) { | 6423 | for (idx = 0; idx < G.prog.strs.len; ++idx) { |
| 6414 | if (!strcmp(*((char **) bc_vec_item(&G.prog.strs, idx)), str)) { | 6424 | if (strcmp(*bc_program_str(idx), str) == 0) { |
| 6415 | len = idx; | 6425 | len = idx; |
| 6416 | break; | 6426 | break; |
| 6417 | } | 6427 | } |
| @@ -6453,7 +6463,7 @@ static BcStatus bc_program_printStream(void) | |||
| 6453 | s = bc_num_stream(n, &G.prog.strmb); | 6463 | s = bc_num_stream(n, &G.prog.strmb); |
| 6454 | else { | 6464 | else { |
| 6455 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; | 6465 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
| 6456 | str = *((char **) bc_vec_item(&G.prog.strs, idx)); | 6466 | str = *bc_program_str(idx); |
| 6457 | printf("%s", str); | 6467 | printf("%s", str); |
| 6458 | } | 6468 | } |
| 6459 | 6469 | ||
| @@ -6553,8 +6563,8 @@ static BcStatus bc_program_execStr(char *code, size_t *bgn, | |||
| 6553 | 6563 | ||
| 6554 | fidx = sidx + BC_PROG_REQ_FUNCS; | 6564 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6555 | 6565 | ||
| 6556 | str = bc_vec_item(&G.prog.strs, sidx); | 6566 | str = bc_program_str(sidx); |
| 6557 | f = bc_vec_item(&G.prog.fns, fidx); | 6567 | f = bc_program_func(fidx); |
| 6558 | 6568 | ||
| 6559 | if (f->code.len == 0) { | 6569 | if (f->code.len == 0) { |
| 6560 | common_parse_init(&prs, fidx); | 6570 | common_parse_init(&prs, fidx); |
| @@ -6582,7 +6592,7 @@ static BcStatus bc_program_execStr(char *code, size_t *bgn, | |||
| 6582 | 6592 | ||
| 6583 | err: | 6593 | err: |
| 6584 | bc_parse_free(&prs); | 6594 | bc_parse_free(&prs); |
| 6585 | f = bc_vec_item(&G.prog.fns, fidx); | 6595 | f = bc_program_func(fidx); |
| 6586 | bc_vec_pop_all(&f->code); | 6596 | bc_vec_pop_all(&f->code); |
| 6587 | exit: | 6597 | exit: |
| 6588 | bc_vec_pop(&G.prog.results); | 6598 | bc_vec_pop(&G.prog.results); |
| @@ -6625,7 +6635,7 @@ static void bc_program_addFunc(char *name, size_t *idx) | |||
| 6625 | 6635 | ||
| 6626 | if (!inserted) { | 6636 | if (!inserted) { |
| 6627 | 6637 | ||
| 6628 | BcFunc *func = bc_vec_item(&G.prog.fns, entry_ptr->idx); | 6638 | BcFunc *func = bc_program_func(entry_ptr->idx); |
| 6629 | 6639 | ||
| 6630 | // We need to reset these, so the function can be repopulated. | 6640 | // We need to reset these, so the function can be repopulated. |
| 6631 | func->nparams = 0; | 6641 | func->nparams = 0; |
| @@ -6646,7 +6656,7 @@ static BcStatus bc_program_exec(void) | |||
| 6646 | BcResult r, *ptr; | 6656 | BcResult r, *ptr; |
| 6647 | BcNum *num; | 6657 | BcNum *num; |
| 6648 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); | 6658 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
| 6649 | BcFunc *func = bc_vec_item(&G.prog.fns, ip->func); | 6659 | BcFunc *func = bc_program_func(ip->func); |
| 6650 | char *code = func->code.v; | 6660 | char *code = func->code.v; |
| 6651 | bool cond = false; | 6661 | bool cond = false; |
| 6652 | 6662 | ||
| @@ -6956,7 +6966,7 @@ static BcStatus bc_program_exec(void) | |||
| 6956 | 6966 | ||
| 6957 | // If the stack has changed, pointers may be invalid. | 6967 | // If the stack has changed, pointers may be invalid. |
| 6958 | ip = bc_vec_top(&G.prog.stack); | 6968 | ip = bc_vec_top(&G.prog.stack); |
| 6959 | func = bc_vec_item(&G.prog.fns, ip->func); | 6969 | func = bc_program_func(ip->func); |
| 6960 | code = func->code.v; | 6970 | code = func->code.v; |
| 6961 | } | 6971 | } |
| 6962 | 6972 | ||
| @@ -7089,7 +7099,7 @@ static BcStatus bc_vm_file(const char *file) | |||
| 7089 | s = bc_vm_process(data); | 7099 | s = bc_vm_process(data); |
| 7090 | if (s) goto err; | 7100 | if (s) goto err; |
| 7091 | 7101 | ||
| 7092 | main_func = bc_vec_item(&G.prog.fns, BC_PROG_MAIN); | 7102 | main_func = bc_program_func(BC_PROG_MAIN); |
| 7093 | ip = bc_vec_item(&G.prog.stack, 0); | 7103 | ip = bc_vec_item(&G.prog.stack, 0); |
| 7094 | 7104 | ||
| 7095 | if (main_func->code.len < ip->idx) | 7105 | if (main_func->code.len < ip->idx) |
