authorgravatar for me@paperdave.netdave caruso <me@paperdave.net> 2024-05-27 21:13:12-07:00
committergravatar for me@paperdave.netdave caruso <me@paperdave.net> 2024-05-27 21:13:12-07:00
log3ff6202073407ee0560d083011736c187448b190
tree78d773144e6b7d05e374542fcd27e82f8a435b17
parent5d80ae2c0e35a2f617b42d22cf112402ad5ca63b
signature Commit is signed but in an unrecognized format.

feat: implement parsing for keyword unary ops


6 files changed, 47 insertions(+), 13 deletions(-)

src/core/Ast.zig+4-2
......@@ -204,9 +204,9 @@ pub const Node = struct {
204204 /// `comptime expr`. main_token is the operator.
205205 @"comptime",
206206 /// `pure expr`. main_token is the operator.
207 // pure,
207 pure,
208208 /// `assert expr`. main_token is the operator.
209 // assert,
209 assert,
210210
211211 pub fn isAssignBin(op: Tag) bool {
212212 return switch (op) {
......@@ -319,6 +319,8 @@ pub const Node = struct {
319319 .negate = .un,
320320 .bit_not = .un,
321321 .@"comptime" = .un,
322 .pure = .un,
323 .assert = .un,
322324 .label = .label,
323325 });
324326
src/core/Parse.zig+21-9
......@@ -478,19 +478,22 @@ fn parseVarDecl(p: *Parse) Error!NodeIndex {
478478
479479fn isExpressionStart(p: *Parse) bool {
480480 return switch (p.currentTag()) {
481 .id,
482 .number_int,
483 .number_float,
484 .string,
481 .bang,
485482 .builtin_id,
486 .kw_return,
483 .id,
484 .kw_assert,
487485 .kw_break,
486 .kw_comptime,
488487 .kw_if,
489 .start_paren,
488 .kw_pure,
489 .kw_return,
490 .minus,
491 .number_float,
492 .number_int,
490493 .start_brace,
491 .bang,
494 .start_paren,
495 .string,
492496 .tilde,
493 .minus,
494497 => true,
495498 else => false,
496499 };
......@@ -731,7 +734,13 @@ fn parseExpressionPart(p: *Parse) Error!NodeIndex {
731734 .start_brace => try p.parseBlock(),
732735
733736 // Unary operators
734 inline .bang, .tilde, .minus => |tag| unary: {
737 inline .bang,
738 .tilde,
739 .minus,
740 .kw_comptime,
741 .kw_pure,
742 .kw_assert,
743 => |tag| unary: {
735744 const main_token = p.nextToken();
736745 const expr = try p.parseExpressionPart();
737746
......@@ -740,6 +749,9 @@ fn parseExpressionPart(p: *Parse) Error!NodeIndex {
740749 .bang => .bool_not,
741750 .tilde => .bit_not,
742751 .minus => .negate,
752 .kw_comptime => .@"comptime",
753 .kw_pure => .pure,
754 .kw_assert => .assert,
743755 else => comptime unreachable,
744756 },
745757 .main_token = main_token,
src/core/Teg.Dump.zig+7-1
......@@ -181,7 +181,7 @@ const State = struct {
181181 const call: Teg.Inst.Call = teg.instData(.call, inst).extra_index.get(teg);
182182 try writer.writeAll("(");
183183 try s.dumpRefMarker(writer, call.base);
184 try writer.writeAll(", ");
184 try writer.writeAll(",");
185185 try s.dumpRefMarkerList(writer, call.args.slice(teg), '[', ']');
186186 try writer.writeAll(")");
187187 },
......@@ -208,6 +208,12 @@ const State = struct {
208208 try s.newline(writer);
209209 try writer.writeAll("}");
210210 },
211 .@"comptime" => {
212 const data: Teg.Inst.RefNode = teg.instData(.@"comptime", inst);
213 try writer.writeAll("(");
214 try s.dumpRef(writer, data.operand);
215 try writer.writeAll(")");
216 },
211217 }
212218 }
213219
src/core/Teg.zig+2
......@@ -68,6 +68,7 @@ pub const Inst = struct {
6868 bit_not,
6969 field,
7070 call,
71 @"comptime",
7172
7273 /// Implements the `@js` builtin
7374 js,
......@@ -92,6 +93,7 @@ pub const Inst = struct {
9293 .load_decl => StrNode,
9394 .param => DataNode,
9495 .store => PlNode(Store),
96 .@"comptime" => RefNode,
9597
9698 // Single instruction operand
9799 .memory,
src/core/Transform.zig+12
......@@ -780,6 +780,18 @@ fn transformExpression(t: *Transform, decl: BlockContext, node: NodeIndex) Alloc
780780 });
781781 },
782782
783 .@"comptime" => {
784 const ast_un = t.ast.nodeData(node).un;
785 const operand = try t.transformExpressionToBlock(decl, ast_un.expr, false);
786 return try t.addInstRef(.{
787 .tag = .@"comptime",
788 .data = EncodedData.encode(.@"comptime", .{
789 .src_node = decl.nodeOffset(node),
790 .operand = operand,
791 }),
792 });
793 },
794
783795 .label => {
784796 const expr = t.ast.nodeData(node).label.expr;
785797 const label = t.ast.tokenSlice(t.ast.nodeToken(node));
src/core/util.zig+1-1
......@@ -47,7 +47,7 @@ pub fn Ordinal(Int: type) type {
4747 _: std.fmt.FormatOptions,
4848 _: anytype,
4949 ) !void {
50 @compileError("Do not format `Ordinal` directly. Call oneBased or zeroBased to specify.");
50 // @compileError("Do not format `Ordinal` directly. Call oneBased or zeroBased to specify.");
5151 }
5252 };
5353}