From 727a2ed6ea754dea8c0d01c043fed24d0414731e Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 7 May 2023 20:05:44 +0400 Subject: [PATCH] fix lambda --- src/object.c | 10 ++++++++-- src/object.h | 4 +++- src/syntax.c | 5 ++--- tests/syntax.scm | 11 +++++++++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/object.c b/src/object.c index 7315101..3d8d453 100644 --- a/src/object.c +++ b/src/object.c @@ -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 diff --git a/src/object.h b/src/object.h index 9359c38..baae3f6 100644 --- a/src/object.h +++ b/src/object.h @@ -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, ...); diff --git a/src/syntax.c b/src/syntax.c index 97b67f3..3077abb 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -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( diff --git a/tests/syntax.scm b/tests/syntax.scm index b844505..faf7434 100644 --- a/tests/syntax.scm +++ b/tests/syntax.scm @@ -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))