From 853dd18fb7d1ba82bb840ecfa91740f137e52b47 Mon Sep 17 00:00:00 2001 From: gidra5 Date: Wed, 18 Dec 2024 20:03:42 +0200 Subject: [PATCH] fix --- src/environment.ts | 12 ++++++++++++ src/evaluate/index.ts | 4 ++-- src/index.ts | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/environment.ts b/src/environment.ts index 0a1d8de..383cba0 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -88,6 +88,18 @@ export class Environment { return [...new Set(keys)]; } + readonlyKeys(): symbol[] { + const keys: symbol[] = [...this.readonly.keys()]; + if (this.parent) keys.push(...this.parent.readonlyKeys()); + return [...new Set(keys)]; + } + + mutableKeys(): symbol[] { + const keys: symbol[] = [...this.mutable.keys()]; + if (this.parent) keys.push(...this.parent.mutableKeys()); + return [...new Set(keys)]; + } + shallowCopy(): Environment { const copy = new Environment({ parent: this.parent }); copy.readonly = new Map(this.readonly); diff --git a/src/evaluate/index.ts b/src/evaluate/index.ts index 12a7682..2e5e672 100644 --- a/src/evaluate/index.ts +++ b/src/evaluate/index.ts @@ -133,7 +133,7 @@ const incAssign = (position: Position, context: CompileContext) => { context.env.has(patternKey), undeclaredName( patternKey.description!, - context.env.keys().map((k) => k.description ?? String(k)) + context.env.mutableKeys().map((k) => k.description ?? String(k)) ) ); @@ -200,7 +200,7 @@ const assign = (position: Position, context: CompileContext) => { context.env.set(patternKey, value), undeclaredName( patternKey.description!, - context.env.keys().map((k) => k.description ?? String(k)) + context.env.mutableKeys().map((k) => k.description ?? String(k)) ) ); } else { diff --git a/src/index.ts b/src/index.ts index 1804e08..93fe3ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,6 +29,7 @@ program console.log('Starting REPL...'); const file = ''; + const context = newContext(); console.log('Waiting for next input...'); const rl = readline.createInterface({ input, output, prompt: '>> ' }); rl.prompt(); @@ -42,7 +43,6 @@ program default: { const fileId = addFile(file, line); const compileContext = newCompileContext(fileId, file); - const context = newContext(); const compiled = compileScriptString(line, compileContext); const result = await compiled(context); console.dir(result, { depth: null });