make defaults work, like magic

This commit is contained in:
Chris Wanstrath 2025-11-02 16:34:59 -08:00
parent 1791e5a6c7
commit 8addb77e90
2 changed files with 18 additions and 2 deletions

View File

@ -282,3 +282,19 @@ describe('dot get', () => {
expect(`a = 1; arr = array 'a' 'b' 'c'; arr.(1 + a)`).toEvaluateTo('c', { array }) 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) => { 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( 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.from,
param.to param.to
) )