Skip to content

Commit 53b0992

Browse files
committed
initial impl: basic comptime if
1 parent 6f5d79b commit 53b0992

File tree

8 files changed

+274
-263
lines changed

8 files changed

+274
-263
lines changed

lib/rivet/src/ast/Decl.ri

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
2-
// source code is governed by an MIT license that can be found in the LICENSE
1+
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
2+
// source code is governed by an MIT license that can be found in the LICENSE
33
// file.
44

55
import ../token;
@@ -8,6 +8,7 @@ import ../token;
88
pub enum Decl {
99
Empty(token.Pos),
1010
Comment(Comment),
11+
ComptimeIf(ComptimeIf),
1112
Import {
1213
attributes: Attributes;
1314
is_public: bool;
@@ -154,6 +155,7 @@ pub enum Decl {
154155
return match self {
155156
.Empty(empty_pos) -> empty_pos,
156157
.Comment(comment) -> comment.pos,
158+
.ComptimeIf(comptime_if) -> comptime_if.pos,
157159
.Import(import_d) -> import_d.pos,
158160
.Extern(extern_d) -> extern_d.pos,
159161
.Alias(alias_d) -> alias_d.pos,
@@ -171,6 +173,9 @@ pub enum Decl {
171173

172174
pub func decls(self) -> ?[]Self {
173175
return match self {
176+
.ComptimeIf(comptime_if) -> nodes_to_decls(
177+
comptime_if.branches[comptime_if.branch_idx?].nodes
178+
),
174179
.Extern(extern_decl) -> extern_decl.decls,
175180
.Trait(trait_decl) -> trait_decl.decls,
176181
.Enum(enum_decl) -> enum_decl.decls,

lib/rivet/src/ast/Expr.ri

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ pub enum Expr < traits.Stringable {
265265
mut type: Type;
266266
mut defer_stmts: []Stmt.Defer;
267267
},
268+
ComptimeIf(ComptimeIf),
268269
If {
269270
mut branches: []mut IfBranch;
270271
has_else: bool;
@@ -338,6 +339,7 @@ pub enum Expr < traits.Stringable {
338339
.Return(return_expr) -> return_expr.pos,
339340
.Throw(throw_expr) -> throw_expr.pos,
340341
.Block(block) -> block.pos,
342+
.ComptimeIf(comptime_if) -> comptime_if.pos,
341343
.If(if_expr) -> if_expr.pos,
342344
.Match(match_expr) -> match_expr.pos,
343345
.Guard(guard) -> guard.pos
@@ -523,6 +525,22 @@ pub enum Expr < traits.Stringable {
523525
sb.write_string(" }");
524526
sb.to_string()
525527
},
528+
.ComptimeIf(comptime_if) -> {
529+
mut sb := strings.Builder.new();
530+
for branch in comptime_if.branches {
531+
if branch.is_else {
532+
sb.write_string("else ");
533+
} else if branch.is_else_if {
534+
sb.write_string("else if ");
535+
sb.write_string(branch.cond.to_string());
536+
} else {
537+
sb.write_string("comptime if ");
538+
sb.write_string(branch.cond.to_string());
539+
}
540+
sb.write_fmt("{{ {} nodes }}", branch.nodes.len);
541+
}
542+
sb.to_string()
543+
},
526544
.If(if_expr) -> {
527545
mut sb := strings.Builder.new();
528546
for branch in if_expr.branches {

lib/rivet/src/ast/Stmt.ri

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
2-
// source code is governed by an MIT license that can be found in the LICENSE
1+
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
2+
// source code is governed by an MIT license that can be found in the LICENSE
33
// file.
44

55
import ../token;
@@ -8,6 +8,7 @@ import ../token;
88
pub enum Stmt {
99
Empty(token.Pos),
1010
Comment(Comment),
11+
ComptimeIf(ComptimeIf),
1112
Expr(Expr),
1213
VarDecl {
1314
mut lefts: []mut ObjectData;
@@ -50,6 +51,7 @@ pub enum Stmt {
5051
return match self {
5152
.Empty(empty_pos) -> empty_pos,
5253
.Comment(comment) -> comment.pos,
54+
.ComptimeIf(comptime_if) -> comptime_if.pos,
5355
.Expr(expr) -> expr.position(),
5456
.VarDecl(var_decl) -> var_decl.pos,
5557
.While(while_stmt) -> while_stmt.pos,

lib/rivet/src/ast/mod.ri

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
2-
// source code is governed by an MIT license that can be found in the LICENSE
1+
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
2+
// source code is governed by an MIT license that can be found in the LICENSE
33
// file.
44

55
import ../token;
66

7-
pub enum Node {
8-
Decl(Decl),
9-
Stmt(Stmt),
10-
Expr(Expr);
11-
12-
pub func position(self) -> token.Pos {
13-
return match self {
14-
.Decl(decl) -> decl.position(),
15-
.Stmt(stmt) -> stmt.position(),
16-
.Expr(expr) -> expr.position()
17-
};
18-
}
19-
}
20-
217
pub struct Comment {
228
pub mut is_doc: bool;
239
pub mut text: string;
@@ -34,37 +20,6 @@ pub struct Comment {
3420
}
3521
}
3622

37-
#[boxed]
38-
pub struct ImportedSymbol {
39-
pub name: string;
40-
pub sym: Sym;
41-
pub pos: token.Pos;
42-
pub mut is_used: bool;
43-
}
44-
45-
pub struct ImportedSymbols {
46-
pub mut syms: []ImportedSymbol;
47-
48-
#[inline]
49-
pub func add(mut self, name: string, sym: Sym, pos: token.Pos, is_used: bool := false) {
50-
self.syms.push(ImportedSymbol(name, sym, pos, is_used));
51-
}
52-
53-
pub func find(&self, name: string) -> ?ImportedSymbol {
54-
for imported_sym in self.syms {
55-
if name == imported_sym.name {
56-
return imported_sym;
57-
}
58-
}
59-
return none;
60-
}
61-
62-
#[inline]
63-
pub func exists(&self, name: string) -> bool {
64-
return self.find(name) != none;
65-
}
66-
}
67-
6823
// Used in variable declarations/statements and guard expressions.
6924
#[boxed]
7025
pub struct ObjectData {

lib/rivet/src/parser/decls.ri

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ extend Parser {
9797
mut pos := self.tok.pos;
9898
match {
9999
self.tok.kind == .Comment -> return .Comment(self.parse_comment()),
100+
self.accept(.KwComptime) -> match self.tok.kind {
101+
.KwIf -> return .ComptimeIf(self.parse_comptime_if(.Decl)),
102+
else -> {
103+
report.error("invalid comptime construction", self.tok.pos);
104+
return .Empty(self.tok.pos);
105+
}
106+
},
100107
self.accept(.KwImport) -> {
101108
mut has_custom_alias := false;
102109
mut mod_path_pos := self.tok.pos;

lib/rivet/src/parser/exprs.ri

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ extend Parser {
214214

215215
func parse_primary_expr(mut self) -> ast.Expr {
216216
mut expr: ast.Expr := match {
217+
self.accept(.KwComptime) -> match self.tok.kind {
218+
.KwIf -> .ComptimeIf(self.parse_comptime_if(.Expr)),
219+
else -> {
220+
report.error("invalid comptime construction", self.tok.pos);
221+
.Empty(self.tok.pos)
222+
}
223+
},
217224
self.tok.kind in [
218225
.KwTrue, .KwFalse, .Char, .Number, .String,
219226
.KwNone, .KwSelf, .KwSelfTy

lib/rivet/src/parser/stmts.ri

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ extend Parser {
99
func parse_stmt(mut self) -> ast.Stmt {
1010
return match {
1111
self.tok.kind == .Comment -> .Comment(self.parse_comment()),
12+
self.accept(.KwComptime) -> match self.tok.kind {
13+
.KwIf -> .ComptimeIf(self.parse_comptime_if(.Stmt)),
14+
else -> {
15+
report.error("invalid comptime construction", self.tok.pos);
16+
.Empty(self.tok.pos)
17+
}
18+
},
1219
self.accept(.KwWhile) -> {
1320
pos := self.prev_tok.pos;
1421
mut is_inf := false;

0 commit comments

Comments
 (0)