Refactor to encapsulate all state in a 'ucl'

This commit is contained in:
2022-11-22 15:50:46 -05:00
parent 13062a5b86
commit 5320c70dea
23 changed files with 641 additions and 663 deletions

View File

@@ -10,44 +10,26 @@
#include "builtins.h"
#include "special.h"
static struct ucl *state;
static struct ucl_object *input;
static struct ucl_object *response;
static struct ucl_scope *scope;
void setUp(void) {
input = NULL;
response = NULL;
scope = ucl_scope_create();
ucl_scope_put(scope, "let", ucl_special_create(ucl_special_let));
ucl_scope_put(scope, "defun", ucl_special_create(ucl_special_defun));
ucl_scope_put(scope, "+", ucl_builtin_create(ucl_builtin_add));
ucl_scope_put(scope, "error", ucl_builtin_create(ucl_builtin_error));
ucl_scope_put(scope, "list", ucl_builtin_create(ucl_builtin_list));
ucl_scope_put(scope, "setq", ucl_special_create(ucl_special_setq));
ucl_scope_put(scope, "car", ucl_builtin_create(ucl_builtin_car));
ucl_scope_put(scope, "cdr", ucl_builtin_create(ucl_builtin_cdr));
ucl_scope_put(scope, "nth", ucl_builtin_create(ucl_builtin_nth));
ucl_scope_put(scope, "mapcar", ucl_builtin_create(ucl_builtin_mapcar));
ucl_scope_put(scope, "filter", ucl_builtin_create(ucl_builtin_filter));
ucl_scope_put(scope, "reduce", ucl_builtin_create(ucl_builtin_reduce));
ucl_scope_put(scope, "lambda", ucl_special_create(ucl_special_lambda));
ucl_scope_put(scope, "quote", ucl_special_create(ucl_special_quote));
ucl_scope_put(scope, "%", ucl_builtin_create(ucl_builtin_mod));
state = ucl_create();
}
void tearDown(void) {
ucl_scope_delete(scope);
ucl_gc();
// TODO: Actually release resources
state = NULL;
input = NULL;
response = NULL;
scope = NULL;
}
static struct ucl_object *eval (const char *code) {
struct ucl_object *sexp = ucl_parse(code);
return ucl_evaluate(scope, ucl_car(sexp));
struct ucl_object *sexp = ucl_parse(state, code);
return ucl_evaluate(state, ucl_car(state, sexp));
}
void test_simple_add(void) {
@@ -96,7 +78,7 @@ void test_eval_string(void) {
}
void test_eval_sym_defined(void) {
ucl_scope_put(scope, "foo", ucl_int_create(2));
ucl_scope_put(state, state->global_scope, "foo", ucl_int_create(state, 2));
response = eval("foo");
TEST_ASSERT_OBJ_INT_V(response, 2);
}
@@ -114,7 +96,7 @@ void test_eval_nested_error(void) {
void test_eval_list(void) {
response = eval("(list 1 2 3)");
TEST_ASSERT_OBJ_LIST(response);
TEST_ASSERT_EQUAL(ucl_list_length(response)->integer, 3);
TEST_ASSERT_EQUAL(ucl_list_length(state, response)->integer, 3);
int i = 1;
FOREACH_LIST(response, iter, item) {
@@ -227,7 +209,7 @@ void test_eval_defun_gc(void) {
TEST_ASSERT_OBJ_SYMBOL_V(response, "foo");
ucl_gc();
ucl_gc(state);
response = eval("(foo 10 15)");
TEST_ASSERT_OBJ_INT_V(response, 25);