ReefVM/src/scope.ts
2025-10-05 15:21:51 -07:00

36 lines
697 B
TypeScript

import type { Value } from "./value"
export class Scope {
locals = new Map<string, Value>()
parent?: Scope
constructor(parent?: Scope) {
this.parent = parent
}
get(name: string): Value | undefined {
if (this.locals.has(name))
return this.locals.get(name)!
if (this.parent)
return this.parent.get(name)
return undefined
}
set(name: string, value: Value) {
if (this.locals.has(name))
this.locals.set(name, value)
else if (this.parent?.has(name))
this.parent.set(name, value)
else
this.locals.set(name, value)
}
has(name: string): boolean {
return this.locals.has(name) || this.parent?.has(name) || false
}
}