Add unit value to interpreter.

This commit is contained in:
Danila Fedorin 2018-06-02 15:50:42 -07:00
parent 3fcdd55395
commit 8207f1f450
3 changed files with 36 additions and 4 deletions

View File

@ -14,6 +14,10 @@ struct libab_s;
*/ */
struct libab_interpreter_s { struct libab_interpreter_s {
struct libab_s* ab; 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; 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 intr the interpreter to initialize.
* @param ab the libabacus instance this interpreter belongs to. * @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. * Uses the interpreter to run the given parse tree.
* @param intr the interpreter to use to run the code. * @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, const char* function,
libab_ref_vec* params, libab_ref_vec* params,
libab_ref* into); 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. * Frees the given interpreter.
* @param intr the interpreter to free. * @param intr the interpreter to free.

View File

@ -2,8 +2,16 @@
#include "util.h" #include "util.h"
#include "value.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; 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 { struct interpreter_state {
@ -844,4 +852,10 @@ libab_result libab_interpreter_run_function(libab_interpreter* intr,
return result; 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);
}

View File

@ -35,6 +35,7 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*),
void (*free_function)(void*)) { void (*free_function)(void*)) {
int parser_initialized = 0; int parser_initialized = 0;
int lexer_initialized = 0; int lexer_initialized = 0;
int interpreter_initialized = 0;
libab_ref null_ref; libab_ref null_ref;
libab_result result; libab_result result;
libab_ref_null(&null_ref); libab_ref_null(&null_ref);
@ -55,7 +56,11 @@ 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); 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); result = libab_lexer_init(&ab->lexer);
} }
@ -72,6 +77,9 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*),
if (parser_initialized) { if (parser_initialized) {
libab_parser_free(&ab->parser); libab_parser_free(&ab->parser);
}
if (interpreter_initialized) {
libab_interpreter_free(&ab->intr); libab_interpreter_free(&ab->intr);
} }