This commit is contained in:
Chris Wanstrath 2025-10-28 21:18:24 -07:00
parent 982054eb54
commit 8112515278
2 changed files with 30 additions and 19 deletions

View File

@ -470,7 +470,18 @@ export class Compiler {
case terms.Array: {
const children = getAllChildren(node)
// todo: [ = ]
// We can easily parse [=] as an empty dict, but `[ = ]` is tougher.
// = can be a valid word, and also valid in words, so for now we cheat
// and check for arrays that look like `[ = ]` to interpret them as
// empty dicts
if (children.length === 1 && children[0].name === 'Word') {
const child = children[0]
if (input.slice(child.from, child.to) === '=') {
return [['MAKE_DICT', 0]]
}
}
const instructions: ProgramItem[] = children.map((x) => this.#compileNode(x, input)).flat()
instructions.push(['MAKE_ARRAY', children.length])
return instructions

View File

@ -127,6 +127,7 @@ describe('dict literals', () => {
test('empty dict', () => {
expect('[=]').toEvaluateTo({})
expect('[ = ]').toEvaluateTo({})
})
test('mixed types', () => {
expect("[a=1 b='two' c=three d=true e=null]").toEvaluateTo({
@ -136,6 +137,7 @@ describe('dict literals', () => {
d: true,
e: null,
})
})
test('semicolons as separators', () => {
expect('[a=1; b=2; c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 })
@ -153,5 +155,3 @@ describe('dict literals', () => {
c=3]`).toEvaluateTo({ a: 1, b: 2, c: 3 })
})
})
})
})