use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Program {
pub imports: Vec<Import>,
pub statements: Vec<Statement>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Import {
pub module: String,
pub items: Vec<String>, }
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CobbleType {
Integer, Boolean, String, List, Map, Unknown, }
impl CobbleType {
pub fn name(&self) -> &str {
match self {
CobbleType::Integer => "integer",
CobbleType::Boolean => "boolean",
CobbleType::String => "string",
CobbleType::List => "list",
CobbleType::Map => "map",
CobbleType::Unknown => "unknown",
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Statement {
Import(Import),
FunctionDef(FunctionDef),
Assignment(Assignment),
ConstAssignment(ConstAssignment), Expression(Expression),
If(IfStatement),
For(ForLoop),
While(WhileLoop),
Match(MatchStatement), Return(Option<Expression>),
Pass,
MinecraftCommand(String), Global(Vec<String>), Execute(ExecuteBlock), SelectorDef(SelectorDef), EntityDef(EntityDef), CreateEntity(String), }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EntityDef {
pub name: String,
pub selector: String,
pub nbt: Expression,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FunctionDef {
pub name: String,
pub params: Vec<Parameter>,
pub body: Vec<Statement>,
pub decorators: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Parameter {
pub name: String,
pub default: Option<Expression>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Assignment {
pub target: String,
pub value: Expression,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConstAssignment {
pub target: String,
pub value: Expression,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IfStatement {
pub condition: Expression,
pub then_branch: Vec<Statement>,
pub elif_branches: Vec<(Expression, Vec<Statement>)>,
pub else_branch: Option<Vec<Statement>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ForLoop {
pub target: String,
pub iter: Expression,
pub step: Option<Expression>, pub body: Vec<Statement>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WhileLoop {
pub condition: Expression,
pub body: Vec<Statement>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MatchStatement {
pub value: Expression,
pub cases: Vec<MatchCase>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MatchCase {
pub pattern: MatchPattern,
pub body: Vec<Statement>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum MatchPattern {
Literal(i32), Range(i32, i32), Wildcard, }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExecuteBlock {
pub modifiers: Vec<ExecuteModifier>, pub body: Vec<Statement>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ExecuteModifier {
As(String), At(String), If(Expression), IfRaw(String), Unless(Expression), UnlessRaw(String), Positioned(String), Rotated(String), In(String), Anchored(String), Align(String), Store(String), }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SelectorDef {
pub name: String, pub selector: String, }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Expression {
Number(f64),
String(String),
Boolean(bool),
None,
Array(Vec<Expression>),
Map(Vec<(String, Expression)>),
Identifier(String),
Attribute(Box<Expression>, String),
Binary(Box<Expression>, BinaryOp, Box<Expression>),
Unary(UnaryOp, Box<Expression>),
Call(Box<Expression>, Vec<Expression>),
Subscript(Box<Expression>, Box<Expression>),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BinaryOp {
Add,
Sub,
Mul,
Div,
Mod,
Pow,
Eq,
NotEq,
Lt,
LtEq,
Gt,
GtEq,
And,
Or,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum UnaryOp {
Not,
Neg,
Pos,
}