more fixes for latest zig

This commit is contained in:
Luna 2020-04-01 21:48:08 -03:00
parent a623d9b2ca
commit 049a388660
7 changed files with 63 additions and 67 deletions

View file

@ -25,7 +25,7 @@ pub const Analyzer = struct {
}; };
} }
fn setErrContext(self: *@This(), comptime fmt: ?[]const u8, args: ...) void { fn setErrContext(self: *@This(), comptime fmt: ?[]const u8, args: var) void {
if (fmt == null) { if (fmt == null) {
self.err_ctx = null; self.err_ctx = null;
return; return;
@ -76,7 +76,7 @@ pub const Analyzer = struct {
var sym = ctx.symbol_table.get(val); var sym = ctx.symbol_table.get(val);
if (sym == null) { if (sym == null) {
self.doError("Unknown type: '{}'", val); self.doError("Unknown type: '{}'", .{val});
return null; return null;
} }
@ -244,7 +244,7 @@ pub const Analyzer = struct {
const name = struc.name.lexeme; const name = struc.name.lexeme;
var typ = self.resolveGlobalType(ctx, name); var typ = self.resolveGlobalType(ctx, name);
if (typ == null) { if (typ == null) {
self.doError("Unknown struct name '{}'\n", name); self.doError("Unknown struct name '{}'\n", .{name});
return CompileError.TypeError; return CompileError.TypeError;
} }
@ -346,7 +346,7 @@ pub const Analyzer = struct {
.Assign => |assign| { .Assign => |assign| {
if (ctx.current_scope == null) { if (ctx.current_scope == null) {
self.doError("Can't assign without a scope"); self.doError("Can't assign without a scope", .{});
return CompileError.Invalid; return CompileError.Invalid;
} }
@ -355,7 +355,7 @@ pub const Analyzer = struct {
); );
if (var_type == null) { if (var_type == null) {
self.doError("Assign target variable not found"); self.doError("Assign target variable not found", .{});
return CompileError.Invalid; return CompileError.Invalid;
} }
@ -393,7 +393,7 @@ pub const Analyzer = struct {
var var_type = try self.resolveExprType(ctx, vardecl.value); var var_type = try self.resolveExprType(ctx, vardecl.value);
if (ctx.current_scope == null) { if (ctx.current_scope == null) {
self.doError("Can't declare without a scope"); self.doError("Can't declare without a scope", .{});
return CompileError.Invalid; return CompileError.Invalid;
} }
@ -462,7 +462,7 @@ pub const Analyzer = struct {
node: *ast.Node, node: *ast.Node,
) !void { ) !void {
self.setErrToken(null); self.setErrToken(null);
self.setErrContext(null); self.setErrContext(null, .{});
// always reset the contexts' current function // always reset the contexts' current function
ctx.cur_function = null; ctx.cur_function = null;
@ -472,7 +472,7 @@ pub const Analyzer = struct {
.FnDecl => |decl| { .FnDecl => |decl| {
self.setErrToken(decl.return_type); self.setErrToken(decl.return_type);
const name = decl.func_name.lexeme; const name = decl.func_name.lexeme;
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 });
@ -496,10 +496,10 @@ pub const Analyzer = struct {
try ctx.insertFn(decl, ret_type.?, parameters, scope); try ctx.insertFn(decl, ret_type.?, parameters, scope);
} else { } else {
if (ret_type != null) if (ret_type != null)
self.doError("Return type was not fully resolved"); self.doError("Return type was not fully resolved", .{});
if (parameters.len != decl.params.len) if (parameters.len != decl.params.len)
self.doError("Fully analyzed {} parameters, wanted {}", parameters.len, decl.params.len); self.doError("Fully analyzed {} parameters, wanted {}", .{ parameters.len, decl.params.len });
return CompileError.TypeError; return CompileError.TypeError;
} }
@ -520,7 +520,7 @@ pub const Analyzer = struct {
.Struct => |struc| { .Struct => |struc| {
self.setErrToken(struc.name); self.setErrToken(struc.name);
self.setErrContext("struct {}", struc.name.lexeme); self.setErrContext("struct {}", .{struc.name.lexeme});
var types = comp.TypeList.init(self.allocator); var types = comp.TypeList.init(self.allocator);
@ -543,7 +543,7 @@ pub const Analyzer = struct {
// TODO change enums to u32 // TODO change enums to u32
.Enum => |enu| { .Enum => |enu| {
self.setErrToken(enu.name); self.setErrToken(enu.name);
self.setErrContext("enum {}", enu.name.lexeme); self.setErrContext("enum {}", .{enu.name.lexeme});
try ctx.insertEnum(enu); try ctx.insertEnum(enu);
}, },
@ -551,7 +551,7 @@ pub const Analyzer = struct {
.ConstDecl => |constlist| { .ConstDecl => |constlist| {
for (constlist.toSlice()) |constdecl| { for (constlist.toSlice()) |constdecl| {
self.setErrToken(constdecl.name); self.setErrToken(constdecl.name);
self.setErrContext("const {}", constdecl.name.lexeme); self.setErrContext("const {}", .{constdecl.name.lexeme});
var expr_type = try self.resolveExprType(ctx, constdecl.expr); var expr_type = try self.resolveExprType(ctx, constdecl.expr);
try ctx.insertConst(constdecl, expr_type); try ctx.insertConst(constdecl, expr_type);
@ -559,7 +559,7 @@ pub const Analyzer = struct {
}, },
.Block => { .Block => {
self.doError("Block can't be found at root"); self.doError("Block can't be found at root", .{});
return CompileError.Invalid; return CompileError.Invalid;
}, },
} }

View file

@ -14,7 +14,7 @@ fn printIdent(ident: usize) void {
} }
} }
fn print(ident: usize, comptime fmt: []const u8, args: ...) void { fn print(ident: usize, comptime fmt: []const u8, args: var) void {
printIdent(ident); printIdent(ident);
std.debug.warn(fmt, args); std.debug.warn(fmt, args);
} }
@ -29,9 +29,9 @@ fn printBlock(ident: usize, block: var, endNewline: bool) void {
} }
if (endNewline) { if (endNewline) {
print(ident - 1, ")\n"); print(ident - 1, ")\n", .{});
} else { } else {
print(ident - 1, ")"); print(ident - 1, ")", .{});
} }
} }
@ -63,7 +63,7 @@ pub fn printNode(node: *const Node, ident: usize) void {
}, },
.ConstDecl => |consts| { .ConstDecl => |consts| {
print(ident, "(const (\n"); print(ident, "(const (\n", .{});
for (consts.toSlice()) |const_decl| { for (consts.toSlice()) |const_decl| {
print( print(
@ -76,11 +76,11 @@ pub fn printNode(node: *const Node, ident: usize) void {
std.debug.warn(")\n", .{}); std.debug.warn(")\n", .{});
} }
print(ident, "))\n"); print(ident, "))\n", .{});
}, },
.Enum => |decl| { .Enum => |decl| {
print(ident, "(enum {} (\n", decl.name.lexeme); print(ident, "(enum {} (\n", .{decl.name.lexeme});
for (decl.fields.toSlice()) |field| { for (decl.fields.toSlice()) |field| {
print( print(
@ -90,7 +90,7 @@ pub fn printNode(node: *const Node, ident: usize) void {
); );
} }
print(ident, "))\n"); print(ident, "))\n", .{});
}, },
.Root => { .Root => {
@ -100,15 +100,15 @@ pub fn printNode(node: *const Node, ident: usize) void {
}, },
.Struct => |struc| { .Struct => |struc| {
print(ident, "(struct {} (\n", struc.name.lexeme); print(ident, "(struct {} (\n", .{struc.name.lexeme});
for (struc.fields.toSlice()) |field| { for (struc.fields.toSlice()) |field| {
print(ident + 1, "({} {})\n", field.name.lexeme, field.typ.lexeme); print(ident + 1, "({} {})\n", .{ field.name.lexeme, field.typ.lexeme });
} }
print(ident, "))\n"); print(ident, "))\n", .{});
}, },
else => { else => {
print(ident, "unknown node: {}\n", node); print(ident, "unknown node: {}\n", .{node});
}, },
} }
} }
@ -330,7 +330,7 @@ pub fn printStmt(ident: usize, stmt: *const Stmt) void {
// very bad but be like that // very bad but be like that
fn retWithName(prefix: []const u8, inner: []const u8) []const u8 { fn retWithName(prefix: []const u8, inner: []const u8) []const u8 {
var ret_nam_buf = std.heap.direct_allocator.alloc(u8, 256) catch unreachable; var ret_nam_buf = std.heap.direct_allocator.alloc(u8, 256) catch unreachable;
return std.fmt.bufPrint(ret_nam_buf[0..], "{}({})", prefix, inner) catch unreachable; return std.fmt.bufPrint(ret_nam_buf[0..], "{}({})", .{ prefix, inner }) catch unreachable;
} }
fn prettyType(typ: SymbolUnderlyingType) []const u8 { fn prettyType(typ: SymbolUnderlyingType) []const u8 {
@ -347,11 +347,11 @@ fn prettyType(typ: SymbolUnderlyingType) []const u8 {
} }
pub fn printScope(scope: *Scope, ident: usize) void { pub fn printScope(scope: *Scope, ident: usize) void {
print(ident, "scope '{}' at addr {}\n", scope.id, @ptrToInt(scope)); print(ident, "scope '{}' at addr {}\n", .{ scope.id, @ptrToInt(scope) });
var it = scope.env.iterator(); var it = scope.env.iterator();
while (it.next()) |kv| { while (it.next()) |kv| {
print(ident + 1, "sym: {}, typ: {}\n", kv.key, prettyType(kv.value)); print(ident + 1, "sym: {}, typ: {}\n", .{ kv.key, prettyType(kv.value) });
} }
for (scope.children.toSlice()) |child| { for (scope.children.toSlice()) |child| {
@ -365,19 +365,17 @@ pub fn printContext(ctx: CompilationContext) void {
while (it.next()) |kv| { while (it.next()) |kv| {
switch (kv.value.*) { switch (kv.value.*) {
.Function => |fn_sym| { .Function => |fn_sym| {
std.debug.warn( std.debug.warn("function {} returns {}\n", .{
"function {} returns {}\n",
kv.key, kv.key,
prettyType(fn_sym.return_type), prettyType(fn_sym.return_type),
); });
for (fn_sym.decl.params.toSlice()) |param| { for (fn_sym.decl.params.toSlice()) |param| {
var param_kv = fn_sym.parameters.get(param.name.lexeme).?; var param_kv = fn_sym.parameters.get(param.name.lexeme).?;
std.debug.warn( std.debug.warn("\tparameter {} typ {}\n", .{
"\tparameter {} typ {}\n",
param_kv.key, param_kv.key,
prettyType(param_kv.value.typ), prettyType(param_kv.value.typ),
); });
} }
// go through scopes // go through scopes

View file

@ -5,7 +5,7 @@ const comp = @import("../comp_ctx.zig");
const CompileError = @import("../codegen.zig").CompileError; const CompileError = @import("../codegen.zig").CompileError;
fn sliceify(non_slice: ?[*:0]const u8) []const u8 { fn sliceify(non_slice: ?[*:0]const u8) []const u8 {
return non_slice.?[0..std.mem.len(u8, non_slice.?)]; return non_slice.?[0..std.mem.len(non_slice.?)];
} }
fn mkLLVMBool(val: bool) llvm.LLVMValueRef { fn mkLLVMBool(val: bool) llvm.LLVMValueRef {
@ -71,11 +71,10 @@ pub const Codegen = struct {
.Enum => |map| { .Enum => |map| {
var val = map.get(get.name.lexeme); var val = map.get(get.name.lexeme);
if (val == null) { if (val == null) {
std.debug.warn( std.debug.warn("enum {} does not have field {}\n", .{
"enum {} does not have field {}\n",
vari.lexeme, vari.lexeme,
get.name.lexeme, get.name.lexeme,
); });
} }
return llvm.LLVMConstInt(llvm.LLVMInt32Type(), val.?.value, 1); return llvm.LLVMConstInt(llvm.LLVMInt32Type(), val.?.value, 1);
}, },

View file

@ -224,11 +224,10 @@ pub const CompilationContext = struct {
const parent_id: ?[]const u8 = if (self.current_scope.?.parent == null) null else self.current_scope.?.parent.?.id; const parent_id: ?[]const u8 = if (self.current_scope.?.parent == null) null else self.current_scope.?.parent.?.id;
std.debug.warn( std.debug.warn("==scope dump== {} to {}\n", .{
"==scope dump== {} to {}\n",
self.current_scope.?.id, self.current_scope.?.id,
parent_id, parent_id,
); });
self.current_scope = self.current_scope.?.parent; self.current_scope = self.current_scope.?.parent;
} }

View file

@ -12,7 +12,7 @@ pub fn reportN(line: usize, message: []const u8) void {
report(line, "", message); report(line, "", message);
} }
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: var) 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 {

View file

@ -69,7 +69,7 @@ pub fn run(allocator: *std.mem.Allocator, slice: []const u8) !Result {
} }
pub fn main() anyerror!void { pub fn main() anyerror!void {
const allocator = std.heap.direct_allocator; const allocator = std.heap.page_allocator;
var args_it = std.process.args(); var args_it = std.process.args();
_ = args_it.skip(); _ = args_it.skip();

View file

@ -94,7 +94,7 @@ pub const Parser = struct {
self.tokens.deinit(); self.tokens.deinit();
} }
fn setErrContext(self: *Parser, comptime fmt: ?[]const u8, args: ...) void { fn setErrContext(self: *Parser, comptime fmt: ?[]const u8, args: var) void {
if (fmt == null) { if (fmt == null) {
self.err_ctx = null; self.err_ctx = null;
return; return;
@ -104,7 +104,7 @@ pub const Parser = struct {
self.err_ctx = std.fmt.bufPrint(buf, fmt.?, args) catch unreachable; self.err_ctx = std.fmt.bufPrint(buf, fmt.?, args) catch unreachable;
} }
fn doError(self: *Parser, comptime fmt: []const u8, args: ...) ParseError { fn doError(self: *Parser, comptime fmt: []const u8, args: var) 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});
@ -150,7 +150,10 @@ pub const Parser = struct {
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 {
ereport.reportFmt(token.line, self.err_ctx, " at '{}': {}", token.lexeme, msg); ereport.reportFmt(token.line, self.err_ctx, " at '{}': {}", .{
token.lexeme,
msg,
});
} }
return ParseError.CompileError; return ParseError.CompileError;
@ -203,12 +206,10 @@ pub const Parser = struct {
// TODO maybe this could be entirely comptime? // TODO maybe this could be entirely comptime?
var buf_main: [1000]u8 = undefined; var buf_main: [1000]u8 = undefined;
var buf = try std.fmt.bufPrint( var buf = try std.fmt.bufPrint(&buf_main, "expected {}, got {}", .{
buf_main[0..],
"expected {}, got {}",
ttype, ttype,
self.peek().typ, self.peek().typ,
); });
return self.tokenError(self.peek(), buf); return self.tokenError(self.peek(), buf);
} }
@ -466,7 +467,7 @@ pub const Parser = struct {
break :blk orig_name; break :blk orig_name;
}; };
self.setErrContext("function {}", name.lexeme); self.setErrContext("function {}", .{name.lexeme});
_ = try self.consumeSingle(.LeftParen); _ = try self.consumeSingle(.LeftParen);
@ -529,14 +530,14 @@ pub const Parser = struct {
var consts = ast.ConstList.init(self.allocator); var consts = ast.ConstList.init(self.allocator);
errdefer consts.deinit(); errdefer consts.deinit();
self.setErrContext("const"); self.setErrContext("const", .{});
_ = try self.consumeSingle(.Const); _ = try self.consumeSingle(.Const);
_ = try self.consumeSingle(.LeftParen); _ = try self.consumeSingle(.LeftParen);
while (self.peek().typ != .RightParen) { while (self.peek().typ != .RightParen) {
const const_name = try self.consumeSingle(.Identifier); const const_name = try self.consumeSingle(.Identifier);
self.setErrContext("const {}", const_name); self.setErrContext("const {}", .{const_name});
_ = try self.consumeSingle(.Equal); _ = try self.consumeSingle(.Equal);
@ -558,18 +559,18 @@ pub const Parser = struct {
fn parseStructDecl(self: *@This()) !*Node { fn parseStructDecl(self: *@This()) !*Node {
var fields = ast.FieldList.init(self.allocator); var fields = ast.FieldList.init(self.allocator);
errdefer fields.deinit(); errdefer fields.deinit();
self.setErrContext("struct"); self.setErrContext("struct", .{});
_ = try self.consumeSingle(.Struct); _ = try self.consumeSingle(.Struct);
var name = try self.consumeSingle(.Identifier); var name = try self.consumeSingle(.Identifier);
self.setErrContext("struct {}", name); self.setErrContext("struct {}", .{name});
_ = try self.consumeSingle(.LeftBrace); _ = try self.consumeSingle(.LeftBrace);
while (!self.check(.RightBrace)) { while (!self.check(.RightBrace)) {
const field_name = try self.consumeSingle(.Identifier); const field_name = try self.consumeSingle(.Identifier);
self.setErrContext("struct {} field {}", name, field_name); self.setErrContext("struct {} field {}", .{ name, field_name });
const field_type = try self.consumeSingle(.Identifier); const field_type = try self.consumeSingle(.Identifier);
try fields.append(ast.StructField{ try fields.append(ast.StructField{
@ -587,12 +588,12 @@ pub const Parser = struct {
var fields = ast.TokenList.init(self.allocator); var fields = ast.TokenList.init(self.allocator);
errdefer fields.deinit(); errdefer fields.deinit();
self.setErrContext("enum"); self.setErrContext("enum", .{});
_ = try self.consumeSingle(.Enum); _ = try self.consumeSingle(.Enum);
const name = try self.consumeSingle(.Identifier); const name = try self.consumeSingle(.Identifier);
self.setErrContext("enum {}", name); self.setErrContext("enum {}", .{name});
_ = try self.consumeSingle(.LeftBrace); _ = try self.consumeSingle(.LeftBrace);
@ -606,7 +607,7 @@ pub const Parser = struct {
} }
fn parseTopDecl(self: *@This()) !*Node { fn parseTopDecl(self: *@This()) !*Node {
self.setErrContext(null); self.setErrContext(null, .{});
return switch (self.peek().typ) { return switch (self.peek().typ) {
.Fn => try self.parseFnDecl(), .Fn => try self.parseFnDecl(),
@ -615,7 +616,7 @@ pub const Parser = struct {
.Enum => try self.parseEnumDecl(), .Enum => try self.parseEnumDecl(),
else => |typ| blk: { else => |typ| blk: {
return self.doError("expected Fn, Const, Struct, got {}\n", typ); return self.doError("expected Fn, Const, Struct, got {}\n", .{typ});
}, },
}; };
} }
@ -837,7 +838,7 @@ pub const Parser = struct {
switch (op_tok.typ) { switch (op_tok.typ) {
// TODO remove .ColonEqual from language // TODO remove .ColonEqual from language
.ColonEqual => { .ColonEqual => {
return self.doError("can not initialize struct field"); return self.doError("can not initialize struct field", .{});
}, },
.Equal => return try self.mkSet(get.target, get.name, value), .Equal => return try self.mkSet(get.target, get.name, value),
@ -855,7 +856,7 @@ pub const Parser = struct {
}, },
else => |expr_typ| { else => |expr_typ| {
return self.doError("Invalid assignment target {}", expr_typ); return self.doError("Invalid assignment target {}", .{expr_typ});
}, },
} }
} }
@ -1028,7 +1029,7 @@ pub const Parser = struct {
// for this to work properly, <expr> must be Variable, since its a type. // for this to work properly, <expr> must be Variable, since its a type.
const expr_type = @as(ast.ExprType, expr.*); const expr_type = @as(ast.ExprType, expr.*);
if (expr_type != .Variable) { if (expr_type != .Variable) {
return self.doError("Expected variable for struct type, got {}", expr_type); return self.doError("Expected variable for struct type, got {}", .{expr_type});
} }
var inits = ast.StructInitList.init(self.allocator); var inits = ast.StructInitList.init(self.allocator);
@ -1065,11 +1066,10 @@ pub const Parser = struct {
// parseInt(i64) on the catch block of parseInt(i32) // parseInt(i64) on the catch block of parseInt(i32)
var i32_num_opt: ?i32 = std.fmt.parseInt(i32, lexeme, 10) catch null; var i32_num_opt: ?i32 = std.fmt.parseInt(i32, lexeme, 10) catch null;
var i64_num: i64 = std.fmt.parseInt(i64, lexeme, 10) catch |err| { var i64_num: i64 = std.fmt.parseInt(i64, lexeme, 10) catch |err| {
return self.doError( return self.doError("Invalid integer (not 32bit or 64bit) '{}': {}", .{
"Invalid integer (not 32bit or 64bit) '{}': {}",
lexeme, lexeme,
err, err,
); });
}; };
if (i32_num_opt) |i32_num| { if (i32_num_opt) |i32_num| {
@ -1124,7 +1124,7 @@ pub const Parser = struct {
}, },
else => blk: { else => blk: {
return self.doError("expected literal, got {}", curtype); return self.doError("expected literal, got {}", .{curtype});
}, },
}; };