Add true / false values to interpreter.

This commit is contained in:
Danila Fedorin 2018-08-10 15:29:56 -07:00
parent 25f5d3469b
commit 7c8c547540
4 changed files with 87 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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