1use serde::{Deserialize, Serialize};
7use std::fmt;
8
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11pub struct GlyphModule {
12 pub program: ProgramDecorator,
14 pub imports: Vec<Import>,
16 pub statements: Vec<Statement>,
18}
19
20#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22pub struct ProgramDecorator {
23 pub name: String,
25 pub version: String,
27 pub requires: Vec<String>,
29}
30
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33pub enum Import {
34 Module { name: String },
36 FromImport { module: String, items: Vec<String> },
38}
39
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub enum Statement {
43 FunctionDef(Function),
45 Expression(Expression),
47 If(IfStatement),
49 Match(MatchStatement),
51 Return(Option<Expression>),
53 Let {
55 name: String,
56 value: Expression,
57 type_hint: Option<Type>,
58 },
59 Assignment { target: String, value: Expression },
61 While {
63 condition: Expression,
64 body: Vec<Statement>,
65 },
66 For {
68 variable: String,
69 iterable: Expression,
70 body: Vec<Statement>,
71 },
72 Break,
74 Continue,
76}
77
78#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
80pub struct Function {
81 pub name: String,
83 pub params: Vec<Parameter>,
85 pub return_type: Option<Type>,
87 pub body: Vec<Statement>,
89 pub is_async: bool,
91}
92
93#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
95pub struct Parameter {
96 pub name: String,
98 pub type_hint: Option<Type>,
100 pub default: Option<Expression>,
102}
103
104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
106pub struct IfStatement {
107 pub condition: Expression,
109 pub then_body: Vec<Statement>,
111 pub elif_clauses: Vec<ElifClause>,
113 pub else_body: Option<Vec<Statement>>,
115}
116
117#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
119pub struct ElifClause {
120 pub condition: Expression,
122 pub body: Vec<Statement>,
124}
125
126#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
128pub struct MatchStatement {
129 pub subject: Expression,
131 pub cases: Vec<CaseClause>,
133}
134
135#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
137pub struct CaseClause {
138 pub pattern: Pattern,
140 pub body: Vec<Statement>,
142}
143
144#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
146pub enum Pattern {
147 Literal(Literal),
149 Variable(String),
151 Constructor { name: String, args: Vec<Pattern> },
153 Wildcard,
155}
156
157#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
159pub enum Expression {
160 Literal(Literal),
162 Identifier(String),
164 BinaryOp {
166 left: Box<Expression>,
167 op: BinaryOperator,
168 right: Box<Expression>,
169 },
170 UnaryOp {
172 op: UnaryOperator,
173 operand: Box<Expression>,
174 },
175 Call {
177 func: Box<Expression>,
178 args: Vec<Expression>,
179 kwargs: Vec<(String, Expression)>,
180 },
181 Await(Box<Expression>),
183 FString(Vec<FStringPart>),
185 List(Vec<Expression>),
187 Dict(Vec<(Expression, Expression)>),
189 Attribute {
191 value: Box<Expression>,
192 attr: String,
193 },
194 IfExpr {
196 test: Box<Expression>,
197 if_true: Box<Expression>,
198 if_false: Box<Expression>,
199 },
200}
201
202#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
204pub enum FStringPart {
205 Text(String),
207 Expression(Expression),
209}
210
211#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
213pub enum Literal {
214 Int(i64),
216 Float(f64),
218 String(String),
220 Bool(bool),
222 Unit,
224}
225
226#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
228pub enum BinaryOperator {
229 Add,
231 Subtract,
232 Multiply,
233 Divide,
234 Modulo,
235 Power,
236 Equal,
238 NotEqual,
239 Less,
240 Greater,
241 LessEqual,
242 GreaterEqual,
243 And,
245 Or,
246}
247
248#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
250pub enum UnaryOperator {
251 Negate,
253 Not,
255}
256
257#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
259pub enum Type {
260 Named(String),
262 List(Box<Type>),
264 Dict { key: Box<Type>, value: Box<Type> },
266 Optional(Box<Type>),
268 Promise(Box<Type>),
270 Result { ok: Box<Type>, err: Box<Type> },
272}
273
274pub type GlyphAst = GlyphModule;
276
277impl fmt::Display for Type {
278 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
279 match self {
280 Type::Named(name) => write!(f, "{name}"),
281 Type::List(inner) => write!(f, "list[{inner}]"),
282 Type::Dict { key, value } => write!(f, "dict[{key}, {value}]"),
283 Type::Optional(inner) => write!(f, "optional[{inner}]"),
284 Type::Promise(inner) => write!(f, "promise[{inner}]"),
285 Type::Result { ok, err } => write!(f, "result[{ok}, {err}]"),
286 }
287 }
288}
289
290impl fmt::Display for BinaryOperator {
291 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
292 use BinaryOperator::*;
293 match self {
294 Add => write!(f, "+"),
295 Subtract => write!(f, "-"),
296 Multiply => write!(f, "*"),
297 Divide => write!(f, "/"),
298 Modulo => write!(f, "%"),
299 Power => write!(f, "**"),
300 Equal => write!(f, "=="),
301 NotEqual => write!(f, "!="),
302 Less => write!(f, "<"),
303 Greater => write!(f, ">"),
304 LessEqual => write!(f, "<="),
305 GreaterEqual => write!(f, ">="),
306 And => write!(f, "and"),
307 Or => write!(f, "or"),
308 }
309 }
310}