sesh-ast 1.0.0

Abstract Syntax Tree (AST) definitions for the Sesh Robotics Programming Language
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
use serde::{Deserialize, Serialize};
use std::fmt;

/// Source code span for error reporting and diagnostics
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Span {
    pub start_line: usize,
    pub start_col: usize,
    pub end_line: usize,
    pub end_col: usize,
}

impl Span {
    pub fn new(start_line: usize, start_col: usize, end_line: usize, end_col: usize) -> Self {
        Self {
            start_line,
            start_col,
            end_line,
            end_col,
        }
    }
}

impl fmt::Display for Span {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}:{}-{}:{}",
            self.start_line, self.start_col, self.end_line, self.end_col
        )
    }
}

/// Primitive and user-defined data types in Sesh
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Type {
    I8,
    I16,
    I32,
    I64,
    U8,
    U16,
    U32,
    U64,
    F32,
    F64,
    Bool,
    String,
    Void,
    Pin,      // Hardware GPIO pin handle
    Bus,      // Hardware bus handle (I2C, SPI, CAN, UART)
    Duration, // Realtime duration (e.g., 10ms, 500us)
    Custom(String),
    Array(Box<Type>, usize),
}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Type::I8 => write!(f, "i8"),
            Type::I16 => write!(f, "i16"),
            Type::I32 => write!(f, "i32"),
            Type::I64 => write!(f, "i64"),
            Type::U8 => write!(f, "u8"),
            Type::U16 => write!(f, "u16"),
            Type::U32 => write!(f, "u32"),
            Type::U64 => write!(f, "u64"),
            Type::F32 => write!(f, "f32"),
            Type::F64 => write!(f, "f64"),
            Type::Bool => write!(f, "bool"),
            Type::String => write!(f, "string"),
            Type::Void => write!(f, "void"),
            Type::Pin => write!(f, "pin"),
            Type::Bus => write!(f, "bus"),
            Type::Duration => write!(f, "duration"),
            Type::Custom(name) => write!(f, "{}", name),
            Type::Array(inner, size) => write!(f, "[{}; {}]", inner, size),
        }
    }
}

/// Pin configuration modes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PinMode {
    In,
    Out,
    Alt,
    Pwm,
    Analog,
}

/// Pin pull resistors
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PinPull {
    None,
    Up,
    Down,
}

/// Task execution priority in Real-Time Sesh scheduler
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TaskPriority {
    Low,
    Normal,
    High,
    Critical,
    Realtime,
}

/// Hardware bus type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BusType {
    I2C,
    SPI,
    UART,
    CAN,
}

/// Top-level program structure
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Program {
    pub imports: Vec<ImportDecl>,
    pub items: Vec<Item>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImportDecl {
    pub path: Vec<String>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Item {
    Pin(PinDecl),
    Bus(BusDecl),
    Robot(RobotDecl),
    Task(TaskDecl),
    Function(FnDecl),
    Struct(StructDecl),
    Enum(EnumDecl),
    StateMachine(StateMachineDecl),
    TopicHandler(TopicHandlerDecl),
    EventHandler(EventHandlerDecl),
    InterruptHandler(InterruptHandlerDecl),
    ServiceHandler(ServiceHandlerDecl),
    ActionHandler(ActionHandlerDecl),
    Watchdog(WatchdogDecl),
    SensorPipeline(SensorPipelineDecl),
    SafetyInvariant(SafetyInvariantDecl),
}

/// Hardware Watchdog Timer declaration: `watchdog wdt0 = Watchdog(100ms, CRITICAL);`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WatchdogDecl {
    pub name: String,
    pub timeout_ms: u32,
    pub priority: TaskPriority,
    pub span: Span,
}

/// Structured Sensor Data-Flow Pipeline: `sensor_pipeline imu_filter { from: I2C(0, 0x68), filter: EKF, out: Pose3D }`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SensorPipelineDecl {
    pub name: String,
    pub source: String,
    pub filter: String,
    pub output_type: Type,
    pub span: Span,
}

/// Static & Runtime Safety Guard: `safety_invariant(self.voltage > 10.5, "LOW_BATTERY");`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SafetyInvariantDecl {
    pub condition: Expr,
    pub panic_msg: String,
    pub span: Span,
}

/// Hardware State Machine declaration: `state_machine RoverState { ... }`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StateMachineDecl {
    pub name: String,
    pub initial_state: String,
    pub states: Vec<StateBlock>,
    pub transitions: Vec<StateTransition>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StateBlock {
    pub name: String,
    pub on_enter: Block,
    pub on_update: Block,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StateTransition {
    pub from_state: String,
    pub to_state: String,
    pub condition: String,
    pub span: Span,
}

/// Hardware Interrupt Handler: `on_interrupt(18, RISING) fn handle_irq() { ... }`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InterruptHandlerDecl {
    pub pin_num: u32,
    pub edge: String,
    pub handler_name: String,
    pub body: Block,
    pub span: Span,
}

/// ROS 2 Service Server: `on_service("rover/get_status") fn srv_handler(req: String) -> String { ... }`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ServiceHandlerDecl {
    pub service: String,
    pub req_name: String,
    pub req_type: Type,
    pub ret_type: Type,
    pub body: Block,
    pub span: Span,
}

/// ROS 2 Action Server: `on_action("rover/navigate") fn action_handler(goal: String) { ... }`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ActionHandlerDecl {
    pub action: String,
    pub goal_name: String,
    pub goal_type: Type,
    pub body: Block,
    pub span: Span,
}

/// Pin declaration: `pin led = GPIO(18, OUT);`
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PinDecl {
    pub name: String,
    pub pin_num: u32,
    pub mode: PinMode,
    pub pull: PinPull,
    pub span: Span,
}

/// Hardware Bus declaration: `i2c bus0 = I2C(0, sda, scl, 400000);`
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BusDecl {
    pub name: String,
    pub bus_type: BusType,
    pub bus_id: u32,
    pub pins: Vec<String>,
    pub baudrate: u32,
    pub span: Span,
}

/// Robot encapsulation block
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RobotDecl {
    pub name: String,
    pub pins: Vec<PinDecl>,
    pub buses: Vec<BusDecl>,
    pub fields: Vec<StructField>,
    pub methods: Vec<FnDecl>,
    pub tasks: Vec<TaskDecl>,
    pub topic_handlers: Vec<TopicHandlerDecl>,
    pub event_handlers: Vec<EventHandlerDecl>,
    pub state_machines: Vec<StateMachineDecl>,
    pub interrupt_handlers: Vec<InterruptHandlerDecl>,
    pub service_handlers: Vec<ServiceHandlerDecl>,
    pub action_handlers: Vec<ActionHandlerDecl>,
    pub watchdogs: Vec<WatchdogDecl>,
    pub sensor_pipelines: Vec<SensorPipelineDecl>,
    pub safety_invariants: Vec<SafetyInvariantDecl>,
    pub span: Span,
}

/// Real-Time task declaration with explicit timing constraints
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskDecl {
    pub name: String,
    pub deadline_ms: u32,
    pub priority: TaskPriority,
    pub body: Block,
    pub span: Span,
}

/// ROS2 topic subscription handler: `on_topic("rover/cmd_vel") fn on_velocity(msg: String) { ... }`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TopicHandlerDecl {
    pub topic: String,
    pub param_name: String,
    pub param_type: Type,
    pub body: Block,
    pub span: Span,
}

/// Interrupt / Hardware Event Handler: `on_event(PinChange(18, RISING)) { ... }`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EventHandlerDecl {
    pub event_type: String,
    pub target_id: u32,
    pub condition: String,
    pub body: Block,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FnDecl {
    pub name: String,
    pub params: Vec<Param>,
    pub ret_type: Type,
    pub body: Block,
    pub is_test: bool,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Param {
    pub name: String,
    pub ty: Type,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StructDecl {
    pub name: String,
    pub fields: Vec<StructField>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StructField {
    pub name: String,
    pub ty: Type,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EnumDecl {
    pub name: String,
    pub variants: Vec<String>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Block {
    pub statements: Vec<Stmt>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Stmt {
    Let {
        name: String,
        mutability: bool,
        ty: Option<Type>,
        init: Expr,
        span: Span,
    },
    Assign {
        target: Expr,
        value: Expr,
        span: Span,
    },
    If {
        cond: Expr,
        then_block: Block,
        else_block: Option<Block>,
        span: Span,
    },
    While {
        cond: Expr,
        body: Block,
        span: Span,
    },
    For {
        var: String,
        start: Expr,
        end: Expr,
        body: Block,
        span: Span,
    },
    Loop {
        body: Block,
        span: Span,
    },
    Match {
        target: Expr,
        arms: Vec<MatchArm>,
        span: Span,
    },
    Return {
        value: Option<Expr>,
        span: Span,
    },
    Emit {
        topic: String,
        payload: Expr,
        span: Span,
    },
    Break(Span),
    Continue(Span),
    Expr(Expr),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MatchArm {
    pub pattern: Pattern,
    pub body: Block,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Pattern {
    Literal(Literal),
    EnumVariant { enum_name: String, variant: String },
    Wildcard,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Expr {
    Literal(Literal, Span),
    Variable(String, Span),
    FieldAccess(Box<Expr>, String, Span),
    Index(Box<Expr>, Box<Expr>, Span),
    Binary {
        op: BinaryOp,
        left: Box<Expr>,
        right: Box<Expr>,
        span: Span,
    },
    Unary {
        op: UnaryOp,
        operand: Box<Expr>,
        span: Span,
    },
    Call {
        func: String,
        args: Vec<Expr>,
        span: Span,
    },
    MethodCall {
        receiver: Box<Expr>,
        method: String,
        args: Vec<Expr>,
        span: Span,
    },
    StructInit {
        name: String,
        fields: Vec<(String, Expr)>,
        span: Span,
    },
    ArrayInit(Vec<Expr>, Span),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
    Int(i64),
    Float(f64),
    Bool(bool),
    String(String),
    DurationMs(u32),
    HardwareConstant(String), // e.g., HIGH, LOW, RISING, FALLING, IN, OUT
}

impl Eq for Literal {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BinaryOp {
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    Eq,
    NotEq,
    Lt,
    LtEq,
    Gt,
    GtEq,
    And,
    Or,
    BitAnd,
    BitOr,
    BitXor,
    Shl,
    Shr,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UnaryOp {
    Neg,
    Not,
    BitNot,
}