1
0
Fork 0

Move syntax to separate module

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

View File

@ -10,6 +10,7 @@ OBJS = \
src/lexer.c.o \
src/object.c.o \
src/parser.c.o \
src/syntax.c.o \
src/tokens.c.o
MAIN_OBJS = $(OBJS) src/main.c.o

View File

@ -3,6 +3,7 @@
#include "builtins.h"
#include "lexer.h"
#include "parser.h"
#include "syntax.h"
#include <assert.h>
#include <stddef.h>
@ -53,31 +54,8 @@ 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, "quote") == 0) {
assert(Object_is_pair(args));
assert(args->pair.b == NULL);
return args->pair.a;
}
if (strcmp(func_expr->s, "if") == 0) {
assert(Object_is_pair(args));
struct Object *const cond = args->pair.a;
struct Object *const then_else_list = args->pair.b;
assert(then_else_list);
assert(Object_is_pair(then_else_list));
struct Object *const then_branch = then_else_list->pair.a;
struct Object *const else_list = then_else_list->pair.b;
assert(else_list);
assert(Object_is_pair(else_list));
struct Object *const else_branch = else_list->pair.a;
assert(else_list->pair.b == NULL);
if (Object_is_false(cond)) {
return eval(else_branch);
} else {
return eval(then_branch);
}
}
if (strcmp(func_expr->s, "quote") == 0) return syntax_quote(args);
if (strcmp(func_expr->s, "if") == 0) return syntax_if(args);
}
struct Object *const func = eval(func_expr);

34
src/syntax.c Normal file
View File

@ -0,0 +1,34 @@
#include "syntax.h"
#include "eval.h"
#include "object.h"
#include <assert.h>
struct Object *syntax_if(struct Object *const args)
{
assert(Object_is_pair(args));
struct Object *const cond = args->pair.a;
struct Object *const then_else_list = args->pair.b;
assert(then_else_list);
assert(Object_is_pair(then_else_list));
struct Object *const then_branch = then_else_list->pair.a;
struct Object *const else_list = then_else_list->pair.b;
assert(else_list);
assert(Object_is_pair(else_list));
struct Object *const else_branch = else_list->pair.a;
assert(else_list->pair.b == NULL);
if (Object_is_false(cond)) {
return eval(else_branch);
} else {
return eval(then_branch);
}
}
struct Object *syntax_quote(struct Object *const args)
{
assert(Object_is_pair(args));
assert(OBJECT_IS_NULL(args->pair.b));
return args->pair.a;
}

9
src/syntax.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __ARCANE_SCHEME_LISP_SYNTAX_H__
#define __ARCANE_SCHEME_LISP_SYNTAX_H__
#include "object.h"
struct Object *syntax_if(struct Object *args);
struct Object *syntax_quote(struct Object *args);
#endif