I have extended vscode with an extension #23

Merged
probablycorey merged 15 commits from vscode into main 2025-11-06 00:20:29 +00:00
5 changed files with 30 additions and 12 deletions
Showing only changes of commit 66fa15595c - Show all commits

View File

@ -75,6 +75,7 @@ export class Compiler {
if (DEBUG) {
const bytecodeString = bytecodeToString(this.bytecode)
console.log(`\n🤖 bytecode:\n----------------\n${bytecodeString}\n\n`)
console.log(`\n🤖 bytecode:\n----------------\n${this.instructions}\n\n`)
}
} catch (error) {
if (error instanceof CompilerError) {
@ -432,8 +433,9 @@ export class Compiler {
instructions.push(['JUMP', afterLabel])
instructions.push([`${fnLabel}:`])
instructions.push(
...block.filter(x => x.type.name !== 'keyword')
.map(x => this.#compileNode(x!, input))
...block
.filter((x) => x.type.name !== 'keyword')
.map((x) => this.#compileNode(x!, input))
.flat()
)
instructions.push(['RETURN'])
@ -453,15 +455,16 @@ export class Compiler {
body = [
...body.slice(0, startSlice),
['MAKE_FUNCTION', [], fnLabel],
...body.slice(startSlice)
...body.slice(startSlice),
]
// @ts-ignore
body[body.length - 3]![1] += 1
instructions.push(...body)
} else {
throw new Error(`FunctionCallWithBlock: Expected FunctionCallOrIdentifier or FunctionCall`)
throw new Error(
`FunctionCallWithBlock: Expected FunctionCallOrIdentifier or FunctionCall`
)
}
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
// and check for arrays that look like `[ = ]` to interpret them as
// empty dicts
if (children.length === 1 && children[0]!.name === 'Word') {
if (children.length === 1 && children[0]!.type.id === terms.Word) {
const child = children[0]!
if (input.slice(child.from, child.to) === '=') {
return [['MAKE_DICT', 0]]
@ -709,6 +712,10 @@ export class Compiler {
return instructions
}
case terms.Comment: {
return [] // ignore comments
}
default:
throw new CompilerError(
`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', () => {
expect(`[1 # first
2 # second
expect(`[1
2
]`).toEvaluateTo([1, 2])
})

View File

@ -22,7 +22,8 @@ export const getAllChildren = (node: SyntaxNode): SyntaxNode[] => {
children.push(child)
child = child.nextSibling
}
return children
return children.filter((n) => n.type.id !== terms.Comment)
}
export const getBinaryParts = (node: SyntaxNode) => {
@ -50,13 +51,15 @@ export const getAssignmentParts = (node: SyntaxNode) => {
// array destructuring
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 }
}
if (!left || left.type.id !== terms.AssignableIdentifier) {
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.to
)
@ -71,7 +74,9 @@ export const getCompoundAssignmentParts = (node: SyntaxNode) => {
if (!left || left.type.id !== terms.AssignableIdentifier) {
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.to
)

View File

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

Binary file not shown.