Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class Interpreter {
for (const node of script) {
switch (node.type) {
case 'ns': {
await this.collectNsMember(node);
await this.collectNsMember(node, this.scope);
break;
}

Expand All @@ -191,10 +191,11 @@ export class Interpreter {
}

@autobind
private async collectNsMember(ns: Ast.Namespace): Promise<void> {
const scope = this.scope.createChildScope();
private async collectNsMember(nsNode: Ast.Namespace, parentScope: Scope, parentPrefix?: string): Promise<void> {
const scope = parentScope.createChildScope();
const prefix = (parentPrefix ?? '') + nsNode.name + ':';

for (const node of ns.members) {
for (const node of nsNode.members) {
switch (node.type) {
case 'def': {
if (node.mut) {
Expand All @@ -207,12 +208,13 @@ export class Interpreter {
};
scope.add(node.name, variable);

this.scope.add(ns.name + ':' + node.name, variable);
this.scope.add(prefix + node.name, variable);
break;
}

case 'ns': {
break; // TODO
this.collectNsMember(node, scope, prefix);
break;
}

default: {
Expand Down
15 changes: 15 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,21 @@ describe('namespace', () => {
eq(res, STR('kawaii'));
});

test.concurrent('recursive', async () => {
const res = await exe(`
<: Foo:Bar:baz()

:: Foo {
let ai = 'aichan'
:: Bar {
let kawa = 'kawaii'
@baz() { \`{ai} {kawa}\` }
}
}
`);
eq(res, STR('aichan kawaii'));
});

test.concurrent('cannot declare mutable variable', async () => {
try {
await exe(`
Expand Down