#define bc_parse_push(p, i) (bc_vec_pushByte(&(p)->func->code, (char) (i)))
#define bc_parse_updateFunc(p, f) \
- ((p)->func = bc_vec_item(&(p)->prog->fns, ((p)->fidx = (f))))
+ ((p)->func = bc_vec_item(&G.prog.fns, ((p)->fidx = (f))))
#define BC_PARSE_REL (1 << 0)
#define BC_PARSE_PRINT (1 << 1)
BcVec ops;
- struct BcProgram *prog;
BcFunc *func;
size_t fidx;
typedef unsigned long (*BcProgramBuiltIn)(BcNum *);
-static void bc_program_addFunc(BcProgram *p, char *name, size_t *idx);
-static BcStatus bc_program_reset(BcProgram *p, BcStatus s);
+static void bc_program_addFunc(char *name, size_t *idx);
+static BcStatus bc_program_reset(BcStatus s);
#define BC_FLAG_X (1 << 0)
#define BC_FLAG_W (1 << 1)
static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
{
- bc_program_addFunc(p->prog, name, idx);
- p->func = bc_vec_item(&p->prog->fns, p->fidx);
+ bc_program_addFunc(name, idx);
+ p->func = bc_vec_item(&G.prog.fns, p->fidx);
}
static void bc_parse_pushName(BcParse *p, char *name)
static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
{
char *num = xstrdup(p->l.t.v.v);
- size_t idx = p->prog->consts.len;
+ size_t idx = G.prog.consts.len;
- bc_vec_push(&p->prog->consts, &num);
+ bc_vec_push(&G.prog.consts, &num);
bc_parse_push(p, BC_INST_NUM);
bc_parse_pushIndex(p, idx);
{
BcStatus s;
- p->func = bc_vec_item(&p->prog->fns, p->fidx);
+ p->func = bc_vec_item(&G.prog.fns, p->fidx);
if (!strcmp(text, "") && !BC_PARSE_CAN_EXEC(p)) {
p->l.t.t = BC_LEX_INVALID;
bc_vec_npop(&p->conds, p->conds.len);
bc_vec_npop(&p->ops, p->ops.len);
- return bc_program_reset(p->prog, s);
+ return bc_program_reset(s);
}
static void bc_parse_free(BcParse *p)
bc_lex_free(&p->l);
}
-static void bc_parse_create(BcParse *p, BcProgram *prog, size_t func,
+static void bc_parse_create(BcParse *p, size_t func,
BcParseParse parse, BcLexNext next)
{
memset(p, 0, sizeof(BcParse));
bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
p->parse = parse;
- p->prog = prog;
p->auto_part = (p->nbraces = 0);
bc_parse_updateFunc(p, func);
}
goto err;
}
- idx = bc_map_index(&p->prog->fn_map, &entry);
+ idx = bc_map_index(&G.prog.fn_map, &entry);
if (idx == BC_VEC_INVALID_IDX) {
name = xstrdup(entry.name);
bc_parse_addFunc(p, name, &idx);
- idx = bc_map_index(&p->prog->fn_map, &entry);
+ idx = bc_map_index(&G.prog.fn_map, &entry);
free(entry.name);
}
else
free(name);
- entry_ptr = bc_vec_item(&p->prog->fn_map, idx);
+ entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
bc_parse_pushIndex(p, entry_ptr->idx);
return bc_lex_next(&p->l);
char *str = xstrdup(p->l.t.v.v);
bc_parse_push(p, BC_INST_STR);
- bc_parse_pushIndex(p, p->prog->strs.len);
- bc_vec_push(&p->prog->strs, &str);
+ bc_parse_pushIndex(p, G.prog.strs.len);
+ bc_vec_push(&G.prog.strs, &str);
bc_parse_push(p, inst);
return bc_lex_next(&p->l);
return s;
}
-static void bc_parse_init(BcParse *p, BcProgram *prog, size_t func)
+static void bc_parse_init(BcParse *p, size_t func)
{
- bc_parse_create(p, prog, func, bc_parse_parse, bc_lex_token);
+ bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
}
static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
static BcStatus dc_parse_string(BcParse *p)
{
char *str, *name, b[DC_PARSE_BUF_LEN + 1];
- size_t idx, len = p->prog->strs.len;
+ size_t idx, len = G.prog.strs.len;
sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
name = xstrdup(b);
str = xstrdup(p->l.t.v.v);
bc_parse_push(p, BC_INST_STR);
bc_parse_pushIndex(p, len);
- bc_vec_push(&p->prog->strs, &str);
+ bc_vec_push(&G.prog.strs, &str);
bc_parse_addFunc(p, name, &idx);
return bc_lex_next(&p->l);
BcInst inst;
BcLexType t;
- if (flags & BC_PARSE_NOCALL) p->nbraces = p->prog->results.len;
+ if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
return s;
}
-static void dc_parse_init(BcParse *p, BcProgram *prog, size_t func)
+static void dc_parse_init(BcParse *p, size_t func)
{
- bc_parse_create(p, prog, func, dc_parse_parse, dc_lex_token);
+ bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
}
#endif // ENABLE_DC
-static void common_parse_init(BcParse *p, BcProgram *prog, size_t func)
+static void common_parse_init(BcParse *p, size_t func)
{
if (IS_BC) {
- bc_parse_init(p, prog, func);
+ bc_parse_init(p, func);
} else {
- dc_parse_init(p, prog, func);
+ dc_parse_init(p, func);
}
}
}
}
-static void bc_program_search(BcProgram *p, char *id, BcVec **ret, bool var)
+static void bc_program_search(char *id, BcVec **ret, bool var)
{
BcStatus s;
BcId e, *ptr;
BcResultData data;
bool new;
- v = var ? &p->vars : &p->arrs;
- map = var ? &p->var_map : &p->arr_map;
+ v = var ? &G.prog.vars : &G.prog.arrs;
+ map = var ? &G.prog.var_map : &G.prog.arr_map;
e.name = id;
e.idx = v->len;
ptr = bc_vec_item(map, i);
if (new) ptr->name = xstrdup(e.name);
*ret = bc_vec_item(v, ptr->idx);
+/// convert to better return convention
}
-static BcStatus bc_program_num(BcProgram *p, BcResult *r, BcNum **num, bool hex)
+static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex)
{
BcStatus s = BC_STATUS_SUCCESS;
case BC_RESULT_CONSTANT:
{
- char **str = bc_vec_item(&p->consts, r->d.id.idx);
+ char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
size_t base_t, len = strlen(*str);
BcNum *base;
bc_num_init(&r->d.n, len);
hex = hex && len == 1;
- base = hex ? &p->hexb : &p->ib;
- base_t = hex ? BC_NUM_MAX_IBASE : p->ib_t;
+ base = hex ? &G.prog.hexb : &G.prog.ib;
+ base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
s = bc_num_parse(&r->d.n, *str, base, base_t);
if (s) {
{
BcVec *v;
- bc_program_search(p, r->d.id.name, &v, r->t == BC_RESULT_VAR);
+ bc_program_search(r->d.id.name, &v, r->t == BC_RESULT_VAR);
if (r->t == BC_RESULT_ARRAY_ELEM) {
v = bc_vec_top(v);
case BC_RESULT_LAST:
{
- *num = &p->last;
+ *num = &G.prog.last;
break;
}
case BC_RESULT_ONE:
{
- *num = &p->one;
+ *num = &G.prog.one;
break;
}
}
return s;
}
-static BcStatus bc_program_binOpPrep(BcProgram *p, BcResult **l, BcNum **ln,
+static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln,
BcResult **r, BcNum **rn, bool assign)
{
BcStatus s;
bool hex;
BcResultType lt, rt;
- if (!BC_PROG_STACK(&p->results, 2)) return BC_STATUS_EXEC_STACK;
+ if (!BC_PROG_STACK(&G.prog.results, 2)) return BC_STATUS_EXEC_STACK;
- *r = bc_vec_item_rev(&p->results, 0);
- *l = bc_vec_item_rev(&p->results, 1);
+ *r = bc_vec_item_rev(&G.prog.results, 0);
+ *l = bc_vec_item_rev(&G.prog.results, 1);
lt = (*l)->t;
rt = (*r)->t;
hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
- s = bc_program_num(p, *l, ln, false);
+ s = bc_program_num(*l, ln, false);
if (s) return s;
- s = bc_program_num(p, *r, rn, hex);
+ s = bc_program_num(*r, rn, hex);
if (s) return s;
// We run this again under these conditions in case any vector has been
// reallocated out from under the BcNums or arrays we had.
if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
- s = bc_program_num(p, *l, ln, false);
+ s = bc_program_num(*l, ln, false);
if (s) return s;
}
return s;
}
-static void bc_program_binOpRetire(BcProgram *p, BcResult *r)
+static void bc_program_binOpRetire(BcResult *r)
{
r->t = BC_RESULT_TEMP;
- bc_vec_pop(&p->results);
- bc_vec_pop(&p->results);
- bc_vec_push(&p->results, r);
+ bc_vec_pop(&G.prog.results);
+ bc_vec_pop(&G.prog.results);
+ bc_vec_push(&G.prog.results, r);
}
-static BcStatus bc_program_prep(BcProgram *p, BcResult **r, BcNum **n)
+static BcStatus bc_program_prep(BcResult **r, BcNum **n)
{
BcStatus s;
- if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
- *r = bc_vec_top(&p->results);
+ if (!BC_PROG_STACK(&G.prog.results, 1)) return BC_STATUS_EXEC_STACK;
+ *r = bc_vec_top(&G.prog.results);
- s = bc_program_num(p, *r, n, false);
+ s = bc_program_num(*r, n, false);
if (s) return s;
if (!BC_PROG_NUM((*r), (*n))) return BC_STATUS_EXEC_BAD_TYPE;
return s;
}
-static void bc_program_retire(BcProgram *p, BcResult *r, BcResultType t)
+static void bc_program_retire(BcResult *r, BcResultType t)
{
r->t = t;
- bc_vec_pop(&p->results);
- bc_vec_push(&p->results, r);
+ bc_vec_pop(&G.prog.results);
+ bc_vec_push(&G.prog.results, r);
}
-static BcStatus bc_program_op(BcProgram *p, char inst)
+static BcStatus bc_program_op(char inst)
{
BcStatus s;
BcResult *opd1, *opd2, res;
BcNum *n1, *n2 = NULL;
- s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, false);
+ s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
if (s) return s;
bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
- s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, p->scale);
+ s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
if (s) goto err;
- bc_program_binOpRetire(p, &res);
+ bc_program_binOpRetire(&res);
return s;
static BcStatus bc_program_read(void)
{
- BcProgram *p = &G.prog;
-
BcStatus s;
BcParse parse;
BcVec buf;
BcInstPtr ip;
size_t i;
- BcFunc *f = bc_vec_item(&p->fns, BC_PROG_READ);
+ BcFunc *f = bc_vec_item(&G.prog.fns, BC_PROG_READ);
- for (i = 0; i < p->stack.len; ++i) {
- BcInstPtr *ip_ptr = bc_vec_item(&p->stack, i);
+ for (i = 0; i < G.prog.stack.len; ++i) {
+ BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
if (ip_ptr->func == BC_PROG_READ) return BC_STATUS_EXEC_REC_READ;
}
s = bc_read_line(&buf, "read> ");
if (s) goto io_err;
- common_parse_init(&parse, p, BC_PROG_READ);
+ common_parse_init(&parse, BC_PROG_READ);
bc_lex_file(&parse.l, bc_program_stdin_name);
s = bc_parse_text(&parse, buf.v);
ip.func = BC_PROG_READ;
ip.idx = 0;
- ip.len = p->results.len;
+ ip.len = G.prog.results.len;
// Update this pointer, just in case.
- f = bc_vec_item(&p->fns, BC_PROG_READ);
+ f = bc_vec_item(&G.prog.fns, BC_PROG_READ);
bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
- bc_vec_push(&p->stack, &ip);
+ bc_vec_push(&G.prog.stack, &ip);
exec_err:
bc_parse_free(&parse);
}
}
-static BcStatus bc_program_print(BcProgram *p, char inst, size_t idx)
+static BcStatus bc_program_print(char inst, size_t idx)
{
BcStatus s = BC_STATUS_SUCCESS;
BcResult *r;
BcNum *num = NULL;
bool pop = inst != BC_INST_PRINT;
- if (!BC_PROG_STACK(&p->results, idx + 1)) return BC_STATUS_EXEC_STACK;
+ if (!BC_PROG_STACK(&G.prog.results, idx + 1)) return BC_STATUS_EXEC_STACK;
- r = bc_vec_item_rev(&p->results, idx);
- s = bc_program_num(p, r, &num, false);
+ r = bc_vec_item_rev(&G.prog.results, idx);
+ s = bc_program_num(r, &num, false);
if (s) return s;
if (BC_PROG_NUM(r, num)) {
- s = bc_num_print(num, &p->ob, p->ob_t, !pop, &p->nchars, p->len);
- if (!s) bc_num_copy(&p->last, num);
+ s = bc_num_print(num, &G.prog.ob, G.prog.ob_t, !pop, &G.prog.nchars, G.prog.len);
+ if (!s) bc_num_copy(&G.prog.last, num);
}
else {
idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
- str = *((char **) bc_vec_item(&p->strs, idx));
+ str = *((char **) bc_vec_item(&G.prog.strs, idx));
if (inst == BC_INST_PRINT_STR) {
for (i = 0, len = strlen(str); i < len; ++i) {
char c = str[i];
bb_putchar(c);
- if (c == '\n') p->nchars = SIZE_MAX;
- ++p->nchars;
+ if (c == '\n') G.prog.nchars = SIZE_MAX;
+ ++G.prog.nchars;
}
}
else {
- bc_program_printString(str, &p->nchars);
+ bc_program_printString(str, &G.prog.nchars);
if (inst == BC_INST_PRINT) bb_putchar('\n');
}
}
- if (!s && pop) bc_vec_pop(&p->results);
+ if (!s && pop) bc_vec_pop(&G.prog.results);
return s;
}
-static BcStatus bc_program_negate(BcProgram *p)
+static BcStatus bc_program_negate(void)
{
BcStatus s;
BcResult res, *ptr;
BcNum *num = NULL;
- s = bc_program_prep(p, &ptr, &num);
+ s = bc_program_prep(&ptr, &num);
if (s) return s;
bc_num_init(&res.d.n, num->len);
bc_num_copy(&res.d.n, num);
if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
- bc_program_retire(p, &res, BC_RESULT_TEMP);
+ bc_program_retire(&res, BC_RESULT_TEMP);
return s;
}
-static BcStatus bc_program_logical(BcProgram *p, char inst)
+static BcStatus bc_program_logical(char inst)
{
BcStatus s;
BcResult *opd1, *opd2, res;
bool cond = 0;
ssize_t cmp;
- s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, false);
+ s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
if (s) return s;
bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
if (inst == BC_INST_BOOL_AND)
- cond = bc_num_cmp(n1, &p->zero) && bc_num_cmp(n2, &p->zero);
+ cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
else if (inst == BC_INST_BOOL_OR)
- cond = bc_num_cmp(n1, &p->zero) || bc_num_cmp(n2, &p->zero);
+ cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
else {
cmp = bc_num_cmp(n1, n2);
(cond ? bc_num_one : bc_num_zero)(&res.d.n);
- bc_program_binOpRetire(p, &res);
+ bc_program_binOpRetire(&res);
return s;
}
#if ENABLE_DC
-static BcStatus bc_program_assignStr(BcProgram *p, BcResult *r, BcVec *v,
+static BcStatus bc_program_assignStr(BcResult *r, BcVec *v,
bool push)
{
BcNum n2;
res.t = BC_RESULT_STR;
if (!push) {
- if (!BC_PROG_STACK(&p->results, 2)) return BC_STATUS_EXEC_STACK;
+ if (!BC_PROG_STACK(&G.prog.results, 2)) return BC_STATUS_EXEC_STACK;
bc_vec_pop(v);
- bc_vec_pop(&p->results);
+ bc_vec_pop(&G.prog.results);
}
- bc_vec_pop(&p->results);
+ bc_vec_pop(&G.prog.results);
- bc_vec_push(&p->results, &res);
+ bc_vec_push(&G.prog.results, &res);
bc_vec_push(v, &n2);
return BC_STATUS_SUCCESS;
}
#endif // ENABLE_DC
-static BcStatus bc_program_copyToVar(BcProgram *p, char *name, bool var)
+static BcStatus bc_program_copyToVar(char *name, bool var)
{
BcStatus s;
BcResult *ptr, r;
BcVec *v;
BcNum *n;
- if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
+ if (!BC_PROG_STACK(&G.prog.results, 1)) return BC_STATUS_EXEC_STACK;
- ptr = bc_vec_top(&p->results);
+ ptr = bc_vec_top(&G.prog.results);
if ((ptr->t == BC_RESULT_ARRAY) != !var) return BC_STATUS_EXEC_BAD_TYPE;
- bc_program_search(p, name, &v, var);
+ bc_program_search(name, &v, var);
#if ENABLE_DC
if (ptr->t == BC_RESULT_STR && !var) return BC_STATUS_EXEC_BAD_TYPE;
- if (ptr->t == BC_RESULT_STR) return bc_program_assignStr(p, ptr, v, true);
+ if (ptr->t == BC_RESULT_STR) return bc_program_assignStr(ptr, v, true);
#endif
- s = bc_program_num(p, ptr, &n, false);
+ s = bc_program_num(ptr, &n, false);
if (s) return s;
// Do this once more to make sure that pointers were not invalidated.
- bc_program_search(p, name, &v, var);
+ bc_program_search(name, &v, var);
if (var) {
bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
}
bc_vec_push(v, &r.d);
- bc_vec_pop(&p->results);
+ bc_vec_pop(&G.prog.results);
return s;
}
-static BcStatus bc_program_assign(BcProgram *p, char inst)
+static BcStatus bc_program_assign(char inst)
{
BcStatus s;
BcResult *left, *right, res;
unsigned long val, max;
bool assign = inst == BC_INST_ASSIGN, ib, sc;
- s = bc_program_binOpPrep(p, &left, &l, &right, &r, assign);
+ s = bc_program_binOpPrep(&left, &l, &right, &r, assign);
if (s) return s;
ib = left->t == BC_RESULT_IBASE;
BcVec *v;
if (left->t != BC_RESULT_VAR) return BC_STATUS_EXEC_BAD_TYPE;
- bc_program_search(p, left->d.id.name, &v, true);
+ bc_program_search(left->d.id.name, &v, true);
- return bc_program_assignStr(p, right, v, false);
+ return bc_program_assignStr(right, v, false);
}
#endif
return BC_STATUS_PARSE_BAD_ASSIGN;
#if ENABLE_BC
- if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &p->zero))
+ if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
return BC_STATUS_MATH_DIVIDE_BY_ZERO;
if (assign)
bc_num_copy(l, r);
else
- s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, p->scale);
+ s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
if (s) return s;
#else
if (sc) {
max = BC_MAX_SCALE;
- ptr = &p->scale;
+ ptr = &G.prog.scale;
}
else {
if (val < BC_NUM_MIN_BASE) return s;
max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
- ptr = ib ? &p->ib_t : &p->ob_t;
+ ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
}
if (val > max) return s;
- if (!sc) bc_num_copy(ib ? &p->ib : &p->ob, l);
+ if (!sc) bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
*ptr = (size_t) val;
s = BC_STATUS_SUCCESS;
bc_num_init(&res.d.n, l->len);
bc_num_copy(&res.d.n, l);
- bc_program_binOpRetire(p, &res);
+ bc_program_binOpRetire(&res);
return s;
}
-static BcStatus bc_program_pushVar(BcProgram *p, char *code, size_t *bgn,
+static BcStatus bc_program_pushVar(char *code, size_t *bgn,
bool pop, bool copy)
{
BcStatus s = BC_STATUS_SUCCESS;
r.d.id.name = name;
#if ENABLE_DC
- bc_program_search(p, name, &v, true);
+ bc_program_search(name, &v, true);
num = bc_vec_top(v);
if (pop || copy) {
}
#endif // ENABLE_DC
- bc_vec_push(&p->results, &r);
+ bc_vec_push(&G.prog.results, &r);
return s;
}
-static BcStatus bc_program_pushArray(BcProgram *p, char *code, size_t *bgn,
+static BcStatus bc_program_pushArray(char *code, size_t *bgn,
char inst)
{
BcStatus s = BC_STATUS_SUCCESS;
if (inst == BC_INST_ARRAY) {
r.t = BC_RESULT_ARRAY;
- bc_vec_push(&p->results, &r);
+ bc_vec_push(&G.prog.results, &r);
}
else {
BcResult *operand;
unsigned long temp;
- s = bc_program_prep(p, &operand, &num);
+ s = bc_program_prep(&operand, &num);
if (s) goto err;
s = bc_num_ulong(num, &temp);
if (s) goto err;
}
r.d.id.idx = (size_t) temp;
- bc_program_retire(p, &r, BC_RESULT_ARRAY_ELEM);
+ bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
}
err:
}
#if ENABLE_BC
-static BcStatus bc_program_incdec(BcProgram *p, char inst)
+static BcStatus bc_program_incdec(char inst)
{
BcStatus s;
BcResult *ptr, res, copy;
BcNum *num = NULL;
char inst2 = inst;
- s = bc_program_prep(p, &ptr, &num);
+ s = bc_program_prep(&ptr, &num);
if (s) return s;
if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
BC_INST_ASSIGN_PLUS :
BC_INST_ASSIGN_MINUS;
- bc_vec_push(&p->results, &res);
- bc_program_assign(p, inst);
+ bc_vec_push(&G.prog.results, &res);
+ bc_program_assign(inst);
if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
- bc_vec_pop(&p->results);
- bc_vec_push(&p->results, ©);
+ bc_vec_pop(&G.prog.results);
+ bc_vec_push(&G.prog.results, ©);
}
return s;
}
-static BcStatus bc_program_call(BcProgram *p, char *code, size_t *idx)
+static BcStatus bc_program_call(char *code, size_t *idx)
{
BcStatus s = BC_STATUS_SUCCESS;
BcInstPtr ip;
ip.idx = 0;
ip.func = bc_program_index(code, idx);
- func = bc_vec_item(&p->fns, ip.func);
+ func = bc_vec_item(&G.prog.fns, ip.func);
if (func->code.len == 0) return BC_STATUS_EXEC_UNDEFINED_FUNC;
if (nparams != func->nparams) return BC_STATUS_EXEC_MISMATCHED_PARAMS;
- ip.len = p->results.len - nparams;
+ ip.len = G.prog.results.len - nparams;
for (i = 0; i < nparams; ++i) {
a = bc_vec_item(&func->autos, nparams - 1 - i);
- arg = bc_vec_top(&p->results);
+ arg = bc_vec_top(&G.prog.results);
if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
return BC_STATUS_EXEC_BAD_TYPE;
- s = bc_program_copyToVar(p, a->name, a->idx);
+ s = bc_program_copyToVar(a->name, a->idx);
if (s) return s;
}
for (; i < func->autos.len; ++i) {
a = bc_vec_item(&func->autos, i);
- bc_program_search(p, a->name, &v, a->idx);
+ bc_program_search(a->name, &v, a->idx);
if (a->idx) {
bc_num_init(¶m.n, BC_NUM_DEF_SIZE);
}
}
- bc_vec_push(&p->stack, &ip);
+ bc_vec_push(&G.prog.stack, &ip);
return BC_STATUS_SUCCESS;
}
-static BcStatus bc_program_return(BcProgram *p, char inst)
+static BcStatus bc_program_return(char inst)
{
BcStatus s;
BcResult res;
BcFunc *f;
size_t i;
- BcInstPtr *ip = bc_vec_top(&p->stack);
+ BcInstPtr *ip = bc_vec_top(&G.prog.stack);
- if (!BC_PROG_STACK(&p->results, ip->len + inst == BC_INST_RET))
+ if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
return BC_STATUS_EXEC_STACK;
- f = bc_vec_item(&p->fns, ip->func);
+ f = bc_vec_item(&G.prog.fns, ip->func);
res.t = BC_RESULT_TEMP;
if (inst == BC_INST_RET) {
BcNum *num;
- BcResult *operand = bc_vec_top(&p->results);
+ BcResult *operand = bc_vec_top(&G.prog.results);
- s = bc_program_num(p, operand, &num, false);
+ s = bc_program_num(operand, &num, false);
if (s) return s;
bc_num_init(&res.d.n, num->len);
bc_num_copy(&res.d.n, num);
BcVec *v;
BcId *a = bc_vec_item(&f->autos, i);
- bc_program_search(p, a->name, &v, a->idx);
+ bc_program_search(a->name, &v, a->idx);
bc_vec_pop(v);
}
- bc_vec_npop(&p->results, p->results.len - ip->len);
- bc_vec_push(&p->results, &res);
- bc_vec_pop(&p->stack);
+ bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
+ bc_vec_push(&G.prog.results, &res);
+ bc_vec_pop(&G.prog.stack);
return BC_STATUS_SUCCESS;
}
return len;
}
-static BcStatus bc_program_builtin(BcProgram *p, char inst)
+static BcStatus bc_program_builtin(char inst)
{
BcStatus s;
BcResult *opnd;
BcResult res;
bool len = inst == BC_INST_LENGTH;
- if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
- opnd = bc_vec_top(&p->results);
+ if (!BC_PROG_STACK(&G.prog.results, 1)) return BC_STATUS_EXEC_STACK;
+ opnd = bc_vec_top(&G.prog.results);
- s = bc_program_num(p, opnd, &num, false);
+ s = bc_program_num(opnd, &num, false);
if (s) return s;
#if ENABLE_DC
bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
- if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, p->scale);
+ if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale);
#if ENABLE_BC
else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
s = bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
char **str;
size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
- str = bc_vec_item(&p->strs, idx);
+ str = bc_vec_item(&G.prog.strs, idx);
s = bc_num_ulong2num(&res.d.n, strlen(*str));
if (s) goto err;
}
if (s) goto err;
}
- bc_program_retire(p, &res, BC_RESULT_TEMP);
+ bc_program_retire(&res, BC_RESULT_TEMP);
return s;
}
#if ENABLE_DC
-static BcStatus bc_program_divmod(BcProgram *p)
+static BcStatus bc_program_divmod(void)
{
BcStatus s;
BcResult *opd1, *opd2, res, res2;
BcNum *n1, *n2 = NULL;
- s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, false);
+ s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
if (s) return s;
bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
bc_num_init(&res2.d.n, n2->len);
- s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, p->scale);
+ s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
if (s) goto err;
- bc_program_binOpRetire(p, &res2);
+ bc_program_binOpRetire(&res2);
res.t = BC_RESULT_TEMP;
- bc_vec_push(&p->results, &res);
+ bc_vec_push(&G.prog.results, &res);
return s;
return s;
}
-static BcStatus bc_program_modexp(BcProgram *p)
+static BcStatus bc_program_modexp(void)
{
BcStatus s;
BcResult *r1, *r2, *r3, res;
BcNum *n1, *n2, *n3;
- if (!BC_PROG_STACK(&p->results, 3)) return BC_STATUS_EXEC_STACK;
- s = bc_program_binOpPrep(p, &r2, &n2, &r3, &n3, false);
+ if (!BC_PROG_STACK(&G.prog.results, 3)) return BC_STATUS_EXEC_STACK;
+ s = bc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
if (s) return s;
- r1 = bc_vec_item_rev(&p->results, 2);
- s = bc_program_num(p, r1, &n1, false);
+ r1 = bc_vec_item_rev(&G.prog.results, 2);
+ s = bc_program_num(r1, &n1, false);
if (s) return s;
if (!BC_PROG_NUM(r1, n1)) return BC_STATUS_EXEC_BAD_TYPE;
if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
if (r1->t == r2->t) {
- s = bc_program_num(p, r2, &n2, false);
+ s = bc_program_num(r2, &n2, false);
if (s) return s;
}
if (r1->t == r3->t) {
- s = bc_program_num(p, r3, &n3, false);
+ s = bc_program_num(r3, &n3, false);
if (s) return s;
}
}
s = bc_num_modexp(n1, n2, n3, &res.d.n);
if (s) goto err;
- bc_vec_pop(&p->results);
- bc_program_binOpRetire(p, &res);
+ bc_vec_pop(&G.prog.results);
+ bc_program_binOpRetire(&res);
return s;
return s;
}
-static BcStatus bc_program_stackLen(BcProgram *p)
+static BcStatus bc_program_stackLen(void)
{
BcStatus s;
BcResult res;
- size_t len = p->results.len;
+ size_t len = G.prog.results.len;
res.t = BC_RESULT_TEMP;
bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
s = bc_num_ulong2num(&res.d.n, len);
if (s) goto err;
- bc_vec_push(&p->results, &res);
+ bc_vec_push(&G.prog.results, &res);
return s;
return s;
}
-static BcStatus bc_program_asciify(BcProgram *p)
+static BcStatus bc_program_asciify(void)
{
BcStatus s;
BcResult *r, res;
BcNum *num = NULL, n;
char *str, *str2, c;
- size_t len = p->strs.len, idx;
+ size_t len = G.prog.strs.len, idx;
unsigned long val;
- if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
- r = bc_vec_top(&p->results);
+ if (!BC_PROG_STACK(&G.prog.results, 1)) return BC_STATUS_EXEC_STACK;
+ r = bc_vec_top(&G.prog.results);
- s = bc_program_num(p, r, &num, false);
+ s = bc_program_num(r, &num, false);
if (s) return s;
if (BC_PROG_NUM(r, num)) {
bc_num_copy(&n, num);
bc_num_truncate(&n, n.rdx);
- s = bc_num_mod(&n, &p->strmb, &n, 0);
+ s = bc_num_mod(&n, &G.prog.strmb, &n, 0);
if (s) goto num_err;
s = bc_num_ulong(&n, &val);
if (s) goto num_err;
}
else {
idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
- str2 = *((char **) bc_vec_item(&p->strs, idx));
+ str2 = *((char **) bc_vec_item(&G.prog.strs, idx));
c = str2[0];
}
str[1] = '\0';
str2 = xstrdup(str);
- bc_program_addFunc(p, str2, &idx);
+ bc_program_addFunc(str2, &idx);
if (idx != len + BC_PROG_REQ_FUNCS) {
- for (idx = 0; idx < p->strs.len; ++idx) {
- if (!strcmp(*((char **) bc_vec_item(&p->strs, idx)), str)) {
+ for (idx = 0; idx < G.prog.strs.len; ++idx) {
+ if (!strcmp(*((char **) bc_vec_item(&G.prog.strs, idx)), str)) {
len = idx;
break;
}
free(str);
}
else
- bc_vec_push(&p->strs, &str);
+ bc_vec_push(&G.prog.strs, &str);
res.t = BC_RESULT_STR;
res.d.id.idx = len;
- bc_vec_pop(&p->results);
- bc_vec_push(&p->results, &res);
+ bc_vec_pop(&G.prog.results);
+ bc_vec_push(&G.prog.results, &res);
return BC_STATUS_SUCCESS;
return s;
}
-static BcStatus bc_program_printStream(BcProgram *p)
+static BcStatus bc_program_printStream(void)
{
BcStatus s;
BcResult *r;
size_t idx;
char *str;
- if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
- r = bc_vec_top(&p->results);
+ if (!BC_PROG_STACK(&G.prog.results, 1)) return BC_STATUS_EXEC_STACK;
+ r = bc_vec_top(&G.prog.results);
- s = bc_program_num(p, r, &n, false);
+ s = bc_program_num(r, &n, false);
if (s) return s;
if (BC_PROG_NUM(r, n))
- s = bc_num_stream(n, &p->strmb, &p->nchars, p->len);
+ s = bc_num_stream(n, &G.prog.strmb, &G.prog.nchars, G.prog.len);
else {
idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
- str = *((char **) bc_vec_item(&p->strs, idx));
+ str = *((char **) bc_vec_item(&G.prog.strs, idx));
printf("%s", str);
}
static BcStatus bc_program_nquit(void)
{
- BcProgram *p = &G.prog;
-
BcStatus s;
BcResult *opnd;
BcNum *num = NULL;
unsigned long val;
- s = bc_program_prep(p, &opnd, &num);
+ s = bc_program_prep(&opnd, &num);
if (s) return s;
s = bc_num_ulong(num, &val);
if (s) return s;
- bc_vec_pop(&p->results);
+ bc_vec_pop(&G.prog.results);
- if (p->stack.len < val)
+ if (G.prog.stack.len < val)
return BC_STATUS_EXEC_STACK;
- else if (p->stack.len == val)
+ else if (G.prog.stack.len == val)
return BC_STATUS_QUIT;
- bc_vec_npop(&p->stack, val);
+ bc_vec_npop(&G.prog.stack, val);
return s;
}
static BcStatus bc_program_execStr(char *code, size_t *bgn,
bool cond)
{
- BcProgram *p = &G.prog;
-
BcStatus s = BC_STATUS_SUCCESS;
BcResult *r;
char **str;
BcNum *n;
bool exec;
- if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
+ if (!BC_PROG_STACK(&G.prog.results, 1)) return BC_STATUS_EXEC_STACK;
- r = bc_vec_top(&p->results);
+ r = bc_vec_top(&G.prog.results);
if (cond) {
}
if (exec) {
- bc_program_search(p, name, &v, true);
+ bc_program_search(name, &v, true);
n = bc_vec_top(v);
}
if (r->t == BC_RESULT_STR)
sidx = r->d.id.idx;
else if (r->t == BC_RESULT_VAR) {
- s = bc_program_num(p, r, &n, false);
+ s = bc_program_num(r, &n, false);
if (s || !BC_PROG_STR(n)) goto exit;
sidx = n->rdx;
}
fidx = sidx + BC_PROG_REQ_FUNCS;
- str = bc_vec_item(&p->strs, sidx);
- f = bc_vec_item(&p->fns, fidx);
+ str = bc_vec_item(&G.prog.strs, sidx);
+ f = bc_vec_item(&G.prog.fns, fidx);
if (f->code.len == 0) {
- common_parse_init(&prs, p, fidx);
+ common_parse_init(&prs, fidx);
s = bc_parse_text(&prs, *str);
if (s) goto err;
s = common_parse_expr(&prs, BC_PARSE_NOCALL);
}
ip.idx = 0;
- ip.len = p->results.len;
+ ip.len = G.prog.results.len;
ip.func = fidx;
- bc_vec_pop(&p->results);
- bc_vec_push(&p->stack, &ip);
+ bc_vec_pop(&G.prog.results);
+ bc_vec_push(&G.prog.stack, &ip);
return BC_STATUS_SUCCESS;
err:
bc_parse_free(&prs);
- f = bc_vec_item(&p->fns, fidx);
+ f = bc_vec_item(&G.prog.fns, fidx);
bc_vec_npop(&f->code, f->code.len);
exit:
- bc_vec_pop(&p->results);
+ bc_vec_pop(&G.prog.results);
return s;
}
#endif // ENABLE_DC
-static BcStatus bc_program_pushGlobal(BcProgram *p, char inst)
+static BcStatus bc_program_pushGlobal(char inst)
{
BcStatus s;
BcResult res;
res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
if (inst == BC_INST_IBASE)
- val = (unsigned long) p->ib_t;
+ val = (unsigned long) G.prog.ib_t;
else if (inst == BC_INST_SCALE)
- val = (unsigned long) p->scale;
+ val = (unsigned long) G.prog.scale;
else
- val = (unsigned long) p->ob_t;
+ val = (unsigned long) G.prog.ob_t;
bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
s = bc_num_ulong2num(&res.d.n, val);
if (s) goto err;
- bc_vec_push(&p->results, &res);
+ bc_vec_push(&G.prog.results, &res);
return s;
return s;
}
-static void bc_program_addFunc(BcProgram *p, char *name, size_t *idx)
+static void bc_program_addFunc(char *name, size_t *idx)
{
BcStatus s;
BcId entry, *entry_ptr;
BcFunc f;
entry.name = name;
- entry.idx = p->fns.len;
+ entry.idx = G.prog.fns.len;
- s = bc_map_insert(&p->fn_map, &entry, idx);
+ s = bc_map_insert(&G.prog.fn_map, &entry, idx);
if (s) free(name);
- entry_ptr = bc_vec_item(&p->fn_map, *idx);
+ entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
*idx = entry_ptr->idx;
if (s == BC_STATUS_VEC_ITEM_EXISTS) {
- BcFunc *func = bc_vec_item(&p->fns, entry_ptr->idx);
+ BcFunc *func = bc_vec_item(&G.prog.fns, entry_ptr->idx);
// We need to reset these, so the function can be repopulated.
func->nparams = 0;
}
else {
bc_func_init(&f);
- bc_vec_push(&p->fns, &f);
+ bc_vec_push(&G.prog.fns, &f);
}
}
-static BcStatus bc_program_reset(BcProgram *p, BcStatus s)
+static BcStatus bc_program_reset(BcStatus s)
{
BcFunc *f;
BcInstPtr *ip;
- bc_vec_npop(&p->stack, p->stack.len - 1);
- bc_vec_npop(&p->results, p->results.len);
+ bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
+ bc_vec_npop(&G.prog.results, G.prog.results.len);
- f = bc_vec_item(&p->fns, 0);
- ip = bc_vec_top(&p->stack);
+ f = bc_vec_item(&G.prog.fns, 0);
+ ip = bc_vec_top(&G.prog.stack);
ip->idx = f->code.len;
if (!s && G_interrupt && !G.tty) return BC_STATUS_QUIT;
static BcStatus bc_program_exec(void)
{
- BcProgram *p = &G.prog;
-
BcStatus s = BC_STATUS_SUCCESS;
size_t idx;
BcResult r, *ptr;
BcNum *num;
- BcInstPtr *ip = bc_vec_top(&p->stack);
- BcFunc *func = bc_vec_item(&p->fns, ip->func);
+ BcInstPtr *ip = bc_vec_top(&G.prog.stack);
+ BcFunc *func = bc_vec_item(&G.prog.fns, ip->func);
char *code = func->code.v;
bool cond = false;
#if ENABLE_BC
case BC_INST_JUMP_ZERO:
{
- s = bc_program_prep(p, &ptr, &num);
+ s = bc_program_prep(&ptr, &num);
if (s) return s;
- cond = !bc_num_cmp(num, &p->zero);
- bc_vec_pop(&p->results);
+ cond = !bc_num_cmp(num, &G.prog.zero);
+ bc_vec_pop(&G.prog.results);
}
// Fallthrough.
case BC_INST_JUMP:
case BC_INST_CALL:
{
- s = bc_program_call(p, code, &ip->idx);
+ s = bc_program_call(code, &ip->idx);
break;
}
case BC_INST_INC_POST:
case BC_INST_DEC_POST:
{
- s = bc_program_incdec(p, inst);
+ s = bc_program_incdec(inst);
break;
}
case BC_INST_RET:
case BC_INST_RET0:
{
- s = bc_program_return(p, inst);
+ s = bc_program_return(inst);
break;
}
case BC_INST_REL_LT:
case BC_INST_REL_GT:
{
- s = bc_program_logical(p, inst);
+ s = bc_program_logical(inst);
break;
}
case BC_INST_VAR:
{
- s = bc_program_pushVar(p, code, &ip->idx, false, false);
+ s = bc_program_pushVar(code, &ip->idx, false, false);
break;
}
case BC_INST_ARRAY_ELEM:
case BC_INST_ARRAY:
{
- s = bc_program_pushArray(p, code, &ip->idx, inst);
+ s = bc_program_pushArray(code, &ip->idx, inst);
break;
}
case BC_INST_LAST:
{
r.t = BC_RESULT_LAST;
- bc_vec_push(&p->results, &r);
+ bc_vec_push(&G.prog.results, &r);
break;
}
case BC_INST_SCALE:
case BC_INST_OBASE:
{
- s = bc_program_pushGlobal(p, inst);
+ s = bc_program_pushGlobal(inst);
break;
}
case BC_INST_LENGTH:
case BC_INST_SQRT:
{
- s = bc_program_builtin(p, inst);
+ s = bc_program_builtin(inst);
break;
}
{
r.t = BC_RESULT_CONSTANT;
r.d.id.idx = bc_program_index(code, &ip->idx);
- bc_vec_push(&p->results, &r);
+ bc_vec_push(&G.prog.results, &r);
break;
}
case BC_INST_POP:
{
- if (!BC_PROG_STACK(&p->results, 1))
+ if (!BC_PROG_STACK(&G.prog.results, 1))
s = BC_STATUS_EXEC_STACK;
else
- bc_vec_pop(&p->results);
+ bc_vec_pop(&G.prog.results);
break;
}
case BC_INST_POP_EXEC:
{
- bc_vec_pop(&p->stack);
+ bc_vec_pop(&G.prog.stack);
break;
}
case BC_INST_PRINT_POP:
case BC_INST_PRINT_STR:
{
- s = bc_program_print(p, inst, 0);
+ s = bc_program_print(inst, 0);
break;
}
{
r.t = BC_RESULT_STR;
r.d.id.idx = bc_program_index(code, &ip->idx);
- bc_vec_push(&p->results, &r);
+ bc_vec_push(&G.prog.results, &r);
break;
}
case BC_INST_PLUS:
case BC_INST_MINUS:
{
- s = bc_program_op(p, inst);
+ s = bc_program_op(inst);
break;
}
case BC_INST_BOOL_NOT:
{
- s = bc_program_prep(p, &ptr, &num);
+ s = bc_program_prep(&ptr, &num);
if (s) return s;
bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
- (!bc_num_cmp(num, &p->zero) ? bc_num_one : bc_num_zero)(&r.d.n);
- bc_program_retire(p, &r, BC_RESULT_TEMP);
+ (!bc_num_cmp(num, &G.prog.zero) ? bc_num_one : bc_num_zero)(&r.d.n);
+ bc_program_retire(&r, BC_RESULT_TEMP);
break;
}
case BC_INST_NEG:
{
- s = bc_program_negate(p);
+ s = bc_program_negate();
break;
}
#endif
case BC_INST_ASSIGN:
{
- s = bc_program_assign(p, inst);
+ s = bc_program_assign(inst);
break;
}
#if ENABLE_DC
case BC_INST_MODEXP:
{
- s = bc_program_modexp(p);
+ s = bc_program_modexp();
break;
}
case BC_INST_DIVMOD:
{
- s = bc_program_divmod(p);
+ s = bc_program_divmod();
break;
}
case BC_INST_PRINT_STACK:
{
- for (idx = 0; !s && idx < p->results.len; ++idx)
- s = bc_program_print(p, BC_INST_PRINT, idx);
+ for (idx = 0; !s && idx < G.prog.results.len; ++idx)
+ s = bc_program_print(BC_INST_PRINT, idx);
break;
}
case BC_INST_CLEAR_STACK:
{
- bc_vec_npop(&p->results, p->results.len);
+ bc_vec_npop(&G.prog.results, G.prog.results.len);
break;
}
case BC_INST_STACK_LEN:
{
- s = bc_program_stackLen(p);
+ s = bc_program_stackLen();
break;
}
case BC_INST_DUPLICATE:
{
- if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
- ptr = bc_vec_top(&p->results);
+ if (!BC_PROG_STACK(&G.prog.results, 1)) return BC_STATUS_EXEC_STACK;
+ ptr = bc_vec_top(&G.prog.results);
bc_result_copy(&r, ptr);
- bc_vec_push(&p->results, &r);
+ bc_vec_push(&G.prog.results, &r);
break;
}
{
BcResult *ptr2;
- if (!BC_PROG_STACK(&p->results, 2)) return BC_STATUS_EXEC_STACK;
+ if (!BC_PROG_STACK(&G.prog.results, 2)) return BC_STATUS_EXEC_STACK;
- ptr = bc_vec_item_rev(&p->results, 0);
- ptr2 = bc_vec_item_rev(&p->results, 1);
+ ptr = bc_vec_item_rev(&G.prog.results, 0);
+ ptr2 = bc_vec_item_rev(&G.prog.results, 1);
memcpy(&r, ptr, sizeof(BcResult));
memcpy(ptr, ptr2, sizeof(BcResult));
memcpy(ptr2, &r, sizeof(BcResult));
case BC_INST_ASCIIFY:
{
- s = bc_program_asciify(p);
+ s = bc_program_asciify();
break;
}
case BC_INST_PRINT_STREAM:
{
- s = bc_program_printStream(p);
+ s = bc_program_printStream();
break;
}
case BC_INST_PUSH_VAR:
{
bool copy = inst == BC_INST_LOAD;
- s = bc_program_pushVar(p, code, &ip->idx, true, copy);
+ s = bc_program_pushVar(code, &ip->idx, true, copy);
break;
}
case BC_INST_PUSH_TO_VAR:
{
char *name = bc_program_name(code, &ip->idx);
- s = bc_program_copyToVar(p, name, true);
+ s = bc_program_copyToVar(name, true);
free(name);
break;
}
case BC_INST_QUIT:
{
- if (p->stack.len <= 2)
+ if (G.prog.stack.len <= 2)
s = BC_STATUS_QUIT;
else
- bc_vec_npop(&p->stack, 2);
+ bc_vec_npop(&G.prog.stack, 2);
break;
}
#endif // ENABLE_DC
}
- if ((s && s != BC_STATUS_QUIT) || G_interrupt) s = bc_program_reset(p, s);
+ if ((s && s != BC_STATUS_QUIT) || G_interrupt) s = bc_program_reset(s);
// If the stack has changed, pointers may be invalid.
- ip = bc_vec_top(&p->stack);
- func = bc_vec_item(&p->fns, ip->func);
+ ip = bc_vec_top(&G.prog.stack);
+ func = bc_vec_item(&G.prog.fns, ip->func);
code = func->code.v;
}
s = bc_program_exec();
if (!s && G.tty) fflush(stdout);
if (s && s != BC_STATUS_QUIT)
- s = bc_vm_error(bc_program_reset(&G.prog, s), G.prs.l.f, 0);
+ s = bc_vm_error(bc_program_reset(s), G.prs.l.f, 0);
}
return s;
}
#if ENABLE_FEATURE_CLEAN_UP
-static void bc_program_free(BcProgram *p)
+static void bc_program_free()
{
- bc_num_free(&p->ib);
- bc_num_free(&p->ob);
- bc_num_free(&p->hexb);
+ bc_num_free(&G.prog.ib);
+ bc_num_free(&G.prog.ob);
+ bc_num_free(&G.prog.hexb);
# if ENABLE_DC
- bc_num_free(&p->strmb);
+ bc_num_free(&G.prog.strmb);
# endif
- bc_vec_free(&p->fns);
- bc_vec_free(&p->fn_map);
- bc_vec_free(&p->vars);
- bc_vec_free(&p->var_map);
- bc_vec_free(&p->arrs);
- bc_vec_free(&p->arr_map);
- bc_vec_free(&p->strs);
- bc_vec_free(&p->consts);
- bc_vec_free(&p->results);
- bc_vec_free(&p->stack);
- bc_num_free(&p->last);
- bc_num_free(&p->zero);
- bc_num_free(&p->one);
+ bc_vec_free(&G.prog.fns);
+ bc_vec_free(&G.prog.fn_map);
+ bc_vec_free(&G.prog.vars);
+ bc_vec_free(&G.prog.var_map);
+ bc_vec_free(&G.prog.arrs);
+ bc_vec_free(&G.prog.arr_map);
+ bc_vec_free(&G.prog.strs);
+ bc_vec_free(&G.prog.consts);
+ bc_vec_free(&G.prog.results);
+ bc_vec_free(&G.prog.stack);
+ bc_num_free(&G.prog.last);
+ bc_num_free(&G.prog.zero);
+ bc_num_free(&G.prog.one);
}
static void bc_vm_free(void)
{
bc_vec_free(&G.files);
- bc_program_free(&G.prog);
+ bc_program_free();
bc_parse_free(&G.prs);
free(G.env_args);
}
bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
bc_map_init(&G.prog.fn_map);
- bc_program_addFunc(&G.prog, xstrdup(bc_func_main), &idx);
- bc_program_addFunc(&G.prog, xstrdup(bc_func_read), &idx);
+ bc_program_addFunc(xstrdup(bc_func_main), &idx);
+ bc_program_addFunc(xstrdup(bc_func_read), &idx);
bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
bc_map_init(&G.prog.var_map);
bc_program_init(len);
if (IS_BC) {
- bc_parse_init(&G.prs, &G.prog, BC_PROG_MAIN);
+ bc_parse_init(&G.prs, BC_PROG_MAIN);
} else {
- dc_parse_init(&G.prs, &G.prog, BC_PROG_MAIN);
+ dc_parse_init(&G.prs, BC_PROG_MAIN);
}
}