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: { case terms.Array: {
const children = getAllChildren(node) 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() const instructions: ProgramItem[] = children.map((x) => this.#compileNode(x, input)).flat()
instructions.push(['MAKE_ARRAY', children.length]) instructions.push(['MAKE_ARRAY', children.length])
return instructions return instructions

View File

@ -127,31 +127,31 @@ describe('dict literals', () => {
test('empty dict', () => { test('empty dict', () => {
expect('[=]').toEvaluateTo({}) expect('[=]').toEvaluateTo({})
expect('[ = ]').toEvaluateTo({}) expect('[ = ]').toEvaluateTo({})
})
test('mixed types', () => { test('mixed types', () => {
expect("[a=1 b='two' c=three d=true e=null]").toEvaluateTo({ expect("[a=1 b='two' c=three d=true e=null]").toEvaluateTo({
a: 1, a: 1,
b: 'two', b: 'two',
c: 'three', c: 'three',
d: true, d: true,
e: null, e: null,
}) })
})
test('semicolons as separators', () => { test('semicolons as separators', () => {
expect('[a=1; b=2; c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 }) expect('[a=1; b=2; c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 })
}) })
test('expressions in dicts', () => { test('expressions in dicts', () => {
expect('[a=(1 + 2) b=(3 * 4)]').toEvaluateTo({ a: 3, b: 12 }) expect('[a=(1 + 2) b=(3 * 4)]').toEvaluateTo({ a: 3, b: 12 })
}) })
test('empty lines within dicts', () => { test('empty lines within dicts', () => {
expect(`[a=1 expect(`[a=1
b=2 b=2
c=3]`).toEvaluateTo({ a: 1, b: 2, c: 3 }) c=3]`).toEvaluateTo({ a: 1, b: 2, c: 3 })
})
})
}) })
}) })