X-Git-Url: https://git.ayoreis.com/zlox.git/blobdiff_plain/6d286699938880d7a1482be65e5e4771788c6b6f..af5a4d8d95bf091ac4139094ed27265fe04a2634:/src/ast_printer.zig?ds=inline diff --git a/src/ast_printer.zig b/src/ast_printer.zig index 21c8702..2df8c8e 100644 --- a/src/ast_printer.zig +++ b/src/ast_printer.zig @@ -5,15 +5,15 @@ const Token = @import("Token.zig"); 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) { - .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, ')'); - return try builder.toOwnedSlice(allocator); + return builder.toOwnedSlice(allocator); }