4 #include "ebisp/interpreter.h"
5 #include "ebisp/builtins.h"
6 #include "ebisp/scope.h"
11 lambda(Gc *gc, struct Expr args, struct Expr body)
15 CONS(gc, args, body));
18 static struct EvalResult
19 quasiquote(void *param, Gc *gc, struct Scope *scope, struct Expr expr)
26 /* TODO(#582): quasiquote special form is not implemented */
28 return not_implemented(gc);
31 /* TODO(#536): greaterThan does not support arbitrary amount of arguments */
32 static struct EvalResult
33 greaterThan(void *param, Gc *gc, struct Scope *scope, struct Expr args)
39 long int x = 0, y = 0;
41 struct EvalResult result = match_list(gc, "dd", args, &x, &y);
42 if (result.is_error) {
47 return eval_success(T(gc));
49 return eval_success(NIL(gc));
53 static struct EvalResult
54 list_op(void *param, Gc *gc, struct Scope *scope, struct Expr args)
60 return eval_success(args);
63 static struct EvalResult
64 plus_op(void *param, Gc *gc, struct Scope *scope, struct Expr args)
72 while (!nil_p(args)) {
74 return wrong_argument_type(gc, "consp", args);
77 if (!number_p(CAR(args))) {
78 return wrong_argument_type(gc, "numberp", CAR(args));
81 result += CAR(args).atom->num;
85 return eval_success(NUMBER(gc, result));
88 static struct EvalResult
89 assoc_op(void *param, Gc *gc, struct Scope *scope, struct Expr args)
95 struct Expr key = NIL(gc);
96 struct Expr alist = NIL(gc);
97 struct EvalResult result = match_list(gc, "ee", args, &key, &alist);
98 if (result.is_error) {
102 return eval_success(assoc(key, alist));
105 static struct EvalResult
106 set(void *param, Gc *gc, struct Scope *scope, struct Expr args)
112 const char *name = NULL;
113 struct Expr value = void_expr();
114 struct EvalResult result = match_list(gc, "qe", args, &name, &value);
115 if (result.is_error) {
119 result = eval(gc, scope, value);
120 if (result.is_error) {
124 set_scope_value(gc, scope, SYMBOL(gc, name), result.expr);
126 return eval_success(result.expr);
129 static struct EvalResult
130 quote(void *param, Gc *gc, struct Scope *scope, struct Expr args)
136 struct Expr expr = void_expr();
137 struct EvalResult result = match_list(gc, "e", args, &expr);
138 if (result.is_error) {
142 return eval_success(expr);
145 static struct EvalResult
146 begin(void *param, Gc *gc, struct Scope *scope, struct Expr args)
152 struct Expr block = void_expr();
153 struct EvalResult result = match_list(gc, "*", args, &block);
154 if (result.is_error) {
158 return eval_block(gc, scope, block);
161 static struct EvalResult
162 defun(void *param, Gc *gc, struct Scope *scope, struct Expr args)
168 struct Expr name = void_expr();
169 struct Expr args_list = void_expr();
170 struct Expr body = void_expr();
172 struct EvalResult result = match_list(gc, "ee*", args, &name, &args_list, &body);
173 if (result.is_error) {
177 return eval(gc, scope,
181 lambda(gc, args_list, body)));
184 static struct EvalResult
185 when(void *param, Gc *gc, struct Scope *scope, struct Expr args)
191 struct Expr condition = void_expr();
192 struct Expr body = void_expr();
194 struct EvalResult result = match_list(
195 gc, "e*", args, &condition, &body);
196 if (result.is_error) {
200 result = eval(gc, scope, condition);
201 if (result.is_error) {
205 if (!nil_p(result.expr)) {
206 return eval_block(gc, scope, body);
209 return eval_success(NIL(gc));
212 static struct EvalResult
213 lambda_op(void *param, Gc *gc, struct Scope *scope, struct Expr args)
219 struct Expr args_list = void_expr();
220 struct Expr body = void_expr();
222 struct EvalResult result = match_list(gc, "e*", args, &args_list, &body);
223 if (result.is_error) {
227 if (!list_of_symbols_p(args_list)) {
228 return wrong_argument_type(gc, "list-of-symbolsp", args_list);
231 return eval_success(lambda(gc, args_list, body));
234 void load_std_library(Gc *gc, struct Scope *scope)
236 set_scope_value(gc, scope, SYMBOL(gc, "car"), NATIVE(gc, car, NULL));
237 set_scope_value(gc, scope, SYMBOL(gc, ">"), NATIVE(gc, greaterThan, NULL));
238 set_scope_value(gc, scope, SYMBOL(gc, "+"), NATIVE(gc, plus_op, NULL));
239 set_scope_value(gc, scope, SYMBOL(gc, "list"), NATIVE(gc, list_op, NULL));
240 set_scope_value(gc, scope, SYMBOL(gc, "t"), SYMBOL(gc, "t"));
241 set_scope_value(gc, scope, SYMBOL(gc, "nil"), SYMBOL(gc, "nil"));
242 set_scope_value(gc, scope, SYMBOL(gc, "assoc"), NATIVE(gc, assoc_op, NULL));
243 set_scope_value(gc, scope, SYMBOL(gc, "quasiquote"), NATIVE(gc, quasiquote, NULL));
244 set_scope_value(gc, scope, SYMBOL(gc, "set"), NATIVE(gc, set, NULL));
245 set_scope_value(gc, scope, SYMBOL(gc, "quote"), NATIVE(gc, quote, NULL));
246 set_scope_value(gc, scope, SYMBOL(gc, "begin"), NATIVE(gc, begin, NULL));
247 set_scope_value(gc, scope, SYMBOL(gc, "defun"), NATIVE(gc, defun, NULL));
248 set_scope_value(gc, scope, SYMBOL(gc, "when"), NATIVE(gc, when, NULL));
249 set_scope_value(gc, scope, SYMBOL(gc, "lambda"), NATIVE(gc, lambda_op, NULL));
250 set_scope_value(gc, scope, SYMBOL(gc, "λ"), NATIVE(gc, lambda_op, NULL));