diff --git a/src/eval.c b/src/eval.c index a61668b..bb1c0cd 100644 --- a/src/eval.c +++ b/src/eval.c @@ -54,6 +54,7 @@ struct Object *eval(struct Object *const object) struct Object *const args = object->pair.b; if (Object_is_symbol(func_expr)) { + if (strcmp(func_expr->s, "begin") == 0) return syntax_begin(args); if (strcmp(func_expr->s, "quote") == 0) return syntax_quote(args); if (strcmp(func_expr->s, "if") == 0) return syntax_if(args); } diff --git a/src/main-test.c b/src/main-test.c index 10bf684..341d03d 100644 --- a/src/main-test.c +++ b/src/main-test.c @@ -5,7 +5,8 @@ #include #include -// Macros +// Syntax +static void test_begin(); static void test_if(); // Lexer & parser static void test_arcane_SLASH_tokenize(); @@ -31,7 +32,8 @@ static void test_EQ(); int main() { - // Macros + // Syntax + test_begin(); test_if(); // Lexer & parser test_arcane_SLASH_tokenize(); @@ -99,9 +101,36 @@ int main() } /********** - * Macros * + * Syntax * **********/ +void test_begin() +{ + struct Object *result = NULL; + + // (begin) + // () + assert(OBJECT_IS_NULL(eval_str("(begin)"))); + + // (begin 123) + // 123 + result = eval_str("(begin 123)"); + assert(Object_is_number(result)); + assert(result->i == 123); + + // (begin 123 456) + // 456 + result = eval_str("(begin 123 456)"); + assert(Object_is_number(result)); + assert(result->i == 456); + + // (begin 123 456 789) + // 789 + result = eval_str("(begin 123 456 789)"); + assert(Object_is_number(result)); + assert(result->i == 789); +} + void test_if() { struct Object *const num_123 = Object_new_number(123); diff --git a/src/syntax.c b/src/syntax.c index ebeea99..bcc7ae8 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -5,6 +5,18 @@ #include +struct Object *syntax_begin(struct Object *args) +{ + assert(OBJECT_IS_LIST_HEAD(args)); + struct Object *result = NULL; + while (!OBJECT_IS_NULL(args)) { + assert(Object_is_pair(args)); + result = eval(args->pair.a); + args = args->pair.b; + } + return result; +} + struct Object *syntax_if(struct Object *const args) { assert(Object_is_pair(args)); @@ -17,7 +29,7 @@ struct Object *syntax_if(struct Object *const args) assert(else_list); assert(Object_is_pair(else_list)); struct Object *const else_branch = else_list->pair.a; - assert(else_list->pair.b == NULL); + assert(OBJECT_IS_NULL(else_list->pair.b)); if (Object_is_false(cond)) { return eval(else_branch); diff --git a/src/syntax.h b/src/syntax.h index d8d7616..af6d4c6 100644 --- a/src/syntax.h +++ b/src/syntax.h @@ -3,6 +3,7 @@ #include "object.h" +struct Object *syntax_begin(struct Object *args); struct Object *syntax_if(struct Object *args); struct Object *syntax_quote(struct Object *args);