7 #include "./builtins.h"
9 #include "./interpreter.h"
12 struct EvalResult eval_success(struct Expr expr)
14 struct EvalResult result = {
22 struct EvalResult eval_failure(struct Expr error)
24 struct EvalResult result = {
33 wrong_argument_type(Gc *gc, const char *type, struct Expr obj)
37 SYMBOL(gc, "wrong-argument-type"),
43 wrong_number_of_arguments(Gc *gc, long int count)
47 SYMBOL(gc, "wrong-number-of-arguments"),
52 not_implemented(Gc *gc)
54 return eval_failure(SYMBOL(gc, "not-implemented"));
57 static struct EvalResult eval_atom(Gc *gc, struct Scope *scope, struct Atom *atom)
66 return eval_success(atom_as_expr(atom));
69 struct Expr value = get_scope_value(scope, atom_as_expr(atom));
72 return eval_failure(CONS(gc,
73 SYMBOL(gc, "void-variable"),
77 return eval_success(value.cons->cdr);
81 return eval_failure(CONS(gc,
82 SYMBOL(gc, "unexpected-expression"),
86 static struct EvalResult eval_all_args(Gc *gc, struct Scope *scope, struct Expr args)
93 return eval_atom(gc, scope, args.atom);
96 struct EvalResult car = eval(gc, scope, args.cons->car);
101 struct EvalResult cdr = eval_all_args(gc, scope, args.cons->cdr);
106 return eval_success(cons_as_expr(create_cons(gc, car.expr, cdr.expr)));
112 return eval_failure(CONS(gc,
113 SYMBOL(gc, "unexpected-expression"),
117 static struct EvalResult call_lambda(Gc *gc,
121 if (!lambda_p(lambda)) {
122 return eval_failure(CONS(gc,
123 SYMBOL(gc, "expected-callable"),
128 return eval_failure(CONS(gc,
129 SYMBOL(gc, "expected-list"),
133 struct Expr vars = lambda.cons->cdr.cons->car;
135 if (length_of_list(args) != length_of_list(vars)) {
136 return eval_failure(CONS(gc,
137 SYMBOL(gc, "wrong-number-of-arguments"),
138 NUMBER(gc, length_of_list(args))));
141 push_scope_frame(gc, scope, vars, args);
142 struct Expr body = lambda.cons->cdr.cons->cdr;
144 struct EvalResult result = eval_success(NIL(gc));
146 while (!nil_p(body)) {
147 result = eval(gc, scope, body.cons->car);
148 if (result.is_error) {
151 body = body.cons->cdr;
154 pop_scope_frame(gc, scope);
159 static struct EvalResult call_callable(Gc *gc,
161 struct Expr callable_expr,
162 struct Expr args_expr) {
163 struct EvalResult callable_result = eval(gc, scope, callable_expr);
164 if (callable_result.is_error) {
165 return callable_result;
168 struct EvalResult args_result = symbol_p(callable_expr) && is_special(callable_expr.atom->sym)
169 ? eval_success(args_expr)
170 : eval_all_args(gc, scope, args_expr);
172 if (args_result.is_error) {
176 if (callable_result.expr.type == EXPR_ATOM &&
177 callable_result.expr.atom->type == ATOM_NATIVE) {
178 return ((NativeFunction)callable_result.expr.atom->native.fun)(
179 callable_result.expr.atom->native.param, gc, scope, args_result.expr);
182 return call_lambda(gc, scope, callable_result.expr, args_result.expr);
186 struct EvalResult eval_block(Gc *gc, struct Scope *scope, struct Expr block)
191 if (!list_p(block)) {
192 return wrong_argument_type(gc, "listp", block);
195 struct Expr head = block;
196 struct EvalResult eval_result = eval_success(NIL(gc));
198 while (cons_p(head)) {
199 eval_result = eval(gc, scope, CAR(head));
200 if (eval_result.is_error) {
210 static struct EvalResult eval_funcall(Gc *gc, struct Scope *scope, struct Cons *cons)
215 return call_callable(gc, scope, cons->car, cons->cdr);
218 struct EvalResult eval(Gc *gc, struct Scope *scope, struct Expr expr)
222 return eval_atom(gc, scope, expr.atom);
225 return eval_funcall(gc, scope, expr.cons);
230 return eval_failure(CONS(gc,
231 SYMBOL(gc, "unexpected-expression"),
236 car(void *param, Gc *gc, struct Scope *scope, struct Expr args)
242 struct Expr xs = NIL(gc);
244 struct EvalResult result = match_list(gc, "e", args, &xs);
245 if (result.is_error) {
250 return eval_success(xs);
254 return wrong_argument_type(gc, "consp", xs);
257 return eval_success(CAR(xs));
261 match_list(struct Gc *gc, const char *format, struct Expr xs, ...)
264 va_start(args_list, xs);
267 for (i = 0; *format != 0 && !nil_p(xs); ++i) {
270 return wrong_argument_type(gc, "consp", xs);
273 struct Expr x = CAR(xs);
279 return wrong_argument_type(gc, "numberp", x);
282 long int *p = va_arg(args_list, long int *);
291 return wrong_argument_type(gc, "stringp", x);
294 const char **p = va_arg(args_list, const char**);
303 return wrong_argument_type(gc, "symbolp", x);
306 const char **p = va_arg(args_list, const char**);
313 struct Expr *p = va_arg(args_list, struct Expr*);
318 struct Expr *p = va_arg(args_list, struct Expr*);
332 if (*format == '*' && nil_p(xs)) {
333 struct Expr *p = va_arg(args_list, struct Expr*);
340 if (*format != 0 || !nil_p(xs)) {
342 return wrong_number_of_arguments(gc, i);
346 return eval_success(NIL(gc));
350 format_list_rec(Gc *gc, const char *format, va_list args)
361 long int p = va_arg(args, long int);
362 return CONS(gc, NUMBER(gc, p),
363 format_list_rec(gc, format + 1, args));
367 const char* p = va_arg(args, const char*);
368 return CONS(gc, STRING(gc, p),
369 format_list_rec(gc, format + 1, args));
373 const char* p = va_arg(args, const char*);
374 return CONS(gc, SYMBOL(gc, p),
375 format_list_rec(gc, format + 1, args));
379 struct Expr p = va_arg(args, struct Expr);
380 return CONS(gc, p, format_list_rec(gc, format + 1, args));
384 fprintf(stderr, "Wrong format parameter: %c\n", *format);
391 format_list(Gc *gc, const char *format, ...)
394 va_start(args, format);
395 struct Expr result = format_list_rec(gc, format, args);