|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +/// Types supported by the language |
| 4 | +#[derive(Debug, Clone, PartialEq)] |
| 5 | +pub enum Type { |
| 6 | + Int64, |
| 7 | + String, |
| 8 | + Bool, |
| 9 | + Float64, |
| 10 | + Array(Box<Type>), // Array types like [T] |
| 11 | + Map(Box<Type>, Box<Type>), // Map types like map[K->V] |
| 12 | + Tuple(Vec<Type>), // Tuple types like (T1, T2) |
| 13 | + Function(Box<Type>, Box<Type>), // Function types like (T1)->T2 |
| 14 | + Operator(OperatorKind), // Operator types (scalar/logical) |
| 15 | +} |
| 16 | + |
| 17 | +/// Kinds of operators supported in the language |
| 18 | +#[derive(Debug, Clone, PartialEq)] |
| 19 | +pub enum OperatorKind { |
| 20 | + Scalar, // Scalar operators |
| 21 | + Logical, // Logical operators with derivable properties |
| 22 | +} |
| 23 | + |
| 24 | +/// A field in an operator or properties block |
| 25 | +#[derive(Debug, Clone)] |
| 26 | +pub struct Field { |
| 27 | + pub name: String, |
| 28 | + pub ty: Type, |
| 29 | +} |
| 30 | + |
| 31 | +/// Logical properties block that must appear exactly once per file |
| 32 | +#[derive(Debug, Clone)] |
| 33 | +pub struct Properties { |
| 34 | + pub fields: Vec<Field>, |
| 35 | +} |
| 36 | + |
| 37 | +/// Top-level operator definition |
| 38 | +#[derive(Debug, Clone)] |
| 39 | +pub enum Operator { |
| 40 | + Scalar(ScalarOp), |
| 41 | + Logical(LogicalOp), |
| 42 | +} |
| 43 | + |
| 44 | +/// Scalar operator definition |
| 45 | +#[derive(Debug, Clone)] |
| 46 | +pub struct ScalarOp { |
| 47 | + pub name: String, |
| 48 | + pub fields: Vec<Field>, |
| 49 | +} |
| 50 | + |
| 51 | +/// Logical operator definition with derived properties |
| 52 | +#[derive(Debug, Clone)] |
| 53 | +pub struct LogicalOp { |
| 54 | + pub name: String, |
| 55 | + pub fields: Vec<Field>, |
| 56 | + pub derived_props: HashMap<String, Expr>, // Maps property names to their derivation expressions |
| 57 | +} |
| 58 | + |
| 59 | +/// Patterns used in match expressions |
| 60 | +#[derive(Debug, Clone)] |
| 61 | +pub enum Pattern { |
| 62 | + Bind(String, Box<Pattern>), // Binding patterns like x@p or x:p |
| 63 | + Constructor( |
| 64 | + String, // Constructor name |
| 65 | + Vec<Pattern>, // Subpatterns, can be named (x:p) or positional |
| 66 | + ), |
| 67 | + Literal(Literal), // Literal patterns like 42 or "hello" |
| 68 | + Wildcard, // Wildcard pattern _ |
| 69 | + Var(String), // Variable binding pattern |
| 70 | +} |
| 71 | + |
| 72 | +/// Literal values |
| 73 | +#[derive(Debug, Clone)] |
| 74 | +pub enum Literal { |
| 75 | + Int64(i64), |
| 76 | + String(String), |
| 77 | + Bool(bool), |
| 78 | + Float64(f64), |
| 79 | + Array(Vec<Expr>), // Array literals [e1, e2, ...] |
| 80 | + Tuple(Vec<Expr>), // Tuple literals (e1, e2, ...) |
| 81 | +} |
| 82 | + |
| 83 | +/// Expressions - the core of the language |
| 84 | +#[derive(Debug, Clone)] |
| 85 | +pub enum Expr { |
| 86 | + Match(Box<Expr>, Vec<MatchArm>), // Pattern matching |
| 87 | + If(Box<Expr>, Box<Expr>, Box<Expr>), // If-then-else |
| 88 | + Val(String, Box<Expr>, Box<Expr>), // Local binding (val x = e1; e2) |
| 89 | + Constructor(String, Vec<Expr>), // Constructor application (currently only operators) |
| 90 | + Binary(Box<Expr>, BinOp, Box<Expr>), // Binary operations |
| 91 | + Unary(UnaryOp, Box<Expr>), // Unary operations |
| 92 | + Call(Box<Expr>, Vec<Expr>), // Function application |
| 93 | + Member(Box<Expr>, String), // Field access (e.f) |
| 94 | + MemberCall(Box<Expr>, String, Vec<Expr>), // Method call (e.f(args)) |
| 95 | + ArrayIndex(Box<Expr>, Box<Expr>), // Array indexing (e[i]) |
| 96 | + Var(String), // Variable reference |
| 97 | + Literal(Literal), // Literal values |
| 98 | + Fail(String), // Failure with message |
| 99 | + Closure(Vec<String>, Box<Expr>), // Anonymous functions v = (x, y) => x + y; |
| 100 | +} |
| 101 | + |
| 102 | +/// A case in a match expression |
| 103 | +#[derive(Debug, Clone)] |
| 104 | +pub struct MatchArm { |
| 105 | + pub pattern: Pattern, |
| 106 | + pub expr: Expr, |
| 107 | +} |
| 108 | + |
| 109 | +/// Binary operators with fixed precedence |
| 110 | +#[derive(Debug, Clone)] |
| 111 | +pub enum BinOp { |
| 112 | + Add, // + |
| 113 | + Sub, // - |
| 114 | + Mul, // * |
| 115 | + Div, // / |
| 116 | + Concat, // ++ |
| 117 | + Eq, // == |
| 118 | + Neq, // != |
| 119 | + Gt, // > |
| 120 | + Lt, // < |
| 121 | + Ge, // >= |
| 122 | + Le, // <= |
| 123 | + And, // && |
| 124 | + Or, // || |
| 125 | + Range, // .. |
| 126 | +} |
| 127 | + |
| 128 | +/// Unary operators |
| 129 | +#[derive(Debug, Clone)] |
| 130 | +pub enum UnaryOp { |
| 131 | + Neg, // - |
| 132 | + Not, // ! |
| 133 | +} |
| 134 | + |
| 135 | +/// Function definition |
| 136 | +#[derive(Debug, Clone)] |
| 137 | +pub struct Function { |
| 138 | + pub name: String, |
| 139 | + pub params: Vec<(String, Type)>, // Parameter name and type pairs |
| 140 | + pub return_type: Type, |
| 141 | + pub body: Expr, |
| 142 | + pub rule_type: Option<OperatorKind>, // Some if this is a rule, indicating what kind |
| 143 | +} |
| 144 | + |
| 145 | +/// A complete source file |
| 146 | +#[derive(Debug, Clone)] |
| 147 | +pub struct File { |
| 148 | + pub properties: Properties, // The single logical properties block |
| 149 | + pub operators: Vec<Operator>, // All operator definitions |
| 150 | + pub functions: Vec<Function>, // All function definitions |
| 151 | +} |
0 commit comments