mimispec 0.2.1

A high-density intent description language for human-AI collaboration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
use serde::Serialize;

/// 意图后缀:附加在关键字、标识符或字符串上,表示作者对该节点的锁定与不确定程度。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize)]
#[non_exhaustive]
pub enum Commitment {
    #[serde(rename = "none")]
    #[default]
    None,
    #[serde(rename = "?")]
    Question,
    #[serde(rename = "??")]
    QuestionQuestion,
    #[serde(rename = "$")]
    Locked,
    #[serde(rename = "$$")]
    StrongLocked,
    #[serde(rename = "$?")]
    LockedQuestion,
    #[serde(rename = "$$?")]
    StrongLockedQuestion,
    #[serde(rename = "$??")]
    LockedQuestionQuestion,
    #[serde(rename = "$$??")]
    StrongLockedQuestionQuestion,
}

impl Commitment {
    /// 是否处于某种锁定状态(含锁定但存疑)。
    pub fn is_locked(&self) -> bool {
        matches!(
            self,
            Self::Locked
                | Self::StrongLocked
                | Self::LockedQuestion
                | Self::StrongLockedQuestion
                | Self::LockedQuestionQuestion
                | Self::StrongLockedQuestionQuestion
        )
    }

    /// 是否为强锁定。
    pub fn is_strong_locked(&self) -> bool {
        matches!(
            self,
            Self::StrongLocked | Self::StrongLockedQuestion | Self::StrongLockedQuestionQuestion
        )
    }

    /// 是否带不确定标记(单 `?`,作用于节点本身或锁定本身)。
    pub fn has_question(&self) -> bool {
        matches!(
            self,
            Self::Question
                | Self::LockedQuestion
                | Self::StrongLockedQuestion
                | Self::QuestionQuestion
                | Self::LockedQuestionQuestion
                | Self::StrongLockedQuestionQuestion
        )
    }

    /// 是否带完全委托标记(`??`,不含锁定成分)。
    #[allow(dead_code)]
    pub fn has_question_question(&self) -> bool {
        matches!(self, Self::QuestionQuestion)
    }
}

impl std::fmt::Display for Commitment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Commitment::None => "",
            Commitment::Question => "?",
            Commitment::QuestionQuestion => "??",
            Commitment::Locked => "$",
            Commitment::StrongLocked => "$$",
            Commitment::LockedQuestion => "$?",
            Commitment::StrongLockedQuestion => "$$?",
            Commitment::LockedQuestionQuestion => "$??",
            Commitment::StrongLockedQuestionQuestion => "$$??",
        };
        write!(f, "{}", s)
    }
}

/// 带模糊后缀的标识符(如 `desc?`、`Order?`)。
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Ident {
    pub name: String,
    #[serde(default)]
    pub commitment: Commitment,
}

/// 带模糊后缀的字符串字面量(如 `"..."?`)。
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct FString {
    pub value: String,
    #[serde(default)]
    pub commitment: Commitment,
}

/// 源文件根节点(v0.3: fragments 而非 modules,含 imports)。
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct File {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub imports: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<RuleDef>,
    pub fragments: Vec<Fragment>,
}

/// 顶层 Fragment(v0.3 新架构)。任何 Fragment 都可以作为合法顶层存在。
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum Fragment {
    Module {
        module: Module,
    },
    TypeDef {
        typedef: TypeDef,
    },
    Flow {
        flow: FlowDef,
    },
    Func {
        func: FuncDef,
    },
    Ui {
        ui: UiDef,
    },
    Steps {
        // v0.3 新增:独立 steps 块
        #[serde(default)]
        keyword_commitment: Commitment,
        steps: Vec<Step>,
    },
    Expr {
        expr: Expr,
    }, // v0.3 新增:裸表达式
    UiNode {
        node: UiNode,
    }, // v0.3 新增:裸 UI 节点
    Placeholder {
        // v0.3 新增:... 占位符
        #[serde(default)]
        keyword_commitment: Commitment,
    },
}

/// 模块或子模块。
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Module {
    pub name: Ident,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<Desc>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<RuleDef>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub math: Option<MathBlock>,
    #[serde(default)]
    pub items: Vec<Fragment>, // v0.3: Vec<Item> → Vec<Fragment>
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TypeDef {
    pub name: Ident,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<Desc>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<RuleDef>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub math: Option<MathBlock>,
    pub body: TypeBody,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum TypeBody {
    Enum { variants: Vec<Ident> },
    Record { fields: Vec<Field> },
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Field {
    pub name: Ident,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<RuleDef>,
    pub type_hint: Vec<Atom>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct RuleDef {
    pub desc: Desc,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct FlowDef {
    pub name: Ident,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<RuleDef>,
    pub entries: Vec<FlowEntry>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct FlowEntry {
    pub state: Ident,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<RuleDef>,
    pub arms: Vec<FlowArm>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct FlowArm {
    pub to: Ident,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub requires: Option<Condition>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<Desc>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<RuleDef>,
    #[serde(default)]
    pub to_keyword_commitment: Commitment,
    #[serde(default)]
    pub requires_keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct FuncDef {
    pub name: Ident,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<Desc>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<RuleDef>,
    pub params: Vec<Param>,
    #[serde(default)]
    pub capabilities: Vec<Capability>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub requires: Option<Condition>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ensures: Option<Condition>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub math: Option<MathBlock>,
    pub steps: Vec<Step>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
    #[serde(default)]
    pub requires_keyword_commitment: Commitment,
    #[serde(default)]
    pub ensures_keyword_commitment: Commitment,
    #[serde(default)]
    pub with_keyword_commitment: Commitment,
    #[serde(default)]
    pub steps_keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Param {
    pub name: Ident,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub type_hint: Vec<Atom>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Capability {
    pub name: Ident,
    #[serde(default)]
    pub commitment: Commitment,
}

/// `requires` / `ensures` 条件:结构化表达式或自然语言字符串。
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum Condition {
    Structured { expr: Expr },
    Natural { text: FString },
}

/// 简单表达式 AST(支持比较、逻辑连接)。
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum Expr {
    Ident {
        value: Ident,
    },
    String {
        value: FString,
    },
    Number {
        value: String,
    },
    Bool {
        value: bool,
        #[serde(default)]
        keyword_commitment: Commitment,
    },
    List {
        items: Vec<Expr>,
    },
    Not {
        expr: Box<Expr>,
        #[serde(default)]
        keyword_commitment: Commitment,
    },
    And {
        left: Box<Expr>,
        right: Box<Expr>,
        #[serde(default)]
        keyword_commitment: Commitment,
    },
    Or {
        left: Box<Expr>,
        right: Box<Expr>,
        #[serde(default)]
        keyword_commitment: Commitment,
    },
    In {
        left: Box<Expr>,
        right: Box<Expr>,
        #[serde(default)]
        keyword_commitment: Commitment,
    },
    Compare {
        left: Box<Expr>,
        op: CompareOp,
        right: Box<Expr>,
        #[serde(default)]
        keyword_commitment: Commitment,
    },
    Neg {
        expr: Box<Expr>,
        #[serde(default)]
        keyword_commitment: Commitment,
    },
    Add {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    Sub {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    Mul {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    Div {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    Pow {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    MatMul {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    BitAnd {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    BitOr {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    BitXor {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    BitNot {
        expr: Box<Expr>,
        #[serde(default)]
        keyword_commitment: Commitment,
    },
    Shl {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    Shr {
        left: Box<Expr>,
        right: Box<Expr>,
    },
    Index {
        object: Box<Expr>,
        field: Ident,
    },
    Subscript {
        object: Box<Expr>,
        indices: Vec<Expr>,
    },
    Call {
        callee: Box<Expr>,
        args: Vec<Expr>,
    },
    Placeholder {
        #[serde(default)]
        keyword_commitment: Commitment,
    },
}

/// math: 块,包含一组数学语句(定义、约束或推导)。
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct MathBlock {
    pub statements: Vec<MathStatement>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum MathStatement {
    /// 定义/赋值式:target = value
    Define { target: Expr, value: Expr },
    /// 纯表达式语句(约束、等式、推导等)
    Expr { expr: Expr },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub enum CompareOp {
    Eq,
    Ne,
    Lt,
    Gt,
    Le,
    Ge,
}

/// 步骤:动作、控制流、错误处理等。
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum Step {
    Action { step: ActionStep },
    Assign { step: AssignStep },
    If { step: IfStep },
    For { step: ForStep },
    While { step: WhileStep },
    Parasteps { step: ParastepsStep },
    Error { step: ErrorStep },
    Desc { content: Desc }, // v0.3.1 新增:desc 作为独立 step
    Placeholder { keyword_commitment: Commitment }, // v0.3 新增:... 占位符
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ActionStep {
    #[serde(default)]
    pub label: Vec<Atom>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<Desc>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub to: Option<ToTarget>,
    #[serde(default)]
    pub on_blocks: Vec<OnBlock>,
}

/// 赋值步骤:target = simple_value。
/// `=` 只能出现在动作行,右侧必须是简单值(枚举值、字段值、字面量、列表字面量)。
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct AssignStep {
    pub target: Expr,
    pub value: SimpleValue,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<Desc>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub to: Option<ToTarget>,
    #[serde(default)]
    pub on_blocks: Vec<OnBlock>,
}

/// 赋值右侧允许的简单值。
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum SimpleValue {
    Ident {
        value: Ident,
    },
    String {
        value: FString,
    },
    Number {
        value: String,
    },
    Bool {
        value: bool,
        #[serde(default)]
        keyword_commitment: Commitment,
    },
    List {
        items: Vec<Vec<Atom>>,
    },
    Placeholder {
        commitment: Commitment,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct IfStep {
    pub cond: Condition,
    pub then_branch: Vec<Step>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub else_branch: Option<Vec<Step>>,
    #[serde(default)]
    pub if_keyword_commitment: Commitment,
    #[serde(default)]
    pub else_keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ForStep {
    pub var: Ident,
    pub iterable: Vec<Atom>,
    pub body: Vec<Step>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct WhileStep {
    pub cond: Condition,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<Desc>,
    pub body: Vec<Step>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ParastepsStep {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<FString>,
    pub steps: Vec<Step>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ErrorStep {
    pub message: FString,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub to: Option<ToTarget>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct OnBlock {
    pub condition: Vec<Atom>,
    pub steps: Vec<Step>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ToTarget {
    pub target: Ident,
}

/// `desc` 独立语义:关键字位置 `?` 表示存在性不确定;字符串位置 `?` 表示内容不确定。
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Desc {
    #[serde(default)]
    pub need_commitment: Commitment,
    pub content: FString,
}

/// 原始词法单元,用于保留 AI/人类书写的自由动作标签或类型提示。
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum Atom {
    Ident { value: Ident },
    String { value: FString },
    Number { value: String },
    Symbol { value: String },
    List { items: Vec<Vec<Atom>> },
    Ellipsis { commitment: Commitment },
}

// ── UI 块 ──────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct UiDef {
    pub name: Ident,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub binds: Option<Ident>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<RuleDef>,
    pub root: UiNode,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum UiNode {
    Stack { stack: StackNode },
    Parallel { parallel: StackNode },
    Leaf { leaf: UiLeaf },
    Error { error: UiErrorNode },
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct UiErrorNode {
    pub message: FString,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<Desc>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct StackNode {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<FString>,
    pub children: Vec<UiNode>,
    #[serde(default)]
    pub keyword_commitment: Commitment,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct UiLeaf {
    pub content: FString,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<Desc>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub requires: Option<Condition>,
    #[serde(default)]
    pub requires_keyword_commitment: Commitment,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub with: Vec<Capability>,
    #[serde(default)]
    pub with_keyword_commitment: Commitment,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub on: Option<OnBinding>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct OnBinding {
    pub event_name: EventName,
    pub action: ActionExpr,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum EventName {
    Ident { value: Ident },
    Natural { text: FString },
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ActionExpr {
    pub actions: Vec<Action>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
#[non_exhaustive]
pub enum Action {
    Call { expr: Expr },
    Navigate { target: Ident },
    Assign { target: Expr, value: Expr },
    Natural { text: FString },
}