bc: style edit, no code changes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-12-18 16:24:07 +01:00
parent 3f940c9c70
commit 7f2d59c38d

View File

@ -236,7 +236,6 @@ typedef struct BcNum {
#define BC_NUM_KARATSUBA_LEN (32) #define BC_NUM_KARATSUBA_LEN (32)
typedef enum BcInst { typedef enum BcInst {
#if ENABLE_BC #if ENABLE_BC
BC_INST_INC_PRE, BC_INST_INC_PRE,
BC_INST_DEC_PRE, BC_INST_DEC_PRE,
@ -333,7 +332,6 @@ typedef enum BcInst {
BC_INST_INVALID = -1, BC_INST_INVALID = -1,
#endif #endif
} BcInst; } BcInst;
typedef struct BcId { typedef struct BcId {
@ -349,7 +347,6 @@ typedef struct BcFunc {
} BcFunc; } BcFunc;
typedef enum BcResultType { typedef enum BcResultType {
BC_RESULT_TEMP, BC_RESULT_TEMP,
BC_RESULT_VAR, BC_RESULT_VAR,
@ -367,7 +364,6 @@ typedef enum BcResultType {
BC_RESULT_ONE, BC_RESULT_ONE,
BC_RESULT_OBASE, BC_RESULT_OBASE,
} BcResultType; } BcResultType;
typedef union BcResultData { typedef union BcResultData {
@ -675,7 +671,6 @@ typedef struct BcParse {
} BcParse; } BcParse;
typedef struct BcProgram { typedef struct BcProgram {
size_t len; size_t len;
size_t scale; size_t scale;
@ -709,7 +704,6 @@ typedef struct BcProgram {
BcNum one; BcNum one;
size_t nchars; size_t nchars;
} BcProgram; } BcProgram;
#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n)) #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
@ -1124,7 +1118,6 @@ static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
if (idx == v->len) if (idx == v->len)
bc_vec_push(v, data); bc_vec_push(v, data);
else { else {
char *ptr; char *ptr;
if (v->len == v->cap) bc_vec_grow(v, 1); if (v->len == v->cap) bc_vec_grow(v, 1);
@ -1210,7 +1203,6 @@ static size_t bc_map_find(const BcVec *v, const void *ptr)
size_t low = 0, high = v->len; size_t low = 0, high = v->len;
while (low < high) { while (low < high) {
size_t mid = (low + high) / 2; size_t mid = (low + high) / 2;
BcId *id = bc_vec_item(v, mid); BcId *id = bc_vec_item(v, mid);
int result = bc_id_cmp(ptr, id); int result = bc_id_cmp(ptr, id);
@ -1421,7 +1413,6 @@ static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
if (n->neg) RETURN_STATUS(bc_error("negative number")); if (n->neg) RETURN_STATUS(bc_error("negative number"));
for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
unsigned long prev = result, powprev = pow; unsigned long prev = result, powprev = pow;
result += ((unsigned long) n->num[i]) * pow; result += ((unsigned long) n->num[i]) * pow;
@ -1461,8 +1452,7 @@ static void bc_num_ulong2num(BcNum *n, unsigned long val)
} }
} }
static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
size_t len)
{ {
size_t i, j; size_t i, j;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
@ -1556,7 +1546,6 @@ static void bc_num_extend(BcNum *n, size_t places)
size_t len = n->len + places; size_t len = n->len + places;
if (places != 0) { if (places != 0) {
if (n->cap < len) bc_num_expand(n, len); if (n->cap < len) bc_num_expand(n, len);
memmove(n->num + places, n->num, sizeof(BcDig) * n->len); memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
@ -1591,15 +1580,13 @@ static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
BcNum *restrict b) BcNum *restrict b)
{ {
if (idx < n->len) { if (idx < n->len) {
b->len = n->len - idx; b->len = n->len - idx;
a->len = idx; a->len = idx;
a->rdx = b->rdx = 0; a->rdx = b->rdx = 0;
memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
memcpy(a->num, n->num, idx * sizeof(BcDig)); memcpy(a->num, n->num, idx * sizeof(BcDig));
} } else {
else {
bc_num_zero(b); bc_num_zero(b);
bc_num_copy(a, n); bc_num_copy(a, n);
} }
@ -1869,8 +1856,7 @@ static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
s = zbc_num_add(&z0, &z1, &temp, 0); s = zbc_num_add(&z0, &z1, &temp, 0);
if (s) goto err; if (s) goto err;
s = zbc_num_add(&temp, &z2, c, 0); s = zbc_num_add(&temp, &z2, c, 0);
err:
err:
bc_num_free(&temp); bc_num_free(&temp);
bc_num_free(&z2); bc_num_free(&z2);
bc_num_free(&z1); bc_num_free(&z1);
@ -1919,8 +1905,7 @@ static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size
c->rdx = maxrdx; c->rdx = maxrdx;
bc_num_retireMul(c, scale, a->neg, b->neg); bc_num_retireMul(c, scale, a->neg, b->neg);
err:
err:
bc_num_free(&cpb); bc_num_free(&cpb);
bc_num_free(&cpa); bc_num_free(&cpa);
RETURN_STATUS(s); RETURN_STATUS(s);
@ -2037,8 +2022,7 @@ static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
neg = d->neg; neg = d->neg;
bc_num_retireMul(d, ts, a->neg, b->neg); bc_num_retireMul(d, ts, a->neg, b->neg);
d->neg = neg; d->neg = neg;
err:
err:
bc_num_free(&temp); bc_num_free(&temp);
RETURN_STATUS(s); RETURN_STATUS(s);
} }
@ -2311,13 +2295,13 @@ static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
if (s) goto err; if (s) goto err;
if (n->len != 0) { if (n->len != 0) {
if (n->rdx < digits) bc_num_extend(n, digits - n->rdx); if (n->rdx < digits)
bc_num_extend(n, digits - n->rdx);
} else } else
bc_num_zero(n); bc_num_zero(n);
err:
err:
bc_num_free(&result); bc_num_free(&result);
int_err: int_err:
bc_num_free(&base); bc_num_free(&base);
bc_num_free(&mult); bc_num_free(&mult);
bc_num_free(&temp); bc_num_free(&temp);
@ -2353,9 +2337,11 @@ static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
if (a->len == 0) { if (a->len == 0) {
bc_num_setToZero(b, scale); bc_num_setToZero(b, scale);
RETURN_STATUS(BC_STATUS_SUCCESS); RETURN_STATUS(BC_STATUS_SUCCESS);
} else if (a->neg) { }
if (a->neg) {
RETURN_STATUS(bc_error("negative number")); RETURN_STATUS(bc_error("negative number"));
} else if (BC_NUM_ONE(a)) { }
if (BC_NUM_ONE(a)) {
bc_num_one(b); bc_num_one(b);
bc_num_extend(b, scale); bc_num_extend(b, scale);
RETURN_STATUS(BC_STATUS_SUCCESS); RETURN_STATUS(BC_STATUS_SUCCESS);
@ -3767,8 +3753,7 @@ static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
bc_parse_pushIndex(p, entry_ptr->idx); bc_parse_pushIndex(p, entry_ptr->idx);
RETURN_STATUS(zbc_lex_next(&p->l)); RETURN_STATUS(zbc_lex_next(&p->l));
err:
err:
free(name); free(name);
RETURN_STATUS(s); RETURN_STATUS(s);
} }
@ -3804,8 +3789,7 @@ static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
bc_parse_push(p, *type); bc_parse_push(p, *type);
bc_parse_pushName(p, name); bc_parse_pushName(p, name);
free(name); free(name);
} } else if (p->l.t.t == BC_LEX_LPAREN) {
else if (p->l.t.t == BC_LEX_LPAREN) {
if (flags & BC_PARSE_NOCALL) { if (flags & BC_PARSE_NOCALL) {
s = bc_error_bad_token(); s = bc_error_bad_token();
goto err; goto err;
@ -3820,8 +3804,7 @@ static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
} }
RETURN_STATUS(s); RETURN_STATUS(s);
err:
err:
free(name); free(name);
RETURN_STATUS(s); RETURN_STATUS(s);
} }
@ -3908,15 +3891,14 @@ static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
char inst; char inst;
BcInst etype = *prev; BcInst etype = *prev;
if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM || if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM
etype == BC_INST_SCALE || etype == BC_INST_LAST || || etype == BC_INST_SCALE || etype == BC_INST_LAST
etype == BC_INST_IBASE || etype == BC_INST_OBASE) || etype == BC_INST_IBASE || etype == BC_INST_OBASE
{ ) {
*prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
bc_parse_push(p, inst); bc_parse_push(p, inst);
s = zbc_lex_next(&p->l); s = zbc_lex_next(&p->l);
} } else {
else {
*prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
*paren_expr = true; *paren_expr = true;
@ -4339,8 +4321,7 @@ static BC_STATUS zbc_parse_funcdef(BcParse *p)
dbg_lex_done("%s:%d done", __func__, __LINE__); dbg_lex_done("%s:%d done", __func__, __LINE__);
RETURN_STATUS(s); RETURN_STATUS(s);
err:
err:
dbg_lex_done("%s:%d done (error)", __func__, __LINE__); dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
free(name); free(name);
RETURN_STATUS(s); RETURN_STATUS(s);
@ -4397,8 +4378,7 @@ static BC_STATUS zbc_parse_auto(BcParse *p)
dbg_lex_done("%s:%d done", __func__, __LINE__); dbg_lex_done("%s:%d done", __func__, __LINE__);
RETURN_STATUS(zbc_lex_next(&p->l)); RETURN_STATUS(zbc_lex_next(&p->l));
err:
err:
free(name); free(name);
dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
RETURN_STATUS(s); RETURN_STATUS(s);
@ -4561,23 +4541,16 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) { for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
switch (t) { switch (t) {
case BC_LEX_OP_INC: case BC_LEX_OP_INC:
case BC_LEX_OP_DEC: case BC_LEX_OP_DEC:
{
s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
rprn = get_token = bin_last = false; rprn = get_token = bin_last = false;
break; break;
}
case BC_LEX_OP_MINUS: case BC_LEX_OP_MINUS:
{
s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
rprn = get_token = false; rprn = get_token = false;
bin_last = prev == BC_INST_MINUS; bin_last = prev == BC_INST_MINUS;
break; break;
}
case BC_LEX_OP_ASSIGN_POWER: case BC_LEX_OP_ASSIGN_POWER:
case BC_LEX_OP_ASSIGN_MULTIPLY: case BC_LEX_OP_ASSIGN_MULTIPLY:
case BC_LEX_OP_ASSIGN_DIVIDE: case BC_LEX_OP_ASSIGN_DIVIDE:
@ -4585,18 +4558,16 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
case BC_LEX_OP_ASSIGN_PLUS: case BC_LEX_OP_ASSIGN_PLUS:
case BC_LEX_OP_ASSIGN_MINUS: case BC_LEX_OP_ASSIGN_MINUS:
case BC_LEX_OP_ASSIGN: case BC_LEX_OP_ASSIGN:
{ if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM
if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM && && prev != BC_INST_SCALE && prev != BC_INST_IBASE
prev != BC_INST_SCALE && prev != BC_INST_IBASE && && prev != BC_INST_OBASE && prev != BC_INST_LAST
prev != BC_INST_OBASE && prev != BC_INST_LAST) ) {
{
s = bc_error("bad assignment:" s = bc_error("bad assignment:"
" left side must be variable" " left side must be variable"
" or array element" " or array element"
); // note: shared string ); // note: shared string
break; break;
} }
}
// Fallthrough. // Fallthrough.
case BC_LEX_OP_POWER: case BC_LEX_OP_POWER:
case BC_LEX_OP_MULTIPLY: case BC_LEX_OP_MULTIPLY:
@ -4612,74 +4583,53 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
case BC_LEX_OP_BOOL_NOT: case BC_LEX_OP_BOOL_NOT:
case BC_LEX_OP_BOOL_OR: case BC_LEX_OP_BOOL_OR:
case BC_LEX_OP_BOOL_AND: case BC_LEX_OP_BOOL_AND:
{
if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
|| (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
) { ) {
return bc_error_bad_expression(); return bc_error_bad_expression();
} }
nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
prev = BC_TOKEN_2_INST(t); prev = BC_TOKEN_2_INST(t);
bc_parse_operator(p, t, ops_bgn, &nexprs); bc_parse_operator(p, t, ops_bgn, &nexprs);
s = zbc_lex_next(&p->l); s = zbc_lex_next(&p->l);
rprn = get_token = false; rprn = get_token = false;
bin_last = t != BC_LEX_OP_BOOL_NOT; bin_last = t != BC_LEX_OP_BOOL_NOT;
break; break;
}
case BC_LEX_LPAREN: case BC_LEX_LPAREN:
{
if (BC_PARSE_LEAF(prev, rprn)) if (BC_PARSE_LEAF(prev, rprn))
return bc_error_bad_expression(); return bc_error_bad_expression();
++nparens; ++nparens;
paren_expr = rprn = bin_last = false; paren_expr = rprn = bin_last = false;
get_token = true; get_token = true;
bc_vec_push(&p->ops, &t); bc_vec_push(&p->ops, &t);
break; break;
}
case BC_LEX_RPAREN: case BC_LEX_RPAREN:
{
if (bin_last || prev == BC_INST_BOOL_NOT) if (bin_last || prev == BC_INST_BOOL_NOT)
return bc_error_bad_expression(); return bc_error_bad_expression();
if (nparens == 0) { if (nparens == 0) {
s = BC_STATUS_SUCCESS; s = BC_STATUS_SUCCESS;
done = true; done = true;
get_token = false; get_token = false;
break; break;
} }
else if (!paren_expr) { if (!paren_expr) {
dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
return BC_STATUS_PARSE_EMPTY_EXP; return BC_STATUS_PARSE_EMPTY_EXP;
} }
--nparens; --nparens;
paren_expr = rprn = true; paren_expr = rprn = true;
get_token = bin_last = false; get_token = bin_last = false;
s = zbc_parse_rightParen(p, ops_bgn, &nexprs); s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
break; break;
}
case BC_LEX_NAME: case BC_LEX_NAME:
{
if (BC_PARSE_LEAF(prev, rprn)) if (BC_PARSE_LEAF(prev, rprn))
return bc_error_bad_expression(); return bc_error_bad_expression();
paren_expr = true; paren_expr = true;
rprn = get_token = bin_last = false; rprn = get_token = bin_last = false;
s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
++nexprs; ++nexprs;
break; break;
}
case BC_LEX_NUMBER: case BC_LEX_NUMBER:
{
if (BC_PARSE_LEAF(prev, rprn)) if (BC_PARSE_LEAF(prev, rprn))
return bc_error_bad_expression(); return bc_error_bad_expression();
bc_parse_number(p); bc_parse_number(p);
@ -4687,58 +4637,40 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
prev = BC_INST_NUM; prev = BC_INST_NUM;
paren_expr = get_token = true; paren_expr = get_token = true;
rprn = bin_last = false; rprn = bin_last = false;
break; break;
}
case BC_LEX_KEY_IBASE: case BC_LEX_KEY_IBASE:
case BC_LEX_KEY_LAST: case BC_LEX_KEY_LAST:
case BC_LEX_KEY_OBASE: case BC_LEX_KEY_OBASE:
{
if (BC_PARSE_LEAF(prev, rprn)) if (BC_PARSE_LEAF(prev, rprn))
return bc_error_bad_expression(); return bc_error_bad_expression();
prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
bc_parse_push(p, (char) prev); bc_parse_push(p, (char) prev);
paren_expr = get_token = true; paren_expr = get_token = true;
rprn = bin_last = false; rprn = bin_last = false;
++nexprs; ++nexprs;
break; break;
}
case BC_LEX_KEY_LENGTH: case BC_LEX_KEY_LENGTH:
case BC_LEX_KEY_SQRT: case BC_LEX_KEY_SQRT:
{
if (BC_PARSE_LEAF(prev, rprn)) if (BC_PARSE_LEAF(prev, rprn))
return bc_error_bad_expression(); return bc_error_bad_expression();
s = zbc_parse_builtin(p, t, flags, &prev); s = zbc_parse_builtin(p, t, flags, &prev);
paren_expr = true; paren_expr = true;
rprn = get_token = bin_last = false; rprn = get_token = bin_last = false;
++nexprs; ++nexprs;
break; break;
}
case BC_LEX_KEY_READ: case BC_LEX_KEY_READ:
{
if (BC_PARSE_LEAF(prev, rprn)) if (BC_PARSE_LEAF(prev, rprn))
return bc_error_bad_expression(); return bc_error_bad_expression();
else if (flags & BC_PARSE_NOREAD) else if (flags & BC_PARSE_NOREAD)
s = bc_error_nested_read_call(); s = bc_error_nested_read_call();
else else
s = zbc_parse_read(p); s = zbc_parse_read(p);
paren_expr = true; paren_expr = true;
rprn = get_token = bin_last = false; rprn = get_token = bin_last = false;
++nexprs; ++nexprs;
prev = BC_INST_READ; prev = BC_INST_READ;
break; break;
}
case BC_LEX_KEY_SCALE: case BC_LEX_KEY_SCALE:
{
if (BC_PARSE_LEAF(prev, rprn)) if (BC_PARSE_LEAF(prev, rprn))
return bc_error_bad_expression(); return bc_error_bad_expression();
s = zbc_parse_scale(p, &prev, flags); s = zbc_parse_scale(p, &prev, flags);
@ -4746,15 +4678,10 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
rprn = get_token = bin_last = false; rprn = get_token = bin_last = false;
++nexprs; ++nexprs;
prev = BC_INST_SCALE; prev = BC_INST_SCALE;
break; break;
}
default: default:
{
s = bc_error_bad_token(); s = bc_error_bad_token();
break; break;
}
} }
if (!s && get_token) s = zbc_lex_next(&p->l); if (!s && get_token) s = zbc_lex_next(&p->l);
@ -4764,7 +4691,6 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
while (p->ops.len > ops_bgn) { while (p->ops.len > ops_bgn) {
top = BC_PARSE_TOP_OP(p); top = BC_PARSE_TOP_OP(p);
assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
@ -4783,8 +4709,7 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
if (!(flags & BC_PARSE_REL) && nrelops) { if (!(flags & BC_PARSE_REL) && nrelops) {
s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
IF_ERROR_RETURN_POSSIBLE(if (s) return s); IF_ERROR_RETURN_POSSIBLE(if (s) return s);
} } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
else if ((flags & BC_PARSE_REL) && nrelops > 1) {
s = bc_POSIX_requires("exactly one comparison operator per condition"); s = bc_POSIX_requires("exactly one comparison operator per condition");
IF_ERROR_RETURN_POSSIBLE(if (s) return s); IF_ERROR_RETURN_POSSIBLE(if (s) return s);
} }
@ -4871,8 +4796,7 @@ static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
s = zdc_parse_register(p); s = zdc_parse_register(p);
if (s) RETURN_STATUS(s); if (s) RETURN_STATUS(s);
s = zbc_lex_next(&p->l); s = zbc_lex_next(&p->l);
} } else
else
bc_parse_push(p, BC_PARSE_STREND); bc_parse_push(p, BC_PARSE_STREND);
RETURN_STATUS(s); RETURN_STATUS(s);
@ -5035,19 +4959,14 @@ static BcVec* bc_program_search(char *id, bool var)
static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
{ {
switch (r->t) { switch (r->t) {
case BC_RESULT_STR: case BC_RESULT_STR:
case BC_RESULT_TEMP: case BC_RESULT_TEMP:
case BC_RESULT_IBASE: case BC_RESULT_IBASE:
case BC_RESULT_SCALE: case BC_RESULT_SCALE:
case BC_RESULT_OBASE: case BC_RESULT_OBASE:
{
*num = &r->d.n; *num = &r->d.n;
break; break;
} case BC_RESULT_CONSTANT: {
case BC_RESULT_CONSTANT:
{
BcStatus s; BcStatus s;
char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
unsigned base_t; unsigned base_t;
@ -5066,14 +4985,11 @@ static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
*num = &r->d.n; *num = &r->d.n;
r->t = BC_RESULT_TEMP; r->t = BC_RESULT_TEMP;
break; break;
} }
case BC_RESULT_VAR: case BC_RESULT_VAR:
case BC_RESULT_ARRAY: case BC_RESULT_ARRAY:
case BC_RESULT_ARRAY_ELEM: case BC_RESULT_ARRAY_ELEM: {
{
BcVec *v; BcVec *v;
v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR); v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
@ -5082,24 +4998,16 @@ static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
v = bc_vec_top(v); v = bc_vec_top(v);
if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
*num = bc_vec_item(v, r->d.id.idx); *num = bc_vec_item(v, r->d.id.idx);
} } else
else
*num = bc_vec_top(v); *num = bc_vec_top(v);
break; break;
} }
case BC_RESULT_LAST: case BC_RESULT_LAST:
{
*num = &G.prog.last; *num = &G.prog.last;
break; break;
}
case BC_RESULT_ONE: case BC_RESULT_ONE:
{
*num = &G.prog.one; *num = &G.prog.one;
break; break;
}
} }
RETURN_STATUS(BC_STATUS_SUCCESS); RETURN_STATUS(BC_STATUS_SUCCESS);
@ -5193,8 +5101,7 @@ static BC_STATUS zbc_program_op(char inst)
bc_program_binOpRetire(&res); bc_program_binOpRetire(&res);
RETURN_STATUS(s); RETURN_STATUS(s);
err:
err:
bc_num_free(&res.d.n); bc_num_free(&res.d.n);
RETURN_STATUS(s); RETURN_STATUS(s);
} }
@ -5244,8 +5151,7 @@ static BC_STATUS zbc_program_read(void)
bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
bc_vec_push(&G.prog.stack, &ip); bc_vec_push(&G.prog.stack, &ip);
exec_err:
exec_err:
bc_parse_free(&parse); bc_parse_free(&parse);
G.in_read = 0; G.in_read = 0;
G.prog.file = sv_file; G.prog.file = sv_file;
@ -5461,8 +5367,7 @@ static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigi
s = zbc_num_mul(&frac_len, base, &frac_len, 0); s = zbc_num_mul(&frac_len, base, &frac_len, 0);
if (s) goto err; if (s) goto err;
} }
err:
err:
bc_num_free(&frac_len); bc_num_free(&frac_len);
bc_num_free(&digit); bc_num_free(&digit);
bc_num_free(&fracp); bc_num_free(&fracp);
@ -5549,8 +5454,7 @@ static BC_STATUS zbc_program_print(char inst, size_t idx)
if (BC_PROG_NUM(r, num)) { if (BC_PROG_NUM(r, num)) {
s = zbc_num_print(num, !pop); s = zbc_num_print(num, !pop);
if (!s) bc_num_copy(&G.prog.last, num); if (!s) bc_num_copy(&G.prog.last, num);
} } else {
else {
char *str; char *str;
idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
@ -5564,8 +5468,7 @@ static BC_STATUS zbc_program_print(char inst, size_t idx)
++G.prog.nchars; ++G.prog.nchars;
if (c == '\n') G.prog.nchars = 0; if (c == '\n') G.prog.nchars = 0;
} }
} } else {
else {
bc_program_printString(str); bc_program_printString(str);
if (inst == BC_INST_PRINT) bb_putchar('\n'); if (inst == BC_INST_PRINT) bb_putchar('\n');
} }
@ -5646,8 +5549,7 @@ static BC_STATUS zbc_program_logical(char inst)
#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS) #define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
#if ENABLE_DC #if ENABLE_DC
static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v, static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v, bool push)
bool push)
{ {
BcNum n2; BcNum n2;
BcResult res; BcResult res;
@ -5704,8 +5606,7 @@ static BC_STATUS zbc_program_copyToVar(char *name, bool var)
if (var) { if (var) {
bc_num_init_DEF_SIZE(&r.d.n); bc_num_init_DEF_SIZE(&r.d.n);
bc_num_copy(&r.d.n, n); bc_num_copy(&r.d.n, n);
} } else {
else {
bc_array_init(&r.d.v, true); bc_array_init(&r.d.v, true);
bc_array_copy(&r.d.v, (BcVec *) n); bc_array_copy(&r.d.v, (BcVec *) n);
} }
@ -5731,9 +5632,7 @@ static BC_STATUS zbc_program_assign(char inst)
sc = left->t == BC_RESULT_SCALE; sc = left->t == BC_RESULT_SCALE;
#if ENABLE_DC #if ENABLE_DC
if (right->t == BC_RESULT_STR) { if (right->t == BC_RESULT_STR) {
BcVec *v; BcVec *v;
if (left->t != BC_RESULT_VAR) if (left->t != BC_RESULT_VAR)
@ -5828,7 +5727,6 @@ static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
BcNum *num = bc_vec_top(v); BcNum *num = bc_vec_top(v);
if (pop || copy) { if (pop || copy) {
if (!BC_PROG_STACK(v, 2 - copy)) { if (!BC_PROG_STACK(v, 2 - copy)) {
free(name); free(name);
RETURN_STATUS(bc_error_stack_has_too_few_elements()); RETURN_STATUS(bc_error_stack_has_too_few_elements());
@ -5838,13 +5736,11 @@ static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
name = NULL; name = NULL;
if (!BC_PROG_STR(num)) { if (!BC_PROG_STR(num)) {
r.t = BC_RESULT_TEMP; r.t = BC_RESULT_TEMP;
bc_num_init_DEF_SIZE(&r.d.n); bc_num_init_DEF_SIZE(&r.d.n);
bc_num_copy(&r.d.n, num); bc_num_copy(&r.d.n, num);
} } else {
else {
r.t = BC_RESULT_STR; r.t = BC_RESULT_STR;
r.d.id.idx = num->rdx; r.d.id.idx = num->rdx;
} }
@ -5860,8 +5756,7 @@ static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
} }
#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) #define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
char inst)
{ {
BcStatus s = BC_STATUS_SUCCESS; BcStatus s = BC_STATUS_SUCCESS;
BcResult r; BcResult r;
@ -5872,9 +5767,7 @@ static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
if (inst == BC_INST_ARRAY) { if (inst == BC_INST_ARRAY) {
r.t = BC_RESULT_ARRAY; r.t = BC_RESULT_ARRAY;
bc_vec_push(&G.prog.results, &r); bc_vec_push(&G.prog.results, &r);
} } else {
else {
BcResult *operand; BcResult *operand;
unsigned long temp; unsigned long temp;
@ -5891,8 +5784,7 @@ static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
r.d.id.idx = (size_t) temp; r.d.id.idx = (size_t) temp;
bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
} }
err:
err:
if (s) free(r.d.id.name); if (s) free(r.d.id.name);
RETURN_STATUS(s); RETURN_STATUS(s);
} }
@ -5976,8 +5868,7 @@ static BC_STATUS zbc_program_call(char *code, size_t *idx)
if (a->idx) { if (a->idx) {
bc_num_init_DEF_SIZE(&param.n); bc_num_init_DEF_SIZE(&param.n);
bc_vec_push(v, &param.n); bc_vec_push(v, &param.n);
} } else {
else {
bc_array_init(&param.v, true); bc_array_init(&param.v, true);
bc_vec_push(v, &param.v); bc_vec_push(v, &param.v);
} }
@ -6011,8 +5902,7 @@ static BC_STATUS zbc_program_return(char inst)
if (s) RETURN_STATUS(s); if (s) RETURN_STATUS(s);
bc_num_init(&res.d.n, num->len); bc_num_init(&res.d.n, num->len);
bc_num_copy(&res.d.n, num); bc_num_copy(&res.d.n, num);
} } else {
else {
bc_num_init_DEF_SIZE(&res.d.n); bc_num_init_DEF_SIZE(&res.d.n);
//bc_num_zero(&res.d.n); - already is //bc_num_zero(&res.d.n); - already is
} }
@ -6075,7 +5965,8 @@ static BC_STATUS zbc_program_builtin(char inst)
bc_num_init_DEF_SIZE(&res.d.n); bc_num_init_DEF_SIZE(&res.d.n);
if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); if (inst == BC_INST_SQRT)
s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
#if ENABLE_BC #if ENABLE_BC
else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
@ -6121,8 +6012,7 @@ static BC_STATUS zbc_program_divmod(void)
bc_vec_push(&G.prog.results, &res); bc_vec_push(&G.prog.results, &res);
RETURN_STATUS(s); RETURN_STATUS(s);
err:
err:
bc_num_free(&res2.d.n); bc_num_free(&res2.d.n);
bc_num_free(&res.d.n); bc_num_free(&res.d.n);
RETURN_STATUS(s); RETURN_STATUS(s);
@ -6148,12 +6038,10 @@ static BC_STATUS zbc_program_modexp(void)
// Make sure that the values have their pointers updated, if necessary. // Make sure that the values have their pointers updated, if necessary.
if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
if (r1->t == r2->t) { if (r1->t == r2->t) {
s = zbc_program_num(r2, &n2, false); s = zbc_program_num(r2, &n2, false);
if (s) RETURN_STATUS(s); if (s) RETURN_STATUS(s);
} }
if (r1->t == r3->t) { if (r1->t == r3->t) {
s = zbc_program_num(r3, &n3, false); s = zbc_program_num(r3, &n3, false);
if (s) RETURN_STATUS(s); if (s) RETURN_STATUS(s);
@ -6168,8 +6056,7 @@ static BC_STATUS zbc_program_modexp(void)
bc_program_binOpRetire(&res); bc_program_binOpRetire(&res);
RETURN_STATUS(s); RETURN_STATUS(s);
err:
err:
bc_num_free(&res.d.n); bc_num_free(&res.d.n);
RETURN_STATUS(s); RETURN_STATUS(s);
} }
@ -6205,7 +6092,6 @@ static BC_STATUS zbc_program_asciify(void)
if (s) RETURN_STATUS(s); if (s) RETURN_STATUS(s);
if (BC_PROG_NUM(r, num)) { if (BC_PROG_NUM(r, num)) {
bc_num_init_DEF_SIZE(&n); bc_num_init_DEF_SIZE(&n);
bc_num_copy(&n, num); bc_num_copy(&n, num);
bc_num_truncate(&n, n.rdx); bc_num_truncate(&n, n.rdx);
@ -6218,8 +6104,7 @@ static BC_STATUS zbc_program_asciify(void)
c = (char) val; c = (char) val;
bc_num_free(&n); bc_num_free(&n);
} } else {
else {
idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
str2 = *bc_program_str(idx); str2 = *bc_program_str(idx);
c = str2[0]; c = str2[0];
@ -6239,10 +6124,8 @@ static BC_STATUS zbc_program_asciify(void)
break; break;
} }
} }
free(str); free(str);
} } else
else
bc_vec_push(&G.prog.strs, &str); bc_vec_push(&G.prog.strs, &str);
res.t = BC_RESULT_STR; res.t = BC_RESULT_STR;
@ -6251,8 +6134,7 @@ static BC_STATUS zbc_program_asciify(void)
bc_vec_push(&G.prog.results, &res); bc_vec_push(&G.prog.results, &res);
RETURN_STATUS(BC_STATUS_SUCCESS); RETURN_STATUS(BC_STATUS_SUCCESS);
num_err:
num_err:
bc_num_free(&n); bc_num_free(&n);
RETURN_STATUS(s); RETURN_STATUS(s);
} }
@ -6311,8 +6193,7 @@ static BC_STATUS zbc_program_nquit(void)
} }
#define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) #define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
static BC_STATUS zbc_program_execStr(char *code, size_t *bgn, static BC_STATUS zbc_program_execStr(char *code, size_t *bgn, bool cond)
bool cond)
{ {
BcStatus s = BC_STATUS_SUCCESS; BcStatus s = BC_STATUS_SUCCESS;
BcResult *r; BcResult *r;
@ -6402,12 +6283,11 @@ static BC_STATUS zbc_program_execStr(char *code, size_t *bgn,
bc_vec_push(&G.prog.stack, &ip); bc_vec_push(&G.prog.stack, &ip);
RETURN_STATUS(BC_STATUS_SUCCESS); RETURN_STATUS(BC_STATUS_SUCCESS);
err:
err:
bc_parse_free(&prs); bc_parse_free(&prs);
f = bc_program_func(fidx); f = bc_program_func(fidx);
bc_vec_pop_all(&f->code); bc_vec_pop_all(&f->code);
exit: exit:
bc_vec_pop(&G.prog.results); bc_vec_pop(&G.prog.results);
RETURN_STATUS(s); RETURN_STATUS(s);
} }
@ -6448,7 +6328,6 @@ static void bc_program_addFunc(char *name, size_t *idx)
*idx = entry_ptr->idx; *idx = entry_ptr->idx;
if (!inserted) { if (!inserted) {
BcFunc *func = bc_program_func(entry_ptr->idx); BcFunc *func = bc_program_func(entry_ptr->idx);
// We need to reset these, so the function can be repopulated. // We need to reset these, so the function can be repopulated.
@ -6456,8 +6335,7 @@ static void bc_program_addFunc(char *name, size_t *idx)
bc_vec_pop_all(&func->autos); bc_vec_pop_all(&func->autos);
bc_vec_pop_all(&func->code); bc_vec_pop_all(&func->code);
bc_vec_pop_all(&func->labels); bc_vec_pop_all(&func->labels);
} } else {
else {
bc_func_init(&f); bc_func_init(&f);
bc_vec_push(&G.prog.fns, &f); bc_vec_push(&G.prog.fns, &f);
} }