Merge remote-tracking branch 'origin/main' into vscode

This commit is contained in:
Corey Johnson 2025-11-05 16:20:10 -08:00
commit 66fa15595c
5 changed files with 30 additions and 12 deletions

View File

@ -75,6 +75,7 @@ export class Compiler {
if (DEBUG) { if (DEBUG) {
const bytecodeString = bytecodeToString(this.bytecode) const bytecodeString = bytecodeToString(this.bytecode)
console.log(`\n🤖 bytecode:\n----------------\n${bytecodeString}\n\n`) console.log(`\n🤖 bytecode:\n----------------\n${bytecodeString}\n\n`)
console.log(`\n🤖 bytecode:\n----------------\n${this.instructions}\n\n`)
} }
} catch (error) { } catch (error) {
if (error instanceof CompilerError) { if (error instanceof CompilerError) {
@ -432,8 +433,9 @@ export class Compiler {
instructions.push(['JUMP', afterLabel]) instructions.push(['JUMP', afterLabel])
instructions.push([`${fnLabel}:`]) instructions.push([`${fnLabel}:`])
instructions.push( instructions.push(
...block.filter(x => x.type.name !== 'keyword') ...block
.map(x => this.#compileNode(x!, input)) .filter((x) => x.type.name !== 'keyword')
.map((x) => this.#compileNode(x!, input))
.flat() .flat()
) )
instructions.push(['RETURN']) instructions.push(['RETURN'])
@ -453,15 +455,16 @@ export class Compiler {
body = [ body = [
...body.slice(0, startSlice), ...body.slice(0, startSlice),
['MAKE_FUNCTION', [], fnLabel], ['MAKE_FUNCTION', [], fnLabel],
...body.slice(startSlice) ...body.slice(startSlice),
] ]
// @ts-ignore // @ts-ignore
body[body.length - 3]![1] += 1 body[body.length - 3]![1] += 1
instructions.push(...body) instructions.push(...body)
} else { } else {
throw new Error(`FunctionCallWithBlock: Expected FunctionCallOrIdentifier or FunctionCall`) throw new Error(
`FunctionCallWithBlock: Expected FunctionCallOrIdentifier or FunctionCall`
)
} }
return instructions return instructions
@ -660,7 +663,7 @@ export class Compiler {
// = can be a valid word, and is also valid inside words, so for now we cheat // = can be a valid word, and is also valid inside words, so for now we cheat
// and check for arrays that look like `[ = ]` to interpret them as // and check for arrays that look like `[ = ]` to interpret them as
// empty dicts // empty dicts
if (children.length === 1 && children[0]!.name === 'Word') { if (children.length === 1 && children[0]!.type.id === terms.Word) {
const child = children[0]! const child = children[0]!
if (input.slice(child.from, child.to) === '=') { if (input.slice(child.from, child.to) === '=') {
return [['MAKE_DICT', 0]] return [['MAKE_DICT', 0]]
@ -709,6 +712,10 @@ export class Compiler {
return instructions return instructions
} }
case terms.Comment: {
return [] // ignore comments
}
default: default:
throw new CompilerError( throw new CompilerError(
`Compiler doesn't know how to handle a "${node.type.name}" node.`, `Compiler doesn't know how to handle a "${node.type.name}" node.`,

View File

@ -66,8 +66,8 @@ describe('array literals', () => {
}) })
test('comments within arrays', () => { test('comments within arrays', () => {
expect(`[1 # first expect(`[1
2 # second 2
]`).toEvaluateTo([1, 2]) ]`).toEvaluateTo([1, 2])
}) })

View File

@ -22,7 +22,8 @@ export const getAllChildren = (node: SyntaxNode): SyntaxNode[] => {
children.push(child) children.push(child)
child = child.nextSibling child = child.nextSibling
} }
return children
return children.filter((n) => n.type.id !== terms.Comment)
} }
export const getBinaryParts = (node: SyntaxNode) => { export const getBinaryParts = (node: SyntaxNode) => {
@ -50,13 +51,15 @@ export const getAssignmentParts = (node: SyntaxNode) => {
// array destructuring // array destructuring
if (left && left.type.id === terms.Array) { if (left && left.type.id === terms.Array) {
const identifiers = getAllChildren(left).filter(child => child.type.id === terms.Identifier) const identifiers = getAllChildren(left).filter((child) => child.type.id === terms.Identifier)
return { arrayPattern: identifiers, right } return { arrayPattern: identifiers, right }
} }
if (!left || left.type.id !== terms.AssignableIdentifier) { if (!left || left.type.id !== terms.AssignableIdentifier) {
throw new CompilerError( throw new CompilerError(
`Assign left child must be an AssignableIdentifier or Array, got ${left ? left.type.name : 'none'}`, `Assign left child must be an AssignableIdentifier or Array, got ${
left ? left.type.name : 'none'
}`,
node.from, node.from,
node.to node.to
) )
@ -71,7 +74,9 @@ export const getCompoundAssignmentParts = (node: SyntaxNode) => {
if (!left || left.type.id !== terms.AssignableIdentifier) { if (!left || left.type.id !== terms.AssignableIdentifier) {
throw new CompilerError( throw new CompilerError(
`CompoundAssign left child must be an AssignableIdentifier, got ${left ? left.type.name : 'none'}`, `CompoundAssign left child must be an AssignableIdentifier, got ${
left ? left.type.name : 'none'
}`,
node.from, node.from,
node.to node.to
) )

View File

@ -150,8 +150,11 @@ describe('array literals', () => {
2 # second 2 # second
]`).toMatchTree(` ]`).toMatchTree(`
Array Array
Comment # something...
Number 1 Number 1
Comment # first
Number 2 Number 2
Comment # second
`) `)
}) })
@ -397,12 +400,15 @@ c=3]`).toMatchTree(`
c=3 c=3
]`).toMatchTree(` ]`).toMatchTree(`
Dict Dict
Comment # something...
NamedArg NamedArg
NamedArgPrefix a= NamedArgPrefix a=
Number 1 Number 1
Comment # first
NamedArg NamedArg
NamedArgPrefix b= NamedArgPrefix b=
Number 2 Number 2
Comment # second
NamedArg NamedArg
NamedArgPrefix c= NamedArgPrefix c=
Number 3 Number 3

Binary file not shown.