]> Repositories - zlox.git/blobdiff - src/ast_printer.zig
Remove unnecessary error logic
[zlox.git] / src / ast_printer.zig
index 21c8702c81e6020ebb855dc5754dad155b53e4d3..2df8c8ecf790282b87913071cd17b78f513dd0ca 100644 (file)
@@ -5,15 +5,15 @@ const Token = @import("Token.zig");
 
 pub fn print(allocator: Allocator, expr: *const Expr) Allocator.Error![]const u8 {
     return switch (expr.*) {
 
 pub fn print(allocator: Allocator, expr: *const Expr) Allocator.Error![]const u8 {
     return switch (expr.*) {
-        .binary => |binary| try parenthesize(allocator, binary.operator.lexeme, &.{ binary.left, binary.right }),
-        .grouping => |grouping| try parenthesize(allocator, "group", &.{grouping.expression}),
+        .binary => |binary| parenthesize(allocator, binary.operator.lexeme, &.{ binary.left, binary.right }),
+        .grouping => |grouping| parenthesize(allocator, "group", &.{grouping.expression}),
         .literal => |literal| switch (literal.value) {
         .literal => |literal| switch (literal.value) {
-            .nil => "nil",
-            .boolean => |boolean| if (boolean) "true" else "false",
-            .string => |string| string,
-            .number => |number| try std.fmt.allocPrint(allocator, "{}", .{number}),
+            .string => |string| allocator.dupe(u8, string),
+            .number => |number| std.fmt.allocPrint(allocator, "{}", .{number}),
+            .boolean => |boolean| std.fmt.allocPrint(allocator, "{}", .{boolean}),
+            .nil => allocator.dupe(u8, "nil"),
         },
         },
-        .unary => |unary| try parenthesize(allocator, unary.operator.lexeme, &.{unary.right}),
+        .unary => |unary| parenthesize(allocator, unary.operator.lexeme, &.{unary.right}),
     };
 }
 
     };
 }
 
@@ -30,5 +30,5 @@ fn parenthesize(allocator: Allocator, name: []const u8, exprs: []const *const Ex
     }
     try builder.append(allocator, ')');
 
     }
     try builder.append(allocator, ')');
 
-    return try builder.toOwnedSlice(allocator);
+    return builder.toOwnedSlice(allocator);
 }
 }