Ainet Flow 语法 v0.1(M3 子集)
源文件扩展名
.flow(也接受.ain)。编译目标:FlowSpec v1(见 flow-spec.md)。 这是 M3 的实践子集,不是完整 Ainet;类型/session/effects 声明在后续里程碑。
1. 词法
- 注释:
//到行尾。 - 空白:空格、制表、换行均忽略;字段之间用逗号分隔(末尾逗号可选)。
- 标识符:
[A-Za-z_][A-Za-z0-9_]*。 - 字符串:双引号;支持
\"、\\、\n、\t、\r。 - 数字:十进制;支持下划线(
10_000)、小数(0.10)、指数(1e-3)、负号。 - 符号:
{ } ( ) [ ] , : . = ->。
2. 语法
flow ::= "flow" ident "{" { item } "}" EOF ;
item ::= budget | agent | edge | input ;
budget ::= "budget" "{" field { "," field } [ "," ] "}" ;
agent ::= "agent" ident "=" ident [ "(" [ field { "," field } [ "," ] ] ")" ] ;
edge ::= ident "." ident "->" ident [ ":" ident ] ;
input ::= "input" ident "{" field { "," field } [ "," ] "}" ;
field ::= ident ":" value ;
value ::= string
| number
| "true" | "false" | "null"
| array
| object ;
array ::= "[" [ value { "," value } [ "," ] ] "]" ;
object ::= "{" [ field { "," field } [ "," ] ] "}" ;
ident 区分大小写;flow/budget/agent/input/true/false/null 是保留字(作为 agent/端口名会被拒绝或产生诊断)。
3. 语义
budget
budget { usd: 0.10, tokens: 10_000, steps: 100 }
- 三个字段都必需。
usd是浮点,>= 0;tokens/steps是非负整数。- 编译为
FlowSpec.budget。省略整个budget块 → 运行时默认预算。
agent
agent researcher = researcher(model: "mock-fast")
agent writer = writer
id唯一;kind必须在运行时注册(M3 默认:researcher/critic/writer)。- 括号内是 JSON-like config;省略括号 =
{}。 - 编译为
FlowSpec.agents[]。
edge
researcher.out -> critic
from/to必须是已声明 agent;port是 from 的发送端口名。- 可选的
: tag是单消息协议标签:运行时要求该端口发出的消息tag匹配。 - 同一
(from, port)只能有一条 edge。 - 没有 edge 的端口 = 终端输出(运行时写到
outputs["<agent>.<port>"])。
input
input researcher {
tag: "task",
question: "How do interaction nets help multi-agent systems?"
}
tag必需且为字符串;其余字段组成 payload。- 编译为
FlowSpec.input。
4. 诊断
| Code | 含义 |
|---|---|
E-LEX-01 |
非法字符 |
E-LEX-02 |
非法数字 |
E-LEX-03 |
未闭合字符串 |
E-LEX-04 |
未知转义 |
E-PARSE-01 |
语法错误(缺 }/)/:/, 等) |
E-DUP-AGENT |
重复 agent id |
E-KIND |
未知 agent kind |
E-EDGE-SOURCE |
edge 源不存在 |
E-EDGE-TARGET |
edge 目标不存在 |
E-DUP-EDGE |
同一端口重复出边 |
E-INPUT-TARGET |
input 目标不存在 |
E-BUDGET-01 |
budget 字段缺失/类型错误 |
E-PROTOCOL |
运行时协议标签不匹配(ProtocolViolation) |
W-UNREACHABLE |
agent 从 input 不可达(警告) |
诊断带 line:column;CLI 会打印源码行与 caret。
5. 限制(M3 子集)
- 没有
effect/type/session声明;agent kind 的效果由运行时 behavior 决定。 - 没有
par/race/timeout/supervise。 - 没有 Core 的
agent/rule/net语法(第 1–3 章与ProgramJson仍用 JSON)。 - 没有类型检查(线性/session/effect row);只有 FlowSpec 结构校验。
- 编译目标是运行时
FlowSpec,不是ain-core的纯网 IR。
6. 版本
v0.1(M3):上述子集。- 新增语法 → 扩展
v0.x;改变语义 → major。 - 测试:
crates/ain-lang/tests/flow.rs;示例:examples/research.flow。