fix lambda
This commit is contained in:
parent
97a7a1f3e6
commit
727a2ed6ea
4 changed files with 24 additions and 6 deletions
10
src/object.c
10
src/object.c
|
@ -88,7 +88,8 @@ struct Object *Object_new_number(const int64_t i64)
|
||||||
struct Object *Object_new_lambda(
|
struct Object *Object_new_lambda(
|
||||||
const char *const name,
|
const char *const name,
|
||||||
struct Object *const arg_names,
|
struct Object *const arg_names,
|
||||||
struct Object *const body
|
struct Object *const body,
|
||||||
|
struct Object *const environment
|
||||||
) {
|
) {
|
||||||
assert(OBJECT_IS_LIST_HEAD(arg_names));
|
assert(OBJECT_IS_LIST_HEAD(arg_names));
|
||||||
for (
|
for (
|
||||||
|
@ -110,6 +111,7 @@ struct Object *Object_new_lambda(
|
||||||
object->procedure.func = NULL;
|
object->procedure.func = NULL;
|
||||||
object->procedure.arg_names = arg_names;
|
object->procedure.arg_names = arg_names;
|
||||||
object->procedure.body = body;
|
object->procedure.body = body;
|
||||||
|
object->procedure.environment = environment;
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,7 +250,11 @@ struct Object *Object_procedure_call(
|
||||||
}
|
}
|
||||||
// Lambda
|
// Lambda
|
||||||
else {
|
else {
|
||||||
struct Object *const environment = Object_new_pair(NULL, NULL);
|
assert(OBJECT_IS_LIST_HEAD(procedure->procedure.arg_names));
|
||||||
|
assert(Object_is_pair(procedure->procedure.environment));
|
||||||
|
|
||||||
|
struct Object *const environment =
|
||||||
|
Object_new_pair(NULL, procedure->procedure.environment);
|
||||||
|
|
||||||
for (
|
for (
|
||||||
struct Object
|
struct Object
|
||||||
|
|
|
@ -31,6 +31,7 @@ struct Procedure {
|
||||||
// For lambdas
|
// For lambdas
|
||||||
struct Object *arg_names;
|
struct Object *arg_names;
|
||||||
struct Object *body;
|
struct Object *body;
|
||||||
|
struct Object *environment;
|
||||||
};
|
};
|
||||||
|
|
||||||
/********
|
/********
|
||||||
|
@ -87,7 +88,8 @@ struct Object *Object_new_number(int64_t i64);
|
||||||
struct Object *Object_new_lambda(
|
struct Object *Object_new_lambda(
|
||||||
const char *name,
|
const char *name,
|
||||||
struct Object *arg_names,
|
struct Object *arg_names,
|
||||||
struct Object *body
|
struct Object *body,
|
||||||
|
struct Object *environment
|
||||||
);
|
);
|
||||||
|
|
||||||
struct Object *Object_build_list(int count, ...);
|
struct Object *Object_build_list(int count, ...);
|
||||||
|
|
|
@ -90,8 +90,6 @@ struct Object *syntax_lambda(
|
||||||
struct Object *const args,
|
struct Object *const args,
|
||||||
struct Object *environment
|
struct Object *environment
|
||||||
) {
|
) {
|
||||||
(void)environment;
|
|
||||||
|
|
||||||
assert(Object_is_pair(args));
|
assert(Object_is_pair(args));
|
||||||
struct Object *const arg_names = args->pair.car;
|
struct Object *const arg_names = args->pair.car;
|
||||||
assert(OBJECT_IS_LIST_HEAD(arg_names));
|
assert(OBJECT_IS_LIST_HEAD(arg_names));
|
||||||
|
@ -99,7 +97,8 @@ struct Object *syntax_lambda(
|
||||||
struct Object *const body = args->pair.cdr->pair.car;
|
struct Object *const body = args->pair.cdr->pair.car;
|
||||||
assert(OBJECT_IS_NULL(args->pair.cdr->pair.cdr));
|
assert(OBJECT_IS_NULL(args->pair.cdr->pair.cdr));
|
||||||
|
|
||||||
return Object_new_lambda(NULL, arg_names, body);
|
return Object_new_lambda(NULL, arg_names, body, environment);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Object *syntax_let(
|
struct Object *syntax_let(
|
||||||
|
|
|
@ -33,6 +33,17 @@
|
||||||
(assert-equal 3 ((lambda (x) (+ 1 x)) 2))
|
(assert-equal 3 ((lambda (x) (+ 1 x)) 2))
|
||||||
(assert-equal 6 ((lambda (x y) (+ 1 x y)) 2 3))
|
(assert-equal 6 ((lambda (x y) (+ 1 x y)) 2 3))
|
||||||
(assert-equal 10 ((lambda (x y z) (+ 1 x y z)) 2 3 4))
|
(assert-equal 10 ((lambda (x y z) (+ 1 x y z)) 2 3 4))
|
||||||
|
(assert-equal
|
||||||
|
5
|
||||||
|
(begin
|
||||||
|
(define sum (lambda (a b) (+ a b)))
|
||||||
|
(define inc (lambda (a) (+ a 1)))
|
||||||
|
(sum (inc 1) (inc 2))))
|
||||||
|
(assert-equal
|
||||||
|
5
|
||||||
|
(let ([sum (lambda (a b) (+ a b))]
|
||||||
|
[inc (lambda (a) (+ a 1))])
|
||||||
|
(sum (inc 1) (inc 2))))
|
||||||
|
|
||||||
;;; let ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;; let ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
(assert-equal 'foo (let () 'foo))
|
(assert-equal 'foo (let () 'foo))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue