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

Function call revamp #8

Open
p7g opened this issue Jul 25, 2020 · 0 comments · May be fixed by #12
Open

Function call revamp #8

p7g opened this issue Jul 25, 2020 · 0 comments · May be fixed by #12

Comments

@p7g
Copy link
Owner

p7g commented Jul 25, 2020

Currently we determine the number of arguments to a function statically. This prevents cool things like variable numbers of arguments and, indirectly, multiple return values. Here's how we can improve it:

When we encounter an infix left paren (i.e. the start of a call expression), we'll add an opcode called OP_PREP_FOR_CALL, which will store the current stack position in the function state. This stack position will be the position after the function we'll be calling.

After that opcode will be the evaluation of all the arguments, and then the OP_CALL. This means we can just compare the stack position at call time with the stored position from prep for call to see how many arguments we actually have. This means that the number of arguments can be dynamic, without incurring much overhead. In fact, while we add an extra operation, we can remove the argument to OP_CALL so the size of a function call is smaller by 32 bits.

This means that splatting an array into the arguments of a function is just a matter of pushing all the elements of the array to the stack, and if calling a function leaves behind multiple values (multiple returns), that's fine too.

@p7g p7g linked a pull request Jul 26, 2020 that will close this issue
3 tasks
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 a pull request may close this issue.

1 participant