tuple-inator pass

This commit is contained in:
Luna 2020-04-01 16:09:39 -03:00
parent cf22d47e13
commit 7976b64cf6
6 changed files with 59 additions and 59 deletions

View File

@ -45,18 +45,18 @@ pub const Analyzer = struct {
fn doError(self: *@This(), comptime fmt: []const u8, args: ...) void { fn doError(self: *@This(), comptime fmt: []const u8, args: ...) void {
self.hadError = true; self.hadError = true;
std.debug.warn("analysis error"); std.debug.warn("analysis error" , .{ });
if (self.err_tok) |tok| { if (self.err_tok) |tok| {
std.debug.warn(" at line {}", tok.line); std.debug.warn(" at line {}" , .{ tok.line });
} }
if (self.err_ctx) |ctx| { if (self.err_ctx) |ctx| {
std.debug.warn(" on {}", ctx); std.debug.warn(" on {}" , .{ ctx });
} }
std.debug.warn("\n\t"); std.debug.warn("\n\t" , .{ });
std.debug.warn(fmt, args); std.debug.warn(fmt, args);
std.debug.warn("\n"); std.debug.warn("\n" , .{ });
} }
/// Resolve a type in global scope /// Resolve a type in global scope
@ -108,7 +108,7 @@ pub const Analyzer = struct {
) !void { ) !void {
var actual_enum = @as(comp.SymbolUnderlyingTypeEnum, symbol_type); var actual_enum = @as(comp.SymbolUnderlyingTypeEnum, symbol_type);
if (actual_enum != wanted_type_enum) { if (actual_enum != wanted_type_enum) {
std.debug.warn("Expected {}, got {}\n", wanted_type_enum, actual_enum); std.debug.warn("Expected {}, got {}\n" , .{ wanted_type_enum, actual_enum });
return CompileError.TypeError; return CompileError.TypeError;
} }
} }
@ -122,7 +122,7 @@ pub const Analyzer = struct {
.Integer32, .Integer64, .Double => {}, .Integer32, .Integer64, .Double => {},
else => { else => {
var actual_enum = @as(comp.SymbolUnderlyingTypeEnum, symbol_type); var actual_enum = @as(comp.SymbolUnderlyingTypeEnum, symbol_type);
std.debug.warn("Expected numeric, got {}\n", actual_enum); std.debug.warn("Expected numeric, got {}\n" , .{ actual_enum });
return CompileError.TypeError; return CompileError.TypeError;
}, },
} }
@ -158,7 +158,7 @@ pub const Analyzer = struct {
const expected_enum = @as(comp.SymbolUnderlyingTypeEnum, expected_type); const expected_enum = @as(comp.SymbolUnderlyingTypeEnum, expected_type);
if (symbol_enum != expected_enum) { if (symbol_enum != expected_enum) {
std.debug.warn("Expected {}, got {}\n", expected_enum, symbol_enum); std.debug.warn("Expected {}, got {}\n" , .{ expected_enum, symbol_enum });
return CompileError.TypeError; return CompileError.TypeError;
} }
@ -292,7 +292,7 @@ pub const Analyzer = struct {
var target = get.target.*; var target = get.target.*;
const target_type = @as(ast.ExprType, target); const target_type = @as(ast.ExprType, target);
if (target_type != .Variable) { if (target_type != .Variable) {
std.debug.warn("Expected Variable as get target, got {}\n", target_type); std.debug.warn("Expected Variable as get target, got {}\n" , .{ target_type });
return CompileError.TypeError; return CompileError.TypeError;
} }
@ -475,7 +475,7 @@ pub const Analyzer = struct {
self.setErrContext("function {}", name); self.setErrContext("function {}", name);
var ret_type = self.resolveGlobalType(ctx, decl.return_type.lexeme); var ret_type = self.resolveGlobalType(ctx, decl.return_type.lexeme);
std.debug.warn("start analysis of fn {}, ret type: {}\n", decl.func_name.lexeme, ret_type); std.debug.warn("start analysis of fn {}, ret type: {}\n" , .{ decl.func_name.lexeme, ret_type });
var parameters = comp.TypeList.init(self.allocator); var parameters = comp.TypeList.init(self.allocator);
for (decl.params.toSlice()) |param| { for (decl.params.toSlice()) |param| {

View File

@ -41,7 +41,7 @@ pub const Codegen = struct {
.Bool => llvm.LLVMInt1Type(), .Bool => llvm.LLVMInt1Type(),
.OpaqueType => |val| { .OpaqueType => |val| {
std.debug.warn("Invalid return type: {}\n", val); std.debug.warn("Invalid return type: {}\n", .{val});
return CompileError.TypeError; return CompileError.TypeError;
}, },
@ -51,14 +51,14 @@ pub const Codegen = struct {
.Struct => unreachable, .Struct => unreachable,
.Enum => llvm.LLVMInt32Type(), .Enum => llvm.LLVMInt32Type(),
else => { else => {
std.debug.warn("Function {} is not a type\n", lex); std.debug.warn("Function {} is not a type\n", .{lex});
return CompileError.TypeError; return CompileError.TypeError;
}, },
}; };
}, },
else => { else => {
std.debug.warn("TODO handle {}\n", typ); std.debug.warn("TODO handle {}\n", .{typ});
return CompileError.TypeError; return CompileError.TypeError;
}, },
}; };
@ -82,7 +82,7 @@ pub const Codegen = struct {
.Struct => @panic("TODO handle struct"), .Struct => @panic("TODO handle struct"),
else => { else => {
std.debug.warn("Invalid get target: {}\n", @as(comp.SymbolType, sym.*)); std.debug.warn("Invalid get target: {}\n", .{@as(comp.SymbolType, sym.*)});
return CompileError.EmitError; return CompileError.EmitError;
}, },
} }
@ -144,7 +144,7 @@ pub const Codegen = struct {
.Or => llvm.LLVMBuildOr(builder, left, right, "ortmp"), .Or => llvm.LLVMBuildOr(builder, left, right, "ortmp"),
else => { else => {
std.debug.warn("Unexpected binary operator: '{}'\n", binary.op); std.debug.warn("Unexpected binary operator: '{}'\n", .{binary.op});
return CompileError.EmitError; return CompileError.EmitError;
}, },
}; };
@ -168,7 +168,7 @@ pub const Codegen = struct {
}, },
else => { else => {
std.debug.warn("Invalid get target: {}\n", @as(ast.ExprType, target)); std.debug.warn("Invalid get target: {}\n", .{@as(ast.ExprType, target)});
return CompileError.EmitError; return CompileError.EmitError;
}, },
} }
@ -179,7 +179,7 @@ pub const Codegen = struct {
var llvm_func = self.llvm_table.get(name); var llvm_func = self.llvm_table.get(name);
if (llvm_func == null) { if (llvm_func == null) {
std.debug.warn("Function '{}' not found\n", name); std.debug.warn("Function '{}' not found\n", .{name});
return CompileError.EmitError; return CompileError.EmitError;
} }
@ -218,7 +218,7 @@ pub const Codegen = struct {
var kv_opt = self.ctx.current_scope.?.meta_map.get(vari.lexeme); var kv_opt = self.ctx.current_scope.?.meta_map.get(vari.lexeme);
if (kv_opt == null) { if (kv_opt == null) {
std.debug.warn("variable {} not fully analyzed\n", vari.lexeme); std.debug.warn("variable {} not fully analyzed\n", .{vari.lexeme});
return CompileError.EmitError; return CompileError.EmitError;
} }
@ -226,7 +226,7 @@ pub const Codegen = struct {
// is coming from the scope or from the function // is coming from the scope or from the function
var metadata = kv_opt.?.value; var metadata = kv_opt.?.value;
std.debug.warn("!! LOAD FROM VAR META {}\n", @ptrToInt(metadata)); std.debug.warn("!! LOAD FROM VAR META {}\n", .{@ptrToInt(metadata)});
var buf = try self.allocator.alloc(u8, 512); var buf = try self.allocator.alloc(u8, 512);
errdefer self.allocator.free(buf); errdefer self.allocator.free(buf);
@ -255,14 +255,14 @@ pub const Codegen = struct {
}, },
else => { else => {
std.debug.warn("Got unexpected expr {}\n", @as(ast.ExprType, expr.*)); std.debug.warn("Got unexpected expr {}\n", .{@as(ast.ExprType, expr.*)});
return CompileError.EmitError; return CompileError.EmitError;
}, },
}; };
} }
fn emitStmt(self: *Codegen, builder: var, stmt: *ast.Stmt) anyerror!void { fn emitStmt(self: *Codegen, builder: var, stmt: *ast.Stmt) anyerror!void {
std.debug.warn("cgen: emitting stmt {}\n", @as(ast.StmtType, stmt.*)); std.debug.warn("cgen: emitting stmt {}\n", .{@as(ast.StmtType, stmt.*)});
switch (stmt.*) { switch (stmt.*) {
.Expr => |expr| _ = try self.emitExpr(builder, expr), .Expr => |expr| _ = try self.emitExpr(builder, expr),
@ -379,14 +379,14 @@ pub const Codegen = struct {
var_metadata.*.llvm_alloca = variable; var_metadata.*.llvm_alloca = variable;
std.debug.warn("!! DECL VAR {} => {}\n", @ptrToInt(var_metadata), variable); std.debug.warn("!! DECL VAR {} => {}\n", .{ @ptrToInt(var_metadata), variable });
var llvm_expr = try self.emitExpr(builder, vardecl.value); var llvm_expr = try self.emitExpr(builder, vardecl.value);
_ = llvm.LLVMBuildStore(builder, llvm_expr, variable); _ = llvm.LLVMBuildStore(builder, llvm_expr, variable);
}, },
else => { else => {
std.debug.warn("Got unexpected stmt {}\n", stmt.*); std.debug.warn("Got unexpected stmt {}\n", .{stmt.*});
return CompileError.EmitError; return CompileError.EmitError;
}, },
} }
@ -409,7 +409,7 @@ pub const Codegen = struct {
.FnDecl => |decl| { .FnDecl => |decl| {
const name = decl.func_name.lexeme; const name = decl.func_name.lexeme;
self.current_function_name = name; self.current_function_name = name;
std.debug.warn("cgen: genning function '{}'\n", name); std.debug.warn("cgen: genning function '{}'\n", .{name});
var fn_sym = self.getFnSymbol(name); var fn_sym = self.getFnSymbol(name);
@ -454,7 +454,7 @@ pub const Codegen = struct {
var alloca = llvm.LLVMBuildAlloca(builder, try self.typeToLLVM(param.typ), param_name_cstr.ptr); var alloca = llvm.LLVMBuildAlloca(builder, try self.typeToLLVM(param.typ), param_name_cstr.ptr);
std.debug.warn("SET PARAM LLVM ALLOCA {} to {}\n", param_node.name.lexeme, alloca); std.debug.warn("SET PARAM LLVM ALLOCA {} to {}\n", .{ param_node.name.lexeme, alloca });
param.llvm_alloca = alloca; param.llvm_alloca = alloca;
_ = llvm.LLVMBuildStore( _ = llvm.LLVMBuildStore(
@ -474,7 +474,7 @@ pub const Codegen = struct {
} }
self.ctx.dumpScope(); self.ctx.dumpScope();
std.debug.warn("cgen: generated function '{}'\n", name); std.debug.warn("cgen: generated function '{}'\n", .{name});
}, },
// NOTE: enums don't have specific llvm ir code generated for them // NOTE: enums don't have specific llvm ir code generated for them
@ -503,14 +503,14 @@ pub const Codegen = struct {
}, },
else => { else => {
std.debug.warn("TODO handle node type {}\n", @tagName(node.*)); std.debug.warn("TODO handle node type {}\n", .{@tagName(node.*)});
return; return;
}, },
} }
} }
pub fn gen(self: *Codegen, root: *ast.Node) !void { pub fn gen(self: *Codegen, root: *ast.Node) !void {
std.debug.warn("cgen: start gen\n"); std.debug.warn("cgen: start gen\n", .{});
_ = llvm.LLVMInitializeNativeTarget(); _ = llvm.LLVMInitializeNativeTarget();
var mod = llvm.LLVMModuleCreateWithName("awoo").?; var mod = llvm.LLVMModuleCreateWithName("awoo").?;
@ -525,16 +525,16 @@ pub const Codegen = struct {
defer llvm.LLVMDisposeMessage(err); defer llvm.LLVMDisposeMessage(err);
if (llvm.LLVMPrintModuleToFile(mod, "output.ll", &err) != 0) { if (llvm.LLVMPrintModuleToFile(mod, "output.ll", &err) != 0) {
std.debug.warn("error printing module to file: {}\n", sliceify(err)); std.debug.warn("error printing module to file: {}\n", .{sliceify(err)});
return CompileError.BackendError; return CompileError.BackendError;
} }
//if (llvm.LLVMWriteBitcodeToFile(mod, "awoo.bc") != 0) { //if (llvm.LLVMWriteBitcodeToFile(mod, "awoo.bc") != 0) {
// std.debug.warn("error writing bitcode to file: {}\n", sliceify(err)); // std.debug.warn("error writing bitcode to file: {}\n" , .{ sliceify(err) });
// return CompileError.BackendError; // return CompileError.BackendError;
//} //}
std.debug.warn("cgen: verify llvm module\n"); std.debug.warn("cgen: verify llvm module\n", .{});
_ = llvm.LLVMVerifyModule( _ = llvm.LLVMVerifyModule(
mod, mod,
llvm.LLVMVerifierFailureAction.LLVMAbortProcessAction, llvm.LLVMVerifierFailureAction.LLVMAbortProcessAction,
@ -549,7 +549,7 @@ pub const Codegen = struct {
var engine: llvm.LLVMExecutionEngineRef = undefined; var engine: llvm.LLVMExecutionEngineRef = undefined;
if (llvm.LLVMCreateExecutionEngineForModule(&engine, mod, &err) != 0) { if (llvm.LLVMCreateExecutionEngineForModule(&engine, mod, &err) != 0) {
std.debug.warn("failed to create execution engine: {}\n", sliceify(err)); std.debug.warn("failed to create execution engine: {}\n", .{sliceify(err)});
return CompileError.BackendError; return CompileError.BackendError;
} }
@ -569,9 +569,9 @@ pub const Codegen = struct {
var features = llvm.LLVMGetTargetMachineFeatureString(machine); var features = llvm.LLVMGetTargetMachineFeatureString(machine);
var triple = llvm.LLVMGetTargetMachineTriple(machine); var triple = llvm.LLVMGetTargetMachineTriple(machine);
std.debug.warn("target: {}\n", sliceify(desc)); std.debug.warn("target: {}\n", .{sliceify(desc)});
std.debug.warn("triple: {}\n", sliceify(triple)); std.debug.warn("triple: {}\n", .{sliceify(triple)});
std.debug.warn("features: {}\n", sliceify(features)); std.debug.warn("features: {}\n", .{sliceify(features)});
//if (llvm.LLVMTargetMachineEmitToFile( //if (llvm.LLVMTargetMachineEmitToFile(
// machine, // machine,
@ -580,7 +580,7 @@ pub const Codegen = struct {
// llvm.LLVMCodeGenFileType.LLVMAssemblyFile, // llvm.LLVMCodeGenFileType.LLVMAssemblyFile,
// &err, // &err,
//) != 0) { //) != 0) {
// std.debug.warn("failed to emit to assembly file: {}\n", sliceify(err)); // std.debug.warn("failed to emit to assembly file: {}\n" , .{ sliceify(err) });
// return CompileError.BackendError; // return CompileError.BackendError;
//} //}
@ -591,7 +591,7 @@ pub const Codegen = struct {
llvm.LLVMCodeGenFileType.LLVMObjectFile, llvm.LLVMCodeGenFileType.LLVMObjectFile,
&err, &err,
) != 0) { ) != 0) {
std.debug.warn("failed to emit to file: {}\n", sliceify(err)); std.debug.warn("failed to emit to file: {}\n", .{sliceify(err)});
return CompileError.BackendError; return CompileError.BackendError;
} }
} }

View File

@ -158,7 +158,7 @@ pub const VariableMetadata = struct {
typ: SymbolUnderlyingType, typ: SymbolUnderlyingType,
) !*VariableMetadata { ) !*VariableMetadata {
var meta = try allocator.create(VariableMetadata); var meta = try allocator.create(VariableMetadata);
std.debug.warn("VARMETA create from scope={}, meta={}\n", @ptrToInt(scope), @ptrToInt(meta)); std.debug.warn("VARMETA create from scope={}, meta={}\n" , .{ @ptrToInt(scope), @ptrToInt(meta) });
meta.* = VariableMetadata{ .typ = typ, .from_scope = scope, .using = .Scope }; meta.* = VariableMetadata{ .typ = typ, .from_scope = scope, .using = .Scope };
return meta; return meta;
} }
@ -169,7 +169,7 @@ pub const VariableMetadata = struct {
typ: SymbolUnderlyingType, typ: SymbolUnderlyingType,
) !*VariableMetadata { ) !*VariableMetadata {
var meta = try allocator.create(VariableMetadata); var meta = try allocator.create(VariableMetadata);
std.debug.warn("VARMETA create from fndecl={}, meta={}\n", @ptrToInt(func), @ptrToInt(meta)); std.debug.warn("VARMETA create from fndecl={}, meta={}\n" , .{ @ptrToInt(func), @ptrToInt(meta) });
meta.* = VariableMetadata{ .typ = typ, .from_function = func, .using = .Function }; meta.* = VariableMetadata{ .typ = typ, .from_function = func, .using = .Function };
return meta; return meta;
} }
@ -204,7 +204,7 @@ pub const CompilationContext = struct {
@panic("can't bump scope from null"); @panic("can't bump scope from null");
} }
std.debug.warn("==scope bump== '{}'\n", scope_id); std.debug.warn("==scope bump== '{}'\n" , .{ scope_id });
var child = try self.current_scope.?.createChild(scope_id); var child = try self.current_scope.?.createChild(scope_id);
self.current_scope = child; self.current_scope = child;
@ -212,7 +212,7 @@ pub const CompilationContext = struct {
/// Set a given scope as the current scope. /// Set a given scope as the current scope.
pub fn setScope(self: *@This(), scope: *Scope) void { pub fn setScope(self: *@This(), scope: *Scope) void {
std.debug.warn("==set== set scope to {}\n", scope.id); std.debug.warn("==set== set scope to {}\n" , .{ scope.id });
self.current_scope = scope; self.current_scope = scope;
} }
@ -349,7 +349,7 @@ pub const CompilationContext = struct {
) !*SymbolData { ) !*SymbolData {
var sym_kv = self.symbol_table.get(identifier); var sym_kv = self.symbol_table.get(identifier);
if (sym_kv == null) { if (sym_kv == null) {
std.debug.warn("Unknown {} '{}'\n", typ, identifier); std.debug.warn("Unknown {} '{}'\n" , .{ typ, identifier });
return CompilationError.TypeError; return CompilationError.TypeError;
} }
@ -357,7 +357,7 @@ pub const CompilationContext = struct {
var sym_typ = @as(SymbolType, value.*); var sym_typ = @as(SymbolType, value.*);
if (sym_typ != typ) { if (sym_typ != typ) {
std.debug.warn("Expected {}, got {}\n", sym_typ, typ); std.debug.warn("Expected {}, got {}\n" , .{ sym_typ, typ });
return CompilationError.TypeError; return CompilationError.TypeError;
} }
@ -417,7 +417,7 @@ pub const CompilationContext = struct {
} }
} }
std.debug.warn("Unknown name {}\n", name); std.debug.warn("Unknown name {}\n" , .{ name });
return CompilationError.UnknownName; return CompilationError.UnknownName;
} }

View File

@ -2,9 +2,9 @@ const std = @import("std");
pub fn report(line: usize, where: []const u8, ctx_opt: ?[]const u8, message: []const u8) void { pub fn report(line: usize, where: []const u8, ctx_opt: ?[]const u8, message: []const u8) void {
if (ctx_opt) |ctx| { if (ctx_opt) |ctx| {
std.debug.warn("[line {}] Error{} on {}: {}", line, where, ctx, message); std.debug.warn("[line {}] Error{} on {}: {}" , .{ line, where, ctx, message });
} else { } else {
std.debug.warn("[line {}] Error{}: {}", line, where, message); std.debug.warn("[line {}] Error{}: {}" , .{ line, where, message });
} }
} }
@ -14,11 +14,11 @@ pub fn reportN(line: usize, message: []const u8) void {
pub fn reportFmt(line: usize, ctx_opt: ?[]const u8, comptime fmt: []const u8, args: ...) void { pub fn reportFmt(line: usize, ctx_opt: ?[]const u8, comptime fmt: []const u8, args: ...) void {
if (ctx_opt) |ctx| { if (ctx_opt) |ctx| {
std.debug.warn("[line {}] Error on {}", line, ctx); std.debug.warn("[line {}] Error on {}" , .{ line, ctx });
} else { } else {
std.debug.warn("[line {}] Error", line); std.debug.warn("[line {}] Error" , .{ line });
} }
std.debug.warn(fmt, args); std.debug.warn(fmt, args);
std.debug.warn("\n"); std.debug.warn("\n" , .{ });
} }

View File

@ -32,7 +32,7 @@ pub fn run(allocator: *std.mem.Allocator, slice: []const u8) !Result {
if (tok.typ == .EOF) break; if (tok.typ == .EOF) break;
// TODO remove // TODO remove
std.debug.warn("{x}\n", tok); std.debug.warn("{x}\n" , .{ tok });
} }
} }
@ -47,13 +47,13 @@ pub fn run(allocator: *std.mem.Allocator, slice: []const u8) !Result {
var root = root_opt.?; var root = root_opt.?;
std.debug.warn("parse tree\n"); std.debug.warn("parse tree\n" , .{ });
printer.printNode(root, 0); printer.printNode(root, 0);
var solver = try analysis.Analyzer.init(allocator); var solver = try analysis.Analyzer.init(allocator);
var ctx = try solver.pass(root); var ctx = try solver.pass(root);
std.debug.warn("symbol table\n"); std.debug.warn("symbol table\n" , .{ });
printer.printContext(ctx); printer.printContext(ctx);
var cgen = codegen.llvm.Codegen.init(allocator, &ctx); var cgen = codegen.llvm.Codegen.init(allocator, &ctx);
@ -94,7 +94,7 @@ pub fn main() anyerror!void {
.ParseError, .ParseError,
.CompileError, .CompileError,
=> { => {
std.debug.warn("error: {}\n", result); std.debug.warn("error: {}\n" , .{ result });
std.os.exit(1); std.os.exit(1);
}, },
} }

View File

@ -107,15 +107,15 @@ pub const Parser = struct {
fn doError(self: *Parser, comptime fmt: []const u8, args: ...) ParseError { fn doError(self: *Parser, comptime fmt: []const u8, args: ...) ParseError {
self.hadError = true; self.hadError = true;
std.debug.warn("parser error at line {}", self.scanner.line); std.debug.warn("parser error at line {}" , .{ self.scanner.line });
if (self.err_ctx) |ctx| { if (self.err_ctx) |ctx| {
std.debug.warn(" on {}", ctx); std.debug.warn(" on {}" , .{ ctx });
} }
std.debug.warn("\n\t"); std.debug.warn("\n\t" , .{ });
std.debug.warn(fmt, args); std.debug.warn(fmt, args);
std.debug.warn("\n"); std.debug.warn("\n" , .{ });
return ParseError.CompileError; return ParseError.CompileError;
} }
@ -146,7 +146,7 @@ pub const Parser = struct {
} }
fn tokenError(self: *Parser, token: Token, msg: []const u8) ParseError { fn tokenError(self: *Parser, token: Token, msg: []const u8) ParseError {
std.debug.warn("ctx: '{}'\n", self.err_ctx); std.debug.warn("ctx: '{}'\n" , .{ self.err_ctx });
if (token.typ == .EOF) { if (token.typ == .EOF) {
ereport.report(token.line, " at end", self.err_ctx, msg); ereport.report(token.line, " at end", self.err_ctx, msg);
} else { } else {
@ -177,7 +177,7 @@ pub const Parser = struct {
} }
try self.tokens.append(token); try self.tokens.append(token);
std.debug.warn("skip to {}\n", token); std.debug.warn("skip to {}\n" , .{ token });
return token; return token;
} }