Reduce LOC
diff --git a/dist/busybox/header.c b/dist/busybox/header.c
index 52e32c1..c3b7eae 100644
--- a/dist/busybox/header.c
+++ b/dist/busybox/header.c
@@ -5,7 +5,7 @@
* Automatically generated from https://github.com/gavinhoward/bc
*/
//config:config BC
-//config: bool "bc (47.83 kb; 57.54 kb when combined with dc)"
+//config: bool "bc (47.78 kb; 57.50 kb when combined with dc)"
//config: default n
//config: help
//config: bc is a command-line, arbitrary-precision calculator with a Turing-complete
@@ -52,7 +52,7 @@
//config: -v --version print version information and copyright and exit
//config:
//config:config DC
-//config: bool "dc (38.15 kb; 57.54 kb when combined with bc)"
+//config: bool "dc (38.15 kb; 57.50 kb when combined with bc)"
//config: default n
//config: help
//config: dc is a reverse-polish notation command-line calculator which supports unlimited
diff --git a/gen/lib.bc b/gen/lib.bc
index 6725a37..c8e4bbc 100644
--- a/gen/lib.bc
+++ b/gen/lib.bc
@@ -134,12 +134,12 @@
x=-x
}
if(x==1){
- if(scale<=64){
+ if(scale<65){
return(.7853981633974483096156608458198757210492923498437764552437361480/n)
}
}
if(x==.2){
- if(scale<=64){
+ if(scale<65){
return(.1973955598498807583700497651947902934475851037878521015176889402/n)
}
}
@@ -189,6 +189,6 @@
}
scale=s
ibase=b
- if(o!=0)return(-a*r/1)
+ if(o!=0)a=-a
return(a*r/1)
}
diff --git a/include/program.h b/include/program.h
index 5c295ea..f085d42 100644
--- a/include/program.h
+++ b/include/program.h
@@ -77,7 +77,7 @@
} BcProgram;
-#define BC_PROG_CHECK_STACK(s, n) ((s)->len >= ((size_t) n))
+#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
#define BC_PROG_MAIN (0)
#define BC_PROG_READ (1)
diff --git a/include/status.h b/include/status.h
index 4b860c3..065a086 100644
--- a/include/status.h
+++ b/include/status.h
@@ -71,7 +71,7 @@
BC_STATUS_EXEC_BAD_TYPE,
BC_STATUS_EXEC_BAD_OBASE,
BC_STATUS_EXEC_SIGNAL,
- BC_STATUS_EXEC_SMALL_STACK,
+ BC_STATUS_EXEC_STACK,
BC_STATUS_VEC_OUT_OF_BOUNDS,
BC_STATUS_VEC_ITEM_EXISTS,
diff --git a/src/lang.c b/src/lang.c
index a08d2fc..f8ae505 100644
--- a/src/lang.c
+++ b/src/lang.c
@@ -83,10 +83,10 @@
BcStatus s;
- if (nums) {
- if ((s = bc_vec_init(a, sizeof(BcNum), bc_num_free))) return s;
- }
- else if ((s = bc_vec_init(a, sizeof(BcVec), bc_vec_free))) return s;
+ if (nums) s = bc_vec_init(a, sizeof(BcNum), bc_num_free);
+ else s = bc_vec_init(a, sizeof(BcVec), bc_vec_free);
+
+ if (s) return s;
if ((s = bc_array_expand(a, 1))) goto err;
@@ -120,23 +120,22 @@
BcStatus bc_array_expand(BcVec *a, size_t len) {
BcStatus s = BC_STATUS_SUCCESS;
- BcNum n;
- BcVec v;
+ BcResultData data;
assert(a);
if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
while (!s && len > a->len) {
- if ((s = bc_num_init(&n, BC_NUM_DEF_SIZE))) return s;
- bc_num_zero(&n);
- if ((s = bc_vec_push(a, &n))) bc_num_free(&n);
+ if ((s = bc_num_init(&data.n, BC_NUM_DEF_SIZE))) return s;
+ bc_num_zero(&data.n);
+ if ((s = bc_vec_push(a, &data.n))) bc_num_free(&data.n);
}
}
else {
assert(a->size == sizeof(BcVec) && a->dtor == bc_vec_free);
while (!s && len > a->len) {
- if ((s = bc_array_init(&v, true))) return s;
- if ((s = bc_vec_push(a, &v))) bc_vec_free(&v);
+ if ((s = bc_array_init(&data.v, true))) return s;
+ if ((s = bc_vec_push(a, &data.v))) bc_vec_free(&data.v);
}
}
diff --git a/src/lex.c b/src/lex.c
index 878b075..5d0fa52 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -98,13 +98,11 @@
BcStatus bc_lex_name(BcLex *l) {
BcStatus s;
- size_t i;
- char c;
+ size_t i = 0;
const char *buf = l->buffer + l->idx - 1;
+ char c = buf[i];
l->t.t = BC_LEX_NAME;
- i = 0;
- c = buf[i];
while ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_')
c = buf[++i];
diff --git a/src/num.c b/src/num.c
index 479c61a..a3042b2 100644
--- a/src/num.c
+++ b/src/num.c
@@ -139,11 +139,9 @@
BcStatus bc_num_extend(BcNum *n, size_t places) {
BcStatus s;
- size_t len;
+ size_t len = n->len + places;
if (!places) return BC_STATUS_SUCCESS;
-
- len = n->len + places;
if (n->cap < len && (s = bc_num_expand(n, len))) return s;
memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
@@ -183,11 +181,10 @@
b->len = n->len - idx;
a->len = idx;
+ a->rdx = b->rdx = 0;
memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
memcpy(a->num, n->num, idx * sizeof(BcDig));
-
- a->rdx = b->rdx = 0;
}
else {
bc_num_zero(b);
@@ -397,12 +394,9 @@
if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
- if (carry) {
- c->num[i + j] += (BcDig) carry;
- carry = 0;
- len = BC_MAX(len, i + j + 1);
- }
- else len = BC_MAX(len, i + j);
+ c->num[i + j] += (BcDig) carry;
+ len = BC_MAX(len, i + j + !!carry);
+ carry = 0;
}
c->len = len;
@@ -466,7 +460,7 @@
BcStatus s;
BcNum cpa, cpb;
- size_t maxrdx = BC_MAX(a->rdx, b->rdx), max;
+ size_t maxrdx = BC_MAX(a->rdx, b->rdx);
scale = BC_MAX(scale, a->rdx);
scale = BC_MAX(scale, b->rdx);
@@ -478,21 +472,19 @@
if ((s = bc_num_copy(&cpa, a))) goto err;
if ((s = bc_num_copy(&cpb, b))) goto err;
-
cpa.neg = cpb.neg = false;
-
if ((s = bc_num_shift(&cpa, maxrdx))) goto err;
if ((s = bc_num_shift(&cpb, maxrdx))) goto err;
if ((s = bc_num_alg_k(&cpa, &cpb, c))) goto err;
- if ((s = bc_num_expand(c, c->len + (max = maxrdx + scale)))) goto err;
- if (c->len < max) {
+ if ((s = bc_num_expand(c, c->len + (maxrdx += scale)))) goto err;
+ if (c->len < maxrdx) {
memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
- c->len += max;
+ c->len += maxrdx;
}
- c->rdx = max;
+ c->rdx = maxrdx;
s = bc_num_retireMul(c, scale, a->neg, b->neg);
err:
@@ -654,11 +646,7 @@
}
for (resrdx = powrdx, pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) {
-
- powrdx <<= 1;
-
- if ((s = bc_num_mul(©, ©, ©, powrdx))) goto err;
-
+ if ((s = bc_num_mul(©, ©, ©, (powrdx <<= 1)))) goto err;
if (pow & 1) {
resrdx += powrdx;
if ((s = bc_num_mul(c, ©, c, resrdx))) goto err;
@@ -881,12 +869,9 @@
for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
-
if ((s = bc_num_printNewline(nchars, line_len))) return s;
-
div = num / pow;
num -= div * pow;
-
if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
}
@@ -1138,6 +1123,7 @@
for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
unsigned long prev = *result, powprev = pow;
+
*result += ((unsigned long) n->num[i]) * pow;
pow *= 10;
diff --git a/src/program.c b/src/program.c
index 8ab2c12..45f904e 100644
--- a/src/program.c
+++ b/src/program.c
@@ -155,7 +155,7 @@
assert(p && l && ln && r && rn);
- if (!BC_PROG_CHECK_STACK(&p->results, 2)) return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 2)) return BC_STATUS_EXEC_STACK;
*r = bc_vec_item_rev(&p->results, 0);
*l = bc_vec_item_rev(&p->results, 1);
@@ -198,7 +198,7 @@
assert(p && r && n);
- if (!BC_PROG_CHECK_STACK(&p->results, 1)) return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
*r = bc_vec_top(&p->results);
@@ -411,8 +411,7 @@
assert(p);
- if (!BC_PROG_CHECK_STACK(&p->results, idx + 1))
- return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, idx + 1)) return BC_STATUS_EXEC_STACK;
r = bc_vec_item_rev(&p->results, idx);
if ((s = bc_program_num(p, r, &num, false))) return s;
@@ -558,8 +557,7 @@
res.t = BC_RESULT_STR;
if (!push) {
- if (!BC_PROG_CHECK_STACK(&p->results, 2))
- return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 2)) return BC_STATUS_EXEC_STACK;
bc_vec_pop(v);
bc_vec_pop(&p->results);
}
@@ -579,7 +577,7 @@
BcVec *v;
BcNum *n;
- if (!BC_PROG_CHECK_STACK(&p->results, 1)) return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
ptr = bc_vec_top(&p->results);
if ((ptr->t == BC_RESULT_ARRAY) != !var) return BC_STATUS_EXEC_BAD_TYPE;
@@ -719,8 +717,8 @@
if (pop || copy) {
- if (!BC_PROG_CHECK_STACK(v, 2 - copy)) {
- s = BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(v, 2 - copy)) {
+ s = BC_STATUS_EXEC_STACK;
goto err;
}
@@ -844,7 +842,7 @@
if (nparams != func->nparams) return BC_STATUS_EXEC_MISMATCHED_PARAMS;
ip.len = p->results.len - nparams;
- assert(BC_PROG_CHECK_STACK(&p->results, nparams));
+ assert(BC_PROG_STACK(&p->results, nparams));
for (i = 0; i < nparams; ++i) {
@@ -888,10 +886,10 @@
size_t i;
BcInstPtr *ip = bc_vec_top(&p->stack);
- assert(BC_PROG_CHECK_STACK(&p->stack, 2));
+ assert(BC_PROG_STACK(&p->stack, 2));
- if (!BC_PROG_CHECK_STACK(&p->results, ip->len + inst == BC_INST_RET))
- return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, ip->len + inst == BC_INST_RET))
+ return BC_STATUS_EXEC_STACK;
f = bc_vec_item(&p->fns, ip->func);
res.t = BC_RESULT_TEMP;
@@ -957,7 +955,7 @@
BcResult res;
bool len = inst == BC_INST_LENGTH;
- if (!BC_PROG_CHECK_STACK(&p->results, 1)) return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
opnd = bc_vec_top(&p->results);
if ((s = bc_program_num(p, opnd, &num, false))) return s;
@@ -1034,7 +1032,7 @@
BcResult *opd1, *opd2, *opd3, res;
BcNum *n1, *n2, *n3;
- if (!BC_PROG_CHECK_STACK(&p->results, 3)) return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 3)) return BC_STATUS_EXEC_STACK;
if ((s = bc_program_binOpPrep(p, &opd2, &n2, &opd3, &n3, false))) return s;
opd1 = bc_vec_item_rev(&p->results, 2);
@@ -1093,7 +1091,7 @@
size_t len = p->strs.len, idx;
unsigned long val;
- if (!BC_PROG_CHECK_STACK(&p->results, 1)) return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
r = bc_vec_top(&p->results);
if ((s = bc_program_num(p, r, &num, false))) return s;
@@ -1166,7 +1164,7 @@
size_t idx;
char *str;
- if (!BC_PROG_CHECK_STACK(&p->results, 1)) return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
r = bc_vec_top(&p->results);
if ((s = bc_program_num(p, r, &n, false))) return s;
@@ -1194,7 +1192,7 @@
bc_vec_pop(&p->results);
- if (p->stack.len < val) return BC_STATUS_EXEC_SMALL_STACK;
+ if (p->stack.len < val) return BC_STATUS_EXEC_STACK;
else if (p->stack.len == val) return BC_STATUS_QUIT;
bc_vec_npop(&p->stack, val);
@@ -1214,7 +1212,7 @@
BcNum *n;
bool exec;
- if (!BC_PROG_CHECK_STACK(&p->results, 1)) return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
r = bc_vec_top(&p->results);
@@ -1641,15 +1639,14 @@
case BC_INST_POP:
{
- if (!BC_PROG_CHECK_STACK(&p->results, 1))
- s = BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 1)) s = BC_STATUS_EXEC_STACK;
else bc_vec_pop(&p->results);
break;
}
case BC_INST_POP_EXEC:
{
- assert(BC_PROG_CHECK_STACK(&p->stack, 2));
+ assert(BC_PROG_STACK(&p->stack, 2));
bc_vec_pop(&p->stack);
break;
}
@@ -1757,8 +1754,7 @@
case BC_INST_DUPLICATE:
{
- if (!BC_PROG_CHECK_STACK(&p->results, 1))
- return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
ptr = bc_vec_top(&p->results);
if ((s = bc_result_copy(&res, ptr))) return s;
s = bc_vec_push(&p->results, &res);
@@ -1769,8 +1765,7 @@
{
BcResult *ptr2;
- if (!BC_PROG_CHECK_STACK(&p->results, 2))
- return BC_STATUS_EXEC_SMALL_STACK;
+ if (!BC_PROG_STACK(&p->results, 2)) return BC_STATUS_EXEC_STACK;
ptr = bc_vec_item_rev(&p->results, 0);
ptr2 = bc_vec_item_rev(&p->results, 1);
diff --git a/src/vm.c b/src/vm.c
index 5a6f383..672b0bb 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -249,9 +249,7 @@
char *str = buf.v;
- len = buf.len - 1;
-
- if (len == 1) {
+ if ((len = buf.len - 1) == 1) {
if (string && buf.v[0] == vm->exe.strend) string -= 1;
else if (buf.v[0] == vm->exe.strbgn) string += 1;
}