Add defun without evaluation

This commit is contained in:
2022-11-03 09:46:23 -04:00
parent 37a614515e
commit ddb5a8f842
17 changed files with 291 additions and 129 deletions

View File

@@ -8,6 +8,7 @@
#include "testing_helpers.h"
#include "state.h"
#include "builtins.h"
#include "special.h"
static struct ucl_object *input;
static struct ucl_object *response;
@@ -18,7 +19,8 @@ void setUp(void) {
response = NULL;
state = ucl_state_create();
ucl_state_put(state, "let", ucl_builtin_create(ucl_builtin_let));
ucl_state_put(state, "let", ucl_special_create(ucl_special_let));
ucl_state_put(state, "defun", ucl_special_create(ucl_special_defun));
ucl_state_put(state, "+", ucl_builtin_create(ucl_builtin_add));
ucl_state_put(state, "error", ucl_builtin_create(ucl_builtin_error));
ucl_state_put(state, "list", ucl_builtin_create(ucl_builtin_list));
@@ -39,77 +41,77 @@ static struct ucl_object *eval (const char *code) {
return ucl_evaluate(state, ucl_car(sexp));
}
static void test_simple_add(void) {
void test_simple_add(void) {
ucl_state_put(state, "+", ucl_builtin_create(ucl_builtin_add));
response = eval("(+ 2 3)");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 5);
}
static void test_simple_let(void) {
void test_simple_let(void) {
response = eval("(let ((x 2)) (+ x 3))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 5);
}
static void test_let_assignment_error(void) {
void test_let_assignment_error(void) {
response = eval("(let ((x (error \"foo\"))) (+ x 3))");
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_nested_let(void) {
void test_nested_let(void) {
response = eval("(let ((x 2)) (let ((y 5)) (+ x y)))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 7);
}
static void test_nested_let_scope(void) {
void test_nested_let_scope(void) {
response = eval("(let ((x 2)) (let ((y 5)) (+ x y)) y)");
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_multi_let(void) {
void test_multi_let(void) {
response = eval("(let ((x 2)(y 5)) (+ x y))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 7);
}
static void test_let_return_sym(void) {
void test_let_return_sym(void) {
response = eval("(let ((x 2)) x)");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2);
}
static void test_eval_int(void) {
void test_eval_int(void) {
response = eval("2");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2);
}
static void test_eval_string(void) {
void test_eval_string(void) {
response = eval("\"foo\"");
TEST_ASSERT_OBJ_STRING(response);
TEST_ASSERT_EQUAL_STRING(response->string, "foo");
}
static void test_eval_sym_defined(void) {
void test_eval_sym_defined(void) {
ucl_state_put(state, "foo", ucl_int_create(2));
response = eval("foo");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2);
}
static void test_eval_sym_undefined(void) {
void test_eval_sym_undefined(void) {
response = eval("foo");
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_eval_nested_error(void) {
void test_eval_nested_error(void) {
response = eval("(+ (error \"foo\") 3)");
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_eval_list(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);
@@ -121,6 +123,12 @@ static void test_eval_list(void) {
}
}
void test_eval_defun(void) {
response = eval("(defun foo (a b) (+ a b))");
TEST_ASSERT_OBJ_SYMBOL(response);
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
}
int main(void) {
UNITY_BEGIN();
@@ -138,6 +146,7 @@ int main(void) {
RUN_TEST(test_eval_sym_undefined);
RUN_TEST(test_eval_nested_error);
RUN_TEST(test_eval_list);
RUN_TEST(test_eval_defun);
return UNITY_END();
}

View File

@@ -5,6 +5,7 @@
#include "uclisp.h"
#include "internal.h"
#include "utility.h"
#include "state.h"
#include "testing_helpers.h"
static struct ucl_object *input;
@@ -194,6 +195,38 @@ static void test_list_nth_bounds_2() {
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_truthy_0() {
response = ucl_truthy(ucl_int_create(0));
TEST_ASSERT_NIL(response);
}
static void test_truthy_1() {
response = ucl_truthy(ucl_int_create(1));
TEST_ASSERT_T(response);
}
static void test_truthy_nil() {
response = ucl_truthy(ucl_nil_create());
TEST_ASSERT_NIL(response);
}
static void test_truthy_list_w_elem() {
struct ucl_object *list = ucl_nil_create();
ucl_list_append(list, ucl_int_create(0));
response = ucl_truthy(list);
TEST_ASSERT_T(response);
}
static void test_truthy_sym() {
response = ucl_truthy(ucl_symbol_create("t"));
TEST_ASSERT_T(response);
}
int main(void) {
UNITY_BEGIN();
@@ -221,6 +254,11 @@ int main(void) {
RUN_TEST(test_list_nth_bounds_0);
RUN_TEST(test_list_nth_bounds_1);
RUN_TEST(test_list_nth_bounds_2);
RUN_TEST(test_truthy_0);
RUN_TEST(test_truthy_1);
RUN_TEST(test_truthy_nil);
RUN_TEST(test_truthy_list_w_elem);
RUN_TEST(test_truthy_sym);
return UNITY_END();
}