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 <noreply@anthropic.com>
This commit is contained in:
225
projects/lux-compiler/ast.lux
Normal file
225
projects/lux-compiler/ast.lux
Normal file
@@ -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<TypeExpr>, Span)
|
||||||
|
|
||||||
|
// === Trait Constraint (needed before WhereClause) ===
|
||||||
|
|
||||||
|
type TraitConstraint = | TraitConstraint(Ident, List<TraitBound>, Span)
|
||||||
|
|
||||||
|
// === Where Clauses ===
|
||||||
|
|
||||||
|
type WhereClause =
|
||||||
|
| WcProperty(Ident, BehavioralProperty, Span)
|
||||||
|
| WcResult(Expr, Span)
|
||||||
|
| WcTrait(TraitConstraint)
|
||||||
|
|
||||||
|
// === Module Path ===
|
||||||
|
|
||||||
|
type ModulePath = | ModulePath(List<Ident>, Span)
|
||||||
|
|
||||||
|
// === Import ===
|
||||||
|
|
||||||
|
// path, alias, items, wildcard, span
|
||||||
|
type ImportDecl = | ImportDecl(ModulePath, Option<Ident>, Option<List<Ident>>, Bool, Span)
|
||||||
|
|
||||||
|
// === Program ===
|
||||||
|
|
||||||
|
type Program = | Program(List<ImportDecl>, List<Declaration>)
|
||||||
|
|
||||||
|
// === 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<Parameter>, TypeExpr, Span)
|
||||||
|
|
||||||
|
// === Record Field ===
|
||||||
|
|
||||||
|
type RecordField = | RecordField(Ident, TypeExpr, Span)
|
||||||
|
|
||||||
|
// === Variant Fields ===
|
||||||
|
|
||||||
|
type VariantFields =
|
||||||
|
| VfUnit
|
||||||
|
| VfTuple(List<TypeExpr>)
|
||||||
|
| VfRecord(List<RecordField>)
|
||||||
|
|
||||||
|
// === 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<Ident>, Option<Ident>, Expr, Span)
|
||||||
|
|
||||||
|
// === Impl Method ===
|
||||||
|
|
||||||
|
// name, params, return_type, body, span
|
||||||
|
type ImplMethod = | ImplMethod(Ident, List<Parameter>, Option<TypeExpr>, Expr, Span)
|
||||||
|
|
||||||
|
// === Trait Method ===
|
||||||
|
|
||||||
|
// name, type_params, params, return_type, default_impl, span
|
||||||
|
type TraitMethod = | TraitMethod(Ident, List<Ident>, List<Parameter>, TypeExpr, Option<Expr>, Span)
|
||||||
|
|
||||||
|
// === Type Expressions ===
|
||||||
|
|
||||||
|
type TypeExpr =
|
||||||
|
| TeNamed(Ident)
|
||||||
|
| TeApp(TypeExpr, List<TypeExpr>)
|
||||||
|
| TeFunction(List<TypeExpr>, TypeExpr, List<Ident>)
|
||||||
|
| TeTuple(List<TypeExpr>)
|
||||||
|
| TeRecord(List<RecordField>)
|
||||||
|
| 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<TypeExpr>, Expr, Span)
|
||||||
|
|
||||||
|
// === Match Arms ===
|
||||||
|
|
||||||
|
type MatchArm = | MatchArm(Pattern, Option<Expr>, Expr, Span)
|
||||||
|
|
||||||
|
// === Patterns ===
|
||||||
|
|
||||||
|
type Pattern =
|
||||||
|
| PatWildcard(Span)
|
||||||
|
| PatVar(Ident)
|
||||||
|
| PatLiteral(Literal)
|
||||||
|
| PatConstructor(Ident, List<Pattern>, Span)
|
||||||
|
| PatRecord(List<(Ident, Pattern)>, Span)
|
||||||
|
| PatTuple(List<Pattern>, Span)
|
||||||
|
|
||||||
|
// === Function Declaration ===
|
||||||
|
// visibility, doc, name, type_params, params, return_type, effects, properties, where_clauses, body, span
|
||||||
|
type FunctionDecl = | FunctionDecl(Visibility, Option<String>, Ident, List<Ident>, List<Parameter>, TypeExpr, List<Ident>, List<BehavioralProperty>, List<WhereClause>, Expr, Span)
|
||||||
|
|
||||||
|
// === Effect Declaration ===
|
||||||
|
// doc, name, type_params, operations, span
|
||||||
|
type EffectDecl = | EffectDecl(Option<String>, Ident, List<Ident>, List<EffectOp>, Span)
|
||||||
|
|
||||||
|
// === Type Declaration ===
|
||||||
|
// visibility, doc, name, type_params, version, definition, migrations, span
|
||||||
|
type TypeDecl = | TypeDecl(Visibility, Option<String>, Ident, List<Ident>, Option<Version>, TypeDef, List<Migration>, Span)
|
||||||
|
|
||||||
|
// === Handler Declaration ===
|
||||||
|
// name, params, effect, implementations, span
|
||||||
|
type HandlerDecl = | HandlerDecl(Ident, List<Parameter>, Ident, List<HandlerImpl>, Span)
|
||||||
|
|
||||||
|
// === Let Declaration ===
|
||||||
|
// visibility, doc, name, typ, value, span
|
||||||
|
type LetDecl = | LetDecl(Visibility, Option<String>, Ident, Option<TypeExpr>, Expr, Span)
|
||||||
|
|
||||||
|
// === Trait Declaration ===
|
||||||
|
// visibility, doc, name, type_params, super_traits, methods, span
|
||||||
|
type TraitDecl = | TraitDecl(Visibility, Option<String>, Ident, List<Ident>, List<TraitBound>, List<TraitMethod>, Span)
|
||||||
|
|
||||||
|
// === Impl Declaration ===
|
||||||
|
// type_params, constraints, trait_name, trait_args, target_type, methods, span
|
||||||
|
type ImplDecl = | ImplDecl(List<Ident>, List<TraitConstraint>, Ident, List<TypeExpr>, TypeExpr, List<ImplMethod>, Span)
|
||||||
|
|
||||||
|
// === Expressions ===
|
||||||
|
|
||||||
|
type Expr =
|
||||||
|
| ExLiteral(Literal)
|
||||||
|
| ExVar(Ident)
|
||||||
|
| ExBinaryOp(BinaryOp, Expr, Expr, Span)
|
||||||
|
| ExUnaryOp(UnaryOp, Expr, Span)
|
||||||
|
| ExCall(Expr, List<Expr>, Span)
|
||||||
|
| ExEffectOp(Ident, Ident, List<Expr>, Span)
|
||||||
|
| ExField(Expr, Ident, Span)
|
||||||
|
| ExTupleIndex(Expr, Int, Span)
|
||||||
|
| ExLambda(List<Parameter>, Option<TypeExpr>, List<Ident>, Expr, Span)
|
||||||
|
| ExLet(Ident, Option<TypeExpr>, Expr, Expr, Span)
|
||||||
|
| ExIf(Expr, Expr, Expr, Span)
|
||||||
|
| ExMatch(Expr, List<MatchArm>, Span)
|
||||||
|
| ExBlock(List<Statement>, Expr, Span)
|
||||||
|
| ExRecord(Option<Expr>, List<(Ident, Expr)>, Span)
|
||||||
|
| ExTuple(List<Expr>, Span)
|
||||||
|
| ExList(List<Expr>, Span)
|
||||||
|
| ExRun(Expr, List<(Ident, Expr)>, Span)
|
||||||
|
| ExResume(Expr, Span)
|
||||||
Reference in New Issue
Block a user