1use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
9pub struct Program {
10 pub items: Vec<Item>,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
14pub enum Item {
15 Import(Import),
16 TypeDecl(TypeDecl),
17 FnDecl(FnDecl),
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
21pub struct Import {
22 pub reference: String,
23 pub alias: String,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
27pub struct TypeDecl {
28 pub name: String,
29 pub params: Vec<String>,
30 pub definition: TypeExpr,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
34pub struct FnDecl {
35 pub name: String,
36 pub type_params: Vec<String>,
37 pub params: Vec<Param>,
38 pub effects: Vec<Effect>,
39 pub return_type: TypeExpr,
40 pub body: Block,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
44pub struct Param {
45 pub name: String,
46 pub ty: TypeExpr,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
50pub struct Effect {
51 pub name: String,
52 pub arg: Option<EffectArg>,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
56pub enum EffectArg {
57 Str(String),
58 Int(i64),
59 Ident(String),
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
63pub enum TypeExpr {
64 Named { name: String, args: Vec<TypeExpr> },
67 Record(Vec<TypeField>),
68 Tuple(Vec<TypeExpr>),
69 Function {
70 params: Vec<TypeExpr>,
71 effects: Vec<Effect>,
72 ret: Box<TypeExpr>,
73 },
74 Union(Vec<UnionVariant>),
75 Refined {
84 base: Box<TypeExpr>,
85 binding: String,
86 predicate: Box<Expr>,
87 },
88}
89
90#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
91pub struct TypeField {
92 pub name: String,
93 pub ty: TypeExpr,
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
97pub struct UnionVariant {
98 pub name: String,
99 pub payload: Option<TypeExpr>,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
104pub struct Block {
105 pub statements: Vec<Statement>,
106 pub result: Box<Expr>,
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
110pub enum Statement {
111 Let { name: String, ty: Option<TypeExpr>, value: Expr },
112 Expr(Expr),
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
116pub enum Expr {
117 Lit(Literal),
118 Var(String),
119 Block(Block),
120 Call { callee: Box<Expr>, args: Vec<Expr> },
121 Pipe { left: Box<Expr>, right: Box<Expr> },
122 Try(Box<Expr>),
124 Field { value: Box<Expr>, field: String },
126 BinOp { op: BinOp, lhs: Box<Expr>, rhs: Box<Expr> },
127 UnaryOp { op: UnaryOp, expr: Box<Expr> },
128 If { cond: Box<Expr>, then_block: Block, else_block: Block },
129 Match { scrutinee: Box<Expr>, arms: Vec<Arm> },
130 RecordLit(Vec<RecordLitField>),
131 TupleLit(Vec<Expr>),
132 ListLit(Vec<Expr>),
133 Constructor { name: String, args: Vec<Expr> },
139 Lambda(Box<Lambda>),
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
143pub struct Lambda {
144 pub params: Vec<Param>,
145 pub return_type: TypeExpr,
146 pub effects: Vec<Effect>,
147 pub body: Block,
148}
149
150#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
151pub struct RecordLitField {
152 pub name: String,
153 pub value: Expr,
154}
155
156#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
157pub enum BinOp {
158 Add, Sub, Mul, Div, Mod,
159 Eq, Neq, Lt, Lte, Gt, Gte,
160 And, Or,
161}
162
163impl BinOp {
164 pub fn precedence(self) -> u8 {
165 use BinOp::*;
166 match self {
167 Or => 1,
168 And => 2,
169 Eq | Neq | Lt | Lte | Gt | Gte => 3,
170 Add | Sub => 4,
171 Mul | Div | Mod => 5,
172 }
173 }
174
175 pub fn as_str(self) -> &'static str {
176 use BinOp::*;
177 match self {
178 Add => "+", Sub => "-", Mul => "*", Div => "/", Mod => "%",
179 Eq => "==", Neq => "!=", Lt => "<", Lte => "<=", Gt => ">", Gte => ">=",
180 And => "and", Or => "or",
181 }
182 }
183}
184
185#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
186pub enum UnaryOp { Neg, Not }
187
188#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
189pub struct Arm {
190 pub pattern: Pattern,
191 pub body: Expr,
192}
193
194#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
195pub enum Pattern {
196 Lit(Literal),
197 Var(String),
199 Wild,
200 Constructor { name: String, args: Vec<Pattern> },
201 Record { fields: Vec<RecordPatField>, rest: bool },
202 Tuple(Vec<Pattern>),
203}
204
205#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
206pub struct RecordPatField {
207 pub name: String,
208 pub pattern: Option<Pattern>,
210}
211
212#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
213pub enum Literal {
214 Int(i64),
215 Float(f64),
216 Str(String),
217 Bytes(Vec<u8>),
218 Bool(bool),
219 Unit,
220}