silo:std.ast

Source src/silo:std.ast

1##! AST types used by the macro system.
2##!
3##! Declares the raw-token vocabulary, the structured
4##! pattern/expression/type-expression trees, and the record/union
5##! shapes consumed by derive-style macros.
6
7:use
8  :open core AnyInt AnyFloat Bool Byte Str Char Symbol Vec Decimal
9:end
10
11# si[impl macro.types]
12# si[impl macro.types.expr]
13# si[impl macro.tokens]
14# Raw lexer tokens — what `:quote` and `'(…)` produce. Freestanding
15# macros consume `(Vec Token)` values and use the parser intrinsics
16# (`parse-expr`, `parse-pattern`, `parse-type`, `parse-block`) to turn
17# them into structured AST values when needed.
18##( Raw token produced by the lexer. Variants cover literals (Int/Dec/Str/
19   Char/Symbol/Bool/ByteString), identifiers (upper/lower/colon), field
20   access/update markers, annotations, keywords (name without the leading
21   `:`), operators, and delimiters. Macros typically receive `(Vec Token)`
22   and hand tokens to the `parse-*` intrinsics for structured consumption.
23)#
24@lang(token)
25:union(pub) Token
26  # Literals
27  | IntLitTok AnyInt
28  | DecLitTok Str
29  | StrLitTok Str
30  | CharLitTok Char
31  | SymbolLitTok Symbol
32  | BoolLitTok Bool
33  | ByteStringLitTok (Vec Byte)
34
35  # Identifiers
36  | UpperIdentTok Str
37  | LowerIdentTok Str
38  | ColonIdentTok Str
39
40  # Fields & bindings
41  | FieldTok Str
42  | FieldUpdateTok Str
43
44  # Annotations
45  | AnnotationTok Str
46
47  # Keyword tokens — name without the leading `:`
48  | KeywordTok Str
49
50  # Operators & punctuation
51  | ArrowTok
52  | PopArrowTok
53  | PeekArrowTok
54  | FatArrowTok
55  | PipeTok
56  | UnderscoreTok
57  | ColonTok
58  | SemiTok
59  | DotDotTok
60  | QuoteOpenTok
61  | PlusTok
62  | PlusOpenTok
63  | LeTok
64  | GeTok
65  | NeTok
66
67  # Delimiters
68  | LParenTok
69  | RParenTok
70  | LBracketTok
71  | RBracketTok
72  | LBraceTok
73  | RBraceTok
74:end
75
76## Alias for a vector of tokens — the canonical type for token streams
77## consumed by macros.
78:alias(pub) TokenSeq (Vec Token)
79
80# si[impl macro.types.pattern]
81## Structured match pattern produced by `parse-pattern`. Variants cover
82## variant destructuring with named bindings, integer and range literals,
83## booleans, string literals, and the wildcard pattern.
84@lang(pattern)
85:union(pub) Pattern
86  | VariantPat Str (Vec Str)
87  | IntPat AnyInt
88  | RangePat AnyInt AnyInt
89  | BoolPat Bool
90  | StrPat Str
91  | WildcardPat
92:end
93
94# si[impl macro.types.typeexpr]
95# Structured type-expression AST. `parse-type` returns a value of this
96# type so macros can introspect types the same way they introspect
97# exprs and patterns.
98## Parsed type expression. Variants cover concrete type names, type
99## variables, applied type constructors, quotation types with input/output
100## rows, row variables, and integer literal types used in dependent
101## indexing.
102@lang(type-expr)
103:union(pub) TypeExprAst
104  | ConcreteType Str
105  | TypeVar Str
106  | AppliedType Str (Vec TypeExprAst)
107  | QuotationType (Vec TypeExprAst) (Vec TypeExprAst)
108  | RowVarType Str
109  | IntLitType AnyInt
110:end
111
112# si[impl macro.types.matcharm]
113## One arm of a `:match` expression: a `pattern` plus the `body` expressions
114## executed when the pattern matches.
115@lang(match-arm)
116:record(pub) MatchArm .pattern Pattern .body (Vec Expr) :end
117
118## Structured expression tree produced by `parse-expr`. Variants include
119## literals, word/qualifier calls, bindings and peeks, field access and
120## update, control flow (`:if`, `:for`, `:loop`, `:break`, `:ret`, `:try`),
121## `:match`, quotations, `:with` blocks, range forms, and literal values.
122@lang(expr)
123:union(pub) Expr
124  | IntLit AnyInt
125  | FloatLit AnyFloat
126  | BoolLit Bool
127  | StrLit Str
128  | WordCall Str
129  | Constructor Str
130  | Binding Str
131  | PeekBinding Str
132  | FieldAccess Str
133  | FieldUpdate Str
134  | IfExpr (Vec Expr) (Vec Expr)
135  | ForExpr Str (Vec Expr)
136  | LoopExpr (Vec Expr)
137  | BreakExpr
138  | RetExpr
139  | TryExpr
140  | MatchExpr (Vec MatchArm)
141  | QuotExpr (Vec Expr)
142  | QualCall Str Str
143  | WithExpr Str (Vec Expr)
144  | RangeBothExpr (Vec Expr) (Vec Expr)
145  | RangeFromExpr (Vec Expr)
146  | RangeToExpr (Vec Expr)
147  | RangeFullExpr
148  | CharLitExpr Char
149  | DecimalLit Decimal
150  | SymbolLit Symbol
151:end
152
153# si[impl macro.types.block]
154## Alias for a sequence of expressions forming a block body.
155@lang(block)
156:alias(pub) Block (Vec Expr)
157
158# si[impl macro.types.typedef]
159# Type definition types for derive macros
160## Describes a single field of a record type definition: its declared name
161## and its type name.
162:record(pub) FieldDef .name Str .type-name Str :end
163
164## Describes a single variant of a union type definition: its name and the
165## number of payload positions it carries.
166:record(pub) VariantDef .name Str .payload-count AnyInt :end
167
168## Describes a record type definition consumed by derive-style macros:
169## type name, type parameter names, and field list.
170:record(pub) RecordTypeDef
171  .name Str
172  .params (Vec Str)
173  .fields (Vec FieldDef)
174:end
175
176## Describes a union type definition consumed by derive-style macros:
177## type name, type parameter names, and variant list.
178:record(pub) UnionTypeDef
179  .name Str
180  .params (Vec Str)
181  .variants (Vec VariantDef)
182:end
183
184## A type definition suitable for a derive macro input — either a record
185## or a union. Enums are not included because they have no payload data
186## to reflect over.
187:union(pub) TypeDef
188  | RecordType RecordTypeDef
189  | UnionType UnionTypeDef
190:end
191
192# si[impl macro.types.decl]
193# Top-level AST types for derive macro output
194## One operation (method) inside a generated `:impl` block: method name,
195## stack-effect signature as a string, and the method body.
196:record(pub) ImplOpDef .name Str .sig Str .body Block :end
197
198## A generated `:impl` block: the trait name, the type arguments applied to
199## it, and the list of operation definitions it contains.
200:record(pub) ImplDef
201  .trait-name Str
202  .type-args (Vec Str)
203  .ops (Vec ImplOpDef)
204:end
205
206## Top-level AST node produced by a derive-style macro. Currently carries
207## a generated `:impl` block; more variants may be added as macro output
208## needs grow.
209:union(pub) TopLevelAst
210  | ImplItem ImplDef
211:end