diff --git a/include/interpreter.h b/include/interpreter.h index ee8eadb..83ae328 100644 --- a/include/interpreter.h +++ b/include/interpreter.h @@ -14,6 +14,10 @@ struct libab_s; */ struct libab_interpreter_s { struct libab_s* ab; + /** + * The unit value, which doesn't need more than one instance. + */ + libab_ref value_unit; }; typedef struct libab_interpreter_s libab_interpreter; @@ -23,7 +27,7 @@ typedef struct libab_interpreter_s libab_interpreter; * @param intr the interpreter to initialize. * @param ab the libabacus instance this interpreter belongs to. */ -void libab_interpreter_init(libab_interpreter* intr, struct libab_s* ab); +libab_result libab_interpreter_init(libab_interpreter* intr, struct libab_s* ab); /** * Uses the interpreter to run the given parse tree. * @param intr the interpreter to use to run the code. @@ -46,6 +50,12 @@ libab_result libab_interpreter_run_function(libab_interpreter* intr, const char* function, libab_ref_vec* params, libab_ref* into); +/** + * Gets the unit value from this interpreter. + * @param intr the interpreter from which to get the unit value. + * @param into the reference into which to store the unit value. + */ +void libab_interpreter_unit_value(libab_interpreter* intr, libab_ref* into); /** * Frees the given interpreter. * @param intr the interpreter to free. diff --git a/src/interpreter.c b/src/interpreter.c index c1c34c0..40891b8 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -2,8 +2,16 @@ #include "util.h" #include "value.h" -void libab_interpreter_init(libab_interpreter* intr, libab* ab) { +libab_result libab_interpreter_init(libab_interpreter* intr, libab* ab) { + libab_result result; + libab_ref unit_data; intr->ab = ab; + + libab_ref_null(&unit_data); + result = libab_create_value_ref(&intr->value_unit, &unit_data, &ab->type_unit); + libab_ref_free(&unit_data); + + return result; } struct interpreter_state { @@ -844,4 +852,10 @@ libab_result libab_interpreter_run_function(libab_interpreter* intr, return result; } -void libab_interpreter_free(libab_interpreter* intr) {} +void libab_interpreter_unit_value(libab_interpreter* intr, libab_ref* into) { + libab_ref_copy(&intr->value_unit, into); +} + +void libab_interpreter_free(libab_interpreter* intr) { + libab_ref_free(&intr->value_unit); +} diff --git a/src/libabacus.c b/src/libabacus.c index 4a61985..1a4d8ae 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -35,6 +35,7 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*), void (*free_function)(void*)) { int parser_initialized = 0; int lexer_initialized = 0; + int interpreter_initialized = 0; libab_ref null_ref; libab_result result; libab_ref_null(&null_ref); @@ -55,7 +56,11 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*), if (result == LIBAB_SUCCESS) { parser_initialized = 1; libab_parser_init(&ab->parser, ab); - libab_interpreter_init(&ab->intr, ab); + result = libab_interpreter_init(&ab->intr, ab); + } + + if(result == LIBAB_SUCCESS) { + interpreter_initialized = 1; result = libab_lexer_init(&ab->lexer); } @@ -72,6 +77,9 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*), if (parser_initialized) { libab_parser_free(&ab->parser); + } + + if (interpreter_initialized) { libab_interpreter_free(&ab->intr); }