From 3a2376cd49befe08b9feb3be3e73dbab28953802 Mon Sep 17 00:00:00 2001 From: Brandon Lucas Date: Thu, 19 Feb 2026 02:07:30 -0500 Subject: [PATCH] feat: port AST definitions to Lux (self-hosting) Translate all 30+ type definitions from src/ast.rs (727 lines Rust) into Lux ADTs in projects/lux-compiler/ast.lux. Types ported: Span, Ident, Visibility, Version, VersionConstraint, BehavioralProperty, WhereClause, ModulePath, ImportDecl, Program, Declaration, FunctionDecl, Parameter, EffectDecl, EffectOp, TypeDecl, TypeDef, RecordField, Variant, VariantFields, Migration, HandlerDecl, HandlerImpl, LetDecl, TraitDecl, TraitMethod, TraitBound, ImplDecl, TraitConstraint, ImplMethod, TypeExpr, Expr (19 variants), Literal, LiteralKind, BinaryOp, UnaryOp, Statement, MatchArm, Pattern. Passes `lux check` and `lux run`. Co-Authored-By: Claude Opus 4.6 --- projects/lux-compiler/ast.lux | 225 ++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 projects/lux-compiler/ast.lux diff --git a/projects/lux-compiler/ast.lux b/projects/lux-compiler/ast.lux new file mode 100644 index 0000000..c564417 --- /dev/null +++ b/projects/lux-compiler/ast.lux @@ -0,0 +1,225 @@ +// Lux AST — Self-hosted Abstract Syntax Tree definitions +// +// Direct translation of src/ast.rs into Lux ADTs. +// These types represent the parsed structure of a Lux program. +// +// Naming conventions to avoid collisions: +// Ex = Expr variant, Pat = Pattern, Te = TypeExpr +// Td = TypeDef, Vf = VariantFields, Op = Operator +// Decl = Declaration, St = Statement + +// === Source Location === + +type Span = | Span(Int, Int) + +// === Identifiers === + +type Ident = | Ident(String, Span) + +// === Visibility === + +type Visibility = | Public | Private + +// === Schema Evolution === + +type Version = | Version(Int, Span) + +type VersionConstraint = + | VcExact(Version) + | VcAtLeast(Version) + | VcLatest(Span) + +// === Behavioral Types === + +type BehavioralProperty = + | BpPure + | BpTotal + | BpIdempotent + | BpDeterministic + | BpCommutative + +// === Trait Bound (needed before WhereClause) === + +type TraitBound = | TraitBound(Ident, List, Span) + +// === Trait Constraint (needed before WhereClause) === + +type TraitConstraint = | TraitConstraint(Ident, List, Span) + +// === Where Clauses === + +type WhereClause = + | WcProperty(Ident, BehavioralProperty, Span) + | WcResult(Expr, Span) + | WcTrait(TraitConstraint) + +// === Module Path === + +type ModulePath = | ModulePath(List, Span) + +// === Import === + +// path, alias, items, wildcard, span +type ImportDecl = | ImportDecl(ModulePath, Option, Option>, Bool, Span) + +// === Program === + +type Program = | Program(List, List) + +// === Declarations === + +type Declaration = + | DeclFunction(FunctionDecl) + | DeclEffect(EffectDecl) + | DeclType(TypeDecl) + | DeclHandler(HandlerDecl) + | DeclLet(LetDecl) + | DeclTrait(TraitDecl) + | DeclImpl(ImplDecl) + +// === Parameter === + +type Parameter = | Parameter(Ident, TypeExpr, Span) + +// === Effect Operation === + +type EffectOp = | EffectOp(Ident, List, TypeExpr, Span) + +// === Record Field === + +type RecordField = | RecordField(Ident, TypeExpr, Span) + +// === Variant Fields === + +type VariantFields = + | VfUnit + | VfTuple(List) + | VfRecord(List) + +// === Variant === + +type Variant = | Variant(Ident, VariantFields, Span) + +// === Migration === + +type Migration = | Migration(Version, Expr, Span) + +// === Handler Impl === + +// op_name, params, resume, body, span +type HandlerImpl = | HandlerImpl(Ident, List, Option, Expr, Span) + +// === Impl Method === + +// name, params, return_type, body, span +type ImplMethod = | ImplMethod(Ident, List, Option, Expr, Span) + +// === Trait Method === + +// name, type_params, params, return_type, default_impl, span +type TraitMethod = | TraitMethod(Ident, List, List, TypeExpr, Option, Span) + +// === Type Expressions === + +type TypeExpr = + | TeNamed(Ident) + | TeApp(TypeExpr, List) + | TeFunction(List, TypeExpr, List) + | TeTuple(List) + | TeRecord(List) + | TeUnit + | TeVersioned(TypeExpr, VersionConstraint) + +// === Literal === + +type LiteralKind = + | LitInt(Int) + | LitFloat(String) + | LitString(String) + | LitChar(Char) + | LitBool(Bool) + | LitUnit + +type Literal = | Literal(LiteralKind, Span) + +// === Binary Operators === + +type BinaryOp = + | OpAdd | OpSub | OpMul | OpDiv | OpMod + | OpEq | OpNe | OpLt | OpLe | OpGt | OpGe + | OpAnd | OpOr + | OpPipe | OpConcat + +// === Unary Operators === + +type UnaryOp = | OpNeg | OpNot + +// === Statements === + +type Statement = + | StExpr(Expr) + | StLet(Ident, Option, Expr, Span) + +// === Match Arms === + +type MatchArm = | MatchArm(Pattern, Option, Expr, Span) + +// === Patterns === + +type Pattern = + | PatWildcard(Span) + | PatVar(Ident) + | PatLiteral(Literal) + | PatConstructor(Ident, List, Span) + | PatRecord(List<(Ident, Pattern)>, Span) + | PatTuple(List, Span) + +// === Function Declaration === +// visibility, doc, name, type_params, params, return_type, effects, properties, where_clauses, body, span +type FunctionDecl = | FunctionDecl(Visibility, Option, Ident, List, List, TypeExpr, List, List, List, Expr, Span) + +// === Effect Declaration === +// doc, name, type_params, operations, span +type EffectDecl = | EffectDecl(Option, Ident, List, List, Span) + +// === Type Declaration === +// visibility, doc, name, type_params, version, definition, migrations, span +type TypeDecl = | TypeDecl(Visibility, Option, Ident, List, Option, TypeDef, List, Span) + +// === Handler Declaration === +// name, params, effect, implementations, span +type HandlerDecl = | HandlerDecl(Ident, List, Ident, List, Span) + +// === Let Declaration === +// visibility, doc, name, typ, value, span +type LetDecl = | LetDecl(Visibility, Option, Ident, Option, Expr, Span) + +// === Trait Declaration === +// visibility, doc, name, type_params, super_traits, methods, span +type TraitDecl = | TraitDecl(Visibility, Option, Ident, List, List, List, Span) + +// === Impl Declaration === +// type_params, constraints, trait_name, trait_args, target_type, methods, span +type ImplDecl = | ImplDecl(List, List, Ident, List, TypeExpr, List, Span) + +// === Expressions === + +type Expr = + | ExLiteral(Literal) + | ExVar(Ident) + | ExBinaryOp(BinaryOp, Expr, Expr, Span) + | ExUnaryOp(UnaryOp, Expr, Span) + | ExCall(Expr, List, Span) + | ExEffectOp(Ident, Ident, List, Span) + | ExField(Expr, Ident, Span) + | ExTupleIndex(Expr, Int, Span) + | ExLambda(List, Option, List, Expr, Span) + | ExLet(Ident, Option, Expr, Expr, Span) + | ExIf(Expr, Expr, Expr, Span) + | ExMatch(Expr, List, Span) + | ExBlock(List, Expr, Span) + | ExRecord(Option, List<(Ident, Expr)>, Span) + | ExTuple(List, Span) + | ExList(List, Span) + | ExRun(Expr, List<(Ident, Expr)>, Span) + | ExResume(Expr, Span)