ast_printer: tuple-ify

This commit is contained in:
Luna 2020-04-01 16:03:46 -03:00
parent ebf4af6537
commit cf22d47e13
1 changed files with 64 additions and 64 deletions

View File

@ -10,7 +10,7 @@ const warn = std.debug.warn;
fn printIdent(ident: usize) void { fn printIdent(ident: usize) void {
var i: usize = 0; var i: usize = 0;
while (i < ident) : (i += 1) { while (i < ident) : (i += 1) {
std.debug.warn("\t"); std.debug.warn("\t", .{});
} }
} }
@ -20,12 +20,12 @@ fn print(ident: usize, comptime fmt: []const u8, args: ...) void {
} }
fn printBlock(ident: usize, block: var, endNewline: bool) void { fn printBlock(ident: usize, block: var, endNewline: bool) void {
std.debug.warn("(\n"); std.debug.warn("(\n", .{});
for (block.toSlice()) |stmt| { for (block.toSlice()) |stmt| {
printIdent(ident); printIdent(ident);
printStmt(ident, &stmt); printStmt(ident, &stmt);
std.debug.warn("\n"); std.debug.warn("\n", .{});
} }
if (endNewline) { if (endNewline) {
@ -48,18 +48,18 @@ pub fn printNode(node: *const Node, ident: usize) void {
const vari = method.variable.lexeme; const vari = method.variable.lexeme;
const typ = method.typ.lexeme; const typ = method.typ.lexeme;
warn("(method {} {} {} {} (", vari, typ, name, ret_type); warn("(method {} {} {} {} (", .{ vari, typ, name, ret_type });
} else { } else {
warn("(fn {} {} (", name, ret_type); warn("(fn {} {} (", .{ name, ret_type });
} }
for (decl.params.toSlice()) |param| { for (decl.params.toSlice()) |param| {
warn(" ({} {})", param.name.lexeme, param.typ.lexeme); warn(" ({} {})", .{ param.name.lexeme, param.typ.lexeme });
} }
warn(") "); warn(") ", .{});
printBlock(ident + 1, decl.body, false); printBlock(ident + 1, decl.body, false);
warn("\n"); warn("\n", .{});
}, },
.ConstDecl => |consts| { .ConstDecl => |consts| {
@ -73,7 +73,7 @@ pub fn printNode(node: *const Node, ident: usize) void {
); );
printExpr(const_decl.expr); printExpr(const_decl.expr);
std.debug.warn(")\n"); std.debug.warn(")\n", .{});
} }
print(ident, "))\n"); print(ident, "))\n");
@ -114,20 +114,20 @@ pub fn printNode(node: *const Node, ident: usize) void {
} }
fn parenthetize(name: []const u8, exprs: []const Expr) void { fn parenthetize(name: []const u8, exprs: []const Expr) void {
std.debug.warn("({}", name); std.debug.warn("({}", .{name});
for (exprs) |expr| { for (exprs) |expr| {
std.debug.warn(" "); std.debug.warn(" ", .{});
printExpr(&expr); printExpr(&expr);
} }
std.debug.warn(")"); std.debug.warn(")", .{});
} }
fn printTwoExprs(expr_a: *const Expr, expr_b: *const Expr) void { fn printTwoExprs(expr_a: *const Expr, expr_b: *const Expr) void {
std.debug.warn(" "); std.debug.warn(" ", .{});
printExpr(expr_a); printExpr(expr_a);
std.debug.warn(" "); std.debug.warn(" ", .{});
printExpr(expr_b); printExpr(expr_b);
} }
@ -161,9 +161,9 @@ fn binOpToStr(op: BinaryOperator) ?[]const u8 {
} }
fn printBinOp(inner: var) void { fn printBinOp(inner: var) void {
std.debug.warn("({}", binOpToStr(inner.op)); std.debug.warn("({}", .{binOpToStr(inner.op)});
printTwoExprs(inner.left, inner.right); printTwoExprs(inner.left, inner.right);
std.debug.warn(")"); std.debug.warn(")", .{});
} }
const unary_operator_tokens = [_][]const u8{ const unary_operator_tokens = [_][]const u8{
@ -187,9 +187,9 @@ fn printSingleOp(op: UnaryOperator, applied: *const Expr) void {
} }
fn printSimpleOp(op: ?[]const u8, applied: *const Expr) void { fn printSimpleOp(op: ?[]const u8, applied: *const Expr) void {
std.debug.warn("({}", op); std.debug.warn("({}", .{op});
printExpr(applied); printExpr(applied);
std.debug.warn(")"); std.debug.warn(")", .{});
} }
pub fn printExpr(expr: *const Expr) void { pub fn printExpr(expr: *const Expr) void {
@ -201,66 +201,66 @@ pub fn printExpr(expr: *const Expr) void {
.Literal => |literal| { .Literal => |literal| {
switch (literal) { switch (literal) {
.Bool => |val| std.debug.warn("{}", val), .Bool => |val| std.debug.warn("{}", .{val}),
.Integer32 => |val| std.debug.warn("{}", val), .Integer32 => |val| std.debug.warn("{}", .{val}),
.Integer64 => |val| std.debug.warn("{}", val), .Integer64 => |val| std.debug.warn("{}", .{val}),
.Float => |val| std.debug.warn("{}", val), .Float => |val| std.debug.warn("{}", .{val}),
.String => |val| std.debug.warn("'{}'", val), .String => |val| std.debug.warn("'{}'", .{val}),
.Array => |exprs| { .Array => |exprs| {
parenthetize("array", exprs.toSlice()); parenthetize("array", exprs.toSlice());
}, },
else => |typ| std.debug.warn("UnknownLiteral-{}", typ), else => |typ| std.debug.warn("UnknownLiteral-{}", .{typ}),
} }
}, },
.Variable => |token| std.debug.warn("{}", token.lexeme), .Variable => |token| std.debug.warn("{}", .{token.lexeme}),
.Assign => |assign| { .Assign => |assign| {
std.debug.warn("(set "); std.debug.warn("(set ", .{});
std.debug.warn("{} ", assign.name.lexeme); std.debug.warn("{} ", .{assign.name.lexeme});
printExpr(assign.value); printExpr(assign.value);
std.debug.warn(")"); std.debug.warn(")", .{});
}, },
.Call => |call| { .Call => |call| {
std.debug.warn("("); std.debug.warn("(", .{});
printExpr(call.callee); printExpr(call.callee);
for (call.arguments.toSlice()) |arg| { for (call.arguments.toSlice()) |arg| {
std.debug.warn(" "); std.debug.warn(" ", .{});
printExpr(&arg); printExpr(&arg);
} }
std.debug.warn(")"); std.debug.warn(")", .{});
}, },
.Struct => |val| { .Struct => |val| {
std.debug.warn("({} (", val.name.lexeme); std.debug.warn("({} (", .{val.name.lexeme});
for (val.inits.toSlice()) |init| { for (val.inits.toSlice()) |init| {
std.debug.warn(" ({} ", init.field.lexeme); std.debug.warn(" ({} ", .{init.field.lexeme});
printExpr(init.expr); printExpr(init.expr);
std.debug.warn(")"); std.debug.warn(")", .{});
} }
std.debug.warn("))"); std.debug.warn("))", .{});
}, },
.Get => |get| { .Get => |get| {
warn("("); warn("(", .{});
printExpr(get.target); printExpr(get.target);
warn(".{})", get.name.lexeme); warn(".{})", get.name.lexeme);
}, },
.Set => |set| { .Set => |set| {
warn("(set "); warn("(set ", .{});
printExpr(set.struc); printExpr(set.struc);
warn(" {} ", set.field.lexeme); warn(" {} ", .{set.field.lexeme});
printExpr(set.value); printExpr(set.value);
warn(")"); warn(")", .{});
}, },
else => std.debug.warn("UnknownExpr-{}", @tagName(expr.*)), else => std.debug.warn("UnknownExpr-{}", .{@tagName(expr.*)}),
} }
} }
@ -270,60 +270,60 @@ pub fn printStmt(ident: usize, stmt: *const Stmt) void {
.Expr => |expr| printExpr(expr), .Expr => |expr| printExpr(expr),
.VarDecl => |decl| { .VarDecl => |decl| {
std.debug.warn("(let {} ", decl.name.lexeme); std.debug.warn("(let {} ", .{decl.name.lexeme});
printExpr(decl.value); printExpr(decl.value);
std.debug.warn(")"); std.debug.warn(")", .{});
}, },
.If => |ifstmt| { .If => |ifstmt| {
std.debug.warn("(if "); std.debug.warn("(if ", .{});
printExpr(ifstmt.condition); printExpr(ifstmt.condition);
std.debug.warn(" "); std.debug.warn(" ", .{});
printBlock(ident + 1, ifstmt.then_branch, false); printBlock(ident + 1, ifstmt.then_branch, false);
if (ifstmt.else_branch) |else_branch| { if (ifstmt.else_branch) |else_branch| {
std.debug.warn(" else "); std.debug.warn(" else ", .{});
printBlock(ident + 1, else_branch, false); printBlock(ident + 1, else_branch, false);
} }
std.debug.warn(")\n"); std.debug.warn(")\n", .{});
}, },
.Loop => |loop| { .Loop => |loop| {
std.debug.warn("(loop "); std.debug.warn("(loop ", .{});
if (loop.condition) |cond| { if (loop.condition) |cond| {
printExpr(cond); printExpr(cond);
} else { } else {
std.debug.warn("true"); std.debug.warn("true", .{});
} }
std.debug.warn(" "); std.debug.warn(" ", .{});
printBlock(ident + 1, loop.then_branch, false); printBlock(ident + 1, loop.then_branch, false);
std.debug.warn(")\n"); std.debug.warn(")\n", .{});
}, },
.For => |forstmt| { .For => |forstmt| {
std.debug.warn("(for "); std.debug.warn("(for ", .{});
if (forstmt.index) |index| { if (forstmt.index) |index| {
std.debug.warn("({} {}) ", index.lexeme, forstmt.value.lexeme); std.debug.warn("({} {}) ", .{ index.lexeme, forstmt.value.lexeme });
} else { } else {
std.debug.warn("{} ", forstmt.value.lexeme); std.debug.warn("{} ", .{forstmt.value.lexeme});
} }
std.debug.warn("{} ", forstmt.array.lexeme); std.debug.warn("{} ", .{forstmt.array.lexeme});
printBlock(ident + 1, forstmt.block, false); printBlock(ident + 1, forstmt.block, false);
std.debug.warn(")\n"); std.debug.warn(")\n", .{});
}, },
.Return => |ret| { .Return => |ret| {
std.debug.warn("(return "); std.debug.warn("(return ", .{});
printExpr(ret.value); printExpr(ret.value);
std.debug.warn(")\n"); std.debug.warn(")\n", .{});
}, },
else => std.debug.warn("UnknownStmt-{}", @tagName(stmt.*)), else => std.debug.warn("UnknownStmt-{}", .{@tagName(stmt.*)}),
} }
} }
@ -381,12 +381,12 @@ pub fn printContext(ctx: CompilationContext) void {
} }
// go through scopes // go through scopes
std.debug.warn("scope info:\n"); std.debug.warn("scope info:\n", .{});
printScope(fn_sym.scope, 1); printScope(fn_sym.scope, 1);
}, },
.Struct => |typemap| { .Struct => |typemap| {
std.debug.warn("struct '{}'\n", kv.key); std.debug.warn("struct '{}'\n", .{kv.key});
var map_it = typemap.iterator(); var map_it = typemap.iterator();
while (map_it.next()) |map_kv| { while (map_it.next()) |map_kv| {
std.debug.warn( std.debug.warn(
@ -404,21 +404,21 @@ pub fn printContext(ctx: CompilationContext) void {
), ),
.Enum => |identmap| { .Enum => |identmap| {
std.debug.warn("enum {}:", kv.key); std.debug.warn("enum {}:", .{kv.key});
var mapit = identmap.iterator(); var mapit = identmap.iterator();
while (mapit.next()) |field_kv| { while (mapit.next()) |field_kv| {
std.debug.warn("\t{} => {}\n", field_kv.key, field_kv.value); std.debug.warn("\t{} => {}\n", .{ field_kv.key, field_kv.value });
} }
}, },
.Const => |typ| { .Const => |typ| {
std.debug.warn("const '{}', typ={}\n", kv.key, prettyType(typ)); std.debug.warn("const '{}', typ={}\n", .{ kv.key, prettyType(typ) });
}, },
else => { else => {
std.debug.warn("TODO handle print of {}\n", kv.value); std.debug.warn("TODO handle print of {}\n", .{kv.value});
unreachable; unreachable;
}, },
} }