1
0
Fork 0

syntax "begin"

This commit is contained in:
Alex Kotov 2023-05-05 22:31:42 +04:00
parent 82380acf0f
commit b3137a216b
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 47 additions and 4 deletions

View File

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

View File

@ -5,7 +5,8 @@
#include <stdlib.h>
#include <string.h>
// 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);

View File

@ -5,6 +5,18 @@
#include <assert.h>
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);

View File

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