From a86252879b27a4951e13aaa6a28ff7fce2505ff8 Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Sun, 1 Jun 2025 01:06:51 +0200 Subject: add (slightly) prett(ier) printing for exprs --- src/parse.zig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src') diff --git a/src/parse.zig b/src/parse.zig index 24d6e42..7daa3f8 100644 --- a/src/parse.zig +++ b/src/parse.zig @@ -24,9 +24,28 @@ pub const Expr = struct { const Op = enum { plus, minus, + + pub fn format(self: Op, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + _ = fmt; + _ = options; + try writer.writeByte(switch (self) { + .plus => '+', + .minus => '-', + }); + } }; }; }; + + pub fn format(self: Expr, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + _ = fmt; + _ = options; + switch (self.type) { + .integer_literal => try writer.print("", .{}), + .bin_op => |bin_op| try writer.print("({} {} {})", .{ bin_op.lhs, bin_op.op, bin_op.rhs }), + .invalid => try writer.print("", .{}), + } + } }; pub fn expression(allocator: Allocator, lexer: *Peekable(Lexer)) error{OutOfMemory}!*Expr { -- cgit v1.2.3