Skip to main content

ternlang_core/
ast.rs

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