rill_lang/ast.rs
1//! Abstract syntax tree produced by the parser.
2
3use crate::error::Span;
4
5/// Binary block-diagram combinators and arithmetic operators.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum BinOp {
8 /// `:` sequential composition.
9 Seq,
10 /// `,` parallel composition.
11 Par,
12 /// `<:` split / fan-out.
13 Split,
14 /// `:>` merge / fan-in.
15 Merge,
16 /// `~` feedback (implicit 1-sample delay).
17 Feedback,
18 /// `@` integer delay.
19 Delay,
20 /// `+`
21 Add,
22 /// `-`
23 Sub,
24 /// `*`
25 Mul,
26 /// `/`
27 Div,
28 /// `%`
29 Rem,
30}
31
32/// A rill-lang expression node.
33#[derive(Debug, Clone, PartialEq)]
34pub enum Expr {
35 /// Integer literal.
36 Int(i64, Span),
37 /// Float literal.
38 Float(f64, Span),
39 /// Identity wire `_` (arity 1→1).
40 Wire(Span),
41 /// Cut `!` (arity 1→0).
42 Cut(Span),
43 /// String literal, e.g. `"cutoff"`.
44 Str(String, Span),
45 /// A reference to a definition or a bound parameter.
46 Ref(String, Span),
47 /// Application `name(arg, ...)`.
48 Apply {
49 /// Function name.
50 name: String,
51 /// Argument expressions.
52 args: Vec<Expr>,
53 /// Full span of the application.
54 span: Span,
55 },
56 /// Unary negation `-expr`.
57 Neg(Box<Expr>, Span),
58 /// A binary combinator/operator.
59 Bin {
60 /// The operator.
61 op: BinOp,
62 /// Left operand.
63 lhs: Box<Expr>,
64 /// Right operand.
65 rhs: Box<Expr>,
66 /// Full span.
67 span: Span,
68 },
69}
70
71impl Expr {
72 /// The source span of this node.
73 pub fn span(&self) -> Span {
74 match self {
75 Expr::Int(_, s)
76 | Expr::Float(_, s)
77 | Expr::Wire(s)
78 | Expr::Cut(s)
79 | Expr::Str(_, s)
80 | Expr::Ref(_, s)
81 | Expr::Neg(_, s) => *s,
82 Expr::Apply { span, .. } | Expr::Bin { span, .. } => *span,
83 }
84 }
85}
86
87/// A top-level definition: `name(params) = body;` (params may be empty).
88#[derive(Debug, Clone, PartialEq)]
89pub struct Def {
90 /// Definition name.
91 pub name: String,
92 /// Formal parameter names (empty for a plain alias).
93 pub params: Vec<String>,
94 /// Right-hand side.
95 pub body: Expr,
96 /// Span of the whole definition.
97 pub span: Span,
98}
99
100/// A whole program: an ordered list of definitions. One MUST be named `process`.
101#[derive(Debug, Clone, PartialEq)]
102pub struct Program {
103 /// The definitions in source order.
104 pub defs: Vec<Def>,
105}