是否有任何带有静态父项(静态链接)的开源解释器实现?

Is there any opensource interpreter implementation with static parents (static links)?

我有兴趣实现自己的语言解释器。根据 Sebesta 在 "Concepts of Programming Languages" 中的第 10 章,当编程语言允许这样的嵌套函数时,新的 ARI(活动记录实例)将填充静态父对象;

f(){
   int x;
   g() { int y = x+1; ..}        // definition of g

   k(t)  {                       // definition of k
           h(t) { t();..}        // definition of h... it calls g() via t()
           h(t);                 // this will call g();
   } 

   k(g);                         // this will call h() with g

}

在这个简单的例子中,当 g 被调用时,为 g 创建一个新的 ARI,并且这个 ARI 的静态 link 被提供给 f 的 ARI,之前存在于运行时堆栈中。

但我很难清楚地理解如何确定 ARI 的静态父代 "at runtime." 最简单的方法是搜索整个运行时堆栈,直到找到静态父代的现有 ARI,但他们说这是不是一种有效的方法。所以我想尝试更好的选择,即 "navigating along caller's static ancestors." (书中说我们可以通过从调用者的静态 link 跟随静态 link 来实现这一点。)

在上面的示例中,当通过 t() 从 h() 调用 g() 时,我们首先转到调用者 h() 的静态父 ARI,然后再次转到该 ARI 的静态父和依此类推,直到我们遇到 f() 的 ARI。因此,我们将在本例中遵循 h-k-f 的 ARI 链。 (我们将遵循更长的 ARI link 链来进行更深层次的嵌套。)

所以我的问题是;

感谢您的帮助。

这是您的代码的翻译 Lua:

function f()
    local x = 0
    local function g () 
        local y = x+1
        -- etc
    end
    local function k (t)  
        local function h (t) 
            t()
            -- etc.
        end
        h(t)
    end
    k(g)
end

这是 returns 值的另一个版本,因此我们可以证明它可以运行:

function f()
    local x = 0
    local function g () 
        local y = x+1
        return y
    end
    local function k (t)  
        local function h (t) 
            return t()
        end
        return h(t)
    end
    return k(g)
end

和一个测试:

> =f()
1
> 

请参阅 Roberto Ierusalimschy、Luiz Henrique de Figueiredo 和 Waldemar Celes 的 The Implementation of Lua 5.0,尤其是第 5 节,函数和闭包。

可以看到Lua annotated source code for version 5.1.4, or canonical source code for Lua 5.3.x