summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2024-06-27 01:58:31 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2024-06-27 01:58:31 -0500
commit36e990c5bdfffb145b7255b8159d3ac879344996 (patch)
treecd6ca3ef975cd6657ee3cd92d9938ebcfa14846e /src/main.zig
downloadcalico-36e990c5bdfffb145b7255b8159d3ac879344996.tar.gz
Initial commit
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig
new file mode 100644
index 0000000..18239b1
--- /dev/null
+++ b/src/main.zig
@@ -0,0 +1,64 @@
+const std = @import("std");
+const tok = @import("tokenize.zig");
+
+const gftCompilerError = error{NoInputFile};
+
+pub fn main() !void {
+ if (std.os.argv.len != 2) return gftCompilerError.NoInputFile;
+ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
+ defer _ = gpa.deinit();
+ var args = std.process.args();
+ _ = args.skip();
+ const inputFileName = args.next();
+ const inputFile = try std.fs.cwd().openFile(inputFileName.?, .{});
+ defer inputFile.close();
+
+ std.fs.cwd().makeDir("out") catch |err| {
+ if (err != error.PathAlreadyExists) return err;
+ };
+ const outfile = try std.fs.cwd().createFile("out/out.asm", .{});
+ const outWriter = outfile.writer();
+ defer outfile.close();
+
+ // Logic here to compile language
+ const all = try inputFile.readToEndAlloc(gpa.allocator(), 2048);
+ defer gpa.allocator().free(all);
+
+ const toks = try tok.tokenize(gpa.allocator(), all);
+ defer gpa.allocator().free(toks);
+ var tokIter = tok.TokenIterator{ .tokens = toks };
+ try outWriter.print("global _start:\n", .{});
+ while (tokIter.next()) |t| {
+ switch (t) {
+ .ret => {
+ const num = tokIter.next();
+ switch (num.?) {
+ .intLit => {},
+ else => break,
+ }
+ switch (tokIter.next().?) {
+ .semiCol => {},
+ else => break,
+ }
+ try outWriter.print(
+ \\ mov rax, 60
+ \\ mov rdi, {}
+ \\ syscall
+ \\
+ , .{num.?.intLit});
+ gpa.allocator().free(t.ret);
+ },
+ else => {},
+ }
+ }
+
+ const nasmargv = [_][]const u8{ "nasm", "-felf64", "out/out.asm" };
+ const nasmproc = try std.ChildProcess.run(.{ .argv = &nasmargv, .allocator = gpa.allocator() });
+ defer gpa.allocator().free(nasmproc.stdout);
+ defer gpa.allocator().free(nasmproc.stderr);
+
+ const ldargv = [_][]const u8{ "ld", "-o", "out/out", "out/out.o" };
+ const ldproc = try std.ChildProcess.run(.{ .argv = &ldargv, .allocator = gpa.allocator() });
+ defer gpa.allocator().free(ldproc.stdout);
+ defer gpa.allocator().free(ldproc.stderr);
+}