Move syntax to separate module
This commit is contained in:
parent
84cb115cdf
commit
82380acf0f
1
Makefile
1
Makefile
|
@ -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
|
||||
|
|
28
src/eval.c
28
src/eval.c
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue