Skip to main content

fic/ast/
event.rs

1//! Event AST nodes for the Fiducia language.
2//!
3//! An *event* is one atomic step in a process — what appears on the
4//! left of `->` in a CSP-style prefix. `CoreFi` (paper §4.2) has three
5//! kinds, all atomic from the process algebra's point of view:
6//!
7//! - **Computation event** ([`Event::CPEvent`]): a named block of
8//!   assignments and external calls, declared at the top level of a
9//!   `.fi` file and referenced from a process by `name` or `name.x.y`
10//!   (carriers).
11//! - **Channel event** ([`Event::CMEvent`]): inline PD-to-PD I/O — `c?x`
12//!   (receive) or `c!e` (send) on a buffered channel, `c?` or `c!` on an
13//!   unbuffered one, which carries no value.
14//! - **Hardware interrupt** ([`Event::IEVENT`]): inline atom on the
15//!   reserved `irq` channel — `irq?N` (await) or `irq!N` (ack), with
16//!   an integer IRQ number.
17
18use std::ops::Range;
19
20use serde::Serialize;
21
22use super::expr::Expr;
23use super::statement::Statement;
24use super::types::Type;
25
26/// An event at a process-prefix use site.
27#[derive(Debug, Clone, Serialize)]
28pub enum Event {
29    /// Computation-event reference: `name` or `name.x.y`. The full
30    /// declaration ([`EvtDecl`]) lives in an `Events:` section and is
31    /// resolved at validation time.
32    CPEvent {
33        /// Name of the referenced computation event.
34        name: String,
35        /// Carrier identifiers passed at the use site. Bare names for
36        /// now; will become `Vec<Expr>` once the expression grammar
37        /// lands.
38        carriers: Vec<String>,
39        /// Byte span of the use site, for diagnostics.
40        span: Range<usize>,
41    },
42    /// PD-to-PD channel I/O: `c?x` or `c!e`, `c?` or `c!`.
43    CMEvent {
44        /// Channel name (resolved against `.fis` declarations).
45        channel: String,
46        /// Direction and direction-specific payload.
47        kind: CommKind,
48        /// Byte span of the use site, for diagnostics.
49        span: Range<usize>,
50    },
51    /// Hardware interrupt on the reserved `irq` channel: `irq?N` /
52    /// `irq!N` with an integer IRQ number.
53    IEVENT {
54        /// IRQ number.
55        irq: u32,
56        /// Direction.
57        kind: IrqKind,
58    },
59}
60
61/// Direction of a [`Event::CMEvent`].
62#[derive(Debug, Clone, Serialize)]
63pub enum CommKind {
64    /// `c?x` — receive, binding the value to `x`; `c?` receives none.
65    Recv {
66        /// Local variable receiving the value, if any.
67        var: Option<String>,
68    },
69    /// `c!e` — send; `c!` sends none.
70    Send {
71        /// Payload expression, if any.
72        expr: Option<Expr>,
73    },
74}
75
76/// Direction of an [`Event::IEVENT`].
77#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
78pub enum IrqKind {
79    /// `irq?N` — await.
80    Await,
81    /// `irq!N` — acknowledge.
82    Ack,
83}
84
85/// A computation-event declaration.
86///
87/// Syntax:
88///
89/// ```text
90/// evt ident carriers? ':' (stmt | '{' stmt* '}')
91/// carriers ::= ('.' '|' param (',' param)* '|')+
92/// param    ::= ident ':' type
93/// ```
94///
95/// Carriers are read-only inputs supplied by the invoking process.
96/// Multiple carriers may be grouped (`.|x: int, y: int|`) or chained
97/// (`.|x: int|.|y: int|`); both forms are equivalent. Names follow
98/// `snake_case` convention (lowercase first letter), distinguishing
99/// events visually from sub-processes.
100///
101/// The body is one statement (`: stmt;`) or any number in braces.
102///
103/// ## Example
104///
105/// ```text
106/// evt handle_irq: eth.handle_irq();
107///
108/// evt handle_packet.|size: int|: {
109///     local_buf = eth.alloc(size);
110///     counter = counter + 1;
111/// }
112/// ```
113///
114/// Language reference: [Event Declarations](https://fiducia-lang.github.io/docs/lang-spec/items/event-blocks.html).
115#[derive(Debug, Clone, Serialize)]
116pub struct EvtDecl {
117    /// Event name (`snake_case`, lowercase by convention).
118    pub name: String,
119    /// Carriers in declaration order. Empty for events with no
120    /// `.|...|` clause. Each entry is `(type, name)`.
121    pub carriers: Vec<(Type, String)>,
122    /// Event body: the statements after the `:`.
123    pub body: Vec<Statement>,
124    /// Byte span of the declaration in the source.
125    pub span: Range<usize>,
126}