refactor(scope): simplify Scope class, remove pending state

- Remove pendingIdentifiers and isInParams from constructor
- Fix has() method null coalescing bug
- Simplify add(), push(), pop() methods
- Remove withPendingIdentifiers, withIsInParams, clearPending methods
- Simplify hash() to only hash vars and parent (not pending state)
- Make pop() return this instead of creating new Scope when no parent

This creates a pure, hashable Scope class that only tracks variable
scope chain. Temporary state (pending identifiers) will be moved to
ScopeContext wrapper in next task.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Corey Johnson 2025-10-17 18:38:19 -07:00
parent 1e6fabf954
commit 2fc321596f

View File

@ -4,39 +4,25 @@ import * as terms from './shrimp.terms'
export class Scope {
constructor(
public parent: Scope | null,
public vars: Set<string>,
public pendingIdentifiers: string[] = [],
public isInParams: boolean = false
public vars: Set<string>
) {}
has(name: string): boolean {
return this.vars.has(name) ?? this.parent?.has(name)
return this.vars.has(name) || (this.parent?.has(name) ?? false)
}
add(...names: string[]): Scope {
const newVars = new Set(this.vars)
names.forEach((name) => newVars.add(name))
return new Scope(this.parent, newVars, [], this.isInParams)
names.forEach(name => newVars.add(name))
return new Scope(this.parent, newVars)
}
push(): Scope {
return new Scope(this, new Set(), [], false)
return new Scope(this, new Set())
}
pop(): Scope {
return this.parent ?? new Scope(null, new Set(), [], false)
}
withPendingIdentifiers(ids: string[]): Scope {
return new Scope(this.parent, this.vars, ids, this.isInParams)
}
withIsInParams(value: boolean): Scope {
return new Scope(this.parent, this.vars, this.pendingIdentifiers, value)
}
clearPending(): Scope {
return new Scope(this.parent, this.vars, [], this.isInParams)
return this.parent ?? this
}
hash(): number {
@ -51,10 +37,6 @@ export class Scope {
h = (h << 5) - h + this.parent.hash()
h |= 0
}
// Include pendingIdentifiers and isInParams in hash
h = (h << 5) - h + this.pendingIdentifiers.length
h = (h << 5) - h + (this.isInParams ? 1 : 0)
h |= 0
return h
}
}