authorchloe caruso <git@paperclover.net> 2024-05-27 21:13:12-07:00
committerchloe caruso <git@paperclover.net> 2024-05-27 21:13:12-07:00
logd290e960b5392267b9ed75aacb7656117809a946
tree78d773144e6b7d05e374542fcd27e82f8a435b17
parenta9459d564baeb0b84693acac09435883a46dd87f

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 {...@@ -204,9 +204,9 @@ pub const Node = struct {
204 /// `comptime expr`. main_token is the operator.204 /// `comptime expr`. main_token is the operator.
205 @"comptime",205 @"comptime",
206 /// `pure expr`. main_token is the operator.206 /// `pure expr`. main_token is the operator.
207 // pure,207 pure,
208 /// `assert expr`. main_token is the operator.208 /// `assert expr`. main_token is the operator.
209 // assert,209 assert,
210210
211 pub fn isAssignBin(op: Tag) bool {211 pub fn isAssignBin(op: Tag) bool {
212 return switch (op) {212 return switch (op) {
...@@ -319,6 +319,8 @@ pub const Node = struct {...@@ -319,6 +319,8 @@ pub const Node = struct {
319 .negate = .un,319 .negate = .un,
320 .bit_not = .un,320 .bit_not = .un,
321 .@"comptime" = .un,321 .@"comptime" = .un,
322 .pure = .un,
323 .assert = .un,
322 .label = .label,324 .label = .label,
323 });325 });
324326
src/core/Parse.zig+21-9
...@@ -478,19 +478,22 @@ fn parseVarDecl(p: *Parse) Error!NodeIndex {...@@ -478,19 +478,22 @@ fn parseVarDecl(p: *Parse) Error!NodeIndex {
478478
479fn isExpressionStart(p: *Parse) bool {479fn isExpressionStart(p: *Parse) bool {
480 return switch (p.currentTag()) {480 return switch (p.currentTag()) {
481 .id,481 .bang,
482 .number_int,
483 .number_float,
484 .string,
485 .builtin_id,482 .builtin_id,
486 .kw_return,483 .id,
484 .kw_assert,
487 .kw_break,485 .kw_break,
486 .kw_comptime,
488 .kw_if,487 .kw_if,
489 .start_paren,488 .kw_pure,
489 .kw_return,
490 .minus,
491 .number_float,
492 .number_int,
490 .start_brace,493 .start_brace,
491 .bang,494 .start_paren,
495 .string,
492 .tilde,496 .tilde,
493 .minus,
494 => true,497 => true,
495 else => false,498 else => false,
496 };499 };
...@@ -731,7 +734,13 @@ fn parseExpressionPart(p: *Parse) Error!NodeIndex {...@@ -731,7 +734,13 @@ fn parseExpressionPart(p: *Parse) Error!NodeIndex {
731 .start_brace => try p.parseBlock(),734 .start_brace => try p.parseBlock(),
732735
733 // Unary operators736 // 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: {
735 const main_token = p.nextToken();744 const main_token = p.nextToken();
736 const expr = try p.parseExpressionPart();745 const expr = try p.parseExpressionPart();
737746
...@@ -740,6 +749,9 @@ fn parseExpressionPart(p: *Parse) Error!NodeIndex {...@@ -740,6 +749,9 @@ fn parseExpressionPart(p: *Parse) Error!NodeIndex {
740 .bang => .bool_not,749 .bang => .bool_not,
741 .tilde => .bit_not,750 .tilde => .bit_not,
742 .minus => .negate,751 .minus => .negate,
752 .kw_comptime => .@"comptime",
753 .kw_pure => .pure,
754 .kw_assert => .assert,
743 else => comptime unreachable,755 else => comptime unreachable,
744 },756 },
745 .main_token = main_token,757 .main_token = main_token,
src/core/Teg.Dump.zig+7-1
...@@ -181,7 +181,7 @@ const State = struct {...@@ -181,7 +181,7 @@ const State = struct {
181 const call: Teg.Inst.Call = teg.instData(.call, inst).extra_index.get(teg);181 const call: Teg.Inst.Call = teg.instData(.call, inst).extra_index.get(teg);
182 try writer.writeAll("(");182 try writer.writeAll("(");
183 try s.dumpRefMarker(writer, call.base);183 try s.dumpRefMarker(writer, call.base);
184 try writer.writeAll(", ");184 try writer.writeAll(",");
185 try s.dumpRefMarkerList(writer, call.args.slice(teg), '[', ']');185 try s.dumpRefMarkerList(writer, call.args.slice(teg), '[', ']');
186 try writer.writeAll(")");186 try writer.writeAll(")");
187 },187 },
...@@ -208,6 +208,12 @@ const State = struct {...@@ -208,6 +208,12 @@ const State = struct {
208 try s.newline(writer);208 try s.newline(writer);
209 try writer.writeAll("}");209 try writer.writeAll("}");
210 },210 },
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 },
211 }217 }
212 }218 }
213219
src/core/Teg.zig+2
...@@ -68,6 +68,7 @@ pub const Inst = struct {...@@ -68,6 +68,7 @@ pub const Inst = struct {
68 bit_not,68 bit_not,
69 field,69 field,
70 call,70 call,
71 @"comptime",
7172
72 /// Implements the `@js` builtin73 /// Implements the `@js` builtin
73 js,74 js,
...@@ -92,6 +93,7 @@ pub const Inst = struct {...@@ -92,6 +93,7 @@ pub const Inst = struct {
92 .load_decl => StrNode,93 .load_decl => StrNode,
93 .param => DataNode,94 .param => DataNode,
94 .store => PlNode(Store),95 .store => PlNode(Store),
96 .@"comptime" => RefNode,
9597
96 // Single instruction operand98 // Single instruction operand
97 .memory,99 .memory,
src/core/Transform.zig+12
...@@ -780,6 +780,18 @@ fn transformExpression(t: *Transform, decl: BlockContext, node: NodeIndex) Alloc...@@ -780,6 +780,18 @@ fn transformExpression(t: *Transform, decl: BlockContext, node: NodeIndex) Alloc
780 });780 });
781 },781 },
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
783 .label => {795 .label => {
784 const expr = t.ast.nodeData(node).label.expr;796 const expr = t.ast.nodeData(node).label.expr;
785 const label = t.ast.tokenSlice(t.ast.nodeToken(node));797 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 {...@@ -47,7 +47,7 @@ pub fn Ordinal(Int: type) type {
47 _: std.fmt.FormatOptions,47 _: std.fmt.FormatOptions,
48 _: anytype,48 _: anytype,
49 ) !void {49 ) !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.");
51 }51 }
52 };52 };
53}53}