use crate::value::PrimitiveType;
#[derive(Clone, Debug, Default)]
pub struct Program {
pub types: Vec<TypeDecl>,
pub mixins: Vec<MixinDecl>,
pub molecules: Vec<MoleculeDecl>,
pub reactions: Vec<ReactionDecl>,
}
#[derive(Clone, Debug)]
pub struct MixinDecl {
pub name: String,
pub fields: Vec<FieldDecl>,
pub primary_key: Option<Vec<String>>,
pub merge: Option<MergeFn>,
}
#[derive(Clone, Debug)]
pub struct TypeDecl {
pub name: String,
pub body: TypeDeclBody,
}
#[derive(Clone, Debug)]
pub enum TypeDeclBody {
Enum(Vec<String>),
}
#[derive(Clone, Debug)]
pub struct MoleculeDecl {
pub name: String,
pub mixins: Vec<String>,
pub fields: Vec<FieldDecl>,
pub primary_key: Option<Vec<String>>,
pub merge: Option<MergeFn>,
}
#[derive(Clone, Debug)]
pub struct FieldDecl {
pub name: String,
pub ty: TypeExpr,
pub default: Option<Expr>,
}
#[derive(Clone, Debug)]
pub enum TypeExpr {
Primitive(PrimitiveType),
Named(String),
List(Box<TypeExpr>),
Optional(Box<TypeExpr>),
}
#[derive(Clone, Debug)]
pub struct MergeFn {
pub old_binding: String,
pub new_binding: String,
pub body: Expr,
}
#[derive(Clone, Debug)]
pub struct ReactionDecl {
pub name: String,
pub when: Vec<MoleculePattern>,
pub rollup: Option<RollupClause>,
pub where_clause: Option<Expr>,
pub emit: Vec<EmitClause>,
}
#[derive(Clone, Debug)]
pub struct RollupClause {
pub molecule_name: String,
pub binding: String,
pub predicate: Expr,
}
#[derive(Clone, Debug)]
pub struct MoleculePattern {
pub molecule_name: String,
pub binding: String,
}
#[derive(Clone, Debug)]
pub struct EmitClause {
pub molecule_name: String,
pub fields: Vec<FieldAssign>,
}
#[derive(Clone, Debug)]
pub struct FieldAssign {
pub name: String,
pub value: Expr,
}
#[derive(Clone, Debug)]
pub enum Expr {
LitNull,
LitBool(bool),
LitInt(i64),
LitFloat(f64),
LitString(String),
LitDuration(i64),
Ident(String),
FieldAccess(Box<Expr>, String),
Call(String, Vec<Expr>),
BinaryOp(BinaryOp, Box<Expr>, Box<Expr>),
UnaryOp(UnaryOp, Box<Expr>),
StructLit(String, Vec<FieldAssign>),
ListLit(Vec<Expr>),
ListComp {
var: String,
src: Box<Expr>,
body: Box<Expr>,
where_clause: Option<Box<Expr>>,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinaryOp {
Add, Sub, Mul, Div,
Eq, Ne, Lt, Le, Gt, Ge,
And, Or,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UnaryOp {
Not,
Neg,
}