1
0
Fork 0

fix lambda

This commit is contained in:
Alex Kotov 2023-05-07 20:05:44 +04:00
parent 97a7a1f3e6
commit 727a2ed6ea
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 24 additions and 6 deletions

View file

@ -88,7 +88,8 @@ struct Object *Object_new_number(const int64_t i64)
struct Object *Object_new_lambda(
const char *const name,
struct Object *const arg_names,
struct Object *const body
struct Object *const body,
struct Object *const environment
) {
assert(OBJECT_IS_LIST_HEAD(arg_names));
for (
@ -110,6 +111,7 @@ struct Object *Object_new_lambda(
object->procedure.func = NULL;
object->procedure.arg_names = arg_names;
object->procedure.body = body;
object->procedure.environment = environment;
return object;
}
@ -248,7 +250,11 @@ struct Object *Object_procedure_call(
}
// Lambda
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 (
struct Object

View file

@ -31,6 +31,7 @@ struct Procedure {
// For lambdas
struct Object *arg_names;
struct Object *body;
struct Object *environment;
};
/********
@ -87,7 +88,8 @@ struct Object *Object_new_number(int64_t i64);
struct Object *Object_new_lambda(
const char *name,
struct Object *arg_names,
struct Object *body
struct Object *body,
struct Object *environment
);
struct Object *Object_build_list(int count, ...);

View file

@ -90,8 +90,6 @@ struct Object *syntax_lambda(
struct Object *const args,
struct Object *environment
) {
(void)environment;
assert(Object_is_pair(args));
struct Object *const arg_names = args->pair.car;
assert(OBJECT_IS_LIST_HEAD(arg_names));
@ -99,7 +97,8 @@ struct Object *syntax_lambda(
struct Object *const body = args->pair.cdr->pair.car;
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(

View file

@ -33,6 +33,17 @@
(assert-equal 3 ((lambda (x) (+ 1 x)) 2))
(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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(assert-equal 'foo (let () 'foo))