Add default param values #18

Merged
probablycorey merged 3 commits from defaults into main 2025-11-03 16:54:43 +00:00
2 changed files with 18 additions and 2 deletions
Showing only changes of commit 8addb77e90 - Show all commits

View File

@ -282,3 +282,19 @@ describe('dot get', () => {
expect(`a = 1; arr = array 'a' 'b' 'c'; arr.(1 + a)`).toEvaluateTo('c', { array })
})
})
describe('default params', () => {
test('parses function with single default parameter', () => {
expect('add1 = do x=1: x + 1 end; add1').toEvaluateTo(2)
expect('add1 = do x=1: x + 1 end; add1 5').toEvaluateTo(6)
})
test('parses function with multiple default parameters', () => {
expect(`weird = do x='something' y=true: [x y] end; weird`).toEvaluateTo(['something', true])
})
test('parses function with mixed parameters', () => {
expect('multiply = do x y=5: x * y end; multiply 5').toEvaluateTo(25)
expect('multiply = do x y=5: x * y end; multiply 5 2').toEvaluateTo(10)
})
})

View File

@ -99,9 +99,9 @@ export const getFunctionDefParts = (node: SyntaxNode, input: string) => {
}
const paramNames = getAllChildren(paramsNode).map((param) => {
if (param.type.id !== terms.Identifier) {
if (param.type.id !== terms.Identifier && param.type.id !== terms.NamedParam) {
throw new CompilerError(
`FunctionDef params must be Identifier, got ${param.type.name}`,
`FunctionDef params must be Identifier or NamedParam, got ${param.type.name}`,
param.from,
param.to
)