From 3e8c814215af3ec779b8bf1be29fe808bdcb5d8b Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 11 May 2018 20:36:27 -0700 Subject: [PATCH] Remove pointless parameter copying. --- include/interpreter.h | 12 +++++------- include/libabacus.h | 2 +- include/parser.h | 6 ++++-- src/interpreter.c | 32 +++++++++++++------------------- src/libabacus.c | 4 ++-- src/parser.c | 10 +++++----- 6 files changed, 30 insertions(+), 36 deletions(-) diff --git a/include/interpreter.h b/include/interpreter.h index 4ea6b62..307ec74 100644 --- a/include/interpreter.h +++ b/include/interpreter.h @@ -4,19 +4,17 @@ #include "table.h" #include "tree.h" #include "impl.h" +#include "libabacus.h" + +struct libab_s; struct libab_interpreter_s { - libab_ref type_num; - libab_ref base_table; - libab_impl* impl; + struct libab_s* ab; }; typedef struct libab_interpreter_s libab_interpreter; -void libab_interpreter_init(libab_interpreter* intr, - libab_ref* table, - libab_ref* type_num, - libab_impl* impl); +void libab_interpreter_init(libab_interpreter* intr, struct libab_s* ab); libab_result libab_interpreter_run(libab_interpreter* intr, libab_tree* tree, libab_ref* into); void libab_interpreter_free(libab_interpreter* intr); diff --git a/include/libabacus.h b/include/libabacus.h index 104df4d..4ae4d54 100644 --- a/include/libabacus.h +++ b/include/libabacus.h @@ -1,11 +1,11 @@ #ifndef LIBABACUS_H #define LIBABACUS_H +#include "interpreter.h" #include "custom.h" #include "ht.h" #include "lexer.h" #include "parser.h" -#include "interpreter.h" #include "result.h" #include "table.h" #include "impl.h" diff --git a/include/parser.h b/include/parser.h index 268a90f..2a804af 100644 --- a/include/parser.h +++ b/include/parser.h @@ -6,13 +6,15 @@ #include "table.h" #include "tree.h" +struct libab_s; + /** * The parser that is used by libabacus * to store information for converting * tokens into trees. */ struct libab_parser_s { - libab_ref base_table; + struct libab_s* ab; }; typedef struct libab_parser_s libab_parser; @@ -22,7 +24,7 @@ typedef struct libab_parser_s libab_parser; * @param parser the parser to intialize. * @param table the table of "reserved" entries like operators. */ -void libab_parser_init(libab_parser* parser, libab_ref* table); +void libab_parser_init(libab_parser* parser, struct libab_s* ab); /** * Parses the given list of tokens into the given tree pointer. * @param parser the parser to use for parsing text. diff --git a/src/interpreter.c b/src/interpreter.c index 6d69809..0470913 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -1,27 +1,22 @@ -#include "interpreter.h" +#include "libabacus.h" #include "util.h" -void libab_interpreter_init(libab_interpreter* intr, - libab_ref* table, - libab_ref* type_num, - libab_impl* impl) { - libab_ref_copy(type_num, &intr->type_num); - libab_ref_copy(table, &intr->base_table); - intr->impl = impl; +void libab_interpreter_init(libab_interpreter* intr, libab* ab) { + intr->ab = ab; } struct interpreter_state { - libab_ref type_num; - libab_impl* impl; + libab* ab; + libab_table* base_table; }; void _interpreter_init(struct interpreter_state* state, libab_interpreter* intr) { - state->impl = intr->impl; - libab_ref_copy(&intr->type_num, &state->type_num); + state->ab = intr->ab; + state->base_table = libab_ref_get(&intr->ab->table); } void _interpreter_free(struct interpreter_state* state) { - libab_ref_free(&state->type_num); + } libab_result _interpreter_create_num_val(struct interpreter_state* state, @@ -29,11 +24,11 @@ libab_result _interpreter_create_num_val(struct interpreter_state* state, void* data; libab_result result = LIBAB_SUCCESS; - if((data = state->impl->parse_num(from))) { - result = libab_create_value(into, data, &state->type_num); + if((data = state->ab->impl.parse_num(from))) { + result = libab_create_value(into, data, &state->ab->type_num); if(result != LIBAB_SUCCESS) { - ((libab_parsetype*) libab_ref_get(&state->type_num))->data_u.base->free_function(data); + ((libab_parsetype*) libab_ref_get(&state->ab->type_num))->data_u.base->free_function(data); } } else { result = LIBAB_MALLOC; @@ -87,13 +82,12 @@ libab_result libab_interpreter_run(libab_interpreter* intr, libab_result result; _interpreter_init(&state, intr); - result = _interpreter_run(&state, tree, into, &intr->base_table, 1); + result = _interpreter_run(&state, tree, into, &state.ab->table, 1); _interpreter_free(&state); return result; } void libab_interpreter_free(libab_interpreter* intr) { - libab_ref_free(&intr->base_table); - libab_ref_free(&intr->type_num); + } diff --git a/src/libabacus.c b/src/libabacus.c index 2458820..3812877 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -25,8 +25,8 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*), if(result == LIBAB_SUCCESS) { parser_initialized = 1; - libab_parser_init(&ab->parser, &ab->table); - libab_interpreter_init(&ab->intr, &ab->table, &ab->type_num, &ab->impl); + libab_parser_init(&ab->parser, ab); + libab_interpreter_init(&ab->intr, ab); result = libab_lexer_init(&ab->lexer); } diff --git a/src/parser.c b/src/parser.c index 6379252..3f8a653 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1162,14 +1162,14 @@ libab_result _parse_block(struct parser_state* state, libab_tree** store_into, return result; } -void libab_parser_init(libab_parser* parser, libab_ref* table) { - libab_ref_copy(table, &parser->base_table); +void libab_parser_init(libab_parser* parser, struct libab_s* ab) { + parser->ab = ab; } libab_result libab_parser_parse(libab_parser* parser, ll* tokens, const char* string, libab_tree** store_into) { libab_result result; struct parser_state state; - _parser_state_init(&state, tokens, string, libab_ref_get(&parser->base_table)); + _parser_state_init(&state, tokens, string, libab_ref_get(&parser->ab->table)); result = _parse_block(&state, store_into, 0); if (result == LIBAB_SUCCESS) { @@ -1182,8 +1182,8 @@ libab_result libab_parser_parse_type(libab_parser* parser, ll* tokens, const char* string, libab_ref* store_into) { struct parser_state state; - _parser_state_init(&state, tokens, string, libab_ref_get(&parser->base_table)); + _parser_state_init(&state, tokens, string, libab_ref_get(&parser->ab->table)); return _parse_type(&state, store_into); } -void libab_parser_free(libab_parser* parser) { libab_ref_free(&parser->base_table); } +void libab_parser_free(libab_parser* parser) { libab_ref_free(&parser->ab->table); }