Avoid creating number type reference in interpreter.

This commit is contained in:
Danila Fedorin 2018-05-06 19:01:47 -07:00
parent 1e215ecfa5
commit 3b0908125d
3 changed files with 16 additions and 28 deletions

View File

@ -6,6 +6,7 @@
#include "impl.h" #include "impl.h"
struct libab_interpreter_s { struct libab_interpreter_s {
libab_ref type_num;
libab_ref base_table; libab_ref base_table;
libab_impl* impl; libab_impl* impl;
}; };
@ -14,6 +15,7 @@ typedef struct libab_interpreter_s libab_interpreter;
void libab_interpreter_init(libab_interpreter* intr, void libab_interpreter_init(libab_interpreter* intr,
libab_ref* table, libab_ref* table,
libab_ref* type_num,
libab_impl* impl); libab_impl* impl);
libab_result libab_interpreter_run(libab_interpreter* intr, libab_result libab_interpreter_run(libab_interpreter* intr,
libab_tree* tree, libab_ref* into); libab_tree* tree, libab_ref* into);

View File

@ -3,37 +3,25 @@
void libab_interpreter_init(libab_interpreter* intr, void libab_interpreter_init(libab_interpreter* intr,
libab_ref* table, libab_ref* table,
libab_ref* type_num,
libab_impl* impl) { libab_impl* impl) {
libab_ref_copy(type_num, &intr->type_num);
libab_ref_copy(table, &intr->base_table); libab_ref_copy(table, &intr->base_table);
intr->impl = impl; intr->impl = impl;
} }
struct interpreter_state { struct interpreter_state {
libab_ref num_ref; libab_ref type_num;
libab_impl* impl; libab_impl* impl;
}; };
libab_result _interpreter_init(struct interpreter_state* state, libab_interpreter* intr) { void _interpreter_init(struct interpreter_state* state, libab_interpreter* intr) {
libab_result result = LIBAB_SUCCESS;
libab_basetype* num_type;
libab_ref_null(&state->num_ref);
state->impl = intr->impl; state->impl = intr->impl;
libab_ref_copy(&intr->type_num, &state->type_num);
num_type = libab_table_search_basetype(libab_ref_get(&intr->base_table), "num");
if(num_type != NULL) {
libab_ref_free(&state->num_ref);
result = libab_instantiate_basetype(num_type, &state->num_ref, 0);
}
if(result != LIBAB_SUCCESS) {
libab_ref_free(&state->num_ref);
}
return result;
} }
void _interpreter_free(struct interpreter_state* state) { void _interpreter_free(struct interpreter_state* state) {
libab_ref_free(&state->num_ref); libab_ref_free(&state->type_num);
} }
libab_result _interpreter_create_num_val(struct interpreter_state* state, libab_result _interpreter_create_num_val(struct interpreter_state* state,
@ -42,10 +30,10 @@ libab_result _interpreter_create_num_val(struct interpreter_state* state,
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
if((data = state->impl->parse_num(from))) { if((data = state->impl->parse_num(from))) {
result = libab_create_value(into, data, &state->num_ref); result = libab_create_value(into, data, &state->type_num);
if(result != LIBAB_SUCCESS) { if(result != LIBAB_SUCCESS) {
((libab_parsetype*) libab_ref_get(&state->num_ref))->data_u.base->free_function(data); ((libab_parsetype*) libab_ref_get(&state->type_num))->data_u.base->free_function(data);
} }
} else { } else {
result = LIBAB_MALLOC; result = LIBAB_MALLOC;
@ -96,18 +84,16 @@ libab_result _interpreter_run(struct interpreter_state* state,
libab_result libab_interpreter_run(libab_interpreter* intr, libab_result libab_interpreter_run(libab_interpreter* intr,
libab_tree* tree, libab_ref* into) { libab_tree* tree, libab_ref* into) {
struct interpreter_state state; struct interpreter_state state;
libab_result result = _interpreter_init(&state, intr); libab_result result;
if(result == LIBAB_SUCCESS) { _interpreter_init(&state, intr);
result = _interpreter_run(&state, tree, into, &intr->base_table, 1); result = _interpreter_run(&state, tree, into, &intr->base_table, 1);
_interpreter_free(&state); _interpreter_free(&state);
} else {
libab_ref_null(into);
}
return result; return result;
} }
void libab_interpreter_free(libab_interpreter* intr) { void libab_interpreter_free(libab_interpreter* intr) {
libab_ref_free(&intr->base_table); libab_ref_free(&intr->base_table);
libab_ref_free(&intr->type_num);
} }

View File

@ -26,7 +26,7 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*),
if(result == LIBAB_SUCCESS) { if(result == LIBAB_SUCCESS) {
parser_initialized = 1; parser_initialized = 1;
libab_parser_init(&ab->parser, &ab->table); libab_parser_init(&ab->parser, &ab->table);
libab_interpreter_init(&ab->intr, &ab->table, &ab->impl); libab_interpreter_init(&ab->intr, &ab->table, &ab->type_num, &ab->impl);
result = libab_lexer_init(&ab->lexer); result = libab_lexer_init(&ab->lexer);
} }