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

First step towards allowing variadic functions #45

Merged
merged 2 commits into from Apr 9, 2024

Conversation

quephird
Copy link
Owner

@quephird quephird commented Apr 7, 2024

In this PR, we introduce a new ParameterList struct. This will ultimately house both a list of normal parameters as well as a single trailing variadic parameter, but for now will only be used for the former. Doing so takes a first step towards fully supporting variadic functions. The following additional changes were made:

  • the lambda case for Expressions and ResolvedExpressions now has an optional ParameterList as an associated value instead of an optional list of Tokens
  • in the parser, parseParameters() now constructs and returns a ParameterList. It should be noted that parseFunction() will still initialize a lambda case with a nil value for ParameterList to preserve support for computed properties
  • in the resolver, handleLambda() continues to declare and define variables, but this time by iterating over the parameter tokens inside the ParameterList instance it receives
  • LoxCallable now has as part of its contract a computed property,parameterList, instead of arity. The three conforming objects, NativeFunction, UserDefinedFunction, and LoxClass, have been updated accordingly, but most notably, NativeFunction synthesizes Token instances with line number values set to 0 in order to return a ParameterList object.
  • in the interpreter, functions and lambdas are created with the ParameterList instance passed in instead of [Token], but it is in handleCallExpression() where the arity check is made significantly differently by calling ParameterList.checkArity(argCount:), which will throw if the checkk fails. For the time being ParameterList.checkArity(argCount:) only examines normal parameters.

@quephird quephird merged commit f620af4 into main Apr 9, 2024
@quephird quephird deleted the first_step_towards_allowing_variadic_functions branch April 9, 2024 02:21
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.

None yet

1 participant