Skip to main content

ternlang_core/
ast.rs

1#[derive(Debug, Clone, PartialEq)]
2pub enum Expr {
3    TritLiteral(i8),
4    IntLiteral(i64),
5    FloatLiteral(f64),
6    StringLiteral(String),
7    Ident(String),
8    BinaryOp {
9        op: BinOp,
10        lhs: Box<Expr>,
11        rhs: Box<Expr>,
12    },
13    UnaryOp {
14        op: UnOp,
15        expr: Box<Expr>,
16    },
17    Call {
18        callee: String,
19        args: Vec<Expr>,
20    },
21    /// field access: `object.field`
22    FieldAccess {
23        object: Box<Expr>,
24        field: String,
25    },
26    /// cast(expr) — type coercion built-in
27    Cast {
28        expr: Box<Expr>,
29        ty: Type,
30    },
31    /// spawn AgentName — creates a local agent instance, evaluates to AgentRef
32    /// spawn remote "addr" AgentName — creates a remote agent instance (Phase 5.1)
33    Spawn {
34        agent_name: String,
35        /// None = local, Some("host:port") = remote node
36        node_addr: Option<String>,
37    },
38    /// await <agentref_expr> — receive result from agent mailbox
39    Await {
40        target: Box<Expr>,
41    },
42    /// tensor[row, col] indexing (Phase 4.1)
43    Index {
44        object: Box<Expr>,
45        row: Box<Expr>,
46        col: Box<Expr>,
47    },
48    /// nodeid — returns the current node's address (Phase 5.1)
49    NodeId,
50    /// expr? — ternary error propagation.
51    /// If the inner expression evaluates to -1 (conflict), the current function
52    /// returns -1 immediately.  Otherwise execution continues with the value.
53    Propagate {
54        expr: Box<Expr>,
55    },
56    /// [1, 0, -1]
57    TritTensorLiteral(Vec<i8>),
58}
59
60#[derive(Debug, Clone, Copy, PartialEq)]
61pub enum BinOp {
62    Add,
63    Sub,
64    Mul,
65    Div,
66    Mod,
67    Equal,
68    NotEqual,
69    Less,
70    Greater,
71    LessEqual,
72    GreaterEqual,
73    And,
74    Or,
75}
76
77#[derive(Debug, Clone, Copy, PartialEq)]
78pub enum UnOp {
79    Neg,
80}
81
82#[derive(Debug, Clone, PartialEq)]
83pub enum Stmt {
84    Let {
85        name: String,
86        ty: Type,
87        value: Expr,
88    },
89    IfTernary {
90        condition: Expr,
91        on_pos: Box<Stmt>,   // branch when +1
92        on_zero: Box<Stmt>,  // branch when  0
93        on_neg: Box<Stmt>,   // branch when -1
94    },
95    Match {
96        condition: Expr,
97        arms: Vec<(i64, Stmt)>,
98    },
99    /// for <var> in <iter_expr> { body }
100    ForIn {
101        var: String,
102        iter: Expr,
103        body: Box<Stmt>,
104    },
105    /// while <condition> ? { on_pos } else { on_zero } else { on_neg }
106    WhileTernary {
107        condition: Expr,
108        on_pos: Box<Stmt>,
109        on_zero: Box<Stmt>,
110        on_neg: Box<Stmt>,
111    },
112    /// loop { body } — infinite loop, exited via break
113    Loop {
114        body: Box<Stmt>,
115    },
116    Break,
117    Continue,
118    Block(Vec<Stmt>),
119    Return(Expr),
120    Expr(Expr),
121    Decorated {
122        directive: String,
123        stmt: Box<Stmt>,
124    },
125    /// use path::to::module;
126    Use {
127        path: Vec<String>,
128    },
129    /// send <agentref_expr> <message_expr>;
130    Send {
131        target: Expr,
132        message: Expr,
133    },
134    /// instance.field = value;
135    FieldSet {
136        object: String,
137        field: String,
138        value: Expr,
139    },
140    /// tensor[row, col] = value;
141    IndexSet {
142        object: String,
143        row: Expr,
144        col: Expr,
145        value: Expr,
146    },
147    /// ident = value;
148    Set {
149        name: String,
150        value: Expr,
151    },
152}
153
154#[derive(Debug, Clone, PartialEq)]
155pub enum Type {
156    Trit,
157    TritTensor { dims: Vec<usize> },
158    Int,
159    Bool,
160    Float,
161    String,
162    /// User-defined struct type
163    Named(String),
164    /// Handle to a running agent instance
165    AgentRef,
166}
167
168#[derive(Debug, Clone, PartialEq)]
169pub struct Function {
170    pub name: String,
171    pub params: Vec<(String, Type)>,
172    pub return_type: Type,
173    pub body: Vec<Stmt>,
174    pub directive: Option<String>,
175}
176
177/// Top-level struct definition: `struct Name { field: type, ... }`
178#[derive(Debug, Clone, PartialEq)]
179pub struct StructDef {
180    pub name: String,
181    pub fields: Vec<(String, Type)>,
182}
183
184/// Top-level agent definition: `agent Name { fn handle(msg: trit) -> trit { ... } }`
185/// v0.1: agents have a single `handle` method that processes each incoming message.
186#[derive(Debug, Clone, PartialEq)]
187pub struct AgentDef {
188    pub name: String,
189    pub methods: Vec<Function>,
190}
191
192#[derive(Debug, Clone, PartialEq)]
193pub struct Program {
194    pub imports: Vec<Vec<String>>,
195    pub structs: Vec<StructDef>,
196    pub agents: Vec<AgentDef>,
197    pub functions: Vec<Function>,
198}