UNPKG

@lcap/nasl-parser

Version:

Take Nasl text to Nasl AST with the help of generalized parsing.

77 lines (72 loc) 3.1 kB
"use strict"; /* Environment 设计。 - Map<K, Stack<V>> 很好地支持 shadow,如 let c;{ let c; } 这种重名情况。 - 但不能很好地支持 fully qualified name: 如 import a.b.* 导入的有 class c,则 可以用 c 访问,也可以用 a.b.c 访问。 只给 c 加进去的话,a.b.c 就访问不到了。反之亦然 如果给 a.b.c 和 c 都加进去的话,Map 的 key 是 c 还是 a.b.c 呢?更新时怎么弄? 静态检查时记录当前的 scope,设当前 scope 是 a.b: - 方法一:c 作为 key 记入 Env。当前 scope 是 a.b,看到名字 c,则先查找 c,查不到再查 b.c,查不到再查 a.b.c - 方法二:由当前 scope a.b 得 c 全名为 a.b.c,所以把 a.b.c 记入 scope。则 Map 的 key 全部是 fully qualified name。 看到 c 查找时也要拼接后用 a.b.c 查找。 暂定方法一。 import a.b 后 当前用 namespace a { let b; a.b } 是否能覆盖导入的 a.b? 暂定能 类型变元的名字能否覆盖类型、覆盖变量等 logic f() { }; logic g<Bool, f>(a : f, b : Bool) { } 暂定能 namespace a { namespace b { let c; } } open a.b 或 using a.b 怎么实现? - 上面,如果用“方法二”,name Env 里所有层级是 a.b.x 的脱去前缀?麻烦啊。而且如果是 open a.b {...} 的话关闭后还要加回来? - 建立查找 scope LkScope 的概念?查找时如果查不到,就拼上 LkScope 再查? 当前 scope a.b.c 的顺路查找(c, b.c, a.b.c) 和 LkScope 的优先级是怎样的? - LkScope 里没查到是否还要再往它上面查? - 否。 open a.b.c 就是只查 a.b.c.*,不查 a.b.* 和 a.*。想要的话,用户需要自己 open a.b 和 open a。 但是这样 open 本来 shadow 的场景可能变成重定义错误。 是否还提供 close a.b 这种语法? */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Env = void 0; class Env { values = new Map(); projK; constructor(projK) { this.projK = projK; this.values = new Map(); } add(key, value) { if (!this.values.has(this.projK(key))) { this.values.set(this.projK(key), []); } this.values.get(this.projK(key))?.push(value); // O(1) } remove(key) { const arr = this.values.get(this.projK(key)); if (!arr) { return; } arr.pop(); // O(1) if (arr.length === 0) { this.values.delete(this.projK(key)); } } removeAll(key) { this.values.delete(this.projK(key)); } get(key) { const arr = this.values.get(this.projK(key)); return arr ? arr.at(-1) : undefined; } getAll(key) { return this.values.get(this.projK(key)) || undefined; } has(key) { return this.values.has(this.projK(key)); } } exports.Env = Env; //# sourceMappingURL=env-stack.js.map