syntax "begin"
This commit is contained in:
parent
82380acf0f
commit
b3137a216b
|
@ -54,6 +54,7 @@ struct Object *eval(struct Object *const object)
|
||||||
struct Object *const args = object->pair.b;
|
struct Object *const args = object->pair.b;
|
||||||
|
|
||||||
if (Object_is_symbol(func_expr)) {
|
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, "quote") == 0) return syntax_quote(args);
|
||||||
if (strcmp(func_expr->s, "if") == 0) return syntax_if(args);
|
if (strcmp(func_expr->s, "if") == 0) return syntax_if(args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// Macros
|
// Syntax
|
||||||
|
static void test_begin();
|
||||||
static void test_if();
|
static void test_if();
|
||||||
// Lexer & parser
|
// Lexer & parser
|
||||||
static void test_arcane_SLASH_tokenize();
|
static void test_arcane_SLASH_tokenize();
|
||||||
|
@ -31,7 +32,8 @@ static void test_EQ();
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Macros
|
// Syntax
|
||||||
|
test_begin();
|
||||||
test_if();
|
test_if();
|
||||||
// Lexer & parser
|
// Lexer & parser
|
||||||
test_arcane_SLASH_tokenize();
|
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()
|
void test_if()
|
||||||
{
|
{
|
||||||
struct Object *const num_123 = Object_new_number(123);
|
struct Object *const num_123 = Object_new_number(123);
|
||||||
|
|
14
src/syntax.c
14
src/syntax.c
|
@ -5,6 +5,18 @@
|
||||||
|
|
||||||
#include <assert.h>
|
#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)
|
struct Object *syntax_if(struct Object *const args)
|
||||||
{
|
{
|
||||||
assert(Object_is_pair(args));
|
assert(Object_is_pair(args));
|
||||||
|
@ -17,7 +29,7 @@ struct Object *syntax_if(struct Object *const args)
|
||||||
assert(else_list);
|
assert(else_list);
|
||||||
assert(Object_is_pair(else_list));
|
assert(Object_is_pair(else_list));
|
||||||
struct Object *const else_branch = else_list->pair.a;
|
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)) {
|
if (Object_is_false(cond)) {
|
||||||
return eval(else_branch);
|
return eval(else_branch);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
|
|
||||||
|
struct Object *syntax_begin(struct Object *args);
|
||||||
struct Object *syntax_if(struct Object *args);
|
struct Object *syntax_if(struct Object *args);
|
||||||
struct Object *syntax_quote(struct Object *args);
|
struct Object *syntax_quote(struct Object *args);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue