Gavin Howard | 6556eed | 2018-02-27 17:10:27 -0700 | [diff] [blame] | 1 | /* |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 2 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 3 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 4 | * Copyright 2018 Gavin D. Howard |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | * PERFORMANCE OF THIS SOFTWARE. |
| 16 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 17 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 18 | * |
| 19 | * Code to execute bc programs. |
| 20 | * |
| 21 | */ |
| 22 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 23 | #include <assert.h> |
Gavin Howard | 411f732 | 2018-09-26 17:21:19 -0600 | [diff] [blame] | 24 | #include <stdbool.h> |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 25 | #include <string.h> |
| 26 | |
Gavin Howard | 82cdc6f | 2018-02-20 14:23:19 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 28 | |
Gavin Howard | a54cabf | 2018-03-28 14:50:33 -0600 | [diff] [blame] | 29 | #include <io.h> |
Gavin Howard | 13ccc4a | 2018-02-15 14:09:41 -0700 | [diff] [blame] | 30 | #include <parse.h> |
Gavin Howard | d2a0525 | 2018-09-27 14:00:40 -0600 | [diff] [blame] | 31 | #include <program.h> |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 32 | #include <vm.h> |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 33 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 34 | BcStatus bc_program_search(BcProgram *p, char *name, BcVec **ret, bool var) { |
Gavin Howard | 3affc80 | 2018-02-27 19:57:38 -0700 | [diff] [blame] | 35 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 36 | BcStatus s; |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 37 | BcEntry e, *ptr; |
| 38 | BcVec *v; |
| 39 | BcVecO *vo; |
| 40 | size_t i; |
| 41 | BcResultData data; |
| 42 | bool new; |
Gavin Howard | 3affc80 | 2018-02-27 19:57:38 -0700 | [diff] [blame] | 43 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 44 | v = var ? &p->vars : &p->arrs; |
| 45 | vo = var ? &p->var_map : &p->arr_map; |
Gavin Howard | 97eb856 | 2018-03-21 08:04:29 -0600 | [diff] [blame] | 46 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 47 | e.name = name; |
| 48 | e.idx = v->len; |
Gavin Howard | 3affc80 | 2018-02-27 19:57:38 -0700 | [diff] [blame] | 49 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 50 | if ((new = (s = bc_veco_insert(vo, &e, &i)) != BC_STATUS_VEC_ITEM_EXISTS)) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 51 | if (s) return s; |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 52 | if ((s = bc_array_init(&data.array, var))) return s; |
| 53 | if ((s = bc_vec_push(v, &data.array))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 54 | } |
Gavin Howard | 3affc80 | 2018-02-27 19:57:38 -0700 | [diff] [blame] | 55 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 56 | ptr = bc_veco_item(vo, i); |
| 57 | if (new && !(ptr->name = strdup(e.name))) return BC_STATUS_ALLOC_ERR; |
| 58 | *ret = bc_vec_item(v, ptr->idx); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 59 | |
| 60 | return BC_STATUS_SUCCESS; |
Gavin Howard | 3729646 | 2018-09-07 15:57:03 -0600 | [diff] [blame] | 61 | |
| 62 | err: |
Gavin Howard | c9a9c47 | 2018-10-02 17:23:01 -0600 | [diff] [blame] | 63 | bc_vec_free(&data.array); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 64 | return s; |
Gavin Howard | 3affc80 | 2018-02-27 19:57:38 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 67 | BcStatus bc_program_num(BcProgram *p, BcResult *r, BcNum **num, bool hex) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 68 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 69 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 70 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 71 | switch (r->t) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 72 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 73 | case BC_RESULT_TEMP: |
| 74 | case BC_RESULT_SCALE: |
| 75 | { |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 76 | *num = &r->data.n; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 77 | break; |
| 78 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 79 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 80 | case BC_RESULT_CONSTANT: |
| 81 | { |
| 82 | char **str = bc_vec_item(&p->consts, r->data.id.idx); |
Gavin Howard | 0361074 | 2018-09-27 10:48:29 -0600 | [diff] [blame] | 83 | size_t base_t, len = strlen(*str); |
| 84 | BcNum *base; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 85 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 86 | if ((s = bc_num_init(&r->data.n, len))) return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 87 | |
Gavin Howard | 0361074 | 2018-09-27 10:48:29 -0600 | [diff] [blame] | 88 | hex = hex && len == 1; |
| 89 | base = hex ? &p->hexb : &p->ib; |
| 90 | base_t = hex ? BC_NUM_MAX_IBASE : p->ib_t; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 91 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 92 | if ((s = bc_num_parse(&r->data.n, *str, base, base_t))) { |
| 93 | bc_num_free(&r->data.n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 94 | return s; |
| 95 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 96 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 97 | *num = &r->data.n; |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 98 | r->t = BC_RESULT_TEMP; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 99 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 100 | break; |
| 101 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 102 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 103 | case BC_RESULT_VAR: |
| 104 | case BC_RESULT_ARRAY: |
Gavin Howard | 13ab015 | 2018-09-27 11:46:55 -0600 | [diff] [blame] | 105 | case BC_RESULT_ARRAY_ELEM: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 106 | { |
Gavin Howard | 13ab015 | 2018-09-27 11:46:55 -0600 | [diff] [blame] | 107 | BcVec *v; |
| 108 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 109 | s = bc_program_search(p, r->data.id.name, &v, r->t == BC_RESULT_VAR); |
Gavin Howard | 13ab015 | 2018-09-27 11:46:55 -0600 | [diff] [blame] | 110 | if (s) return s; |
| 111 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 112 | if (r->t == BC_RESULT_ARRAY_ELEM) { |
Gavin Howard | 13ab015 | 2018-09-27 11:46:55 -0600 | [diff] [blame] | 113 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 114 | if ((v = bc_vec_top(v))->len <= r->data.id.idx) { |
Gavin Howard | 13ab015 | 2018-09-27 11:46:55 -0600 | [diff] [blame] | 115 | if ((s = bc_array_expand(v, r->data.id.idx + 1))) return s; |
| 116 | } |
| 117 | |
| 118 | *num = bc_vec_item(v, r->data.id.idx); |
| 119 | } |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 120 | else if (r->t == BC_RESULT_ARRAY) { |
| 121 | v = bc_vec_top(v); |
| 122 | *num = (BcNum*) v; |
| 123 | } |
| 124 | else *num = bc_vec_top(v); |
Gavin Howard | 13ab015 | 2018-09-27 11:46:55 -0600 | [diff] [blame] | 125 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 126 | break; |
| 127 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 128 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 129 | case BC_RESULT_LAST: |
| 130 | { |
| 131 | *num = &p->last; |
| 132 | break; |
| 133 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 134 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 135 | case BC_RESULT_IBASE: |
| 136 | { |
| 137 | *num = &p->ib; |
| 138 | break; |
| 139 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 140 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 141 | case BC_RESULT_OBASE: |
| 142 | { |
| 143 | *num = &p->ob; |
| 144 | break; |
| 145 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 146 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 147 | case BC_RESULT_ONE: |
| 148 | { |
| 149 | *num = &p->one; |
| 150 | break; |
| 151 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 152 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 153 | default: |
| 154 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 155 | assert(false); |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 156 | (void) r; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 160 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 161 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 162 | } |
Gavin Howard | 6d89b5d | 2018-02-26 13:21:34 -0700 | [diff] [blame] | 163 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 164 | BcStatus bc_program_binOpPrep(BcProgram *p, BcResult **left, BcNum **lval, |
| 165 | BcResult **right, BcNum **rval, bool assign) |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 166 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 167 | BcStatus s; |
| 168 | bool hex; |
| 169 | BcResultType lt, rt; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 170 | |
Gavin Howard | 2192547 | 2018-09-29 19:29:25 -0600 | [diff] [blame] | 171 | assert(p && left && lval && right && rval); |
| 172 | |
Gavin Howard | 1e758c8 | 2018-09-29 19:57:37 -0600 | [diff] [blame] | 173 | if (!BC_PROG_CHECK_STACK(&p->results, 2)) return BC_STATUS_EXEC_SMALL_STACK; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 174 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 175 | *right = bc_vec_item_rev(&p->results, 0); |
| 176 | *left = bc_vec_item_rev(&p->results, 1); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 177 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 178 | lt = (*left)->t; |
| 179 | rt = (*right)->t; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 180 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
Gavin Howard | c498761 | 2018-09-21 13:14:09 -0600 | [diff] [blame] | 181 | |
Gavin Howard | 985ad60 | 2018-10-02 14:50:25 -0600 | [diff] [blame] | 182 | if (lt == BC_RESULT_ARRAY || rt == BC_RESULT_ARRAY || |
| 183 | lt == BC_RESULT_STR || rt == BC_RESULT_STR) |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 184 | { |
| 185 | return BC_STATUS_EXEC_BAD_TYPE; |
| 186 | } |
Gavin Howard | 4c8ff2d | 2018-09-18 15:24:07 -0600 | [diff] [blame] | 187 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 188 | if ((s = bc_program_num(p, *left, lval, false))) return s; |
| 189 | if ((s = bc_program_num(p, *right, rval, hex))) return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 190 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 191 | // We run this again under these conditions in case any vector has been |
| 192 | // reallocated out from under the BcNums or arrays we had. |
| 193 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) |
| 194 | s = bc_program_num(p, *left, lval, false); |
Gavin Howard | ee938c0 | 2018-09-21 12:27:44 -0600 | [diff] [blame] | 195 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 196 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Gavin Howard | 1f3e758 | 2018-09-29 02:19:54 -0600 | [diff] [blame] | 199 | BcStatus bc_program_binOpRetire(BcProgram *p, BcResult *r) { |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 200 | r->t = BC_RESULT_TEMP; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 201 | bc_vec_pop(&p->results); |
| 202 | bc_vec_pop(&p->results); |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 203 | return bc_vec_push(&p->results, r); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Gavin Howard | b4584aa | 2018-09-28 10:45:33 -0600 | [diff] [blame] | 206 | BcStatus bc_program_prep(BcProgram *p, BcResult **r, BcNum **n, bool arr) |
Gavin Howard | 38bea81 | 2018-09-20 12:01:25 -0600 | [diff] [blame] | 207 | { |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 208 | BcResultType t; |
| 209 | |
Gavin Howard | 2192547 | 2018-09-29 19:29:25 -0600 | [diff] [blame] | 210 | assert(p && r && n); |
| 211 | |
Gavin Howard | 1e758c8 | 2018-09-29 19:57:37 -0600 | [diff] [blame] | 212 | if (!BC_PROG_CHECK_STACK(&p->results, 1)) return BC_STATUS_EXEC_SMALL_STACK; |
Gavin Howard | 4c8ff2d | 2018-09-18 15:24:07 -0600 | [diff] [blame] | 213 | |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 214 | *r = bc_vec_top(&p->results); |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 215 | t = (*r)->t; |
Gavin Howard | 4c8ff2d | 2018-09-18 15:24:07 -0600 | [diff] [blame] | 216 | |
Gavin Howard | 985ad60 | 2018-10-02 14:50:25 -0600 | [diff] [blame] | 217 | if ((t == BC_RESULT_ARRAY && !arr) || t == BC_RESULT_STR) |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 218 | return BC_STATUS_EXEC_BAD_TYPE; |
Gavin Howard | 4c8ff2d | 2018-09-18 15:24:07 -0600 | [diff] [blame] | 219 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 220 | return bc_program_num(p, *r, n, false); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Gavin Howard | 3950bb6 | 2018-09-28 15:46:00 -0600 | [diff] [blame] | 223 | BcStatus bc_program_retire(BcProgram *p, BcResult *r, BcResultType t) { |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 224 | r->t = t; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 225 | bc_vec_pop(&p->results); |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 226 | return bc_vec_push(&p->results, r); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 229 | BcStatus bc_program_op(BcProgram *p, uint8_t inst) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 230 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 231 | BcStatus s; |
| 232 | BcResult *opd1, *opd2, res; |
| 233 | BcNum *n1, *n2; |
| 234 | BcNumBinaryOp op; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 235 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 236 | if ((s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, false))) return s; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 237 | if ((s = bc_num_init(&res.data.n, BC_NUM_DEF_SIZE))) return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 238 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 239 | op = bc_program_ops[inst - BC_INST_POWER]; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 240 | if ((s = op(n1, n2, &res.data.n, p->scale))) goto err; |
Gavin Howard | 1f3e758 | 2018-09-29 02:19:54 -0600 | [diff] [blame] | 241 | if ((s = bc_program_binOpRetire(p, &res))) goto err; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 242 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 243 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 244 | |
| 245 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 246 | bc_num_free(&res.data.n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 247 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 250 | BcStatus bc_program_read(BcProgram *p) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 251 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 252 | BcStatus s; |
| 253 | BcParse parse; |
Gavin Howard | 7efe744 | 2018-09-27 10:47:01 -0600 | [diff] [blame] | 254 | BcVec buf; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 255 | BcInstPtr ip; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 256 | BcFunc *func = bc_vec_item(&p->fns, BC_PROG_READ); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 257 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 258 | func->code.len = 0; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 259 | |
Gavin Howard | 7efe744 | 2018-09-27 10:47:01 -0600 | [diff] [blame] | 260 | if ((s = bc_vec_init(&buf, sizeof(char), NULL))) return BC_STATUS_ALLOC_ERR; |
Gavin Howard | 6e59dee | 2018-09-27 12:09:00 -0600 | [diff] [blame] | 261 | if ((s = bc_io_getline(&buf, "read> "))) goto io_err; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 262 | |
Gavin Howard | d2a0525 | 2018-09-27 14:00:40 -0600 | [diff] [blame] | 263 | if ((s = p->parse_init(&parse, p))) goto io_err; |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 264 | bc_lex_file(&parse.l, bc_program_stdin_name); |
| 265 | if ((s = bc_lex_text(&parse.l, buf.v))) goto exec_err; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 266 | |
Gavin Howard | 00a7a5f | 2018-10-01 12:30:38 -0600 | [diff] [blame] | 267 | s = p->parse_exp(&parse, &func->code, BC_PARSE_NOREAD, bc_parse_next_read); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 268 | if (s) return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 269 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 270 | if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 271 | s = BC_STATUS_EXEC_BAD_READ_EXPR; |
| 272 | goto exec_err; |
| 273 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 274 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 275 | ip.func = BC_PROG_READ; |
| 276 | ip.idx = 0; |
| 277 | ip.len = p->results.len; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 278 | |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 279 | if ((s = bc_vec_push(&p->stack, &ip))) goto exec_err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 280 | if ((s = bc_program_exec(p))) goto exec_err; |
Gavin Howard | ceaf64d | 2018-03-05 11:20:34 -0700 | [diff] [blame] | 281 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 282 | bc_vec_pop(&p->stack); |
Gavin Howard | ceaf64d | 2018-03-05 11:20:34 -0700 | [diff] [blame] | 283 | |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 284 | exec_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 285 | bc_parse_free(&parse); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 286 | io_err: |
Gavin Howard | 7efe744 | 2018-09-27 10:47:01 -0600 | [diff] [blame] | 287 | bc_vec_free(&buf); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 288 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 291 | size_t bc_program_index(char *code, size_t *bgn) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 292 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 293 | uint8_t amt = code[(*bgn)++], i = 0; |
| 294 | size_t res = 0; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 295 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 296 | for (; i < amt; ++i) res |= (((size_t) code[(*bgn)++]) << (i * CHAR_BIT)); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 297 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 298 | return res; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 301 | char* bc_program_name(char *code, size_t *bgn) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 302 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 303 | size_t len, i; |
Gavin Howard | 74f9933 | 2018-09-28 10:03:07 -0600 | [diff] [blame] | 304 | char byte, *s, *string = (char*) (code + *bgn), *ptr; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 305 | |
Gavin Howard | 74f9933 | 2018-09-28 10:03:07 -0600 | [diff] [blame] | 306 | ptr = strchr(string, BC_PARSE_STREND); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 307 | if (ptr) len = ((unsigned long) ptr) - ((unsigned long) string); |
| 308 | else len = strlen(string); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 309 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 310 | if (!(s = malloc(len + 1))) return NULL; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 311 | |
Gavin Howard | 74f9933 | 2018-09-28 10:03:07 -0600 | [diff] [blame] | 312 | for (i = 0; (byte = (char) code[(*bgn)++]) && byte != BC_PARSE_STREND; ++i) |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 313 | s[i] = byte; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 314 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 315 | s[i] = '\0'; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 316 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 317 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 320 | BcStatus bc_program_printString(const char *str, size_t *nchars) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 321 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 322 | size_t i, len = strlen(str); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 323 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 324 | for (i = 0; i < len; ++i, ++(*nchars)) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 325 | |
Gavin Howard | 6b7d6cb | 2018-09-28 14:21:47 -0600 | [diff] [blame] | 326 | int err, c; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 327 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 328 | if ((c = str[i]) != '\\') err = putchar(c); |
| 329 | else { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 330 | |
Gavin Howard | 6b7d6cb | 2018-09-28 14:21:47 -0600 | [diff] [blame] | 331 | assert(i + 1 < len); |
| 332 | c = str[++i]; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 333 | |
Gavin Howard | 6b7d6cb | 2018-09-28 14:21:47 -0600 | [diff] [blame] | 334 | switch (c) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 335 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 336 | case 'a': |
| 337 | { |
| 338 | err = putchar('\a'); |
| 339 | break; |
| 340 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 341 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 342 | case 'b': |
| 343 | { |
| 344 | err = putchar('\b'); |
| 345 | break; |
| 346 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 347 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 348 | case 'e': |
| 349 | { |
| 350 | err = putchar('\\'); |
| 351 | break; |
| 352 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 353 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 354 | case 'f': |
| 355 | { |
| 356 | err = putchar('\f'); |
| 357 | break; |
| 358 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 359 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 360 | case 'n': |
| 361 | { |
| 362 | err = putchar('\n'); |
| 363 | *nchars = SIZE_MAX; |
| 364 | break; |
| 365 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 366 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 367 | case 'r': |
| 368 | { |
| 369 | err = putchar('\r'); |
| 370 | break; |
| 371 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 372 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 373 | case 'q': |
| 374 | { |
| 375 | err = putchar('"'); |
| 376 | break; |
| 377 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 378 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 379 | case 't': |
| 380 | { |
| 381 | err = putchar('\t'); |
| 382 | break; |
| 383 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 384 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 385 | default: |
| 386 | { |
Gavin Howard | 07f7ff5 | 2018-09-28 14:22:06 -0600 | [diff] [blame] | 387 | // Just print the character. |
| 388 | err = putchar(c); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 389 | break; |
| 390 | } |
| 391 | } |
| 392 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 393 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 394 | if (err == EOF) return BC_STATUS_IO_ERR; |
| 395 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 396 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 397 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 400 | BcStatus bc_program_print(BcProgram *p, uint8_t inst, size_t idx) { |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 401 | |
| 402 | BcStatus s = BC_STATUS_SUCCESS; |
| 403 | BcResult *r; |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 404 | size_t len, i; |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 405 | char *str; |
| 406 | |
Gavin Howard | 2192547 | 2018-09-29 19:29:25 -0600 | [diff] [blame] | 407 | assert(p); |
| 408 | |
Gavin Howard | 1e758c8 | 2018-09-29 19:57:37 -0600 | [diff] [blame] | 409 | if (!BC_PROG_CHECK_STACK(&p->results, idx + 1)) |
| 410 | return BC_STATUS_EXEC_SMALL_STACK; |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 411 | |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 412 | r = bc_vec_item_rev(&p->results, idx); |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 413 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 414 | if (r->t == BC_RESULT_STR) { |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 415 | |
| 416 | idx = r->data.id.idx; |
| 417 | assert(idx < p->strs.len); |
| 418 | |
| 419 | str = *((char**) bc_vec_item(&p->strs, idx)); |
| 420 | |
| 421 | if (inst == BC_INST_PRINT_STR) { |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 422 | for (i = 0, len = strlen(str); i < len; ++i) { |
| 423 | char c = str[i]; |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 424 | if (putchar(c) == EOF) return BC_STATUS_IO_ERR; |
| 425 | if (c == '\n') p->nchars = SIZE_MAX; |
| 426 | ++p->nchars; |
| 427 | } |
| 428 | } |
| 429 | else s = bc_program_printString(str, &p->nchars); |
| 430 | } |
| 431 | else { |
| 432 | |
| 433 | BcNum *num; |
| 434 | bool nl = inst == BC_INST_PRINT; |
| 435 | |
| 436 | assert(inst != BC_INST_PRINT_STR); |
| 437 | |
| 438 | if ((s = bc_program_prep(p, &r, &num, false))) return s; |
| 439 | |
| 440 | s = bc_num_print(num, &p->ob, p->ob_t, nl, &p->nchars, p->len); |
| 441 | if (s) return s; |
| 442 | if ((s = bc_num_copy(&p->last, num))) return s; |
| 443 | |
| 444 | if (!nl) bc_vec_pop(&p->results); |
| 445 | } |
| 446 | |
| 447 | return s; |
| 448 | } |
| 449 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 450 | BcStatus bc_program_negate(BcProgram *p) { |
Gavin Howard | 3a94736 | 2018-03-02 11:25:48 -0700 | [diff] [blame] | 451 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 452 | BcStatus s; |
| 453 | BcResult res, *ptr; |
| 454 | BcNum *num; |
Gavin Howard | 3a94736 | 2018-03-02 11:25:48 -0700 | [diff] [blame] | 455 | |
Gavin Howard | b4584aa | 2018-09-28 10:45:33 -0600 | [diff] [blame] | 456 | if ((s = bc_program_prep(p, &ptr, &num, false))) return s; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 457 | if ((s = bc_num_init(&res.data.n, num->len))) return s; |
| 458 | if ((s = bc_num_copy(&res.data.n, num))) goto err; |
Gavin Howard | 3a94736 | 2018-03-02 11:25:48 -0700 | [diff] [blame] | 459 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 460 | res.data.n.neg = !res.data.n.neg; |
Gavin Howard | a654f11 | 2018-03-03 09:47:34 -0700 | [diff] [blame] | 461 | |
Gavin Howard | b4584aa | 2018-09-28 10:45:33 -0600 | [diff] [blame] | 462 | if ((s = bc_program_retire(p, &res, BC_RESULT_TEMP))) goto err; |
Gavin Howard | 3a94736 | 2018-03-02 11:25:48 -0700 | [diff] [blame] | 463 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 464 | return s; |
Gavin Howard | 3a94736 | 2018-03-02 11:25:48 -0700 | [diff] [blame] | 465 | |
| 466 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 467 | bc_num_free(&res.data.n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 468 | return s; |
Gavin Howard | 3a94736 | 2018-03-02 11:25:48 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 471 | BcStatus bc_program_logical(BcProgram *p, uint8_t inst) { |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 472 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 473 | BcStatus s; |
| 474 | BcResult *opd1, *opd2, res; |
| 475 | BcNum *n1, *n2; |
| 476 | bool cond; |
| 477 | ssize_t cmp; |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 478 | |
Gavin Howard | 296c98b | 2018-10-02 15:07:36 -0600 | [diff] [blame] | 479 | if ((s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, false))) return s; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 480 | if ((s = bc_num_init(&res.data.n, BC_NUM_DEF_SIZE))) return s; |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 481 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 482 | if (inst == BC_INST_BOOL_AND) |
| 483 | cond = bc_num_cmp(n1, &p->zero) && bc_num_cmp(n2, &p->zero); |
| 484 | else if (inst == BC_INST_BOOL_OR) |
| 485 | cond = bc_num_cmp(n1, &p->zero) || bc_num_cmp(n2, &p->zero); |
| 486 | else { |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 487 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 488 | cmp = bc_num_cmp(n1, n2); |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 489 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 490 | switch (inst) { |
Gavin Howard | 2882780 | 2018-09-27 11:01:51 -0600 | [diff] [blame] | 491 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 492 | case BC_INST_REL_EQ: |
| 493 | { |
| 494 | cond = cmp == 0; |
| 495 | break; |
| 496 | } |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 497 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 498 | case BC_INST_REL_LE: |
| 499 | { |
| 500 | cond = cmp <= 0; |
| 501 | break; |
| 502 | } |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 503 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 504 | case BC_INST_REL_GE: |
| 505 | { |
| 506 | cond = cmp >= 0; |
| 507 | break; |
| 508 | } |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 509 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 510 | case BC_INST_REL_NE: |
| 511 | { |
| 512 | cond = cmp != 0; |
| 513 | break; |
| 514 | } |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 515 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 516 | case BC_INST_REL_LT: |
| 517 | { |
| 518 | cond = cmp < 0; |
| 519 | break; |
| 520 | } |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 521 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 522 | case BC_INST_REL_GT: |
| 523 | { |
| 524 | cond = cmp > 0; |
| 525 | break; |
| 526 | } |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 527 | #ifndef NDEBUG |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 528 | default: |
| 529 | { |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 530 | assert(false); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 531 | break; |
| 532 | } |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 533 | #endif // NDEBUG |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 534 | } |
| 535 | } |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 536 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 537 | (cond ? bc_num_one : bc_num_zero)(&res.data.n); |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 538 | |
Gavin Howard | 1f3e758 | 2018-09-29 02:19:54 -0600 | [diff] [blame] | 539 | if ((s = bc_program_binOpRetire(p, &res))) goto err; |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 540 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 541 | return s; |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 542 | |
| 543 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 544 | bc_num_free(&res.data.n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 545 | return s; |
Gavin Howard | 843fa79 | 2018-02-27 16:42:52 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 548 | #ifdef DC_ENABLED |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 549 | BcStatus bc_program_assignStr(BcProgram *p, BcResult *r, BcVec *var, bool push) |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 550 | { |
| 551 | BcNum n2; |
| 552 | |
| 553 | memset(&n2, 0, sizeof(BcNum)); |
| 554 | n2.rdx = r->data.id.idx; |
| 555 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 556 | if (push) bc_vec_pop(&p->results); |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 557 | |
| 558 | return bc_vec_push(var, &n2); |
| 559 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 560 | #endif // DC_ENABLED |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 561 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 562 | BcStatus bc_program_copyToVar(BcProgram *p, char *name, bool var) { |
| 563 | |
| 564 | BcStatus s; |
| 565 | BcResult *ptr, r; |
| 566 | BcVec *v; |
| 567 | BcNum *n; |
| 568 | |
| 569 | if (!BC_PROG_CHECK_STACK(&p->results, 1)) return BC_STATUS_EXEC_SMALL_STACK; |
| 570 | |
| 571 | ptr = bc_vec_top(&p->results); |
| 572 | if ((ptr->t == BC_RESULT_ARRAY) != !var) return BC_STATUS_EXEC_BAD_TYPE; |
| 573 | |
| 574 | if ((s = bc_program_search(p, name, &v, var))) return s; |
| 575 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 576 | #ifdef DC_ENABLED |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 577 | if (ptr->t == BC_RESULT_STR && !var) return BC_STATUS_EXEC_BAD_TYPE; |
| 578 | if (ptr->t == BC_RESULT_STR) return bc_program_assignStr(p, ptr, v, true); |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 579 | #endif // DC_ENABLED |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 580 | |
| 581 | if ((s = bc_program_num(p, ptr, &n, false))) return s; |
| 582 | |
| 583 | // Do this once more to make sure that pointers were not invalidated. |
| 584 | if ((s = bc_program_search(p, name, &v, var))) return s; |
| 585 | |
| 586 | if (var) { |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 587 | if ((s = bc_num_init(&r.data.n, BC_NUM_DEF_SIZE))) return s; |
| 588 | s = bc_num_copy(&r.data.n, n); |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 589 | } |
| 590 | else { |
| 591 | if ((s = bc_array_init(&r.data.array, true))) return s; |
| 592 | s = bc_array_copy(&r.data.array, (BcVec*) n); |
| 593 | } |
| 594 | |
| 595 | if (s || (s = bc_vec_push(v, &r.data))) goto err; |
| 596 | |
| 597 | bc_vec_pop(&p->results); |
| 598 | |
| 599 | return s; |
| 600 | |
| 601 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 602 | if (var) bc_num_free(&r.data.n); |
Gavin Howard | c9a9c47 | 2018-10-02 17:23:01 -0600 | [diff] [blame] | 603 | else bc_vec_free(&r.data.array); |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 604 | return s; |
| 605 | } |
| 606 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 607 | BcStatus bc_program_assign(BcProgram *p, uint8_t inst) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 608 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 609 | BcStatus s; |
| 610 | BcResult *left, *right, res; |
| 611 | BcNum *l, *r; |
| 612 | unsigned long val, max; |
| 613 | bool assign = inst == BC_INST_ASSIGN; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 614 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 615 | if ((s = bc_program_binOpPrep(p, &left, &l, &right, &r, assign))) return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 616 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 617 | #ifdef DC_ENABLED |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 618 | assert(left->t != BC_RESULT_STR); |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 619 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 620 | if (right->t == BC_RESULT_STR) { |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 621 | |
| 622 | BcVec *v; |
| 623 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 624 | assert(assign && left->t == BC_RESULT_VAR); |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 625 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 626 | if ((s = bc_program_search(p, right->data.id.name, &v, true))) return s; |
| 627 | if ((s = bc_program_assignStr(p, right, v, false))) return s; |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 628 | bc_vec_pop(&p->results); |
| 629 | |
| 630 | return s; |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 631 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 632 | #endif // DC_ENABLED |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 633 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 634 | if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP) |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 635 | return BC_STATUS_PARSE_BAD_ASSIGN; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 636 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 637 | #ifdef BC_ENABLED |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 638 | if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &p->zero)) |
| 639 | return BC_STATUS_MATH_DIVIDE_BY_ZERO; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 640 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 641 | if (assign) s = bc_num_copy(l, r); |
| 642 | else s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, p->scale); |
Gavin Howard | d5a51e6 | 2018-03-26 12:34:27 -0600 | [diff] [blame] | 643 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 644 | if (s) return s; |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 645 | #else // BC_ENABLED |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 646 | assert(assign); |
| 647 | if ((s = bc_num_copy(l, r))) return s; |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 648 | #endif // BC_ENABLED |
Gavin Howard | d5a51e6 | 2018-03-26 12:34:27 -0600 | [diff] [blame] | 649 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 650 | if (left->t == BC_RESULT_IBASE || left->t == BC_RESULT_OBASE) { |
Gavin Howard | d5a51e6 | 2018-03-26 12:34:27 -0600 | [diff] [blame] | 651 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 652 | size_t *ptr = left->t == BC_RESULT_IBASE ? &p->ib_t : &p->ob_t; |
Gavin Howard | d5a51e6 | 2018-03-26 12:34:27 -0600 | [diff] [blame] | 653 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 654 | if ((s = bc_num_ulong(r, &val))) return s; |
Gavin Howard | d5a51e6 | 2018-03-26 12:34:27 -0600 | [diff] [blame] | 655 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 656 | max = left->t == BC_RESULT_IBASE ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Gavin Howard | 691cfd5 | 2018-09-21 10:47:58 -0600 | [diff] [blame] | 657 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 658 | if (val < BC_NUM_MIN_BASE || val > max) |
Gavin Howard | 296c98b | 2018-10-02 15:07:36 -0600 | [diff] [blame] | 659 | return left->t - BC_RESULT_IBASE + BC_STATUS_EXEC_BAD_IBASE; |
Gavin Howard | d5a51e6 | 2018-03-26 12:34:27 -0600 | [diff] [blame] | 660 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 661 | *ptr = (size_t) val; |
| 662 | } |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 663 | else if (left->t == BC_RESULT_SCALE) { |
Gavin Howard | d5a51e6 | 2018-03-26 12:34:27 -0600 | [diff] [blame] | 664 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 665 | if ((s = bc_num_ulong(l, &val))) return s; |
| 666 | if (val > (unsigned long) BC_MAX_SCALE) return BC_STATUS_EXEC_BAD_SCALE; |
Gavin Howard | d5a51e6 | 2018-03-26 12:34:27 -0600 | [diff] [blame] | 667 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 668 | p->scale = (size_t) val; |
| 669 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 670 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 671 | if ((s = bc_num_init(&res.data.n, l->len))) return s; |
| 672 | if ((s = bc_num_copy(&res.data.n, l))) goto err; |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 673 | res.t = BC_RESULT_TEMP; |
Gavin Howard | 1f3e758 | 2018-09-29 02:19:54 -0600 | [diff] [blame] | 674 | if ((s = bc_program_binOpRetire(p, &res))) goto err; |
Gavin Howard | b818116 | 2018-02-28 12:58:09 -0700 | [diff] [blame] | 675 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 676 | return s; |
Gavin Howard | b818116 | 2018-02-28 12:58:09 -0700 | [diff] [blame] | 677 | |
| 678 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 679 | bc_num_free(&res.data.n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 680 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 683 | BcStatus bc_program_pushVar(BcProgram *p, char *code, size_t *bgn, bool pop) { |
| 684 | |
| 685 | BcStatus s; |
| 686 | BcResult r; |
Gavin Howard | c9a9c47 | 2018-10-02 17:23:01 -0600 | [diff] [blame] | 687 | char *name; |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 688 | #ifdef DC_ENABLED // Exclude |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 689 | BcNum *num; |
| 690 | BcVec *v; |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 691 | #else // DC_ENABLED |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 692 | (void) pop; |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 693 | #endif // DC_ENABLED Exclude |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 694 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 695 | if (!(name = bc_program_name(code, bgn))) return BC_STATUS_ALLOC_ERR; |
| 696 | r.t = BC_RESULT_VAR; |
| 697 | r.data.id.name = name; |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 698 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 699 | #ifdef DC_ENABLED |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 700 | if ((s = bc_program_search(p, name, &v, true))) goto err; |
| 701 | assert(BC_PROG_CHECK_STACK(v, 1)); |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 702 | num = bc_vec_top(v); |
| 703 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 704 | if (BC_PROG_STR_VAR(num) || pop) { |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 705 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 706 | if (pop) { |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 707 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 708 | r.t = BC_RESULT_TEMP; |
| 709 | |
| 710 | if (!BC_PROG_CHECK_STACK(v, 2)) { |
| 711 | s = BC_STATUS_EXEC_SMALL_STACK; |
| 712 | goto err; |
| 713 | } |
| 714 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 715 | if ((s = bc_num_init(&r.data.n, BC_NUM_DEF_SIZE))) goto err; |
| 716 | if ((s = bc_num_copy(&r.data.n, num))) goto copy_err; |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 717 | |
| 718 | bc_vec_pop(v); |
| 719 | } |
| 720 | else { |
| 721 | r.t = BC_RESULT_STR; |
| 722 | r.data.id.idx = num->rdx; |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 723 | } |
| 724 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 725 | free(name); |
| 726 | name = NULL; |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 727 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 728 | #endif // DC_ENABLED |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 729 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 730 | s = bc_vec_push(&p->results, &r); |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 731 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 732 | #ifdef DC_ENABLED |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 733 | copy_err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 734 | if (s && pop) bc_num_free(&r.data.n); |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 735 | err: |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 736 | #endif // DC_ENABLED |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 737 | if (name && s) free(name); |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 738 | return s; |
| 739 | } |
| 740 | |
| 741 | BcStatus bc_program_pushArray(BcProgram *p, char *code, |
| 742 | size_t *bgn, uint8_t inst) |
| 743 | { |
| 744 | BcStatus s; |
| 745 | BcResult r; |
| 746 | BcNum *num; |
| 747 | |
| 748 | if (!(r.data.id.name = bc_program_name(code, bgn))) |
| 749 | return BC_STATUS_ALLOC_ERR; |
| 750 | |
| 751 | if (inst == BC_INST_ARRAY) { |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 752 | r.t = BC_RESULT_ARRAY; |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 753 | s = bc_vec_push(&p->results, &r); |
| 754 | } |
| 755 | else { |
| 756 | |
| 757 | BcResult *operand; |
| 758 | unsigned long temp; |
| 759 | |
| 760 | if ((s = bc_program_prep(p, &operand, &num, false))) goto err; |
| 761 | if ((s = bc_num_ulong(num, &temp))) goto err; |
| 762 | |
| 763 | if (temp > (unsigned long) BC_MAX_DIM) { |
| 764 | s = BC_STATUS_EXEC_ARRAY_LEN; |
| 765 | goto err; |
| 766 | } |
| 767 | |
| 768 | r.data.id.idx = (size_t) temp; |
| 769 | s = bc_program_retire(p, &r, BC_RESULT_ARRAY_ELEM); |
| 770 | } |
| 771 | |
| 772 | err: |
| 773 | if (s) free(r.data.id.name); |
| 774 | return s; |
| 775 | } |
| 776 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 777 | #ifdef BC_ENABLED |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 778 | BcStatus bc_program_incdec(BcProgram *p, uint8_t inst) { |
| 779 | |
| 780 | BcStatus s; |
| 781 | BcResult *ptr, res, copy; |
| 782 | BcNum *num; |
| 783 | uint8_t inst2 = inst; |
| 784 | |
| 785 | if ((s = bc_program_prep(p, &ptr, &num, false))) return s; |
| 786 | |
| 787 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 788 | copy.t = BC_RESULT_TEMP; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 789 | if ((s = bc_num_init(©.data.n, num->len))) return s; |
| 790 | if ((s = bc_num_copy(©.data.n, num))) goto err; |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 791 | } |
| 792 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 793 | res.t = BC_RESULT_ONE; |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 794 | inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? |
| 795 | BC_INST_ASSIGN_PLUS : BC_INST_ASSIGN_MINUS; |
| 796 | |
| 797 | if ((s = bc_vec_push(&p->results, &res))) goto err; |
| 798 | if ((s = bc_program_assign(p, inst))) goto err; |
| 799 | |
| 800 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
| 801 | bc_vec_pop(&p->results); |
| 802 | if ((s = bc_vec_push(&p->results, ©))) goto err; |
| 803 | } |
| 804 | |
| 805 | return s; |
| 806 | |
| 807 | err: |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 808 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 809 | bc_num_free(©.data.n); |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 810 | return s; |
| 811 | } |
| 812 | |
Gavin Howard | aac24bf | 2018-09-22 23:24:53 -0600 | [diff] [blame] | 813 | BcStatus bc_program_call(BcProgram *p, char *code, size_t *idx) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 814 | |
Gavin Howard | e956c61 | 2018-09-27 11:40:28 -0600 | [diff] [blame] | 815 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 816 | BcInstPtr ip; |
| 817 | size_t i, nparams = bc_program_index(code, idx); |
| 818 | BcFunc *func; |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 819 | BcVec *v; |
| 820 | BcAuto *a; |
| 821 | BcResultData param; |
| 822 | BcResult *arg; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 823 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 824 | ip.idx = 0; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 825 | ip.func = bc_program_index(code, idx); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 826 | func = bc_vec_item(&p->fns, ip.func); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 827 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 828 | if (!func->code.len) return BC_STATUS_EXEC_UNDEFINED_FUNC; |
| 829 | if (nparams != func->nparams) return BC_STATUS_EXEC_MISMATCHED_PARAMS; |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 830 | ip.len = p->results.len - nparams; |
| 831 | |
| 832 | assert(BC_PROG_CHECK_STACK(&p->results, nparams)); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 833 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 834 | for (i = 0; i < nparams; ++i) { |
Gavin Howard | d6e3b7b | 2018-02-28 12:31:58 -0700 | [diff] [blame] | 835 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 836 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
| 837 | arg = bc_vec_top(&p->results); |
Gavin Howard | e87dcb8 | 2018-09-29 21:37:54 -0600 | [diff] [blame] | 838 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 839 | if (!a->var != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR) |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 840 | return BC_STATUS_EXEC_BAD_TYPE; |
Gavin Howard | 1ce8325 | 2018-09-04 15:21:47 -0600 | [diff] [blame] | 841 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 842 | if ((s = bc_program_copyToVar(p, a->name, a->var))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 843 | } |
Gavin Howard | 90f13ad | 2018-03-17 13:32:16 -0600 | [diff] [blame] | 844 | |
Gavin Howard | e87dcb8 | 2018-09-29 21:37:54 -0600 | [diff] [blame] | 845 | for (; i < func->autos.len; ++i) { |
Gavin Howard | 90f13ad | 2018-03-17 13:32:16 -0600 | [diff] [blame] | 846 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 847 | a = bc_vec_item(&func->autos, i); |
| 848 | if ((s = bc_program_search(p, a->name, &v, a->var))) return s; |
Gavin Howard | 90f13ad | 2018-03-17 13:32:16 -0600 | [diff] [blame] | 849 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 850 | if (a->var) { |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 851 | if ((s = bc_num_init(¶m.n, BC_NUM_DEF_SIZE))) return s; |
| 852 | if ((s = bc_vec_push(v, ¶m.n))) goto err; |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 853 | } |
| 854 | else { |
| 855 | if ((s = bc_array_init(¶m.array, true))) return s; |
| 856 | if ((s = bc_vec_push(v, ¶m.array))) goto err; |
| 857 | } |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 858 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 859 | |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 860 | return bc_vec_push(&p->stack, &ip); |
Gavin Howard | 5732343 | 2018-03-14 20:06:35 -0600 | [diff] [blame] | 861 | |
| 862 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 863 | if (a->var) bc_num_free(¶m.n); |
Gavin Howard | c9a9c47 | 2018-10-02 17:23:01 -0600 | [diff] [blame] | 864 | else bc_vec_free(¶m.array); |
Gavin Howard | e956c61 | 2018-09-27 11:40:28 -0600 | [diff] [blame] | 865 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 866 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 867 | #endif // BC_ENABLED |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 868 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 869 | BcStatus bc_program_return(BcProgram *p, uint8_t inst) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 870 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 871 | BcStatus s; |
| 872 | BcResult res; |
| 873 | BcFunc *f; |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 874 | size_t i; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 875 | BcInstPtr *ip = bc_vec_top(&p->stack); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 876 | |
Gavin Howard | 1e758c8 | 2018-09-29 19:57:37 -0600 | [diff] [blame] | 877 | assert(BC_PROG_CHECK_STACK(&p->stack, 2)); |
Gavin Howard | 2192547 | 2018-09-29 19:29:25 -0600 | [diff] [blame] | 878 | |
Gavin Howard | 1e758c8 | 2018-09-29 19:57:37 -0600 | [diff] [blame] | 879 | if (!BC_PROG_CHECK_STACK(&p->results, ip->len + inst == BC_INST_RET)) |
Gavin Howard | 2192547 | 2018-09-29 19:29:25 -0600 | [diff] [blame] | 880 | return BC_STATUS_EXEC_SMALL_STACK; |
Gavin Howard | c410b0f | 2018-02-27 16:57:05 -0700 | [diff] [blame] | 881 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 882 | f = bc_vec_item(&p->fns, ip->func); |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 883 | res.t = BC_RESULT_TEMP; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 884 | |
Gavin Howard | 65bf036 | 2018-09-28 11:28:56 -0600 | [diff] [blame] | 885 | if (inst == BC_INST_RET) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 886 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 887 | BcNum *num; |
| 888 | BcResult *operand = bc_vec_top(&p->results); |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 889 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 890 | if ((s = bc_program_num(p, operand, &num, false))) return s; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 891 | if ((s = bc_num_init(&res.data.n, num->len))) return s; |
| 892 | if ((s = bc_num_copy(&res.data.n, num))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 893 | } |
| 894 | else { |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 895 | if ((s = bc_num_init(&res.data.n, BC_NUM_DEF_SIZE))) return s; |
| 896 | bc_num_zero(&res.data.n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 897 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 898 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 899 | // We need to pop arguments as well, so this takes that into account. |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 900 | for (i = 0; i < f->autos.len; ++i) { |
Gavin Howard | c410b0f | 2018-02-27 16:57:05 -0700 | [diff] [blame] | 901 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 902 | BcVec *v; |
| 903 | BcAuto *a = bc_vec_item(&f->autos, i); |
| 904 | |
| 905 | if ((s = bc_program_search(p, a->name, &v, a->var))) goto err; |
| 906 | |
| 907 | bc_vec_pop(v); |
| 908 | } |
| 909 | |
| 910 | bc_vec_npop(&p->results, p->results.len - ip->len); |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 911 | if ((s = bc_vec_push(&p->results, &res))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 912 | bc_vec_pop(&p->stack); |
Gavin Howard | 0eb2a37 | 2018-03-24 09:52:55 -0600 | [diff] [blame] | 913 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 914 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 915 | |
| 916 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 917 | bc_num_free(&res.data.n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 918 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 921 | unsigned long bc_program_scale(BcNum *n) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 922 | return (unsigned long) n->rdx; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Gavin Howard | 77d489d | 2018-09-04 15:14:06 -0600 | [diff] [blame] | 925 | unsigned long bc_program_len(BcNum *n) { |
Gavin Howard | 6e2fd06 | 2018-03-20 19:34:12 -0600 | [diff] [blame] | 926 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 927 | unsigned long len = n->len; |
Gavin Howard | 6e2fd06 | 2018-03-20 19:34:12 -0600 | [diff] [blame] | 928 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 929 | if (n->rdx == n->len) { |
| 930 | size_t i; |
| 931 | for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i); |
| 932 | } |
Gavin Howard | 6e2fd06 | 2018-03-20 19:34:12 -0600 | [diff] [blame] | 933 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 934 | return len; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 937 | BcStatus bc_program_builtin(BcProgram *p, uint8_t inst) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 938 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 939 | BcStatus s; |
| 940 | BcResult *opnd; |
| 941 | BcNum *num; |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 942 | BcResult res; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 943 | |
Gavin Howard | b4584aa | 2018-09-28 10:45:33 -0600 | [diff] [blame] | 944 | if ((s = bc_program_prep(p, &opnd, &num, inst == BC_INST_LENGTH))) return s; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 945 | if ((s = bc_num_init(&res.data.n, BC_NUM_DEF_SIZE))) return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 946 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 947 | if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.data.n, p->scale); |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 948 | else if (inst == BC_INST_LENGTH && opnd->t == BC_RESULT_ARRAY) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 949 | BcVec *vec = (BcVec*) num; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 950 | s = bc_num_ulong2num(&res.data.n, (unsigned long) vec->len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 951 | } |
| 952 | else { |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 953 | assert(opnd->t != BC_RESULT_ARRAY); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 954 | BcProgramBuiltIn f = inst == BC_INST_LENGTH ? bc_program_len : |
| 955 | bc_program_scale; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 956 | s = bc_num_ulong2num(&res.data.n, f(num)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 957 | } |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 958 | |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 959 | if (s || (s = bc_program_retire(p, &res, BC_RESULT_TEMP))) goto err; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 960 | |
Gavin Howard | 296c98b | 2018-10-02 15:07:36 -0600 | [diff] [blame] | 961 | return s; |
| 962 | |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 963 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 964 | bc_num_free(&res.data.n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 965 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 968 | #ifdef DC_ENABLED |
Gavin Howard | 24d3dbe | 2018-10-04 10:55:40 -0600 | [diff] [blame] | 969 | BcStatus bc_program_divmod(BcProgram *p) { |
| 970 | |
| 971 | BcStatus s; |
| 972 | BcResult *opd1, *opd2, res, res2; |
| 973 | BcNum *n1, *n2; |
| 974 | |
| 975 | if ((s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, false))) return s; |
| 976 | if ((s = bc_num_init(&res.data.n, BC_NUM_DEF_SIZE))) return s; |
| 977 | if ((s = bc_num_init(&res2.data.n, n2->len))) goto res2_err; |
| 978 | |
| 979 | if ((s = bc_num_div(n1, n2, &res2.data.n, p->scale))) goto err; |
| 980 | if ((s = bc_num_rem(n1, n2, &res.data.n, p->scale))) goto err; |
| 981 | |
| 982 | if ((s = bc_program_binOpRetire(p, &res2))) goto err; |
| 983 | res.t = BC_RESULT_TEMP; |
| 984 | if ((s = bc_vec_push(&p->results, &res))) goto res2_err; |
| 985 | |
| 986 | return s; |
| 987 | |
| 988 | err: |
| 989 | bc_num_free(&res2.data.n); |
| 990 | res2_err: |
| 991 | bc_num_free(&res.data.n); |
| 992 | return s; |
| 993 | } |
| 994 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 995 | BcStatus bc_program_modexp(BcProgram *p) { |
| 996 | |
| 997 | BcStatus s; |
| 998 | BcResult *opd1, *opd2, *opd3, res; |
| 999 | BcNum *n1, *n2, *n3; |
| 1000 | |
Gavin Howard | 1e758c8 | 2018-09-29 19:57:37 -0600 | [diff] [blame] | 1001 | if (!BC_PROG_CHECK_STACK(&p->results, 3)) return BC_STATUS_EXEC_SMALL_STACK; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1002 | if ((s = bc_program_binOpPrep(p, &opd2, &n2, &opd3, &n3, false))) return s; |
| 1003 | |
| 1004 | opd1 = bc_vec_item_rev(&p->results, 2); |
| 1005 | if ((s = bc_program_num(p, opd1, &n1, false))) return s; |
| 1006 | |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 1007 | // Make sure that the values have their pointers updated, if necessary. |
Gavin Howard | 4da6831 | 2018-10-02 15:08:02 -0600 | [diff] [blame] | 1008 | if (opd1->t == BC_RESULT_VAR || opd1->t == BC_RESULT_ARRAY_ELEM) { |
| 1009 | if (opd1->t == opd2->t) { |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 1010 | if ((s = bc_program_num(p, opd2, &n2, false))) return s; |
| 1011 | } |
Gavin Howard | 4da6831 | 2018-10-02 15:08:02 -0600 | [diff] [blame] | 1012 | if (opd1->t == opd3->t) { |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 1013 | if ((s = bc_program_num(p, opd3, &n3, false))) return s; |
| 1014 | } |
| 1015 | } |
| 1016 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1017 | if ((s = bc_num_init(&res.data.n, n3->len))) return s; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1018 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1019 | if ((s = bc_num_modexp(n1, n2, n3, &res.data.n, p->scale))) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1020 | bc_vec_pop(&p->results); |
| 1021 | |
| 1022 | if ((s = bc_program_binOpRetire(p, &res))) goto err; |
| 1023 | |
| 1024 | return s; |
| 1025 | |
| 1026 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1027 | bc_num_free(&res.data.n); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1028 | return s; |
| 1029 | } |
| 1030 | |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1031 | BcStatus bc_program_stackLen(BcProgram *p) { |
| 1032 | |
| 1033 | BcStatus s; |
| 1034 | BcResult res; |
| 1035 | size_t len = p->results.len; |
| 1036 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 1037 | res.t = BC_RESULT_TEMP; |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1038 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1039 | if ((s = bc_num_init(&res.data.n, BC_NUM_DEF_SIZE))) return s; |
| 1040 | if ((s = bc_num_ulong2num(&res.data.n, len))) goto err; |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 1041 | if ((s = bc_vec_push(&p->results, &res))) goto err; |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1042 | |
| 1043 | return s; |
| 1044 | |
| 1045 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1046 | bc_num_free(&res.data.n); |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1047 | return s; |
| 1048 | } |
| 1049 | |
| 1050 | BcStatus bc_program_nquit(BcProgram *p) { |
| 1051 | |
| 1052 | BcStatus s; |
| 1053 | BcResult *opnd; |
| 1054 | BcNum *num; |
| 1055 | unsigned long val; |
| 1056 | |
| 1057 | if ((s = bc_program_prep(p, &opnd, &num, false))) return s; |
| 1058 | if ((s = bc_num_ulong(num, &val))) return s; |
| 1059 | |
| 1060 | bc_vec_pop(&p->results); |
| 1061 | |
| 1062 | if (p->stack.len < val) return BC_STATUS_EXEC_SMALL_STACK; |
| 1063 | else if (p->stack.len == val) return BC_STATUS_QUIT; |
| 1064 | |
| 1065 | bc_vec_npop(&p->stack, val); |
| 1066 | |
| 1067 | return s; |
| 1068 | } |
Gavin Howard | 503f393 | 2018-09-29 19:24:29 -0600 | [diff] [blame] | 1069 | |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 1070 | BcStatus bc_program_executeStr(BcProgram *p) { |
Gavin Howard | 077e30a | 2018-09-29 22:29:56 -0600 | [diff] [blame] | 1071 | |
| 1072 | BcStatus s; |
| 1073 | BcResult *r; |
| 1074 | char *str; |
| 1075 | BcFunc *f; |
| 1076 | BcParse prs; |
| 1077 | BcInstPtr ip; |
| 1078 | size_t fidx; |
| 1079 | bool do_parse; |
| 1080 | |
| 1081 | if (!BC_PROG_CHECK_STACK(&p->results, 1)) return BC_STATUS_EXEC_SMALL_STACK; |
| 1082 | |
| 1083 | r = bc_vec_top(&p->results); |
| 1084 | fidx = r->data.id.idx + 2; |
| 1085 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 1086 | if (r->t != BC_RESULT_STR) return BC_STATUS_SUCCESS; |
Gavin Howard | 077e30a | 2018-09-29 22:29:56 -0600 | [diff] [blame] | 1087 | |
| 1088 | assert(p->strs.len > r->data.id.idx && p->fns.len > fidx); |
| 1089 | |
| 1090 | str = bc_vec_item(&p->strs, r->data.id.idx); |
| 1091 | f = bc_vec_item(&p->fns, fidx); |
| 1092 | do_parse = !f->code.len; |
| 1093 | |
| 1094 | if (do_parse) { |
| 1095 | |
| 1096 | if ((s = p->parse_init(&prs, p))) return s; |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1097 | if ((s = bc_lex_text(&prs.l, str))) goto err; |
Gavin Howard | 077e30a | 2018-09-29 22:29:56 -0600 | [diff] [blame] | 1098 | |
Gavin Howard | 00a7a5f | 2018-10-01 12:30:38 -0600 | [diff] [blame] | 1099 | if ((s = p->parse_exp(&prs, &f->code, 0, bc_parse_next_read))) goto err; |
Gavin Howard | 077e30a | 2018-09-29 22:29:56 -0600 | [diff] [blame] | 1100 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1101 | if (prs.l.t.t != BC_LEX_EOF) { |
Gavin Howard | 077e30a | 2018-09-29 22:29:56 -0600 | [diff] [blame] | 1102 | s = BC_STATUS_PARSE_BAD_EXP; |
| 1103 | goto err; |
| 1104 | } |
| 1105 | |
| 1106 | if ((s = bc_vec_pushByte(&f->code, BC_INST_RET0))) goto err; |
| 1107 | bc_parse_free(&prs); |
| 1108 | } |
| 1109 | |
| 1110 | ip.idx = 0; |
| 1111 | ip.len = p->results.len; |
| 1112 | ip.func = fidx; |
| 1113 | |
| 1114 | return bc_vec_push(&p->stack, &ip); |
| 1115 | |
| 1116 | err: |
| 1117 | bc_parse_free(&prs); |
| 1118 | bc_vec_npop(&f->code, f->code.len); |
| 1119 | return s; |
| 1120 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1121 | #endif // DC_ENABLED |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1122 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 1123 | BcStatus bc_program_pushScale(BcProgram *p) { |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 1124 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1125 | BcStatus s; |
| 1126 | BcResult res; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 1127 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 1128 | res.t = BC_RESULT_SCALE; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 1129 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1130 | if ((s = bc_num_init(&res.data.n, BC_NUM_DEF_SIZE))) return s; |
| 1131 | s = bc_num_ulong2num(&res.data.n, (unsigned long) p->scale); |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 1132 | if (s || (s = bc_vec_push(&p->results, &res))) goto err; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 1133 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1134 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 1135 | |
| 1136 | err: |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1137 | bc_num_free(&res.data.n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1138 | return s; |
Gavin Howard | 8409507 | 2018-02-27 15:36:58 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Gavin Howard | d2a0525 | 2018-09-27 14:00:40 -0600 | [diff] [blame] | 1141 | BcStatus bc_program_init(BcProgram *p, size_t line_len, |
| 1142 | BcParseInit parse_init, BcParseExpr parse_expr) |
| 1143 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1144 | BcStatus s; |
| 1145 | size_t idx; |
| 1146 | char *main_name = NULL, *read_name = NULL; |
| 1147 | BcInstPtr ip; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 1148 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1149 | assert(p); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 1150 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1151 | assert((unsigned long) sysconf(_SC_BC_BASE_MAX) <= BC_MAX_OBASE); |
| 1152 | assert((unsigned long) sysconf(_SC_BC_DIM_MAX) <= BC_MAX_DIM); |
| 1153 | assert((unsigned long) sysconf(_SC_BC_SCALE_MAX) <= BC_MAX_SCALE); |
| 1154 | assert((unsigned long) sysconf(_SC_BC_STRING_MAX) <= BC_MAX_STRING); |
Gavin Howard | 57c843e | 2018-03-28 19:28:08 -0600 | [diff] [blame] | 1155 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1156 | p->nchars = p->scale = 0; |
Gavin Howard | 08f74ad | 2018-09-28 14:46:29 -0600 | [diff] [blame] | 1157 | p->len = line_len; |
Gavin Howard | d2a0525 | 2018-09-27 14:00:40 -0600 | [diff] [blame] | 1158 | p->parse_init = parse_init; |
Gavin Howard | 00a7a5f | 2018-10-01 12:30:38 -0600 | [diff] [blame] | 1159 | p->parse_exp = parse_expr; |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 1160 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1161 | if ((s = bc_num_init(&p->ib, BC_NUM_DEF_SIZE))) return s; |
| 1162 | bc_num_ten(&p->ib); |
| 1163 | p->ib_t = 10; |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 1164 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1165 | if ((s = bc_num_init(&p->ob, BC_NUM_DEF_SIZE))) goto obase_err; |
| 1166 | bc_num_ten(&p->ob); |
| 1167 | p->ob_t = 10; |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 1168 | |
Gavin Howard | 0361074 | 2018-09-27 10:48:29 -0600 | [diff] [blame] | 1169 | if ((s = bc_num_init(&p->hexb, BC_NUM_DEF_SIZE))) goto hexb_err; |
| 1170 | bc_num_ten(&p->hexb); |
| 1171 | p->hexb.num[0] = 6; |
| 1172 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1173 | if ((s = bc_num_init(&p->last, BC_NUM_DEF_SIZE))) goto last_err; |
| 1174 | bc_num_zero(&p->last); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1175 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1176 | if ((s = bc_num_init(&p->zero, BC_NUM_DEF_SIZE))) goto zero_err; |
| 1177 | bc_num_zero(&p->zero); |
Gavin Howard | 5a8949e | 2018-02-05 16:46:06 -0700 | [diff] [blame] | 1178 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1179 | if ((s = bc_num_init(&p->one, BC_NUM_DEF_SIZE))) goto one_err; |
| 1180 | bc_num_one(&p->one); |
Gavin Howard | 645200e | 2018-02-15 15:33:35 -0700 | [diff] [blame] | 1181 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1182 | if ((s = bc_vec_init(&p->fns, sizeof(BcFunc), bc_func_free))) goto fn_err; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 1183 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1184 | s = bc_veco_init(&p->fn_map, sizeof(BcEntry), bc_entry_free, bc_entry_cmp); |
| 1185 | if (s) goto func_map_err; |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 1186 | |
Gavin Howard | 3bc436f | 2018-09-28 14:02:34 -0600 | [diff] [blame] | 1187 | if (!(main_name = strdup(bc_func_main))) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1188 | s = BC_STATUS_ALLOC_ERR; |
| 1189 | goto name_err; |
| 1190 | } |
Gavin Howard | d50539a | 2018-02-08 20:52:57 -0700 | [diff] [blame] | 1191 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1192 | s = bc_program_addFunc(p, main_name, &idx); |
| 1193 | if (s || idx != BC_PROG_MAIN) goto name_err; |
| 1194 | main_name = NULL; |
Gavin Howard | b42810f | 2018-03-09 10:07:58 -0700 | [diff] [blame] | 1195 | |
Gavin Howard | 3bc436f | 2018-09-28 14:02:34 -0600 | [diff] [blame] | 1196 | if (!(read_name = strdup(bc_func_read))) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1197 | s = BC_STATUS_ALLOC_ERR; |
| 1198 | goto name_err; |
| 1199 | } |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 1200 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1201 | s = bc_program_addFunc(p, read_name, &idx); |
| 1202 | if (s || idx != BC_PROG_READ) goto name_err; |
| 1203 | read_name = NULL; |
Gavin Howard | b42810f | 2018-03-09 10:07:58 -0700 | [diff] [blame] | 1204 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 1205 | if ((s = bc_vec_init(&p->vars, sizeof(BcVec), bc_vec_free))) goto name_err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1206 | s = bc_veco_init(&p->var_map, sizeof(BcEntry), bc_entry_free, bc_entry_cmp); |
| 1207 | if (s) goto var_map_err; |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 1208 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1209 | if ((s = bc_vec_init(&p->arrs, sizeof(BcVec), bc_vec_free))) goto arr_err; |
| 1210 | s = bc_veco_init(&p->arr_map, sizeof(BcEntry), bc_entry_free, bc_entry_cmp); |
| 1211 | if (s) goto array_map_err; |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 1212 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1213 | s = bc_vec_init(&p->strs, sizeof(char*), bc_string_free); |
| 1214 | if (s) goto string_err; |
Gavin Howard | 5a8949e | 2018-02-05 16:46:06 -0700 | [diff] [blame] | 1215 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1216 | s = bc_vec_init(&p->consts, sizeof(char*), bc_string_free); |
| 1217 | if (s) goto const_err; |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 1218 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1219 | s = bc_vec_init(&p->results, sizeof(BcResult), bc_result_free); |
| 1220 | if (s) goto expr_err; |
Gavin Howard | 2534a81 | 2018-02-10 15:34:15 -0700 | [diff] [blame] | 1221 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1222 | if ((s = bc_vec_init(&p->stack, sizeof(BcInstPtr), NULL))) goto stack_err; |
Gavin Howard | 2534a81 | 2018-02-10 15:34:15 -0700 | [diff] [blame] | 1223 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1224 | memset(&ip, 0, sizeof(BcInstPtr)); |
Gavin Howard | 2534a81 | 2018-02-10 15:34:15 -0700 | [diff] [blame] | 1225 | |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 1226 | if ((s = bc_vec_push(&p->stack, &ip))) goto push_err; |
Gavin Howard | d28e80a | 2018-01-23 18:24:18 -0700 | [diff] [blame] | 1227 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1228 | return s; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 1229 | |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 1230 | push_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1231 | bc_vec_free(&p->stack); |
Gavin Howard | 2534a81 | 2018-02-10 15:34:15 -0700 | [diff] [blame] | 1232 | stack_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1233 | bc_vec_free(&p->results); |
Gavin Howard | 2534a81 | 2018-02-10 15:34:15 -0700 | [diff] [blame] | 1234 | expr_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1235 | bc_vec_free(&p->consts); |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 1236 | const_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1237 | bc_vec_free(&p->strs); |
Gavin Howard | 5a8949e | 2018-02-05 16:46:06 -0700 | [diff] [blame] | 1238 | string_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1239 | bc_veco_free(&p->arr_map); |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 1240 | array_map_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1241 | bc_vec_free(&p->arrs); |
| 1242 | arr_err: |
| 1243 | bc_veco_free(&p->var_map); |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 1244 | var_map_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1245 | bc_vec_free(&p->vars); |
Gavin Howard | d50539a | 2018-02-08 20:52:57 -0700 | [diff] [blame] | 1246 | name_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1247 | bc_veco_free(&p->fn_map); |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 1248 | func_map_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1249 | bc_vec_free(&p->fns); |
| 1250 | fn_err: |
| 1251 | bc_num_free(&p->one); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1252 | one_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1253 | bc_num_free(&p->zero); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1254 | zero_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1255 | bc_num_free(&p->last); |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 1256 | last_err: |
Gavin Howard | 0361074 | 2018-09-27 10:48:29 -0600 | [diff] [blame] | 1257 | bc_num_free(&p->hexb); |
| 1258 | hexb_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1259 | bc_num_free(&p->ob); |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 1260 | obase_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1261 | bc_num_free(&p->ib); |
| 1262 | return s; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Gavin Howard | 859e290 | 2018-03-17 14:23:35 -0600 | [diff] [blame] | 1265 | BcStatus bc_program_addFunc(BcProgram *p, char *name, size_t *idx) { |
Gavin Howard | 3b90f9f | 2018-01-17 13:01:33 -0700 | [diff] [blame] | 1266 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1267 | BcStatus s; |
| 1268 | BcEntry entry, *entry_ptr; |
| 1269 | BcFunc f; |
Gavin Howard | ab67e7c | 2018-02-09 10:35:44 -0700 | [diff] [blame] | 1270 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1271 | assert(p && name && idx); |
Gavin Howard | 3b90f9f | 2018-01-17 13:01:33 -0700 | [diff] [blame] | 1272 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1273 | entry.name = name; |
| 1274 | entry.idx = p->fns.len; |
Gavin Howard | ab67e7c | 2018-02-09 10:35:44 -0700 | [diff] [blame] | 1275 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1276 | if ((s = bc_veco_insert(&p->fn_map, &entry, idx))) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1277 | free(name); |
| 1278 | if (s != BC_STATUS_VEC_ITEM_EXISTS) return s; |
| 1279 | } |
Gavin Howard | ded20a2 | 2018-03-07 15:15:19 -0700 | [diff] [blame] | 1280 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1281 | entry_ptr = bc_veco_item(&p->fn_map, *idx); |
| 1282 | *idx = entry_ptr->idx; |
Gavin Howard | ab67e7c | 2018-02-09 10:35:44 -0700 | [diff] [blame] | 1283 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1284 | if (s == BC_STATUS_VEC_ITEM_EXISTS) { |
Gavin Howard | ab67e7c | 2018-02-09 10:35:44 -0700 | [diff] [blame] | 1285 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1286 | BcFunc *func = bc_vec_item(&p->fns, entry_ptr->idx); |
| 1287 | s = BC_STATUS_SUCCESS; |
Gavin Howard | ab67e7c | 2018-02-09 10:35:44 -0700 | [diff] [blame] | 1288 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1289 | // We need to reset these, so the function can be repopulated. |
| 1290 | func->nparams = 0; |
| 1291 | bc_vec_npop(&func->autos, func->autos.len); |
| 1292 | bc_vec_npop(&func->code, func->code.len); |
| 1293 | bc_vec_npop(&func->labels, func->labels.len); |
| 1294 | } |
| 1295 | else { |
| 1296 | if ((s = bc_func_init(&f))) return s; |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 1297 | if ((s = bc_vec_push(&p->fns, &f))) bc_func_free(&f); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1298 | } |
Gavin Howard | ded20a2 | 2018-03-07 15:15:19 -0700 | [diff] [blame] | 1299 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1300 | return s; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1303 | BcStatus bc_program_reset(BcProgram *p, BcStatus s) { |
Gavin Howard | f4167dc | 2018-03-26 14:19:28 -0600 | [diff] [blame] | 1304 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1305 | BcFunc *f; |
| 1306 | BcInstPtr *ip; |
Gavin Howard | f4167dc | 2018-03-26 14:19:28 -0600 | [diff] [blame] | 1307 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1308 | bc_vec_npop(&p->stack, p->stack.len - 1); |
| 1309 | bc_vec_npop(&p->results, p->results.len); |
Gavin Howard | abdc2d3 | 2018-03-27 08:01:08 -0600 | [diff] [blame] | 1310 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1311 | f = bc_vec_item(&p->fns, 0); |
| 1312 | ip = bc_vec_top(&p->stack); |
| 1313 | ip->idx = f->code.len; |
Gavin Howard | f4167dc | 2018-03-26 14:19:28 -0600 | [diff] [blame] | 1314 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1315 | if (!s && bcg.signe && !bcg.tty) return BC_STATUS_QUIT; |
Gavin Howard | abdc2d3 | 2018-03-27 08:01:08 -0600 | [diff] [blame] | 1316 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1317 | bcg.sigc += bcg.signe; |
| 1318 | bcg.signe = bcg.sig != bcg.sigc; |
Gavin Howard | abdc2d3 | 2018-03-27 08:01:08 -0600 | [diff] [blame] | 1319 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1320 | if (!s || s == BC_STATUS_EXEC_SIGNAL) { |
Gavin Howard | ca893d7 | 2018-09-28 14:15:16 -0600 | [diff] [blame] | 1321 | if (bcg.ttyin) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1322 | if (fputs(bc_program_ready_msg, stderr) < 0 || fflush(stderr) < 0) |
| 1323 | s = BC_STATUS_IO_ERR; |
| 1324 | else s = BC_STATUS_SUCCESS; |
| 1325 | } |
| 1326 | else s = BC_STATUS_QUIT; |
| 1327 | } |
| 1328 | |
| 1329 | return s; |
Gavin Howard | f4167dc | 2018-03-26 14:19:28 -0600 | [diff] [blame] | 1330 | } |
| 1331 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1332 | BcStatus bc_program_exec(BcProgram *p) { |
Gavin Howard | 5b5dc57 | 2018-01-24 11:57:38 -0700 | [diff] [blame] | 1333 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1334 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 1335 | size_t idx; |
Gavin Howard | b4584aa | 2018-09-28 10:45:33 -0600 | [diff] [blame] | 1336 | BcResult res; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1337 | BcResult *ptr; |
| 1338 | BcNum *num; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1339 | BcInstPtr *ip = bc_vec_top(&p->stack); |
| 1340 | BcFunc *func = bc_vec_item(&p->fns, ip->func); |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1341 | char *code = func->code.v; |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1342 | #ifdef BC_ENABLED |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 1343 | bool cond = false; |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1344 | #endif // BC_ENABLED |
Gavin Howard | b09d9b1 | 2018-03-23 09:57:07 -0600 | [diff] [blame] | 1345 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1346 | while (!s && !bcg.sig_other && ip->idx < func->code.len) { |
Gavin Howard | 368459a | 2018-03-28 13:23:51 -0600 | [diff] [blame] | 1347 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1348 | uint8_t inst = code[(ip->idx)++]; |
Gavin Howard | 7c8dc77 | 2018-02-21 12:12:11 -0700 | [diff] [blame] | 1349 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1350 | switch (inst) { |
Gavin Howard | 7c8dc77 | 2018-02-21 12:12:11 -0700 | [diff] [blame] | 1351 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1352 | #ifdef BC_ENABLED |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1353 | case BC_INST_JUMP_ZERO: |
| 1354 | { |
Gavin Howard | b4584aa | 2018-09-28 10:45:33 -0600 | [diff] [blame] | 1355 | if ((s = bc_program_prep(p, &ptr, &num, false))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1356 | cond = !bc_num_cmp(num, &p->zero); |
| 1357 | bc_vec_pop(&p->results); |
| 1358 | } |
| 1359 | // Fallthrough. |
| 1360 | case BC_INST_JUMP: |
| 1361 | { |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 1362 | size_t *addr; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1363 | idx = bc_program_index(code, &ip->idx); |
| 1364 | addr = bc_vec_item(&func->labels, idx); |
| 1365 | if (inst == BC_INST_JUMP || cond) ip->idx = *addr; |
| 1366 | break; |
| 1367 | } |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 1368 | |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 1369 | case BC_INST_CALL: |
| 1370 | { |
| 1371 | s = bc_program_call(p, code, &ip->idx); |
| 1372 | break; |
| 1373 | } |
| 1374 | |
| 1375 | case BC_INST_INC_PRE: |
| 1376 | case BC_INST_DEC_PRE: |
| 1377 | case BC_INST_INC_POST: |
| 1378 | case BC_INST_DEC_POST: |
| 1379 | { |
| 1380 | s = bc_program_incdec(p, inst); |
| 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | case BC_INST_HALT: |
| 1385 | { |
| 1386 | s = BC_STATUS_QUIT; |
| 1387 | break; |
| 1388 | } |
| 1389 | |
| 1390 | case BC_INST_RET: |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1391 | #endif // BC_ENABLED |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 1392 | case BC_INST_RET0: |
| 1393 | { |
| 1394 | s = bc_program_return(p, inst); |
| 1395 | break; |
| 1396 | } |
| 1397 | |
| 1398 | case BC_INST_READ: |
| 1399 | { |
| 1400 | s = bc_program_read(p); |
| 1401 | break; |
| 1402 | } |
| 1403 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1404 | case BC_INST_VAR: |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 1405 | { |
Gavin Howard | 30ec94e | 2018-10-02 15:08:31 -0600 | [diff] [blame] | 1406 | s = bc_program_pushVar(p, code, &ip->idx, false); |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 1407 | break; |
| 1408 | } |
| 1409 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1410 | case BC_INST_ARRAY_ELEM: |
| 1411 | case BC_INST_ARRAY: |
| 1412 | { |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 1413 | s = bc_program_pushArray(p, code, &ip->idx, inst); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1414 | break; |
| 1415 | } |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 1416 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1417 | case BC_INST_IBASE: |
| 1418 | case BC_INST_LAST: |
| 1419 | case BC_INST_OBASE: |
| 1420 | { |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 1421 | res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE; |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 1422 | s = bc_vec_push(&p->results, &res); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1423 | break; |
| 1424 | } |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 1425 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1426 | case BC_INST_SCALE: |
| 1427 | { |
| 1428 | s = bc_program_pushScale(p); |
| 1429 | break; |
| 1430 | } |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 1431 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1432 | case BC_INST_SCALE_FUNC: |
| 1433 | case BC_INST_LENGTH: |
| 1434 | case BC_INST_SQRT: |
| 1435 | { |
| 1436 | s = bc_program_builtin(p, inst); |
| 1437 | break; |
| 1438 | } |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 1439 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1440 | case BC_INST_NUM: |
| 1441 | { |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 1442 | res.t = BC_RESULT_CONSTANT; |
Gavin Howard | b4584aa | 2018-09-28 10:45:33 -0600 | [diff] [blame] | 1443 | res.data.id.idx = bc_program_index(code, &ip->idx); |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 1444 | s = bc_vec_push(&p->results, &res); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1445 | break; |
| 1446 | } |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 1447 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1448 | case BC_INST_POP: |
| 1449 | { |
| 1450 | bc_vec_pop(&p->results); |
| 1451 | break; |
| 1452 | } |
Gavin Howard | 5c222e3 | 2018-02-28 13:06:41 -0700 | [diff] [blame] | 1453 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1454 | case BC_INST_PRINT: |
Gavin Howard | 08f74ad | 2018-09-28 14:46:29 -0600 | [diff] [blame] | 1455 | case BC_INST_PRINT_POP: |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 1456 | case BC_INST_PRINT_STR: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1457 | { |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1458 | s = bc_program_print(p, inst, 0); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1459 | break; |
| 1460 | } |
Gavin Howard | 7c8dc77 | 2018-02-21 12:12:11 -0700 | [diff] [blame] | 1461 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1462 | case BC_INST_STR: |
| 1463 | { |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 1464 | res.t = BC_RESULT_STR; |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 1465 | res.data.id.idx = bc_program_index(code, &ip->idx); |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 1466 | s = bc_vec_push(&p->results, &res); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1467 | break; |
| 1468 | } |
Gavin Howard | 7c8dc77 | 2018-02-21 12:12:11 -0700 | [diff] [blame] | 1469 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1470 | case BC_INST_POWER: |
| 1471 | case BC_INST_MULTIPLY: |
| 1472 | case BC_INST_DIVIDE: |
| 1473 | case BC_INST_MODULUS: |
| 1474 | case BC_INST_PLUS: |
| 1475 | case BC_INST_MINUS: |
| 1476 | { |
| 1477 | s = bc_program_op(p, inst); |
| 1478 | break; |
| 1479 | } |
Gavin Howard | 7c8dc77 | 2018-02-21 12:12:11 -0700 | [diff] [blame] | 1480 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1481 | #ifdef BC_ENABLED |
Gavin Howard | 972e840 | 2018-10-01 13:11:49 -0600 | [diff] [blame] | 1482 | case BC_INST_BOOL_OR: |
| 1483 | case BC_INST_BOOL_AND: |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1484 | #endif // BC_ENABLED |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1485 | case BC_INST_REL_EQ: |
| 1486 | case BC_INST_REL_LE: |
| 1487 | case BC_INST_REL_GE: |
| 1488 | case BC_INST_REL_NE: |
| 1489 | case BC_INST_REL_LT: |
| 1490 | case BC_INST_REL_GT: |
| 1491 | { |
| 1492 | s = bc_program_logical(p, inst); |
| 1493 | break; |
| 1494 | } |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 1495 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1496 | case BC_INST_BOOL_NOT: |
| 1497 | { |
Gavin Howard | b4584aa | 2018-09-28 10:45:33 -0600 | [diff] [blame] | 1498 | if ((s = bc_program_prep(p, &ptr, &num, false))) return s; |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1499 | if ((s = bc_num_init(&res.data.n, BC_NUM_DEF_SIZE))) return s; |
Gavin Howard | e809535 | 2018-02-27 09:17:20 -0700 | [diff] [blame] | 1500 | |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1501 | if (!bc_num_cmp(num, &p->zero)) bc_num_one(&res.data.n); |
| 1502 | else bc_num_zero(&res.data.n); |
Gavin Howard | e809535 | 2018-02-27 09:17:20 -0700 | [diff] [blame] | 1503 | |
Gavin Howard | b4584aa | 2018-09-28 10:45:33 -0600 | [diff] [blame] | 1504 | s = bc_program_retire(p, &res, BC_RESULT_TEMP); |
Gavin Howard | 648c01c | 2018-10-04 10:14:16 -0600 | [diff] [blame] | 1505 | if (s) bc_num_free(&res.data.n); |
Gavin Howard | e809535 | 2018-02-27 09:17:20 -0700 | [diff] [blame] | 1506 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1507 | break; |
| 1508 | } |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 1509 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1510 | case BC_INST_NEG: |
| 1511 | { |
| 1512 | s = bc_program_negate(p); |
| 1513 | break; |
| 1514 | } |
Gavin Howard | 7c8dc77 | 2018-02-21 12:12:11 -0700 | [diff] [blame] | 1515 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1516 | #ifdef BC_ENABLED |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1517 | case BC_INST_ASSIGN_POWER: |
| 1518 | case BC_INST_ASSIGN_MULTIPLY: |
| 1519 | case BC_INST_ASSIGN_DIVIDE: |
| 1520 | case BC_INST_ASSIGN_MODULUS: |
| 1521 | case BC_INST_ASSIGN_PLUS: |
| 1522 | case BC_INST_ASSIGN_MINUS: |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1523 | #endif // BC_ENABLED |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1524 | case BC_INST_ASSIGN: |
| 1525 | { |
| 1526 | s = bc_program_assign(p, inst); |
| 1527 | break; |
| 1528 | } |
Gavin Howard | 6d89b5d | 2018-02-26 13:21:34 -0700 | [diff] [blame] | 1529 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1530 | #ifdef DC_ENABLED |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1531 | case BC_INST_MODEXP: |
| 1532 | { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1533 | s = bc_program_modexp(p); |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1534 | break; |
| 1535 | } |
| 1536 | |
| 1537 | case BC_INST_DIVMOD: |
| 1538 | { |
Gavin Howard | 503f393 | 2018-09-29 19:24:29 -0600 | [diff] [blame] | 1539 | s = bc_program_divmod(p); |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1540 | break; |
| 1541 | } |
| 1542 | |
Gavin Howard | 077e30a | 2018-09-29 22:29:56 -0600 | [diff] [blame] | 1543 | case BC_INST_EXECUTE: |
| 1544 | { |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 1545 | s = bc_program_executeStr(p); |
Gavin Howard | 077e30a | 2018-09-29 22:29:56 -0600 | [diff] [blame] | 1546 | break; |
| 1547 | } |
| 1548 | |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1549 | case BC_INST_PRINT_STACK: |
| 1550 | { |
| 1551 | for (idx = 0; !s && idx < p->results.len; ++idx) { |
| 1552 | |
| 1553 | if ((s = bc_program_print(p, BC_INST_PRINT, idx))) break; |
| 1554 | |
| 1555 | ptr = bc_vec_item_rev(&p->results, idx); |
| 1556 | assert(ptr); |
| 1557 | |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 1558 | if (ptr->t == BC_RESULT_STR && putchar('\n') == EOF) |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1559 | s = BC_STATUS_IO_ERR; |
| 1560 | } |
| 1561 | |
| 1562 | break; |
| 1563 | } |
| 1564 | |
| 1565 | case BC_INST_CLEAR_STACK: |
| 1566 | { |
| 1567 | bc_vec_npop(&p->results, p->results.len); |
| 1568 | break; |
| 1569 | } |
| 1570 | |
| 1571 | case BC_INST_STACK_LEN: |
| 1572 | { |
| 1573 | s = bc_program_stackLen(p); |
| 1574 | break; |
| 1575 | } |
| 1576 | |
| 1577 | case BC_INST_DUPLICATE: |
| 1578 | { |
| 1579 | ptr = bc_vec_item_rev(&p->results, 1); |
| 1580 | if ((s = bc_result_copy(&res, ptr))) break; |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 1581 | s = bc_vec_push(&p->results, &res); |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1582 | break; |
| 1583 | } |
| 1584 | |
| 1585 | case BC_INST_SWAP: |
| 1586 | { |
Gavin Howard | 4c2a13e | 2018-09-29 19:24:43 -0600 | [diff] [blame] | 1587 | BcResult *ptr2; |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1588 | |
Gavin Howard | 1e758c8 | 2018-09-29 19:57:37 -0600 | [diff] [blame] | 1589 | if (!BC_PROG_CHECK_STACK(&p->results, 2)) |
Gavin Howard | 2192547 | 2018-09-29 19:29:25 -0600 | [diff] [blame] | 1590 | return BC_STATUS_EXEC_SMALL_STACK; |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1591 | |
Gavin Howard | 4c2a13e | 2018-09-29 19:24:43 -0600 | [diff] [blame] | 1592 | ptr = bc_vec_item_rev(&p->results, 0); |
| 1593 | ptr2 = bc_vec_item_rev(&p->results, 1); |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1594 | memcpy(&res, ptr, sizeof(BcResult)); |
| 1595 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 1596 | memcpy(ptr2, &res, sizeof(BcResult)); |
| 1597 | |
| 1598 | break; |
| 1599 | } |
| 1600 | |
Gavin Howard | c8d426a | 2018-10-02 14:47:26 -0600 | [diff] [blame] | 1601 | case BC_INST_COPY_TO_VAR: |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1602 | { |
Gavin Howard | 0e58ded | 2018-10-02 15:06:52 -0600 | [diff] [blame] | 1603 | char *name; |
| 1604 | if (!(name = bc_program_name(code, &ip->idx))) return s; |
| 1605 | s = bc_program_copyToVar(p, name, true); |
| 1606 | if (s) free(name); |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1607 | break; |
| 1608 | } |
| 1609 | |
Gavin Howard | c8d426a | 2018-10-02 14:47:26 -0600 | [diff] [blame] | 1610 | case BC_INST_PUSH_VAR: |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1611 | { |
Gavin Howard | da4cfc0 | 2018-10-01 12:27:34 -0600 | [diff] [blame] | 1612 | s = bc_program_pushVar(p, code, &ip->idx, true); |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1613 | break; |
| 1614 | } |
| 1615 | |
| 1616 | case BC_INST_QUIT: |
| 1617 | { |
| 1618 | if (p->stack.len <= 2) s = BC_STATUS_QUIT; |
| 1619 | else bc_vec_npop(&p->stack, 2); |
| 1620 | break; |
| 1621 | } |
| 1622 | |
| 1623 | case BC_INST_NQUIT: |
| 1624 | { |
| 1625 | s = bc_program_nquit(p); |
| 1626 | break; |
| 1627 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1628 | #endif // DC_ENABLED |
Gavin Howard | c935ff1 | 2018-09-29 02:18:45 -0600 | [diff] [blame] | 1629 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1630 | #ifndef NDEBUG |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1631 | default: |
| 1632 | { |
| 1633 | assert(false); |
| 1634 | break; |
| 1635 | } |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1636 | #endif // NDEBUG |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1637 | } |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 1638 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1639 | if ((s && s != BC_STATUS_QUIT) || bcg.signe) s = bc_program_reset(p, s); |
Gavin Howard | 0a2a674 | 2018-02-27 14:11:26 -0700 | [diff] [blame] | 1640 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1641 | // We need to update bc if the stack changes, pointers may be invalid. |
| 1642 | ip = bc_vec_top(&p->stack); |
| 1643 | func = bc_vec_item(&p->fns, ip->func); |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1644 | code = func->code.v; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1645 | } |
Gavin Howard | 7c8dc77 | 2018-02-21 12:12:11 -0700 | [diff] [blame] | 1646 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1647 | return s; |
Gavin Howard | 7c8dc77 | 2018-02-21 12:12:11 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1650 | void bc_program_free(BcProgram *p) { |
| 1651 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1652 | assert(p); |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1653 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1654 | bc_num_free(&p->ib); |
| 1655 | bc_num_free(&p->ob); |
Gavin Howard | 0361074 | 2018-09-27 10:48:29 -0600 | [diff] [blame] | 1656 | bc_num_free(&p->hexb); |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1657 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1658 | bc_vec_free(&p->fns); |
| 1659 | bc_veco_free(&p->fn_map); |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1660 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1661 | bc_vec_free(&p->vars); |
| 1662 | bc_veco_free(&p->var_map); |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1663 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1664 | bc_vec_free(&p->arrs); |
| 1665 | bc_veco_free(&p->arr_map); |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1666 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1667 | bc_vec_free(&p->strs); |
| 1668 | bc_vec_free(&p->consts); |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1669 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1670 | bc_vec_free(&p->results); |
| 1671 | bc_vec_free(&p->stack); |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1672 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1673 | bc_num_free(&p->last); |
| 1674 | bc_num_free(&p->zero); |
| 1675 | bc_num_free(&p->one); |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | #ifndef NDEBUG |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1679 | BcStatus bc_program_printIndex(char *code, size_t *bgn) { |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1680 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1681 | char byte, i, bytes = code[(*bgn)++]; |
| 1682 | unsigned long val = 0; |
Gavin Howard | 24c675f | 2018-06-22 16:00:02 -0600 | [diff] [blame] | 1683 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1684 | for (byte = 1, i = 0; byte && i < bytes; ++i) { |
| 1685 | byte = code[(*bgn)++]; |
| 1686 | if (byte) val |= ((unsigned long) byte) << (CHAR_BIT * i); |
| 1687 | } |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1688 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1689 | return printf(" (%lu) ", val) < 0 ? BC_STATUS_IO_ERR : BC_STATUS_SUCCESS; |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1690 | } |
| 1691 | |
Gavin Howard | af2b771 | 2018-09-28 16:17:41 -0600 | [diff] [blame] | 1692 | BcStatus bc_program_printName(char *code, size_t *bgn) { |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1693 | |
Gavin Howard | af2b771 | 2018-09-28 16:17:41 -0600 | [diff] [blame] | 1694 | char byte = (char) code[(*bgn)++]; |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1695 | |
Gavin Howard | af2b771 | 2018-09-28 16:17:41 -0600 | [diff] [blame] | 1696 | if (printf(" (") < 0) return BC_STATUS_IO_ERR; |
Gavin Howard | 9d76063 | 2018-08-30 13:03:51 -0600 | [diff] [blame] | 1697 | |
Gavin Howard | af2b771 | 2018-09-28 16:17:41 -0600 | [diff] [blame] | 1698 | for (; byte && byte != BC_PARSE_STREND; byte = (char) code[(*bgn)++]) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1699 | if (putchar(byte) == EOF) return BC_STATUS_IO_ERR; |
| 1700 | } |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1701 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1702 | assert(byte); |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1703 | |
Gavin Howard | af2b771 | 2018-09-28 16:17:41 -0600 | [diff] [blame] | 1704 | if (printf(") ") < 0) return BC_STATUS_IO_ERR; |
| 1705 | |
| 1706 | return BC_STATUS_SUCCESS; |
| 1707 | } |
| 1708 | |
| 1709 | BcStatus bc_program_printStr(BcProgram *p, char *code, size_t *bgn) { |
| 1710 | |
| 1711 | size_t idx = bc_program_index(code, bgn); |
| 1712 | char *s; |
| 1713 | |
| 1714 | assert(idx < p->strs.len); |
| 1715 | |
| 1716 | s = *((char**) bc_vec_item(&p->strs, idx)); |
| 1717 | |
| 1718 | if (printf(" (\"%s\") ", s) < 0) return BC_STATUS_IO_ERR; |
| 1719 | |
| 1720 | return BC_STATUS_SUCCESS; |
| 1721 | } |
| 1722 | |
Gavin Howard | e436e2c | 2018-10-02 15:08:48 -0600 | [diff] [blame] | 1723 | BcStatus bc_program_printInst(BcProgram *p, char *code, size_t *bgn) { |
| 1724 | |
Gavin Howard | e319eb6 | 2018-09-28 16:50:16 -0600 | [diff] [blame] | 1725 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | af2b771 | 2018-09-28 16:17:41 -0600 | [diff] [blame] | 1726 | uint8_t inst = code[(*bgn)++]; |
| 1727 | |
| 1728 | if (putchar(bc_inst_chars[inst]) == EOF) return BC_STATUS_IO_ERR; |
| 1729 | |
| 1730 | if (inst == BC_INST_VAR || inst == BC_INST_ARRAY_ELEM || |
| 1731 | inst == BC_INST_ARRAY) |
| 1732 | { |
| 1733 | s = bc_program_printName(code, bgn); |
| 1734 | } |
| 1735 | else if (inst == BC_INST_STR) { |
| 1736 | s = bc_program_printStr(p, code, bgn); |
| 1737 | } |
Gavin Howard | e436e2c | 2018-10-02 15:08:48 -0600 | [diff] [blame] | 1738 | else if (inst == BC_INST_NUM) { |
| 1739 | size_t idx = bc_program_index(code, bgn); |
| 1740 | char **str = bc_vec_item(&p->consts, idx); |
| 1741 | if (printf("(%s)", *str) < 0) s = BC_STATUS_IO_ERR; |
| 1742 | } |
| 1743 | else if (inst == BC_INST_CALL || |
Gavin Howard | af2b771 | 2018-09-28 16:17:41 -0600 | [diff] [blame] | 1744 | (inst > BC_INST_STR && inst <= BC_INST_JUMP_ZERO)) |
| 1745 | { |
| 1746 | if ((s = bc_program_printIndex(code, bgn))) return s; |
| 1747 | if (inst == BC_INST_CALL) s = bc_program_printIndex(code, bgn); |
| 1748 | } |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1749 | |
Gavin Howard | e319eb6 | 2018-09-28 16:50:16 -0600 | [diff] [blame] | 1750 | if (!s && fflush(stdout) < 0) s = BC_STATUS_IO_ERR; |
| 1751 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1752 | return s; |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1753 | } |
| 1754 | |
Gavin Howard | 4e4a011 | 2018-09-28 15:45:34 -0600 | [diff] [blame] | 1755 | BcStatus bc_program_code(BcProgram *p) { |
Gavin Howard | 0a2a674 | 2018-02-27 14:11:26 -0700 | [diff] [blame] | 1756 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1757 | BcStatus s = BC_STATUS_SUCCESS; |
| 1758 | BcFunc *f; |
| 1759 | char *code; |
| 1760 | BcInstPtr ip; |
| 1761 | size_t i; |
Gavin Howard | 0a2a674 | 2018-02-27 14:11:26 -0700 | [diff] [blame] | 1762 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1763 | for (i = 0; !s && !bcg.sig_other && i < p->fns.len; ++i) { |
Gavin Howard | 0a2a674 | 2018-02-27 14:11:26 -0700 | [diff] [blame] | 1764 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1765 | bool sig; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 1766 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1767 | ip.idx = ip.len = 0; |
| 1768 | ip.func = i; |
Gavin Howard | 0a2a674 | 2018-02-27 14:11:26 -0700 | [diff] [blame] | 1769 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1770 | f = bc_vec_item(&p->fns, ip.func); |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1771 | code = f->code.v; |
Gavin Howard | 0a2a674 | 2018-02-27 14:11:26 -0700 | [diff] [blame] | 1772 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1773 | if (printf("func[%zu]:\n", ip.func) < 0) return BC_STATUS_IO_ERR; |
Gavin Howard | 0a2a674 | 2018-02-27 14:11:26 -0700 | [diff] [blame] | 1774 | |
Gavin Howard | af2b771 | 2018-09-28 16:17:41 -0600 | [diff] [blame] | 1775 | while (ip.idx < f->code.len) s = bc_program_printInst(p, code, &ip.idx); |
Gavin Howard | c0d9f31 | 2018-03-07 15:15:00 -0700 | [diff] [blame] | 1776 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1777 | if (printf("\n\n") < 0) s = BC_STATUS_IO_ERR; |
Gavin Howard | 368459a | 2018-03-28 13:23:51 -0600 | [diff] [blame] | 1778 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1779 | sig = bcg.sig != bcg.sigc; |
| 1780 | if (s || sig) s = bc_program_reset(p, s); |
| 1781 | } |
Gavin Howard | 0a2a674 | 2018-02-27 14:11:26 -0700 | [diff] [blame] | 1782 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1783 | return s; |
Gavin Howard | 0a2a674 | 2018-02-27 14:11:26 -0700 | [diff] [blame] | 1784 | } |
Gavin Howard | 84590c8 | 2018-03-28 15:38:44 -0600 | [diff] [blame] | 1785 | #endif // NDEBUG |