From cf80bb7f1b6fb4ee1d08d3d6850966b4951274b5 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Mon, 12 Aug 2024 00:44:26 -0500 Subject: Calling functions and Function args now works. You can also import functions now. --- src/codegen.zig | 144 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 110 insertions(+), 34 deletions(-) (limited to 'src/codegen.zig') diff --git a/src/codegen.zig b/src/codegen.zig index 59ef03b..3922a75 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -12,10 +12,12 @@ const CodegenError = error{ OutOfMemory, }; -fn toLLVMtype(typ: parse.TypeIdent, sym: *symb.SymbolTable) types.LLVMTypeRef { +fn toLLVMtype(typ: parse.TypeIdent, sym: *symb.SymbolTable, expr: ?parse.NodeExpr) types.LLVMTypeRef { + _ = expr; if (sym.getType(typ)) |t| { return switch (t) { .Integer => core.LLVMInt32Type(), + .String => core.LLVMPointerType(core.LLVMInt8Type(), 0), .Void => core.LLVMVoidType(), else => core.LLVMVoidType(), }; @@ -32,15 +34,16 @@ pub const Generator = struct { currentFunc: ?types.LLVMValueRef, currentFuncIsVoid: bool, references: std.AutoHashMap(u32, types.LLVMValueRef), + stringId: u32, - pub fn init(allocator: std.mem.Allocator, root: parse.NodeStmt) Generator { + pub fn init(allocator: std.mem.Allocator, root: parse.NodeStmt, filename: [*:0]const u8) Generator { _ = target.LLVMInitializeNativeTarget(); _ = target.LLVMInitializeNativeAsmPrinter(); _ = target.LLVMInitializeNativeAsmParser(); const context = core.LLVMContextCreate(); const builder = core.LLVMCreateBuilderInContext(context); - const module = core.LLVMModuleCreateWithNameInContext("_calico_start", context); + const module = core.LLVMModuleCreateWithNameInContext(filename, context); return .{ .root = root, @@ -51,6 +54,7 @@ pub const Generator = struct { .currentFunc = null, .currentFuncIsVoid = false, .references = std.AutoHashMap(u32, types.LLVMValueRef).init(allocator), + .stringId = 0, }; } @@ -65,7 +69,7 @@ pub const Generator = struct { fn genExit(self: *Generator, exit: parse.NodeExit) !void { const expr = exit; - const val = self.genExpr(expr); + const val = try self.genExpr(expr); _ = core.LLVMBuildRet(self.builder, val); } @@ -74,8 +78,8 @@ pub const Generator = struct { const table = stmt.symtable; const symbol = table.getValue(nodeVar.ident.ident).?; - const value = self.genExpr(nodeVar.expr); - const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ.?, table).?, nodeVar.ident.ident); + const value = try self.genExpr(nodeVar.expr); + const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ.?, table, nodeVar.expr).?, nodeVar.ident.ident); _ = core.LLVMBuildStore(self.builder, value, ptr); try self.references.put(symbol.id, ptr); } @@ -85,8 +89,8 @@ pub const Generator = struct { const table = stmt.symtable; const symbol = table.getValue(nodeVar.ident.ident).?; - const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ.?, table), nodeVar.ident.ident); - const value = self.genExpr(nodeVar.expr); + const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ.?, table, nodeVar.expr), nodeVar.ident.ident); + const value = try self.genExpr(nodeVar.expr); _ = core.LLVMBuildStore(self.builder, value, ptr); try self.references.put(symbol.id, ptr); } @@ -115,12 +119,12 @@ pub const Generator = struct { } fn genAssign(self: *Generator, stmt: parse.NodeStmt) !void { - std.debug.print("assign\n", .{}); + // std.debug.print("assign\n", .{}); const table = stmt.symtable; const symbol = table.get(stmt.kind.assignVar.ident.ident).?; if (!symbol.Value.mut) return CodegenError.Immutable; const ptr = self.references.get(symbol.Value.id).?; - const value = self.genExpr(stmt.kind.assignVar.expr); + const value = try self.genExpr(stmt.kind.assignVar.expr); _ = core.LLVMBuildStore(self.builder, value, ptr); } @@ -129,31 +133,48 @@ pub const Generator = struct { } fn genFunc(self: *Generator, stmt: parse.NodeStmt) !void { + self.references.clearAndFree(); const fun = stmt.kind.function; - const table = stmt.symtable; - const block = fun.block; - const codeSlice = block.kind.block; + var table: *symb.SymbolTable = stmt.symtable; + var block: *parse.NodeStmt = undefined; + var codeSlice: []const parse.NodeStmt = undefined; + if (fun.block != null) { + table = fun.block.?.symtable; + block = fun.block.?; + codeSlice = block.kind.block; + } const funcName: [*:0]const u8 = try self.allocator.dupeZ(u8, fun.ident.ident); - const retType = toLLVMtype(fun.retType.?, table); - var params = [0]types.LLVMTypeRef{}; - const funcType = core.LLVMFunctionType(retType, @ptrCast(¶ms), 0, 0); - const func = core.LLVMAddFunction(self.module, funcName, funcType); - self.currentFunc = func; - self.currentFuncIsVoid = switch (table.getType(fun.retType.?).?) { - .Void => true, - else => false, - }; + const retType = toLLVMtype(fun.retType.?, table, null); + var params = std.ArrayList(types.LLVMTypeRef).init(self.allocator); + for (fun.args) |arg| { + try params.append(toLLVMtype(arg.typ, table, null)); + } - const function: types.LLVMValueRef = self.currentFunc.?; - const codeBlock = core.LLVMAppendBasicBlockInContext(self.context, function, "entry"); - core.LLVMPositionBuilderAtEnd(self.builder, codeBlock); - const bodyTable = block.symtable; - _ = bodyTable; - //TODO: codegen for args + const funcType = core.LLVMFunctionType(retType, @ptrCast(params.items), @intCast(params.items.len), 0); + const func = core.LLVMAddFunction(self.module, funcName, funcType); + for (fun.args, 0..) |arg, i| { + const symbol = table.get(arg.ident).?; + const ptr: types.LLVMValueRef = core.LLVMGetParam(func, @intCast(i)); + try self.references.put(symbol.Value.id, ptr); + } - try self.genBlock(codeSlice); - _ = if (self.currentFuncIsVoid) core.LLVMBuildRetVoid(self.builder); + if (fun.block != null) { + self.currentFunc = func; + self.currentFuncIsVoid = switch (table.getType(fun.retType.?).?) { + .Void => true, + else => false, + }; + + const function: types.LLVMValueRef = func; + const codeBlock = core.LLVMAppendBasicBlockInContext(self.context, function, "entry"); + core.LLVMPositionBuilderAtEnd(self.builder, codeBlock); + const bodyTable = block.symtable; + _ = bodyTable; + + try self.genBlock(codeSlice); + _ = if (self.currentFuncIsVoid) core.LLVMBuildRetVoid(self.builder); + } } fn genStmt(self: *Generator, stmt: parse.NodeStmt) !void { @@ -163,19 +184,74 @@ pub const Generator = struct { .defValue => self.genValue(stmt), .defVar => self.genVar(stmt), .assignVar => self.genAssign(stmt), + .expr => |expression| { + _ = try self.genExpr(expression); + }, + else => {}, }; } - fn genExpr(self: *Generator, expr: parse.NodeExpr) types.LLVMValueRef { + fn genExpr(self: *Generator, expr: parse.NodeExpr) !types.LLVMValueRef { return switch (expr.kind) { - .ident => blk: { + .ident => |id| blk: { + // std.debug.print("getValue({s})\n", .{id.ident}); const table = expr.symtable; - const symbol = table.getValue(expr.kind.ident.ident).?; + + // std.debug.print("\n\nEXPERTABLE\n\n", .{}); + // var iterTable = table.scope.?.symbs.iterator(); + // while (iterTable.next()) |entry| { + // // std.debug.print("{s} -> {any}\n", .{ entry.key_ptr.*, entry.value_ptr.* }); + // } + // std.debug.print("\n\nEXPERTABLE\n\n", .{}); + const symbol = table.getValue(id.ident).?; const ptr = self.references.get(symbol.id).?; - break :blk core.LLVMBuildLoad2(self.builder, toLLVMtype(expr.typ.?, table), ptr, ""); + if (core.LLVMIsAArgument(ptr)) |_| + break :blk ptr; + + break :blk core.LLVMBuildLoad2(self.builder, toLLVMtype(expr.typ.?, table, expr), ptr, ""); }, .intLit => |int| core.LLVMConstInt(core.LLVMInt32TypeInContext(self.context), @intCast(int.intLit), 1), + .stringLit => |str| blk: { + const vref = core.LLVMAddGlobal( + self.module, + core.LLVMArrayType(core.LLVMInt8Type(), @intCast(str.stringLit.len + 1)), + try self.allocator.dupeZ(u8, try std.fmt.allocPrint( + self.allocator, + ".str.{d}", + .{self.stringId}, + )), + ); + self.stringId += 1; + const sref = core.LLVMConstString(try self.allocator.dupeZ(u8, str.stringLit), @intCast(str.stringLit.len), 0); + core.LLVMSetInitializer(vref, sref); + core.LLVMSetGlobalConstant(vref, 1); + core.LLVMSetLinkage(vref, .LLVMPrivateLinkage); + core.LLVMSetUnnamedAddr(vref, 1); + break :blk vref; + }, + + .call => |call| blk: { + const ident = try self.allocator.dupeZ(u8, call.ident.ident); + const function = core.LLVMGetNamedFunction(self.module, ident); + var args = std.ArrayList(types.LLVMValueRef).init(self.allocator); + for (call.args) |arg| + try args.append(try self.genExpr(arg)); + const funcType = core.LLVMGlobalGetValueType(function); + // std.debug.print("FUNCTYPE: {s}\n", .{call.ident.ident}); + + const llvmCall = core.LLVMBuildCall2( + self.builder, + funcType, + function, + @ptrCast(args.items), + @intCast(call.args.len), + ident, + ); + // std.debug.print("CALL\n", .{}); + + break :blk llvmCall; + }, }; } -- cgit v1.2.3 From b1ad4a1c280d25f92fdb1103edf2faa5e3e1daac Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Tue, 13 Aug 2024 17:28:34 -0500 Subject: Added type checking on function calls --- examples/helloWorld.nya | 6 ++++++ examples/test.nya | 6 ------ src/codegen.zig | 8 +++++++- src/main.zig | 2 +- src/symtable.zig | 36 +++++++++++++++++++++++------------- 5 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 examples/helloWorld.nya delete mode 100644 examples/test.nya (limited to 'src/codegen.zig') diff --git a/examples/helloWorld.nya b/examples/helloWorld.nya new file mode 100644 index 0000000..0b8c59b --- /dev/null +++ b/examples/helloWorld.nya @@ -0,0 +1,6 @@ +import fn puts(str: [u8]) -> i32; + +fn main(argc: i32) -> i32 { + puts("Hello World!"); + return 0; +} diff --git a/examples/test.nya b/examples/test.nya deleted file mode 100644 index 0b8c59b..0000000 --- a/examples/test.nya +++ /dev/null @@ -1,6 +0,0 @@ -import fn puts(str: [u8]) -> i32; - -fn main(argc: i32) -> i32 { - puts("Hello World!"); - return 0; -} diff --git a/src/codegen.zig b/src/codegen.zig index 3922a75..cdcffdd 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -10,6 +10,7 @@ const types = llvm.types; const CodegenError = error{ Immutable, OutOfMemory, + IncorrectType, }; fn toLLVMtype(typ: parse.TypeIdent, sym: *symb.SymbolTable, expr: ?parse.NodeExpr) types.LLVMTypeRef { @@ -232,11 +233,16 @@ pub const Generator = struct { }, .call => |call| blk: { + std.debug.print("Function {s} requested\n", .{call.ident.ident}); + + const functype = expr.symtable.getValue(call.ident.ident).?.typ.Function; const ident = try self.allocator.dupeZ(u8, call.ident.ident); const function = core.LLVMGetNamedFunction(self.module, ident); var args = std.ArrayList(types.LLVMValueRef).init(self.allocator); - for (call.args) |arg| + for (call.args, functype.input) |arg, intype| { + if (!std.meta.eql(expr.symtable.getType(arg.typ.?).?, intype)) return CodegenError.IncorrectType; try args.append(try self.genExpr(arg)); + } const funcType = core.LLVMGlobalGetValueType(function); // std.debug.print("FUNCTYPE: {s}\n", .{call.ident.ident}); diff --git a/src/main.zig b/src/main.zig index a326685..05dde7d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -67,7 +67,7 @@ pub fn main() !void { var generator = gen.Generator.init(arena.allocator(), tree, @ptrCast(fname)); defer generator.deinit(); const code = try generator.generate(); - std.debug.print("{s}\n", .{code}); + // std.debug.print("{s}\n", .{code}); try outWriter.writeAll(code); const binFile = try getFileName(allocator, out_name, ""); diff --git a/src/symtable.zig b/src/symtable.zig index 6ddee01..5da6081 100644 --- a/src/symtable.zig +++ b/src/symtable.zig @@ -23,10 +23,21 @@ pub const SymbType = union(enum) { pub fn toSymb(self: SymbType) Symbol { return Symbol{ .Type = self }; } - pub fn toString(self: SymbType) []const u8 { + pub fn toString(self: SymbType, allocator: std.mem.Allocator) ![]const u8 { return switch (self) { .Integer => "i32", .Character => "u8", + .String => "[u8]", + .Function => |fun| blk: { + const output = try fun.output.toString(allocator); + var argstring: []const u8 = ""; + if (fun.input.len != 0) { + argstring = try fun.input[0].toString(allocator); + for (1..fun.input.len) |i| + argstring = try std.mem.join(allocator, ", ", &[_][]const u8{ argstring, try fun.input[i].toString(allocator) }); + } + break :blk try std.fmt.allocPrint(allocator, "fn({s})->{s}", .{ argstring, output }); + }, else => "void", }; } @@ -105,7 +116,7 @@ pub const SymbolTable = struct { } pub fn get(self: SymbolTable, ident: []const u8) ?Symbol { - if (self.scope) |scope| return scope.symbs.get(ident); + if (self.scope) |scope| return scope.symbs.get(ident) orelse if (self.parent()) |par| par.get(ident) else null; return null; } @@ -193,20 +204,12 @@ pub const Populator = struct { fun.args, fun.retType, ); + if (!try table.insert(fun.ident.ident, symbol)) return error.FailedToInsert; + std.debug.print("Function {s} inserted\n", .{fun.ident.ident}); if (fun.block == null) return; - // var iter = fun.block.?.symtable.scope.?.symbs.iterator(); - // while (iter.next()) |val| { - // // std.debug.print("{s}\n", .{val.key_ptr.*}); - // } - const block = fun.block.?.asNode(); try self.populateSymtable(&block); - - // var iterTable = bodyTable.scope.?.symbs.iterator(); - // while (iterTable.next()) |entry| { - // // std.debug.print("{s} -> {any}\n", .{ entry.key_ptr.*, entry.value_ptr.* }); - // } }, else => {}, @@ -243,8 +246,15 @@ pub const Populator = struct { }, }; const id = self.reserveId(); + var argstring: []const u8 = ""; + if (input.len != 0) { + argstring = try input[0].toString(self.allocator); + for (1..input.len) |i| + argstring = try std.mem.join(self.allocator, ",", &[_][]const u8{ argstring, try input[i].toString(self.allocator) }); + } - const name = try std.fmt.allocPrint(self.allocator, "func_{d}", .{id}); + const name = try std.fmt.allocPrint(self.allocator, "fn({s})->{s}", .{ argstring, try output.toString(self.allocator) }); + std.debug.print("Function type => \"{s}\"\n", .{name}); _ = try table.insert(name, typ.toSymb()); return Symbol{ -- cgit v1.2.3 From 760a9246618862b56bafe3dd5d95a77fdd668a6c Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Tue, 13 Aug 2024 20:36:31 -0500 Subject: Unit tests now pass --- build.zig | 54 ++++++++----------- examples/helloWorld.nya | 2 +- src/codegen.zig | 140 +++++++++++++++++++++++++++++++++--------------- src/parser.zig | 27 +++++----- src/symtable.zig | 9 ++++ src/tokenize.zig | 23 +++++--- 6 files changed, 161 insertions(+), 94 deletions(-) (limited to 'src/codegen.zig') diff --git a/build.zig b/build.zig index e8b9c5e..4606ade 100644 --- a/build.zig +++ b/build.zig @@ -13,7 +13,8 @@ pub fn build(b: *std.Build) !void { }); const llvm = b.dependency("llvm-zig", .{}); - exe.root_module.addImport("llvm", llvm.module("llvm")); + _ = try b.modules.put("llvm", llvm.module("llvm")); + exe.root_module.addImport("llvm", b.modules.get("llvm").?); b.installArtifact(exe); @@ -28,38 +29,29 @@ pub fn build(b: *std.Build) !void { const run_step = b.step("run", "Run the compiler"); run_step.dependOn(&run_cmd.step); - const exe_unit_tests = b.addTest(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }); - - const token_unit_tests = b.addTest(.{ - .root_source_file = b.path("src/tokenize.zig"), - .target = target, - .optimize = optimize, - }); - - const parse_unit_tests = b.addTest(.{ - .root_source_file = b.path("src/parser.zig"), - .target = target, - .optimize = optimize, - }); + const test_step = b.step("test", "Run unit tests"); + for ([_][]const u8{ + "src/main.zig", + "src/tokenize.zig", + "src/parser.zig", + "src/codegen.zig", + }) |file| + unit_test(b, target, optimize, test_step, file); +} - const codegen_unit_tests = b.addTest(.{ - .root_source_file = b.path("src/codegen.zig"), +fn unit_test( + b: *std.Build, + target: std.Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, + test_step: *std.Build.Step, + fname: []const u8, +) void { + const unit = b.addTest(.{ + .root_source_file = b.path(fname), .target = target, .optimize = optimize, }); - - const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); - const run_token_unit_tests = b.addRunArtifact(token_unit_tests); - const run_parse_unit_tests = b.addRunArtifact(parse_unit_tests); - const run_codegen_unit_tests = b.addRunArtifact(codegen_unit_tests); - - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_exe_unit_tests.step); - test_step.dependOn(&run_token_unit_tests.step); - test_step.dependOn(&run_parse_unit_tests.step); - test_step.dependOn(&run_codegen_unit_tests.step); + const unit_tests = b.addRunArtifact(unit); + test_step.dependOn(&unit_tests.step); + unit.root_module.addImport("llvm", b.modules.get("llvm").?); } diff --git a/examples/helloWorld.nya b/examples/helloWorld.nya index 0b8c59b..160d9d2 100644 --- a/examples/helloWorld.nya +++ b/examples/helloWorld.nya @@ -1,6 +1,6 @@ import fn puts(str: [u8]) -> i32; -fn main(argc: i32) -> i32 { +fn main() -> i32 { puts("Hello World!"); return 0; } diff --git a/src/codegen.zig b/src/codegen.zig index cdcffdd..c1fac76 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -80,7 +80,7 @@ pub const Generator = struct { const table = stmt.symtable; const symbol = table.getValue(nodeVar.ident.ident).?; const value = try self.genExpr(nodeVar.expr); - const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ.?, table, nodeVar.expr).?, nodeVar.ident.ident); + const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ orelse try nodeVar.expr.symtable.getValue(nodeVar.ident.ident).?.typ.toTypeIdent(self.allocator), table, nodeVar.expr).?, nodeVar.ident.ident); _ = core.LLVMBuildStore(self.builder, value, ptr); try self.references.put(symbol.id, ptr); } @@ -210,7 +210,16 @@ pub const Generator = struct { if (core.LLVMIsAArgument(ptr)) |_| break :blk ptr; - break :blk core.LLVMBuildLoad2(self.builder, toLLVMtype(expr.typ.?, table, expr), ptr, ""); + break :blk core.LLVMBuildLoad2( + self.builder, + toLLVMtype( + expr.typ orelse try table.getValue(id.ident).?.typ.toTypeIdent(self.allocator), + table, + expr, + ), + ptr, + "", + ); }, .intLit => |int| core.LLVMConstInt(core.LLVMInt32TypeInContext(self.context), @intCast(int.intLit), 1), .stringLit => |str| blk: { @@ -294,23 +303,22 @@ test "Codegen exit" { \\} \\ ; - var tokenizer = tok.Tokenizer.init(std.testing.allocator, src); - defer tokenizer.deinit(); - const toks = try tokenizer.tokenize(); - var symbTable: *symb.SymbolTable = try main.initSymbolTable(std.testing.allocator); - defer symbTable.deinit(); - var parser = parse.Parser.init(std.testing.allocator, toks, symbTable); - defer parser.deinit(); - const parseTree = try parser.parse(); - var pop = symb.Populator.init(std.testing.allocator); - var treeNode = parseTree.asNode(); - try pop.populateSymtable(&treeNode); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); - var gen = Generator.init(arena.allocator(), parseTree); - defer gen.deinit(); - const actual = try gen.generate(); - try expect(std.mem.eql(u8, actual, expected)); + const allocator = arena.allocator(); + var tokenizer = tok.Tokenizer.init(allocator, src); + defer tokenizer.deinit(); + const tokens = try tokenizer.tokenize(); + const symbTable = try main.initSymbolTable(arena.allocator()); + var parser = parse.Parser.init(arena.allocator(), tokens, symbTable); + const tree = try parser.parse(); + var treeNode = tree.asNode(); + var pop = symb.Populator.init(arena.allocator()); + try pop.populateSymtable(&treeNode); + var generator = Generator.init(arena.allocator(), tree, "_calico_start"); + defer generator.deinit(); + const code = try generator.generate(); + try expect(std.mem.eql(u8, code, expected)); } test "Codegen assign" { @@ -321,7 +329,7 @@ test "Codegen assign" { const src = \\fn main() -> i32 { \\ const testval = 6; - \\ var testvar = testval; + \\ varbl testvar = testval; \\ testvar = 5; \\ return testvar; \\} @@ -343,21 +351,22 @@ test "Codegen assign" { \\} \\ ; - var tokenizer = tok.Tokenizer.init(std.testing.allocator, src); + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + var tokenizer = tok.Tokenizer.init(allocator, src); defer tokenizer.deinit(); - const toks = try tokenizer.tokenize(); - var symbTable: *symb.SymbolTable = try main.initSymbolTable(std.testing.allocator); - defer symbTable.deinit(); - var parser = parse.Parser.init(std.testing.allocator, toks, symbTable); - defer parser.deinit(); - const parseTree = try parser.parse(); - var pop = symb.Populator.init(std.testing.allocator); - var treeNode = parseTree.asNode(); + const tokens = try tokenizer.tokenize(); + const symbTable = try main.initSymbolTable(arena.allocator()); + var parser = parse.Parser.init(arena.allocator(), tokens, symbTable); + const tree = try parser.parse(); + var treeNode = tree.asNode(); + var pop = symb.Populator.init(arena.allocator()); try pop.populateSymtable(&treeNode); - var gen = Generator.init(std.testing.allocator, parseTree); - defer gen.deinit(); - const actual = try gen.generate(); - try expect(std.mem.eql(u8, actual, expected)); + var generator = Generator.init(arena.allocator(), tree, "_calico_start"); + defer generator.deinit(); + const code = try generator.generate(); + try expect(std.mem.eql(u8, code, expected)); } test "Codegen assign constant" { @@ -372,19 +381,62 @@ test "Codegen assign constant" { \\ return testvar; \\} ; - var tokenizer = tok.Tokenizer.init(std.testing.allocator, src); + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + var tokenizer = tok.Tokenizer.init(allocator, src); + defer tokenizer.deinit(); + const tokens = try tokenizer.tokenize(); + const symbTable = try main.initSymbolTable(arena.allocator()); + var parser = parse.Parser.init(arena.allocator(), tokens, symbTable); + const tree = try parser.parse(); + var treeNode = tree.asNode(); + var pop = symb.Populator.init(arena.allocator()); + try pop.populateSymtable(&treeNode); + var generator = Generator.init(arena.allocator(), tree, "_calico_start"); + defer generator.deinit(); + const code = generator.generate(); + try std.testing.expectError(CodegenError.Immutable, code); +} +test "Codegen extern fn string" { + const tok = @import("tokenize.zig"); + const expect = std.testing.expect; + const main = @import("main.zig"); + + const src = + \\import fn puts(str: [u8]) -> i32; + \\fn main() -> i32 { + \\ puts("Hello World!"); + \\} + ; + const expected = + \\; ModuleID = '_calico_start' + \\source_filename = "_calico_start" + \\ + \\@.str.0 = private unnamed_addr constant [13 x i8] c"Hello World!\00" + \\ + \\declare i32 @puts(ptr) + \\ + \\define i32 @main() { + \\entry: + \\ %puts = call i32 @puts(ptr @.str.0) + \\} + \\ + ; + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + var tokenizer = tok.Tokenizer.init(allocator, src); defer tokenizer.deinit(); - const toks = try tokenizer.tokenize(); - var symbTable: *symb.SymbolTable = try main.initSymbolTable(std.testing.allocator); - defer symbTable.deinit(); - var parser = parse.Parser.init(std.testing.allocator, toks, symbTable); - defer parser.deinit(); - const parseTree = try parser.parse(); - var pop = symb.Populator.init(std.testing.allocator); - var treeNode = parseTree.asNode(); + const tokens = try tokenizer.tokenize(); + const symbTable = try main.initSymbolTable(arena.allocator()); + var parser = parse.Parser.init(arena.allocator(), tokens, symbTable); + const tree = try parser.parse(); + var treeNode = tree.asNode(); + var pop = symb.Populator.init(arena.allocator()); try pop.populateSymtable(&treeNode); - var gen = Generator.init(std.testing.allocator, parseTree); - defer gen.deinit(); - const actual = gen.generate(); - try std.testing.expectError(CodegenError.Immutable, actual); + var generator = Generator.init(arena.allocator(), tree, "_calico_start"); + defer generator.deinit(); + const code = try generator.generate(); + try expect(std.mem.eql(u8, code, expected)); } diff --git a/src/parser.zig b/src/parser.zig index 2e6bd85..d4bbfc0 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -315,6 +315,7 @@ pub const Parser = struct { } fn parseAssign(self: *Parser) ParsingError!NodeStmt { + std.debug.print("{any}\n", .{self.tokens.peek().?}); const ident = (try self.tokens.consume(.ident)).?; _ = try self.tokens.consume(.equal); const expr = try self.parseExpr(); @@ -346,7 +347,7 @@ pub const Parser = struct { fn parseVariable(self: *Parser) ParsingError!NodeStmt { _ = try self.tokens.consume(.variable); - var typ: TypeIdent = undefined; + var typ: ?TypeIdent = null; if (self.tokens.consume(.colon)) |_| { typ = .{ .ident = (try self.tokens.consume(.ident)).?.ident, @@ -509,20 +510,22 @@ pub const ExprKind = union(enum) { }; test "Parser" { + const main = @import("main.zig"); const expect = std.testing.expect; const src = "return 120;"; - var tokenizer = tok.Tokenizer.init(std.testing.allocator, src); + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + var tokenizer = tok.Tokenizer.init(allocator, src); defer tokenizer.deinit(); - const toks = try tokenizer.tokenize(); - - var symbTable = try symb.SymbolTable.init(std.testing.allocator); - defer symbTable.deinit(); - - var parser = Parser.init(std.testing.allocator, toks, symbTable); - defer parser.deinit(); - const parseTree = try parser.parse(); - const children = try parseTree.children(std.testing.allocator); - defer std.testing.allocator.free(children); + const tokens = try tokenizer.tokenize(); + const symbTable = try main.initSymbolTable(arena.allocator()); + var parser = Parser.init(arena.allocator(), tokens, symbTable); + const tree = try parser.parse(); + var treeNode = tree.asNode(); + var pop = symb.Populator.init(arena.allocator()); + try pop.populateSymtable(&treeNode); + const children = try treeNode.children(allocator); const exp: []const Node = &[_]Node{Node{ .Stmt = NodeStmt{ .id = 2, diff --git a/src/symtable.zig b/src/symtable.zig index 5da6081..2e6e86f 100644 --- a/src/symtable.zig +++ b/src/symtable.zig @@ -41,6 +41,15 @@ pub const SymbType = union(enum) { else => "void", }; } + pub fn toTypeIdent(self: SymbType, allocator: std.mem.Allocator) !pars.TypeIdent { + return pars.TypeIdent{ + .ident = try self.toString(allocator), + .list = switch (self) { + .String => true, + else => false, + }, + }; + } }; pub const SymbValue = struct { diff --git a/src/tokenize.zig b/src/tokenize.zig index aa3788c..b578b22 100644 --- a/src/tokenize.zig +++ b/src/tokenize.zig @@ -140,7 +140,6 @@ pub fn Iterator(comptime typ: type) type { pub fn consume(self: *Iterator(typ), comptime expected: TokenType) error{ ExpectedToken, TokenIteratorOnly }!?typ { if (typ != Token) return TokenizeError.TokenIteratorOnly; if (!checkType(self.peek().?, expected)) { - // std.debug.print("Got {}, expected {}\n", .{ self.peek().?, expected }); return TokenizeError.ExpectedToken; } return self.next(); @@ -235,7 +234,10 @@ pub const Tokenizer = struct { test "Tokenize Expression" { const expect = std.testing.expect; const testSource: []const u8 = "return 120 + 150 - 260 * 12 / 5 + variable;"; - var tokenizer = Tokenizer.init(std.testing.allocator, testSource); + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + var tokenizer = Tokenizer.init(allocator, testSource); defer tokenizer.deinit(); const tokens = try tokenizer.tokenize(); const expected = &[_]Token{ @@ -270,8 +272,11 @@ test "Tokenize Expression" { test "Tokenize variable" { const expect = std.testing.expect; - const testSource: []const u8 = "var five = 5;"; - var tokenizer = Tokenizer.init(std.testing.allocator, testSource); + const testSource: []const u8 = "varbl five = 5;"; + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + var tokenizer = Tokenizer.init(allocator, testSource); defer tokenizer.deinit(); const tokens = try tokenizer.tokenize(); const expected = &[_]Token{ @@ -296,7 +301,10 @@ test "Tokenize variable" { test "Tokenize constant" { const expect = std.testing.expect; const testSource: []const u8 = "const five = 5;"; - var tokenizer = Tokenizer.init(std.testing.allocator, testSource); + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + var tokenizer = Tokenizer.init(allocator, testSource); defer tokenizer.deinit(); const tokens = try tokenizer.tokenize(); const expected = &[_]Token{ @@ -325,7 +333,10 @@ test "Tokenize Function" { \\ return 7; \\} ; - var tokenizer = Tokenizer.init(std.testing.allocator, testSource); + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + var tokenizer = Tokenizer.init(allocator, testSource); defer tokenizer.deinit(); const tokens = try tokenizer.tokenize(); const expected = &[_]Token{ -- cgit v1.2.3 From f1a2e03047c31ca57ca2d79f94f0ae179f0110e2 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Fri, 16 Aug 2024 00:21:00 -0500 Subject: Added type checking to assignments. Types required for varbl now. --- examples/helloWorld.nya | 6 ++++-- src/codegen.zig | 16 +++++++++++----- src/parser.zig | 39 ++++++++++++++++++++++++++------------- src/symtable.zig | 34 ++++++++++++++++++++++------------ 4 files changed, 63 insertions(+), 32 deletions(-) (limited to 'src/codegen.zig') diff --git a/examples/helloWorld.nya b/examples/helloWorld.nya index 160d9d2..625860c 100644 --- a/examples/helloWorld.nya +++ b/examples/helloWorld.nya @@ -1,6 +1,8 @@ import fn puts(str: [u8]) -> i32; fn main() -> i32 { - puts("Hello World!"); - return 0; + varbl: i32 pog = puts("Hello World!"); + pog = 8; + const value = pog; + return value; } diff --git a/src/codegen.zig b/src/codegen.zig index c1fac76..8f3160e 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -11,6 +11,7 @@ const CodegenError = error{ Immutable, OutOfMemory, IncorrectType, + UnknownIdentifier, }; fn toLLVMtype(typ: parse.TypeIdent, sym: *symb.SymbolTable, expr: ?parse.NodeExpr) types.LLVMTypeRef { @@ -80,7 +81,14 @@ pub const Generator = struct { const table = stmt.symtable; const symbol = table.getValue(nodeVar.ident.ident).?; const value = try self.genExpr(nodeVar.expr); - const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ orelse try nodeVar.expr.symtable.getValue(nodeVar.ident.ident).?.typ.toTypeIdent(self.allocator), table, nodeVar.expr).?, nodeVar.ident.ident); + const ptr = try self.genAlloc( + toLLVMtype( + nodeVar.expr.typ orelse try nodeVar.expr.symtable.getValue(nodeVar.ident.ident).?.typ.toTypeIdent(self.allocator), + table, + nodeVar.expr, + ).?, + nodeVar.ident.ident, + ); _ = core.LLVMBuildStore(self.builder, value, ptr); try self.references.put(symbol.id, ptr); } @@ -90,7 +98,7 @@ pub const Generator = struct { const table = stmt.symtable; const symbol = table.getValue(nodeVar.ident.ident).?; - const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ.?, table, nodeVar.expr), nodeVar.ident.ident); + const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ orelse try nodeVar.expr.inferType(self.allocator, table), table, nodeVar.expr), nodeVar.ident.ident); const value = try self.genExpr(nodeVar.expr); _ = core.LLVMBuildStore(self.builder, value, ptr); try self.references.put(symbol.id, ptr); @@ -242,8 +250,6 @@ pub const Generator = struct { }, .call => |call| blk: { - std.debug.print("Function {s} requested\n", .{call.ident.ident}); - const functype = expr.symtable.getValue(call.ident.ident).?.typ.Function; const ident = try self.allocator.dupeZ(u8, call.ident.ident); const function = core.LLVMGetNamedFunction(self.module, ident); @@ -329,7 +335,7 @@ test "Codegen assign" { const src = \\fn main() -> i32 { \\ const testval = 6; - \\ varbl testvar = testval; + \\ varbl: i32 testvar = testval; \\ testvar = 5; \\ return testvar; \\} diff --git a/src/parser.zig b/src/parser.zig index d4bbfc0..f2dba0a 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -17,6 +17,13 @@ const ParsingError = error{ ExpectedToken, OutOfMemory, TokenIteratorOnly, + TypeRequiredForVarbl, +}; + +const TypeError = error{ + OutOfMemory, + IncorrectType, + UnknownIdentifier, }; fn errcast(err: anytype) ParsingError { @@ -56,6 +63,17 @@ pub const NodeExpr = struct { } return try childrenArray.toOwnedSlice(); } + pub fn inferType(self: NodeExpr, allocator: std.mem.Allocator, table: *symb.SymbolTable) TypeError!TypeIdent { + const expectedType = try switch (self.kind) { + .call => |call| if (table.getValue(call.ident.ident)) |symbol| try symbol.typ.Function.output.toTypeIdent(allocator) else TypeError.UnknownIdentifier, + .ident => |id| if (table.getValue(id.ident)) |symbol| try symbol.typ.toTypeIdent(allocator) else TypeError.UnknownIdentifier, + .intLit => TypeIdent{ .ident = "i32", .list = false }, + .stringLit => TypeIdent{ .ident = "[u8]", .list = true }, + }; + if (self.typ) |typ| { + return if (std.mem.eql(u8, typ.ident, expectedType.ident)) expectedType else TypeError.IncorrectType; + } else return expectedType; + } }; pub fn map(comptime T: type, comptime F: type, slice: []const F, func: fn (F) T) []const T { @@ -182,10 +200,7 @@ pub const Parser = struct { }, }; } - typ = TypeIdent{ - .ident = "i32", - .list = false, - }; + typ = null; break :blk ExprKind{ .ident = ident }; }, .stringLit => { @@ -315,7 +330,6 @@ pub const Parser = struct { } fn parseAssign(self: *Parser) ParsingError!NodeStmt { - std.debug.print("{any}\n", .{self.tokens.peek().?}); const ident = (try self.tokens.consume(.ident)).?; _ = try self.tokens.consume(.equal); const expr = try self.parseExpr(); @@ -348,14 +362,13 @@ pub const Parser = struct { fn parseVariable(self: *Parser) ParsingError!NodeStmt { _ = try self.tokens.consume(.variable); var typ: ?TypeIdent = null; - if (self.tokens.consume(.colon)) |_| { - typ = .{ - .ident = (try self.tokens.consume(.ident)).?.ident, - .list = false, - }; - } else |err| { - if (err != tok.TokenizeError.ExpectedToken) return errcast(.{err}); - } + _ = self.tokens.consume(.colon) catch { + return ParsingError.TypeRequiredForVarbl; + }; + typ = .{ + .ident = (try self.tokens.consume(.ident)).?.ident, + .list = false, + }; const ident = (try self.tokens.consume(.ident)).?; _ = try self.tokens.consume(.equal); const expr = try self.parseExpr(); diff --git a/src/symtable.zig b/src/symtable.zig index 2e6e86f..71596e2 100644 --- a/src/symtable.zig +++ b/src/symtable.zig @@ -23,7 +23,7 @@ pub const SymbType = union(enum) { pub fn toSymb(self: SymbType) Symbol { return Symbol{ .Type = self }; } - pub fn toString(self: SymbType, allocator: std.mem.Allocator) ![]const u8 { + pub fn toString(self: SymbType, allocator: std.mem.Allocator) error{OutOfMemory}![]const u8 { return switch (self) { .Integer => "i32", .Character => "u8", @@ -33,15 +33,20 @@ pub const SymbType = union(enum) { var argstring: []const u8 = ""; if (fun.input.len != 0) { argstring = try fun.input[0].toString(allocator); - for (1..fun.input.len) |i| - argstring = try std.mem.join(allocator, ", ", &[_][]const u8{ argstring, try fun.input[i].toString(allocator) }); + for (1..fun.input.len) |i| { + const inputTyp = [_][]const u8{ + argstring, + try fun.input[i].toString(allocator), + }; + argstring = try std.mem.join(allocator, ", ", &inputTyp); + } } break :blk try std.fmt.allocPrint(allocator, "fn({s})->{s}", .{ argstring, output }); }, else => "void", }; } - pub fn toTypeIdent(self: SymbType, allocator: std.mem.Allocator) !pars.TypeIdent { + pub fn toTypeIdent(self: SymbType, allocator: std.mem.Allocator) error{OutOfMemory}!pars.TypeIdent { return pars.TypeIdent{ .ident = try self.toString(allocator), .list = switch (self) { @@ -66,7 +71,7 @@ pub const SymbolTable = struct { scope: ?*Scope = null, allocator: std.mem.Allocator, - pub fn init(allocator: std.mem.Allocator) !*SymbolTable { + pub fn init(allocator: std.mem.Allocator) error{OutOfMemory}!*SymbolTable { const scope = try allocator.create(Scope); scope.par = null; scope.symbs = std.StringHashMap(Symbol).init(allocator); @@ -100,7 +105,7 @@ pub const SymbolTable = struct { self.allocator.destroy(self); } - pub fn makeChild(self: *SymbolTable) !*SymbolTable { + pub fn makeChild(self: *SymbolTable) error{OutOfMemory}!*SymbolTable { const scope = try self.allocator.create(Scope); scope.par = self.scope; scope.symbs = try self.scope.?.symbs.clone(); @@ -185,10 +190,10 @@ pub const Populator = struct { .Stmt => |stmt| { const table: *SymbolTable = stmt.symtable; switch (stmt.kind) { - .defVar => |variable| { + .defVar => |*variable| { const symbol: Symbol = try self.buildValueSymb( table, - if (variable.expr.typ) |typ| typ else pars.TypeIdent{ .ident = "i32", .list = false }, + try variable.expr.inferType(self.allocator, table), true, ); if (!try table.insert(variable.ident.ident, symbol)) return error.FailedToInsert; @@ -196,7 +201,7 @@ pub const Populator = struct { .defValue => |value| { const symbol: Symbol = try self.buildValueSymb( table, - if (value.expr.typ) |typ| typ else pars.TypeIdent{ .ident = "i32", .list = false }, + try value.expr.inferType(self.allocator, table), false, ); if (!try table.insert(value.ident.ident, symbol)) return error.FailedToInsert; @@ -215,13 +220,19 @@ pub const Populator = struct { ); if (!try table.insert(fun.ident.ident, symbol)) return error.FailedToInsert; - std.debug.print("Function {s} inserted\n", .{fun.ident.ident}); if (fun.block == null) return; const block = fun.block.?.asNode(); try self.populateSymtable(&block); }, + .assignVar => |assign| _ = { + const expectedType = try if (table.getValue(assign.ident.ident)) |symbol| try symbol.typ.toTypeIdent(self.allocator) else error.UnknownIdentifier; + const actualType = try assign.expr.inferType(self.allocator, table); + if (!std.mem.eql(u8, actualType.ident, expectedType.ident)) return error.IncorrectType; + }, + .exit => |exit| _ = try exit.inferType(self.allocator, table), + .expr => {}, - else => {}, + // else => return error.Unimplemented, } }, else => { @@ -263,7 +274,6 @@ pub const Populator = struct { } const name = try std.fmt.allocPrint(self.allocator, "fn({s})->{s}", .{ argstring, try output.toString(self.allocator) }); - std.debug.print("Function type => \"{s}\"\n", .{name}); _ = try table.insert(name, typ.toSymb()); return Symbol{ -- cgit v1.2.3