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:
parent
1e6fabf954
commit
2fc321596f
|
|
@ -4,39 +4,25 @@ import * as terms from './shrimp.terms'
|
||||||
export class Scope {
|
export class Scope {
|
||||||
constructor(
|
constructor(
|
||||||
public parent: Scope | null,
|
public parent: Scope | null,
|
||||||
public vars: Set<string>,
|
public vars: Set<string>
|
||||||
public pendingIdentifiers: string[] = [],
|
|
||||||
public isInParams: boolean = false
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
has(name: string): boolean {
|
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 {
|
add(...names: string[]): Scope {
|
||||||
const newVars = new Set(this.vars)
|
const newVars = new Set(this.vars)
|
||||||
names.forEach((name) => newVars.add(name))
|
names.forEach(name => newVars.add(name))
|
||||||
return new Scope(this.parent, newVars, [], this.isInParams)
|
return new Scope(this.parent, newVars)
|
||||||
}
|
}
|
||||||
|
|
||||||
push(): Scope {
|
push(): Scope {
|
||||||
return new Scope(this, new Set(), [], false)
|
return new Scope(this, new Set())
|
||||||
}
|
}
|
||||||
|
|
||||||
pop(): Scope {
|
pop(): Scope {
|
||||||
return this.parent ?? new Scope(null, new Set(), [], false)
|
return this.parent ?? this
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hash(): number {
|
hash(): number {
|
||||||
|
|
@ -51,10 +37,6 @@ export class Scope {
|
||||||
h = (h << 5) - h + this.parent.hash()
|
h = (h << 5) - h + this.parent.hash()
|
||||||
h |= 0
|
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
|
return h
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user