vig/src/ast_printer.zig
2019-06-29 23:50:43 -03:00

32 lines
1.1 KiB
Zig

const std = @import("std");
const ast = @import("ast.zig");
fn parenthesize(name: []const u8, exprs: []*ast.Expr) void {
std.debug.warn("({}", name);
for (exprs) |expr| {
std.debug.warn(" ");
printAst(expr);
}
std.debug.warn(")");
}
pub fn printAst(ast_expr: *ast.Expr) void {
switch (ast_expr.*) {
.Binary => |expr| parenthesize(expr.operator.lexeme, &[_]*ast.Expr{ expr.left, expr.right }),
.Grouping => |expr| parenthesize("group", &[_]*ast.Expr{expr.expression}),
.Unary => |expr| parenthesize(expr.operator.lexeme, &[_]*ast.Expr{expr.right}),
.Number => |ast_num| {
switch (ast_num) {
.Integer32 => |num| std.debug.warn("{}", num),
.Integer64 => |num| std.debug.warn("{}", num),
.Unsigned32 => |num| std.debug.warn("{}", num),
.Unsigned64 => |num| std.debug.warn("{}", num),
.Float32 => |num| std.debug.warn("{}", num),
.Float64 => |num| std.debug.warn("{}", num),
}
},
else => unreachable,
}
}