Prelude of builtin functions #7

Merged
defunkt merged 45 commits from prelude into main 2025-10-29 20:15:37 +00:00
2 changed files with 30 additions and 19 deletions
Showing only changes of commit 8112515278 - Show all commits

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,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 })
})
})
})
})