aeri 0.2.7

Aeri is a Cardano smart contract language by Knott Dynamics, created by Trevor Knott, with its official compiler and CLI.
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
//! Parsed abstract syntax tree nodes for Aeri modules.
//!
//! This module keeps language structure in a lightweight, owned form for later
//! checking, lowering, and inspection passes.

use crate::diagnostic::Span;

#[derive(Debug, Clone, PartialEq, Eq)]
/// Top-level module in a single source file.
pub struct Module {
    /// Module name as declared in the source header.
    pub name: String,
    /// Collected top-level declarations.
    pub items: Vec<Item>,
}

impl Module {
    /// Constructs a module from a name and declaration list.
    pub fn new(name: impl Into<String>, items: Vec<Item>) -> Self {
        Self {
            name: name.into(),
            items,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// A top-level declaration stored in a parsed module.
pub enum Item {
    /// User-defined algebraic-like type.
    Type(TypeDecl),
    /// Constant definition with value and inferred type.
    Const(Const),
    /// Helper or exported function declaration.
    Function(Function),
    /// Validator entrypoint.
    Validator(Validator),
    /// Pure test block.
    Test(Test),
}

impl Item {
    /// Human-friendly declaration name.
    pub fn name(&self) -> &str {
        match self {
            Item::Type(type_decl) => &type_decl.name,
            Item::Const(constant) => &constant.name,
            Item::Function(function) => &function.name,
            Item::Validator(validator) => &validator.name,
            Item::Test(test) => &test.name,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// User-defined type declaration with optional constructor variants.
pub struct TypeDecl {
    /// Type name in source.
    pub name: String,
    /// Type variants for tuple-like or custom constructors.
    pub variants: Vec<Variant>,
    /// Span covering the entire declaration.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// A constructor variant for a custom type.
pub struct Variant {
    /// Variant name in source.
    pub name: String,
    /// Positional fields in the variant constructor.
    pub fields: Vec<VariantField>,
    /// Span covering the variant declaration.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// A single field in a type variant.
pub struct VariantField {
    /// Optional field name (if omitted, constructor positional style).
    pub name: Option<String>,
    /// Field type.
    pub ty: TypeRef,
    /// Span covering this field.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Constant declaration.
pub struct Const {
    /// Constant name.
    pub name: String,
    /// Declared type annotation.
    pub ty: TypeRef,
    /// Initialized expression.
    pub value: Expr,
    /// Span covering this declaration.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Function declaration used by validators and helpers.
pub struct Function {
    /// Function name.
    pub name: String,
    /// Ordered function parameters.
    pub params: Vec<Param>,
    /// Declared return type.
    pub return_type: TypeRef,
    /// Parsed function body.
    pub body: Block,
    /// Span covering this declaration.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Validator declaration with fixed `(redeemer: ByteArray, ctx: Tx)` context.
pub struct Validator {
    /// Validator name.
    pub name: String,
    /// Validator parameters.
    pub params: Vec<Param>,
    /// Parsed validator body.
    pub body: Block,
    /// Span covering this declaration.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Pure test block declaration.
pub struct Test {
    /// Test name.
    pub name: String,
    /// If true, test should fail and treated as pass when it does.
    pub should_fail: bool,
    /// Parsed test body.
    pub body: Block,
    /// Span covering this declaration.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// A function/validator parameter.
pub struct Param {
    /// Parameter identifier.
    pub name: String,
    /// Parameter type annotation.
    pub ty: TypeRef,
    /// Span covering this parameter.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// A type reference in parsed source.
pub struct TypeRef {
    /// Base type name.
    pub name: String,
    /// Generic argument list for types like `List<A>`.
    pub args: Vec<TypeRef>,
    /// Span covering this type reference.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// An ordered statement list plus block span.
pub struct Block {
    /// Block statements in source order.
    pub statements: Vec<Statement>,
    /// Span covering the block.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// A statement in an Aeri block.
pub enum Statement {
    Let {
        /// Bound identifier name.
        name: String,
        /// Optional annotation.
        ty: Option<TypeRef>,
        /// Initialization expression.
        value: Expr,
        /// Span covering the statement.
        span: Span,
    },
    Require {
        /// Predicate expression to assert.
        condition: Expr,
        /// Span covering the statement.
        span: Span,
    },
    Trace {
        /// String-like message expression.
        message: Expr,
        /// Span covering the statement.
        span: Span,
    },
    Return {
        /// Value returned from function-like context.
        value: Expr,
        /// Span covering the statement.
        span: Span,
    },
    Expr {
        /// Standalone expression statement.
        value: Expr,
        /// `true` if terminated by `;`.
        trailing_semicolon: bool,
        /// Span covering the statement.
        span: Span,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Parsed expression node.
pub struct Expr {
    /// Expression kind and payload.
    pub kind: ExprKind,
    /// Span covering this expression.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Kinds of expressions available in Aeri.
pub enum ExprKind {
    /// Boolean literal.
    Bool(bool),
    /// Integer literal.
    Int(i64),
    /// UTF-8 string literal.
    String(String),
    /// Hex bytes literal.
    ByteArray(String),
    /// Unit literal `()`.
    Unit,
    /// List literal `[a, b, c]`.
    List(Vec<Expr>),
    /// Built-in `fail` expression.
    Fail,
    /// Variable or parameter reference.
    Variable(String),
    Unary {
        /// Unary operator.
        op: UnaryOp,
        /// Operand expression.
        expr: Box<Expr>,
    },
    Binary {
        /// Left-hand expression.
        left: Box<Expr>,
        /// Operator.
        op: BinaryOp,
        /// Right-hand expression.
        right: Box<Expr>,
    },
    Call {
        /// Function name.
        callee: String,
        /// Argument list.
        args: Vec<Expr>,
    },
    If {
        /// Branch condition expression.
        condition: Box<Expr>,
        /// `then` branch block.
        then_branch: Block,
        /// `else` branch block.
        else_branch: Block,
    },
    Match {
        /// Match subject expression.
        subject: Box<Expr>,
        /// Match arms.
        arms: Vec<MatchArm>,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Pattern matching arm for match expressions.
pub struct MatchArm {
    /// Match pattern.
    pub pattern: Pattern,
    /// Body executed when pattern matches.
    pub body: Block,
    /// Span covering the arm.
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Match pattern variants.
pub enum Pattern {
    Wildcard {
        /// Wildcard `_` pattern span.
        span: Span,
    },
    Variable {
        /// Bound variable name.
        name: String,
        /// Span covering the variable pattern.
        span: Span,
    },
    Constructor {
        /// Constructor name.
        name: String,
        /// Nested field patterns for constructor payload.
        bindings: Vec<PatternBinding>,
        /// Span covering constructor pattern.
        span: Span,
    },
    Bool {
        /// Matched boolean value.
        value: bool,
        /// Span covering the literal pattern.
        span: Span,
    },
    Int {
        /// Matched integer value.
        value: i64,
        /// Span covering the literal pattern.
        span: Span,
    },
    ByteArray {
        /// Matched byte array payload.
        hex: String,
        /// Span covering literal pattern.
        span: Span,
    },
    String {
        /// Matched string payload.
        value: String,
        /// Span covering literal pattern.
        span: Span,
    },
    Unit {
        /// Unit `()` pattern span.
        span: Span,
    },
}

impl Pattern {
    /// Returns the source span for this pattern.
    pub fn span(&self) -> Span {
        match self {
            Pattern::Wildcard { span }
            | Pattern::Variable { span, .. }
            | Pattern::Constructor { span, .. }
            | Pattern::Bool { span, .. }
            | Pattern::Int { span, .. }
            | Pattern::ByteArray { span, .. }
            | Pattern::String { span, .. }
            | Pattern::Unit { span } => *span,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Binding entry in constructor patterns.
pub struct PatternBinding {
    /// Optional alias or field label.
    pub name: Option<String>,
    /// Span covering the binding token.
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Unary operators.
pub enum UnaryOp {
    /// Logical negation `not`.
    Not,
    /// Arithmetic negation `-`.
    Negate,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Binary operators.
pub enum BinaryOp {
    /// Logical disjunction.
    Or,
    /// Logical conjunction.
    And,
    /// Equality comparison.
    Equal,
    /// Inequality comparison.
    NotEqual,
    /// `<` comparison.
    Less,
    /// `<=` comparison.
    LessEqual,
    /// `>` comparison.
    Greater,
    /// `>=` comparison.
    GreaterEqual,
    /// `+` arithmetic.
    Add,
    /// `-` arithmetic.
    Subtract,
    /// `*` arithmetic.
    Multiply,
    /// `/` arithmetic.
    Divide,
    /// `%` arithmetic.
    Remainder,
}