diff options
author | Gavin Howard <yzena.tech@gmail.com> | 2019-06-12 23:38:01 -0600 |
---|---|---|
committer | Gavin Howard <yzena.tech@gmail.com> | 2019-06-12 23:38:01 -0600 |
commit | cd09f8c05a1aff4facbf960bd74a023d7b65c6f1 (patch) | |
tree | 773a06abf6ab31c88ce440dce45b1b8c8c188706 /src/lang.c | |
parent | e453a78adff7c5af6b02fa8025eaf8c6004780ac (diff) | |
download | platform_external_bc-cd09f8c05a1aff4facbf960bd74a023d7b65c6f1.tar.gz platform_external_bc-cd09f8c05a1aff4facbf960bd74a023d7b65c6f1.tar.bz2 platform_external_bc-cd09f8c05a1aff4facbf960bd74a023d7b65c6f1.zip |
Make the parser search for vars and arrays
This means that the parser (for both calculators) is the one to search
for vars and arrays, and it directly injects the index of the location
into the bytestream.
This has several advantages:
1. There is no separate pushing of names and indices, just indices.
2. This reduces allocations.
3. The all-around speed up puts this bc's interpreter ahead of GNU's.
Diffstat (limited to 'src/lang.c')
-rw-r--r-- | src/lang.c | 28 |
1 files changed, 12 insertions, 16 deletions
@@ -62,23 +62,26 @@ void bc_const_free(void *constant) { } #if BC_ENABLED -BcStatus bc_func_insert(BcFunc *f, char *name, BcType type, size_t line) { +BcStatus bc_func_insert(BcFunc *f, BcProgram *p, char *name, + BcType type, size_t line) +{ + BcLoc a; + size_t i, idx; - BcId a; - size_t i; + assert(f); - assert(f && name); + idx = bc_program_search(p, name, type == BC_TYPE_VAR); for (i = 0; i < f->autos.len; ++i) { - BcId *id = bc_vec_item(&f->autos, i); - if (BC_ERR(!strcmp(name, id->name) && type == (BcType) id->idx)) { + BcLoc *id = bc_vec_item(&f->autos, i); + if (BC_ERR(idx == id->loc && type == (BcType) id->idx)) { const char *array = type == BC_TYPE_ARRAY ? "[]" : ""; return bc_vm_error(BC_ERROR_PARSE_DUP_LOCAL, line, name, array); } } + a.loc = idx; a.idx = type; - a.name = name; bc_vec_push(&f->autos, &a); @@ -92,7 +95,7 @@ void bc_func_init(BcFunc *f, const char *name) { bc_vec_init(&f->strs, sizeof(char*), bc_string_free); bc_vec_init(&f->consts, sizeof(BcConst), bc_const_free); #if BC_ENABLED - bc_vec_init(&f->autos, sizeof(BcId), bc_id_free); + bc_vec_init(&f->autos, sizeof(BcLoc), NULL); bc_vec_init(&f->labels, sizeof(size_t), NULL); f->nparams = 0; f->voidfn = false; @@ -192,8 +195,7 @@ void bc_result_copy(BcResult *d, BcResult *src) { case BC_RESULT_ARRAY: case BC_RESULT_ARRAY_ELEM: { - assert(src->d.id.name); - d->d.id.name = bc_vm_strdup(src->d.id.name); + memcpy(&d->d.loc, &src->d.loc, sizeof(BcLoc)); break; } @@ -239,12 +241,6 @@ void bc_result_free(void *result) { case BC_RESULT_VAR: case BC_RESULT_ARRAY: case BC_RESULT_ARRAY_ELEM: - { - assert(r->d.id.name); - free(r->d.id.name); - break; - } - case BC_RESULT_STR: case BC_RESULT_CONSTANT: #if BC_ENABLED |