Skip to content

Improve Lisp binding#848

Merged
vinc merged 10 commits into
trunkfrom
lisp-binding
May 13, 2026
Merged

Improve Lisp binding#848
vinc merged 10 commits into
trunkfrom
lisp-binding

Conversation

@vinc
Copy link
Copy Markdown
Owner

@vinc vinc commented May 12, 2026

  • Move eval::eval_args calls out of env::inner_env in interpreter
  • Remove env::function_env and env::macro_env in interpreter
  • Rename env::inner_env to env::bind in interpreter
  • Allow optional arguments
  • Add eval::apply in interpreter
  • Add back apply special form without double eval bug
  • Add fold special form
  • Update doc

@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 12, 2026

If we dissociate function arguments evaluation from parameters binding we don't need a special case for macros, so we can merge function_env and macro_env into bind and simplify the code.

@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 12, 2026

We couldn't have optional args before:

> ((fun (a b @opts) opts) 1 2 3 4)
(3 4)

> ((fun (a b @opts) opts) 1 2)
Error: Expected at least 3 arguments, got 2

This is now fixed:

> ((fun (a b @opts) opts) 1 2 3 4)
(3 4)

> ((fun (a b @opts) opts) 1 2)
()

@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 12, 2026

This last commit change the evaluation order but I think the new order is fine:

Before:

> (+ 1 2 3)
6

> (a 1 2 3)
Error: Could not find symbol 'a'

> (1 2 3)
Error: Expected first argument to be a function

> (1 2 a)
Error: Expected first argument to be a function

After:

> (+ 1 2 3)
6

> (a 1 2 3)
Error: Could not find symbol 'a'

> (1 2 3)
Error: Expected first argument to be a function

> (1 2 a)
Error: Could not find symbol 'a'

@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 12, 2026

Now that bind is no longer evaluating the function arguments it is possible to reintroduce the apply special form without the double eval issue from before.

This works now:

> (apply (fun (x) x) (list '(1 2)))
(1 2)

@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 12, 2026

With the apply helper it was straightforward to add a (left) fold special form:

> (fold - 0 '(1 2 3))
-6

@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 12, 2026

With fold we can now fix reduce:

> (reduce - '(1 2 3))
-4

> (reduce - '(1 2))
-1

> (reduce - '(1))
1

> (reduce - '())
()

@vinc vinc marked this pull request as ready for review May 13, 2026 22:37
@vinc vinc merged commit 91a8902 into trunk May 13, 2026
1 check passed
@vinc vinc deleted the lisp-binding branch May 13, 2026 22:42
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

Successfully merging this pull request may close these issues.

1 participant