<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Interpreter - 标签 - 星河拾贝录</title><link>https://blog.liubang.cc/tags/interpreter/</link><description>Interpreter - 标签 - 星河拾贝录</description><generator>Hugo -- gohugo.io</generator><language>zh-CN</language><managingEditor>it.liubang@gmail.com (liubang)</managingEditor><webMaster>it.liubang@gmail.com (liubang)</webMaster><copyright>Copyright © 2019-2026 LiuBang. All Rights Reserved.</copyright><lastBuildDate>Sat, 23 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.liubang.cc/tags/interpreter/" rel="self" type="application/rss+xml"/><item><title>表达式解释器：让 Flux 代码真正跑起来</title><link>https://blog.liubang.cc/flux/2026-05-23-flux-engine-03-runtime-evaluator/</link><pubDate>Sat, 23 May 2026 00:00:00 +0000</pubDate><author><name>liubang</name></author><guid>https://blog.liubang.cc/flux/2026-05-23-flux-engine-03-runtime-evaluator/</guid><description><![CDATA[<p>Parser 负责把源码变成 AST，但 AST 本身不会执行。<code>cpp/pl/flux</code> 的执行入口主要在 <code>runtime/runtime_eval.cpp</code> 和 <code>runtime/runtime_exec.cpp</code>：前者处理表达式求值，后者处理文件级语句执行、结果收集和顶层环境。</p>
<h2 id="value运行时的统一数据模型" class="headerLink">
    <a href="#value%e8%bf%90%e8%a1%8c%e6%97%b6%e7%9a%84%e7%bb%9f%e4%b8%80%e6%95%b0%e6%8d%ae%e6%a8%a1%e5%9e%8b" class="header-mark"></a>Value：运行时的统一数据模型</h2><p>解释器首先需要一个统一的运行时值类型。当前 <code>Value</code> 覆盖了 Flux 子集执行所需的主要类型：</p>
<ul>
<li><code>null</code></li>
<li><code>bool</code></li>
<li><code>int</code> / <code>uint</code> / <code>float</code></li>
<li><code>string</code></li>
<li><code>time</code></li>
<li><code>duration</code></li>
<li><code>regexp</code></li>
<li><code>array</code></li>
<li><code>object</code></li>
<li><code>function</code></li>
<li><code>table</code></li>
</ul>
<p>数组和对象是很多高级能力的基础。数组承载 <code>array.map/filter/reduce</code> 这类高阶函数；对象既表示普通 record，也表示 package object、函数命名参数和 table row。</p>
<p><code>table</code> 是项目中特别重要的一类值。它不是单张二维表的简单包装，而是 Flux table stream 的内存表示，包含 rows、logical tables、group key、bucket、result name 等信息。后续查询执行文章会单独展开。</p>
<h2 id="value-的工程取舍" class="headerLink">
    <a href="#value-%e7%9a%84%e5%b7%a5%e7%a8%8b%e5%8f%96%e8%88%8d" class="header-mark"></a>Value 的工程取舍</h2><p><code>Value</code> 是解释器最核心的数据结构之一。它需要在两个方向之间取平衡：一方面要足够通用，能承载 Flux 的动态值；另一方面不能变成“什么都能塞”的无约束容器。</p>
<p>当前实现把类型枚举和具体 payload 绑定在一起，调用方通过 <code>type()</code> 判断，再使用 <code>as_int()</code>、<code>as_array()</code>、<code>as_object()</code> 这类访问器取值。这种设计比到处使用 <code>std::any</code> 更可控，也比一开始引入复杂类型系统更轻量。</p>
<p>它的代价是 runtime 需要大量显式类型检查。例如 builtin 取参数时必须确认字段存在、类型正确、数组元素符合预期。项目里很多 helper 的价值就在这里：把重复的参数检查、错误信息和 <code>StatusOr</code> 返回模式收敛起来，避免每个 builtin 都手写一套脆弱逻辑。</p>]]></description></item></channel></rss>