Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anonymous functions cannot refer to bound variables bound in an outer scope #2

Open
CaspianA1 opened this issue Nov 16, 2020 · 0 comments

Comments

@CaspianA1
Copy link
Owner

I recently added let as a feature:

(define (g a) (let ((b 3)) (* a b)))
(display (g 2))

The let expression above is transformed into the one below, which is perfectly fine.

(define (f a) ((lambda (b) (* a b)) 3))
(display (f 2))

The problem arises when a lambda expression refers to a variable that has been bound in a nested manner outside of it. In this case, that is a. The result for both of these is 9, but it should be 6. The first argument to f or g is referred to as [rbp + 16] as a stack offset. The bound variable b is also referred to as [rbp + 16] since it is the first argument passed to that function. That creates a collision. I believe that this is part of what makes the funarg problem so difficult.
In order for this code to work, I would need a record structure storing the current bound variables within the environment with a pointer to that environment's enclosing environment, if any. Newly bound variables would have to live on the heap rather than the stack. Seeing as my compiler is built fundamentally on passing arguments via the stack I do not know when this problem can be solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant