The GitHub repository (github.com/Alpaq92/Riposte) is gone with the suspended account. These are the files npm installed for ReaderJS from commit 307ef7dfd9c5e8c0c07a840874e013cf94a85ada, so this is that commit's packaged content, without its history. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
34 lines
1.0 KiB
JavaScript
34 lines
1.0 KiB
JavaScript
// engine/dictstack.js
|
|
// The dictionary stack. systemdict (built-ins) sits at the bottom, userdict
|
|
// above it; `begin`/`end` push/pop further dicts. Name resolution searches
|
|
// top-down, so user definitions can shadow built-ins and `bind` works.
|
|
import { PSError } from './errors.js';
|
|
|
|
export class DictStack {
|
|
constructor(systemdict, userdict) {
|
|
this.stack = [systemdict, userdict];
|
|
this.systemdict = systemdict;
|
|
this.userdict = userdict;
|
|
}
|
|
push(dictObj) { this.stack.push(dictObj); }
|
|
pop() {
|
|
if (this.stack.length <= 2) throw new PSError('dictstackunderflow');
|
|
return this.stack.pop();
|
|
}
|
|
top() { return this.stack[this.stack.length - 1]; }
|
|
def(key, val) { this.top().value.put(key, val); }
|
|
load(key) {
|
|
for (let i = this.stack.length - 1; i >= 0; i--) {
|
|
const v = this.stack[i].value.get(key);
|
|
if (v !== undefined) return v;
|
|
}
|
|
return undefined;
|
|
}
|
|
where(key) {
|
|
for (let i = this.stack.length - 1; i >= 0; i--) {
|
|
if (this.stack[i].value.knows(key)) return this.stack[i];
|
|
}
|
|
return null;
|
|
}
|
|
}
|