feat: engram-lang — new programming language, quantum-sealed prod target, spreading activation types
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
//! Abstract syntax tree node types.
|
||||
|
||||
use el_lexer::Span;
|
||||
|
||||
// ── Literals ──────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Literal {
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Str(String),
|
||||
Bool(bool),
|
||||
}
|
||||
|
||||
// ── Type expressions ──────────────────────────────────────────────────────────
|
||||
|
||||
/// A type annotation in source code, e.g. `String`, `[Int]`, `User?`.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum TypeExpr {
|
||||
/// A named type: `Int`, `String`, `User`, …
|
||||
Named(String),
|
||||
/// An array type: `[T]`
|
||||
Array(Box<TypeExpr>),
|
||||
/// An optional type: `T?`
|
||||
Optional(Box<TypeExpr>),
|
||||
/// A function type: `fn(A, B) -> C`
|
||||
Fn { params: Vec<TypeExpr>, return_type: Box<TypeExpr> },
|
||||
}
|
||||
|
||||
// ── Patterns (for match arms) ─────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Pattern {
|
||||
/// `Status::Active`
|
||||
EnumVariant { enum_name: String, variant: String, payload: Option<String> },
|
||||
/// A wildcard `_`
|
||||
Wildcard,
|
||||
/// A literal: `42`, `"str"`, `true`
|
||||
Literal(Literal),
|
||||
/// A binding: `x`
|
||||
Binding(String),
|
||||
}
|
||||
|
||||
// ── Binary operators ──────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum BinOp {
|
||||
Add, Sub, Mul, Div,
|
||||
Eq, NotEq, Lt, Gt, LtEq, GtEq,
|
||||
And, Or,
|
||||
}
|
||||
|
||||
// ── Expressions ───────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Expr {
|
||||
Literal(Literal),
|
||||
Ident(String),
|
||||
BinOp { op: BinOp, left: Box<Expr>, right: Box<Expr> },
|
||||
UnaryNot(Box<Expr>),
|
||||
Call { func: Box<Expr>, args: Vec<Expr> },
|
||||
Block(Vec<Stmt>),
|
||||
Match { subject: Box<Expr>, arms: Vec<MatchArm> },
|
||||
/// `activate TypeName where "semantic query string"`
|
||||
Activate { type_name: String, query: String },
|
||||
/// `sealed { stmts... }` — quantum-sealed block
|
||||
Sealed(Vec<Stmt>),
|
||||
If { cond: Box<Expr>, then: Box<Expr>, else_: Option<Box<Expr>> },
|
||||
Field { object: Box<Expr>, field: String },
|
||||
/// Array constructor: `[a, b, c]`
|
||||
Array(Vec<Expr>),
|
||||
/// Path expression: `Status::Active` (enum variant ref)
|
||||
Path { segments: Vec<String> },
|
||||
/// Index expression: `arr[0]`
|
||||
Index { object: Box<Expr>, index: Box<Expr> },
|
||||
}
|
||||
|
||||
// ── Match arm ─────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct MatchArm {
|
||||
pub pattern: Pattern,
|
||||
pub body: Expr,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
// ── Statements ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// A named parameter in a function definition.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Param {
|
||||
pub name: String,
|
||||
pub type_ann: TypeExpr,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// A field in a type definition.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Field {
|
||||
pub name: String,
|
||||
pub type_ann: TypeExpr,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// A variant in an enum definition.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Variant {
|
||||
pub name: String,
|
||||
/// Payload type, if any (e.g. `Pending(String)`)
|
||||
pub payload: Option<TypeExpr>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Stmt {
|
||||
/// `let name: Type = expr`
|
||||
Let {
|
||||
name: String,
|
||||
type_ann: Option<TypeExpr>,
|
||||
value: Expr,
|
||||
span: Span,
|
||||
},
|
||||
/// `return expr`
|
||||
Return(Expr, Span),
|
||||
/// A bare expression used as a statement (usually a call).
|
||||
Expr(Expr, Span),
|
||||
/// `fn name(params) -> ReturnType { body }`
|
||||
FnDef {
|
||||
name: String,
|
||||
params: Vec<Param>,
|
||||
return_type: TypeExpr,
|
||||
body: Vec<Stmt>,
|
||||
span: Span,
|
||||
},
|
||||
/// `type Name { fields... }`
|
||||
TypeDef {
|
||||
name: String,
|
||||
fields: Vec<Field>,
|
||||
span: Span,
|
||||
},
|
||||
/// `enum Name { variants... }`
|
||||
EnumDef {
|
||||
name: String,
|
||||
variants: Vec<Variant>,
|
||||
span: Span,
|
||||
},
|
||||
}
|
||||
|
||||
// ── Top-level program ─────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Program {
|
||||
pub stmts: Vec<Stmt>,
|
||||
/// The original source, kept for diagnostics and source maps.
|
||||
pub source: String,
|
||||
}
|
||||
Reference in New Issue
Block a user