fic/ast/data.rs
1//! `.fis` data-layer AST nodes: struct types and constants (#69).
2//!
3//! The data layer is the system-wide vocabulary shared by every PD and by
4//! the C/Pancake implementations: payload struct types (referenced by typed
5//! channel buffers, #122) and layout constants. Items are flat, C-like,
6//! top-level declarations in the `.fis` file:
7//!
8//! ```text
9//! struct Packet {
10//! id: uint32;
11//! length: uint16;
12//! payload: uint8;
13//! }
14//!
15//! const RBD_COUNT: uint = 64;
16//! const RING_BYTES: uint = RBD_COUNT * sizeof(Packet);
17//! ```
18//!
19//! Const right-hand sides parse as full [`Expr`]; `tast` folds them
20//! (`tast/fold.rs`) and admits only the constant subset:
21//! literals, references to other consts, `sizeof(T)`, and unary/binary
22//! arithmetic — calls and runtime variables are rejected in const position.
23//!
24//! Language reference: [Data Layer: Structs and Constants](https://fiducia-lang.github.io/docs/lang-spec/system/data-layer.html).
25
26use std::fmt::{self, Display, Formatter};
27use std::ops::Range;
28
29use serde::Serialize;
30
31use super::expr::Expr;
32use super::types::Type;
33
34/// One field of a struct declaration: `name: Type;`.
35#[derive(Debug, Clone, Serialize, PartialEq)]
36pub struct StructField {
37 /// Field name.
38 pub name: String,
39 /// Field type — a primitive or another declared struct (no recursion).
40 pub ty: Type,
41 /// Byte span of the field in the source.
42 pub span: Range<usize>,
43}
44
45/// A struct type declaration: `struct Name { field: Type; ... }`.
46///
47/// Struct names use `UpperCamelCase` by convention. Fields are flat — no
48/// collection types, no pointers (#107); a field may be another declared
49/// struct, but recursion (direct or indirect) is rejected so `sizeof` is
50/// always computable at compile time. `@attr(packed)` before `struct`
51/// drops the padding, as C's `__attribute__((packed))`.
52#[derive(Debug, Clone, Serialize, PartialEq)]
53pub struct StructDecl {
54 /// Type name.
55 pub name: String,
56 /// Packed: no padding, alignment 1.
57 pub packed: bool,
58 /// Fields, in declaration (and layout) order.
59 pub fields: Vec<StructField>,
60 /// Byte span of the declaration in the source.
61 pub span: Range<usize>,
62}
63
64/// A constant declaration: `const NAME: Type = expr;`.
65///
66/// `NAME` uses `SCREAMING_SNAKE_CASE` by convention. The value is folded
67/// at compile time; it may reference previously declared constants and
68/// `sizeof` of declared types.
69#[derive(Debug, Clone, Serialize)]
70pub struct ConstDecl {
71 /// Constant name.
72 pub name: String,
73 /// Declared type.
74 pub ty: Type,
75 /// Right-hand side expression (const-folding subset).
76 pub value: Expr,
77 /// Byte span of the declaration in the source.
78 pub span: Range<usize>,
79}
80
81/// A data-layer item — dispatch type for the `.fis` top-level parser.
82#[derive(Debug, Clone, Serialize)]
83pub enum DataDecl {
84 /// `struct Name { ... }`
85 Struct(StructDecl),
86 /// `const NAME: Type = expr;`
87 Const(ConstDecl),
88}
89
90impl Display for StructDecl {
91 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
92 let fields: Vec<String> = self
93 .fields
94 .iter()
95 .map(|field| format!("{}: {}", field.name, field.ty))
96 .collect();
97 write!(f, "{}", fields.join("; "))
98 }
99}
100
101impl Display for ConstDecl {
102 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
103 write!(f, "type: {}", self.ty)
104 }
105}