spudplate
Template scaffolding compiler for spudlang .spud files
Loading...
Searching...
No Matches
ast.h
1#ifndef SPUDPLATE_AST_H
2#define SPUDPLATE_AST_H
3
4#include <cstdint>
5#include <memory>
6#include <optional>
7#include <string>
8#include <variant>
9#include <vector>
10
11#include "spudplate/token.h"
12
13namespace spudplate {
14
17 std::string value;
18 int line;
19 int column;
20};
21
24 int value;
25 int line;
26 int column;
27};
28
31 bool value;
32 int line;
33 int column;
34};
35
38 std::string name;
39 int line;
40 int column;
41};
42
43// Forward declare Expr so recursive types can hold pointers to it
44struct Expr;
46using ExprPtr = std::unique_ptr<Expr>;
47
49struct UnaryExpr {
50 TokenType op;
51 ExprPtr operand;
52 int line;
53 int column;
54};
55
61struct BinaryExpr {
62 TokenType op;
63 ExprPtr left;
64 ExprPtr right;
65 int line;
66 int column;
67};
68
76 std::string name;
77 std::vector<ExprPtr> arguments;
78 int line;
79 int column;
80};
81
93 std::vector<std::variant<std::string, ExprPtr>> parts;
94 int line;
95 int column;
96};
97
99using ExprData = std::variant<StringLiteralExpr, IntegerLiteralExpr, BoolLiteralExpr,
102
104struct Expr {
105 ExprData data;
106};
107
109enum class VarType { String, Bool, Int };
110
111// ---------------------------------------------------------------------------
112// Path expression types
113// ---------------------------------------------------------------------------
114
117 std::string value;
118 int line;
119 int column;
120};
121
123struct PathVar {
124 std::string name;
125 int line;
126 int column;
127};
128
131 ExprPtr expression;
132 int line;
133 int column;
134};
135
137using PathSegment = std::variant<PathLiteral, PathVar, PathInterp>;
138
146struct PathExpr {
147 std::vector<PathSegment> segments;
148 int line;
149 int column;
150};
151
157
160 ExprPtr value;
161};
162
164using FileSource = std::variant<FileFromSource, FileContentSource>;
165
166// ---------------------------------------------------------------------------
167// Statement types
168// ---------------------------------------------------------------------------
169
178struct AskStmt {
179 std::string name;
180 std::string prompt;
181 VarType var_type;
182 std::optional<ExprPtr> default_value;
183 std::vector<ExprPtr> options;
184 std::optional<ExprPtr> when_clause;
185 int line;
186 int column;
187};
188
194struct LetStmt {
195 std::string name;
196 ExprPtr value;
197 int line;
198 int column;
199};
200
212 std::string name;
213 ExprPtr value;
214 int line;
215 int column;
216};
217
226struct MkdirStmt {
228 std::optional<std::string> alias;
229 bool mkdir_p;
230 std::optional<PathExpr> from_source;
231 bool verbatim;
232 std::optional<int> mode;
233 std::optional<ExprPtr> when_clause;
234 int line;
235 int column;
236};
237
243struct FileStmt {
245 std::optional<std::string> alias;
246 FileSource source;
247 bool append;
248 std::optional<int> mode;
249 std::optional<ExprPtr> when_clause;
250 int line;
251 int column;
252};
253
254// Forward declare Stmt so RepeatStmt can hold a vector of them
255struct Stmt;
257using StmtPtr = std::unique_ptr<Stmt>;
258
270 std::string collection_var;
271 std::string iterator_var;
272 std::vector<StmtPtr> body;
273 std::optional<ExprPtr> when_clause;
274 int line;
275 int column;
276};
277
287struct CopyStmt {
290 bool verbatim;
291 std::optional<ExprPtr> when_clause;
292 int line;
293 int column;
294};
295
309struct RunStmt {
310 ExprPtr command;
311 std::optional<PathExpr> cwd;
312 std::optional<ExprPtr> timeout;
313 std::optional<ExprPtr> when_clause;
314 int line;
315 int column;
316};
317
332 std::string name;
333 std::optional<std::uint32_t> version_pin;
334 std::optional<ExprPtr> when_clause;
335 int line;
336 int column;
337};
338
356struct IfStmt {
357 ExprPtr condition;
358 std::vector<StmtPtr> body;
359 int line;
360 int column;
361};
362
364using StmtData = std::variant<AskStmt, LetStmt, AssignStmt, MkdirStmt, FileStmt,
366 IfStmt>;
367
369struct Stmt {
370 StmtData data;
371};
372
374struct Program {
375 std::vector<StmtPtr> statements;
376};
377
378} // namespace spudplate
379
380#endif // SPUDPLATE_AST_H
An ask statement - declares a variable and prompts the user.
Definition ast.h:178
VarType var_type
Expected type of the answer.
Definition ast.h:181
int line
1-based source line where this node begins.
Definition ast.h:185
std::string name
Variable name to bind the answer to.
Definition ast.h:179
std::optional< ExprPtr > when_clause
Optional condition guarding the prompt.
Definition ast.h:184
int column
1-based source column where this node begins.
Definition ast.h:186
std::vector< ExprPtr > options
Allowed literal values; empty means any.
Definition ast.h:183
std::string prompt
The prompt string shown to the user.
Definition ast.h:180
std::optional< ExprPtr > default_value
Expression evaluated when the user skips the prompt.
Definition ast.h:182
A bare assignment statement - rebinds an existing variable.
Definition ast.h:211
std::string name
Variable name to rebind.
Definition ast.h:212
int column
1-based source column where this node begins.
Definition ast.h:215
int line
1-based source line where this node begins.
Definition ast.h:214
ExprPtr value
Expression whose value replaces the current binding.
Definition ast.h:213
A binary expression node, e.g.
Definition ast.h:61
ExprPtr right
Right-hand operand.
Definition ast.h:64
int column
1-based source column where this node begins.
Definition ast.h:66
ExprPtr left
Left-hand operand.
Definition ast.h:63
int line
1-based source line where this node begins.
Definition ast.h:65
TokenType op
The operator token.
Definition ast.h:62
A bool literal expression node, e.g.
Definition ast.h:30
int line
1-based source line where this node begins.
Definition ast.h:32
int column
1-based source column where this node begins.
Definition ast.h:33
bool value
The literal boolean value.
Definition ast.h:31
A copy statement - copies contents from a source directory into an existing destination directory.
Definition ast.h:287
std::optional< ExprPtr > when_clause
Optional condition guarding the copy.
Definition ast.h:291
int column
1-based source column where this node begins.
Definition ast.h:293
bool verbatim
If true, suppress {var} interpolation.
Definition ast.h:290
int line
1-based source line where this node begins.
Definition ast.h:292
PathExpr destination
Existing destination directory path.
Definition ast.h:289
PathExpr source
Source directory path.
Definition ast.h:288
An expression node wrapping an ExprData variant.
Definition ast.h:104
ExprData data
The concrete expression alternative.
Definition ast.h:105
File source: content comes from an inline expression.
Definition ast.h:159
ExprPtr value
The expression whose string value becomes the file content.
Definition ast.h:160
File source: content comes from an embedded source file.
Definition ast.h:153
PathExpr path
Path to the source file (resolved at compile time).
Definition ast.h:154
bool verbatim
If true, suppress {var} interpolation at runtime.
Definition ast.h:155
A file statement - creates or appends to a file.
Definition ast.h:243
std::optional< int > mode
Optional permission bits.
Definition ast.h:248
PathExpr path
File path expression.
Definition ast.h:244
std::optional< std::string > alias
Optional as <name> binding; empty if no as clause.
Definition ast.h:245
int column
1-based source column where this node begins.
Definition ast.h:251
bool append
If true, append; otherwise create/overwrite.
Definition ast.h:247
std::optional< ExprPtr > when_clause
Optional condition guarding creation.
Definition ast.h:249
int line
1-based source line where this node begins.
Definition ast.h:250
FileSource source
Where the file content comes from.
Definition ast.h:246
A built-in function call expression, e.g.
Definition ast.h:75
int line
1-based source line where this node begins.
Definition ast.h:78
std::string name
Function name.
Definition ast.h:76
std::vector< ExprPtr > arguments
Argument expressions.
Definition ast.h:77
int column
1-based source column where this node begins.
Definition ast.h:79
A variable reference expression node, e.g.
Definition ast.h:37
int line
1-based source line where this node begins.
Definition ast.h:39
std::string name
The variable name.
Definition ast.h:38
int column
1-based source column where this node begins.
Definition ast.h:40
An if statement - executes its body when the condition is true.
Definition ast.h:356
int column
1-based source column where this node begins.
Definition ast.h:360
ExprPtr condition
Expression that must evaluate to bool true to enter the body.
Definition ast.h:357
int line
1-based source line where this node begins.
Definition ast.h:359
std::vector< StmtPtr > body
Statements inside the conditional block.
Definition ast.h:358
An include statement - runs another bundled template inline.
Definition ast.h:331
int column
1-based source column where this node begins.
Definition ast.h:336
std::optional< ExprPtr > when_clause
Optional condition guarding the include.
Definition ast.h:334
std::optional< std::uint32_t > version_pin
Optional @N pin from source. Bundler resolves to that exact installed/archived version.
Definition ast.h:333
int line
1-based source line where this node begins.
Definition ast.h:335
std::string name
Name of the installed template to run.
Definition ast.h:332
An integer literal expression node, e.g.
Definition ast.h:23
int line
1-based source line where this node begins.
Definition ast.h:25
int value
The literal integer value.
Definition ast.h:24
int column
1-based source column where this node begins.
Definition ast.h:26
A let statement - declares a derived variable.
Definition ast.h:194
int line
1-based source line where this node begins.
Definition ast.h:197
std::string name
Variable name to bind.
Definition ast.h:195
ExprPtr value
Expression whose value is assigned.
Definition ast.h:196
int column
1-based source column where this node begins.
Definition ast.h:198
A mkdir statement - creates a directory.
Definition ast.h:226
int line
1-based source line where this node begins.
Definition ast.h:234
int column
1-based source column where this node begins.
Definition ast.h:235
PathExpr path
Directory path expression.
Definition ast.h:227
std::optional< ExprPtr > when_clause
Optional condition guarding creation.
Definition ast.h:233
bool mkdir_p
Always true - create intermediate directories.
Definition ast.h:229
std::optional< PathExpr > from_source
Optional source directory to populate from.
Definition ast.h:230
std::optional< std::string > alias
Optional as <name> binding; empty if no as clause.
Definition ast.h:228
std::optional< int > mode
Optional permission bits (e.g. 0755).
Definition ast.h:232
bool verbatim
If true with from_source, suppress interpolation.
Definition ast.h:231
A path expression - an ordered sequence of segments.
Definition ast.h:146
int column
Column of the first segment.
Definition ast.h:149
std::vector< PathSegment > segments
Ordered path components.
Definition ast.h:147
int line
Line of the first segment.
Definition ast.h:148
An inline {expr} interpolation inside a path, e.g.
Definition ast.h:130
int column
1-based source column where this node begins.
Definition ast.h:133
int line
1-based source line where this node begins.
Definition ast.h:132
ExprPtr expression
The expression whose string value is substituted at runtime.
Definition ast.h:131
A literal component of a path expression, e.g.
Definition ast.h:116
std::string value
Literal text (identifiers, dots, slashes already joined).
Definition ast.h:117
int column
1-based source column where this node begins.
Definition ast.h:119
int line
1-based source line where this node begins.
Definition ast.h:118
A reference to a previously-bound path alias, e.g.
Definition ast.h:123
std::string name
The alias name.
Definition ast.h:124
int column
1-based source column where this node begins.
Definition ast.h:126
int line
1-based source line where this node begins.
Definition ast.h:125
The top-level AST node representing a complete .spud program.
Definition ast.h:374
std::vector< StmtPtr > statements
Ordered list of top-level statements.
Definition ast.h:375
A repeat statement - loops a block N times.
Definition ast.h:269
std::string collection_var
Name of the int variable holding the count.
Definition ast.h:270
int column
1-based source column where this node begins.
Definition ast.h:275
int line
1-based source line where this node begins.
Definition ast.h:274
std::vector< StmtPtr > body
Statements inside the loop body.
Definition ast.h:272
std::optional< ExprPtr > when_clause
Optional condition guarding the whole loop.
Definition ast.h:273
std::string iterator_var
Name of the loop index variable (0-based).
Definition ast.h:271
A run statement - executes a shell command after user authorisation.
Definition ast.h:309
ExprPtr command
Expression whose string value is the command.
Definition ast.h:310
int line
1-based source line where this node begins.
Definition ast.h:314
std::optional< ExprPtr > when_clause
Optional condition guarding execution.
Definition ast.h:313
std::optional< ExprPtr > timeout
Optional timeout <int> override in seconds. Absent means use the run-time default unless --no-timeout...
Definition ast.h:312
std::optional< PathExpr > cwd
Optional in <path> working directory.
Definition ast.h:311
int column
1-based source column where this node begins.
Definition ast.h:315
A statement node wrapping a StmtData variant.
Definition ast.h:369
StmtData data
The concrete statement alternative.
Definition ast.h:370
A string literal expression node, e.g.
Definition ast.h:16
int column
1-based source column where this node begins.
Definition ast.h:19
std::string value
The literal string value (without quotes).
Definition ast.h:17
int line
1-based source line where this node begins.
Definition ast.h:18
A string literal containing one or more {expr} interpolations.
Definition ast.h:92
std::vector< std::variant< std::string, ExprPtr > > parts
Ordered literal/expression segments.
Definition ast.h:93
int line
1-based source line where this node begins.
Definition ast.h:94
int column
1-based source column where this node begins.
Definition ast.h:95
A unary expression node, e.g.
Definition ast.h:49
TokenType op
The operator token (NOT).
Definition ast.h:50
ExprPtr operand
The operand expression.
Definition ast.h:51
int line
1-based source line where this node begins.
Definition ast.h:52
int column
1-based source column where this node begins.
Definition ast.h:53