Skip to content

Commit

Permalink
Refactor modules and allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed Jun 16, 2024
1 parent 0edb3ea commit 0db63d2
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 463 deletions.
70 changes: 42 additions & 28 deletions src/ast.zig
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
const std = @import("std");
const token = @import("./token.zig");
const UUID = @import("./utils/uuid.zig").UUID;
const token = @import("token.zig");
const UUID = @import("utils/uuid.zig").UUID;
const Token = token.Token;
const Allocator = std.mem.Allocator;

pub const Tree = struct {
arena: std.heap.ArenaAllocator.State,
allocator: Allocator,

root: []const Statement,
source: []const u8,

pub fn deinit(self: *const Tree) void {
self.arena.promote(self.allocator).deinit();
}

pub fn print(self: *const Tree, writer: anytype) void {
writer.print("\n===TREE===", .{});
for (self.root) |state| {
state.print(writer, "", 0);
}
writer.print("\n", .{});
writer.print("\n=== ===\n", .{});
}
};

Expand Down Expand Up @@ -89,25 +81,35 @@ pub const Expression = struct {
writer.print("\n", .{});
var d: usize = 0;
while (d < depth) : (d += 1) {
writer.print(" ", .{});
writer.print(" ", .{});
}
writer.print("{s}", .{prefix});
switch (self.type) {
.binary => |b| {
writer.print("BINARY::{s}", .{b.operator.toString()});
b.left.print(writer, "LEFT::", depth + 1);
b.right.print(writer, "RIGHT::", depth + 1);
writer.print("BINARY:: {s}", .{@tagName(b.operator)});
b.left.print(writer, "LEFT:: ", depth + 1);
b.right.print(writer, "RIGHT:: ", depth + 1);
},
.call => |c| {
writer.print("CALL::{d}", .{c.arguments.len});
writer.print("CALL::", .{});
c.target.print(writer, "TARGET::", depth + 1);
for (c.arguments) |arg| {
arg.print(writer, "ARG::", depth + 1);
}
},
.identifier => |i| writer.print("{s}", .{i}),
.number => |n| writer.print("{d}", .{n}),
.indexer => |i| {
writer.print("INDEXER:: ", .{});
i.target.print(writer, "TARGET:: ", depth + 1);
i.index.print(writer, "INDEX:: ", depth + 1);
},
.identifier => |i| writer.print("IDENTIFIER:: {s}", .{i}),
.number => |n| writer.print("NUM:: {d}", .{n}),
.string => |s| {
writer.print("STRING:: {s}", .{s.raw});
for (s.expressions) |e| e.print(writer, "EXP:: ", depth + 1);
},
.function => |f| {
writer.print("FUNCTION::{s}", .{f.parameters});
writer.print("FUNCTION:: {s}", .{f.parameters});
for (f.body) |s| {
s.print(writer, "", depth + 1);
}
Expand Down Expand Up @@ -210,22 +212,34 @@ pub const Statement = struct {
}
writer.print("{s}", .{prefix});
switch (self.type) {
.include => |i| {
writer.print("INCLUDE:: {s}", .{i.path});
for (i.contents) |c| c.print(writer, "", depth + 1);
},
.block => |b| {
for (b) |s| s.print(writer, "BLOCK::", depth + 1);
for (b) |s| s.print(writer, "", depth + 1);
},
.expression => |e| e.print(writer, "EXPRESSION::", depth + 1),
.expression => |e| e.print(writer, "EXPRESSION:: ", depth),
.@"if" => |i| {
writer.print("IF", .{});
i.condition.print(writer, "CONDITION", depth + 1);
for (i.then_branch) |s| s.print(writer, "THEN", depth + 1);
writer.print("IF:: ", .{});
i.condition.print(writer, "CONDITION:: ", depth + 1);
for (i.then_branch) |s| s.print(writer, "THEN:: ", depth + 1);
if (i.else_branch) |eb| {
for (eb) |s| s.print(writer, "ELSE", depth + 1);
for (eb) |s| s.print(writer, "ELSE:: ", depth + 1);
}
},
.@"enum" => |e| {
writer.print("ENUM:: {s} [", .{e.name});
for (e.values) |v| {
writer.print("{s},", .{v});
}
writer.print("]", .{});
},
.return_expression => |re| re.print(writer, "RETURN VALUE::", depth + 1),
.return_void => writer.print("RETURN::", .{}),
.return_expression => |re| re.print(writer, "RETURN VALUE:: ", depth + 1),
.return_void => writer.print("RETURN VOID", .{}),
.variable => |v| {
v.initializer.print(writer, "VARIABLE::", depth + 1);
writer.print("VARIABLE:: {s}", .{v.name});
v.initializer.print(writer, "", depth + 1);
},
else => {
writer.print("{any}", .{self});
Expand Down
9 changes: 5 additions & 4 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub fn main() !void {
var mod = Module.init(allocator, full_path) catch |err| {
return checkVerbose(err);
};
errdefer mod.deinit();
mod.use_loc = try checkFlag("--loc");

if (is_loc) {
Expand Down Expand Up @@ -175,11 +176,11 @@ pub fn main() !void {
return;
}

var bytecode = mod.generateBytecode() catch |err| {
var bytecode = mod.generateBytecode(allocator) catch |err| {
return checkVerbose(err);
};
defer bytecode.free(allocator);
mod.deinit();
defer bytecode.free(allocator);

if (is_compile) {
if (is_dry) {
Expand All @@ -203,9 +204,9 @@ pub fn main() !void {
const vm_alloc = arena.allocator();
if (is_test) {
const maybe_count = args.next();
if (maybe_count == null) return usage("Auto requires a play count.");
if (maybe_count == null) return usage("Test requires a play count.");
auto_count = std.fmt.parseInt(u64, maybe_count.?, 10) catch {
return usage("Invalid auto count specified");
return usage("Invalid test count specified");
};

var i: usize = 0;
Expand Down
15 changes: 6 additions & 9 deletions src/compiler-error.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const CompilerErr = struct {
};

pub const CompilerErrors = struct {
list: std.ArrayListUnmanaged(CompilerErr),
list: std.ArrayList(CompilerErr),
allocator: std.mem.Allocator,

// used for interpolatedExpressions
Expand All @@ -24,14 +24,12 @@ pub const CompilerErrors = struct {
offset_col: usize = 0,

pub fn init(allocator: std.mem.Allocator) CompilerErrors {
return .{ .list = std.ArrayListUnmanaged(CompilerErr){}, .allocator = allocator };
return .{ .list = std.ArrayList(CompilerErr).init(allocator), .allocator = allocator };
}

pub fn add(self: *CompilerErrors, comptime fmt: []const u8, token: Token, severity: CompilerErr.Severity, args: anytype) !void {
const msg = try std.fmt.allocPrint(self.allocator, fmt, args);
errdefer self.allocator.free(msg);

return self.list.append(self.allocator, .{
try self.list.append(.{
.fmt = msg,
.severity = severity,
.token = token,
Expand All @@ -41,11 +39,10 @@ pub const CompilerErrors = struct {
for (self.list.items) |err| {
self.allocator.free(err.fmt);
}
self.list.deinit(self.allocator);
self.* = undefined;
self.list.deinit();
}

pub fn write(self: *CompilerErrors, source: []const u8, writer: anytype) !void {
pub fn write(self: *CompilerErrors, file_path: []const u8, source: []const u8, writer: anytype) !void {
while (self.list.popOrNull()) |err| {
defer self.allocator.free(err.fmt);
const color_prefix = switch (err.severity) {
Expand All @@ -64,7 +61,7 @@ pub const CompilerErrors = struct {
start = @min(start, source.len - 1);
end = @min(end, source.len);

try writer.print("type: {s}, line: {}, column_start: {}, column_end: {}, source_start: {}, source_end: {}\n", .{ tok.toString(err.token.token_type), line, column, column + end - start, start, end });
try writer.print("file: {s}\ntype: {s}, line: {}, column_start: {}, column_end: {}, source_start: {}, source_end: {}\n", .{ file_path, tok.toString(err.token.token_type), line, column, column + end - start, start, end });

var lines = std.mem.splitSequence(u8, source, "\n");
var lineNumber: usize = 1;
Expand Down
Loading

0 comments on commit 0db63d2

Please sign in to comment.