From 7c8c5475400c74cafc8decfac4218517821412bc Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 10 Aug 2018 15:29:56 -0700 Subject: [PATCH] Add true / false values to interpreter. --- include/interpreter.h | 20 ++++++++++++++++++ include/libabacus.h | 12 +++++++++++ src/interpreter.c | 47 +++++++++++++++++++++++++++++++++++++++++++ src/libabacus.c | 8 ++++++++ 4 files changed, 87 insertions(+) diff --git a/include/interpreter.h b/include/interpreter.h index d90e0d1..5fde633 100644 --- a/include/interpreter.h +++ b/include/interpreter.h @@ -28,6 +28,14 @@ struct libab_interpreter_s { * The unit value, which doesn't need more than one instance. */ libab_ref value_unit; + /** + * The "true" boolean value, which doesn't need more than one instance. + */ + libab_ref value_true; + /** + * The "false" boolean value, which doesn't need more than one instance. + */ + libab_ref value_false; }; typedef enum libab_interpreter_scope_mode_e libab_interpreter_scope_mode; @@ -73,6 +81,18 @@ libab_result libab_interpreter_run_function(libab_interpreter* intr, * @param into the reference into which to store the unit value. */ void libab_interpreter_unit_value(libab_interpreter* intr, libab_ref* into); +/** + * Gets the true value from this interpreter. + * @param intr the interpreter from which to get the true value. + * @param into the reference into which to store the true value. + */ +void libab_interpreter_true_value(libab_interpreter* intr, libab_ref* into); +/** + * Gets the false value from this interpreter. + * @param intr the interpreter from which to get the false value. + * @param into the reference into which to store the false value. + */ +void libab_interpreter_false_value(libab_interpreter* intr, libab_ref* into); /** * Frees the given interpreter. * @param intr the interpreter to free. diff --git a/include/libabacus.h b/include/libabacus.h index ed46105..ad015e3 100644 --- a/include/libabacus.h +++ b/include/libabacus.h @@ -204,6 +204,18 @@ void libab_get_type_unit(libab* ab, libab_ref* into); * @param into the reference into which to store the unit value. */ void libab_get_unit_value(libab* ab, libab_ref* into); +/** + * Gets the true value form this libab instance. + * @param ab the instance to get the true value from. + * @param into the reference into which to store the true value. + */ +void libab_get_true_value(libab* ab, libab_ref* into); +/** + * Gets the false value form this libab instance. + * @param ab the instance to get the false value from. + * @param into the reference into which to store the false value. + */ +void libab_get_false_value(libab* ab, libab_ref* into); /** * Executes the given string of code. diff --git a/src/interpreter.c b/src/interpreter.c index b95ec03..499a0ee 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -4,15 +4,52 @@ #include "free_functions.h" #include "reserved.h" +libab_result _create_bool_value(libab* ab, int val, libab_ref* into) { + libab_ref type_bool; + int* new_bool; + libab_result result = LIBAB_SUCCESS; + + libab_get_type_bool(ab, &type_bool); + new_bool = malloc(sizeof(*new_bool)); + if(new_bool) { + *new_bool = val; + result = libab_create_value_raw(into, new_bool, &type_bool); + if(result != LIBAB_SUCCESS) { + free(new_bool); + } + } else { + result = LIBAB_MALLOC; + libab_ref_null(into); + } + libab_ref_free(&type_bool); + return result; +} + libab_result libab_interpreter_init(libab_interpreter* intr, libab* ab) { libab_result result; libab_ref unit_data; intr->ab = ab; + libab_ref_null(&intr->value_true); + libab_ref_null(&intr->value_false); libab_ref_null(&unit_data); result = libab_create_value_ref(&intr->value_unit, &unit_data, &ab->type_unit); + if(result == LIBAB_SUCCESS) { + libab_ref_free(&intr->value_true); + result = _create_bool_value(ab, 1, &intr->value_true); + } + if(result == LIBAB_SUCCESS) { + libab_ref_free(&intr->value_false); + result = _create_bool_value(ab, 0, &intr->value_false); + } libab_ref_free(&unit_data); + if(result != LIBAB_SUCCESS) { + libab_ref_free(&intr->value_unit); + libab_ref_free(&intr->value_true); + libab_ref_free(&intr->value_false); + } + return result; } @@ -1225,6 +1262,16 @@ void libab_interpreter_unit_value(libab_interpreter* intr, libab_ref* into) { libab_ref_copy(&intr->value_unit, into); } +void libab_interpreter_true_value(libab_interpreter* intr, libab_ref* into) { + libab_ref_copy(&intr->value_true, into); +} + +void libab_interpreter_false_value(libab_interpreter* intr, libab_ref* into) { + libab_ref_copy(&intr->value_false, into); +} + void libab_interpreter_free(libab_interpreter* intr) { libab_ref_free(&intr->value_unit); + libab_ref_free(&intr->value_true); + libab_ref_free(&intr->value_false); } diff --git a/src/libabacus.c b/src/libabacus.c index 7afa618..ade5d8b 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -402,6 +402,14 @@ void libab_get_unit_value(libab* ab, libab_ref* into) { libab_interpreter_unit_value(&ab->intr, into); } +void libab_get_true_value(libab* ab, libab_ref* into) { + libab_interpreter_true_value(&ab->intr, into); +} + +void libab_get_false_value(libab* ab, libab_ref* into) { + libab_interpreter_false_value(&ab->intr, into); +} + libab_result _create_tree(libab* ab, const char* string, libab_tree** into) { libab_result result = LIBAB_SUCCESS; ll tokens;