Skip to main content

fic/ast/
statement.rs

1//! Statement AST nodes for the Fiducia language.
2//!
3//! Currently supports variable declarations (`ident: Type ;`). Assignment
4//! and call statements are scaffolded but not yet implemented.
5
6use serde::Serialize;
7use std::ops::Range;
8
9use super::expr::{CExpr, Expr};
10use super::types::Type;
11
12/// A statement in the Fiducia language.
13#[derive(Debug, Clone, Serialize)]
14pub enum Statement {
15    /// A variable declaration: `varname: Type;`.
16    Decl(VarDecl),
17    /// Assignment to an existing variable: `x = e;`.
18    Assign(Assign),
19    /// Function call as a statement: `alias.f(..);`.
20    Call(CExpr),
21    /// Debug output: `dbg(arg, ...);`.
22    Dbg(Dbg),
23}
24
25/// Debug output, a line of its arguments.
26///
27/// ## Syntax
28///
29/// ```text
30/// dbg("round ", r);
31/// ```
32///
33/// Erased wherever output has no meaning (the model), so an argument
34/// never calls a function.
35///
36/// Language reference: [Debug Output](https://fiducia-lang.github.io/docs/lang-spec/items/debug-output.html).
37#[derive(Debug, Clone, Serialize)]
38pub struct Dbg {
39    /// The arguments, printed in order.
40    pub args: Vec<DbgArg>,
41    /// Byte span of the whole statement, `dbg(..);`.
42    pub span: Range<usize>,
43}
44
45/// A [`Dbg`] argument.
46#[derive(Debug, Clone, Serialize)]
47pub enum DbgArg {
48    /// A text literal, its escapes resolved.
49    Text(String),
50    /// An integer expression.
51    Expr(Expr),
52}
53
54/// A variable declaration.
55///
56/// ## Syntax
57///
58/// ```text
59/// varname: Type [= expr] ;
60/// ```
61///
62/// When declared at the top level of a `.fi` file, the variable is a
63/// *global* — its scope is the entire file. There is no initialiser
64/// yet: `= expr` does not parse.
65///
66/// ```text
67/// counter: int;              // global
68/// ready: bool;               // boolean global
69/// ```
70///
71/// Language reference: [Variable Declarations](https://fiducia-lang.github.io/docs/lang-spec/items/variable-declarations.html).
72#[derive(Debug, Clone, Serialize, PartialEq)]
73pub struct VarDecl {
74    /// The variable's type.
75    pub type_name: Type,
76    /// The variable's name.
77    pub var_name: String,
78    /// The optional initialiser.
79    //initial: Option<Expr>,
80    /// Error positioning.
81    pub span: Range<usize>,
82}
83
84/// Assignment AST node.
85#[derive(Debug, Clone, Serialize)]
86pub struct Assign {
87    /// The target variable's name.
88    pub var_name: String,
89    /// The right-hand side expression.
90    pub rhs: Expr,
91    /// Byte span of the whole statement, `x = e;`.
92    pub span: Range<usize>,
93}