diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index 96a0b60..429a94b 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -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.`, diff --git a/src/compiler/tests/literals.test.ts b/src/compiler/tests/literals.test.ts index 666a4b5..c1dc14b 100644 --- a/src/compiler/tests/literals.test.ts +++ b/src/compiler/tests/literals.test.ts @@ -66,8 +66,8 @@ describe('array literals', () => { }) test('comments within arrays', () => { - expect(`[1 # first - 2 # second + expect(`[1 + 2 ]`).toEvaluateTo([1, 2]) }) diff --git a/src/compiler/utils.ts b/src/compiler/utils.ts index ed0dfcc..20afa96 100644 --- a/src/compiler/utils.ts +++ b/src/compiler/utils.ts @@ -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 ) diff --git a/src/parser/tests/literals.test.ts b/src/parser/tests/literals.test.ts index 693da17..3a63381 100644 --- a/src/parser/tests/literals.test.ts +++ b/src/parser/tests/literals.test.ts @@ -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 diff --git a/vscode-extension/shrimp-0.0.1.vsix b/vscode-extension/shrimp-0.0.1.vsix new file mode 100644 index 0000000..730cc92 Binary files /dev/null and b/vscode-extension/shrimp-0.0.1.vsix differ