From 8112515278f4d3db99180c08c3287053833ad6f8 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Tue, 28 Oct 2025 21:18:24 -0700 Subject: [PATCH] [ = ] --- src/compiler/compiler.ts | 13 ++++++++++- src/compiler/tests/literals.test.ts | 36 ++++++++++++++--------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index 7ce259f..4f8b15b 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -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 diff --git a/src/compiler/tests/literals.test.ts b/src/compiler/tests/literals.test.ts index 5c93e2a..666a4b5 100644 --- a/src/compiler/tests/literals.test.ts +++ b/src/compiler/tests/literals.test.ts @@ -127,31 +127,31 @@ 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({ - a: 1, - b: 'two', - c: 'three', - d: true, - e: null, - }) + test('mixed types', () => { + expect("[a=1 b='two' c=three d=true e=null]").toEvaluateTo({ + a: 1, + b: 'two', + c: 'three', + d: true, + e: null, + }) + }) - test('semicolons as separators', () => { - expect('[a=1; b=2; c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 }) - }) + test('semicolons as separators', () => { + expect('[a=1; b=2; c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 }) + }) - test('expressions in dicts', () => { - expect('[a=(1 + 2) b=(3 * 4)]').toEvaluateTo({ a: 3, b: 12 }) - }) + test('expressions in dicts', () => { + expect('[a=(1 + 2) b=(3 * 4)]').toEvaluateTo({ a: 3, b: 12 }) + }) - test('empty lines within dicts', () => { - expect(`[a=1 + test('empty lines within dicts', () => { + expect(`[a=1 b=2 c=3]`).toEvaluateTo({ a: 1, b: 2, c: 3 }) - }) - }) }) })