Remove pointless parameter copying.

This commit is contained in:
Danila Fedorin 2018-05-11 20:36:27 -07:00
parent 989774cec5
commit 3e8c814215
6 changed files with 30 additions and 36 deletions

View File

@ -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);

View File

@ -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"

View File

@ -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.

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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); }