thp-zig/src/main.zig

101 lines
2.8 KiB
Zig
Raw Normal View History

2024-11-16 01:00:15 +00:00
const std = @import("std");
2024-12-06 02:02:09 +00:00
const lexic = @import("lexic");
const syntax = @import("syntax");
2024-12-21 11:46:53 +00:00
const errors = @import("errors");
const tracing = @import("config").tracing;
2024-11-16 11:02:56 +00:00
2024-12-06 02:02:09 +00:00
const thp_version: []const u8 = "0.0.1";
2024-11-16 01:00:15 +00:00
pub fn main() !void {
2024-11-16 10:46:19 +00:00
try repl();
}
2024-11-16 01:00:15 +00:00
2024-11-16 10:46:19 +00:00
fn repl() !void {
2024-11-16 01:00:15 +00:00
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
2024-12-21 11:46:53 +00:00
if (tracing) {
try stdout.print("\n|\n| DEBUG MODE\n|\n\n", .{});
try bw.flush();
}
2024-11-16 10:46:19 +00:00
try stdout.print("The THP REPL, v{s}\n", .{thp_version});
2024-11-16 11:42:26 +00:00
try stdout.print("Enter expressions to evaluate. Enter CTRL-D to exit.\n", .{});
2024-11-16 11:02:56 +00:00
try bw.flush();
2024-12-21 11:46:53 +00:00
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
2024-11-16 11:02:56 +00:00
const stdin = std.io.getStdIn().reader();
while (true) {
2024-12-21 11:46:53 +00:00
//
// Print prompt
//
try stdout.print("\nthp => ", .{});
try bw.flush();
2024-11-16 11:02:56 +00:00
2024-12-21 11:46:53 +00:00
//
// Read stdin, break if EOF (C-d)
2024-12-21 11:46:53 +00:00
//
const bare_line = stdin.readUntilDelimiterAlloc(std.heap.page_allocator, '\n', 8192) catch |e| switch (e) {
error.EndOfStream => {
break;
},
else => return e,
};
2024-12-21 11:46:53 +00:00
defer std.heap.page_allocator.free(bare_line);
const line = std.mem.trim(u8, bare_line, "\r");
2024-11-16 11:42:26 +00:00
2024-12-21 11:46:53 +00:00
//
// Tokenize
//
var error_array = std.ArrayList(errors.ErrorData).init(alloc);
defer error_array.deinit();
const tokens = lexic.tokenize(line, alloc, &error_array) catch |e| switch (e) {
error.OutOfMemory => {
try stdout.print("FATAL ERROR: System Out of Memory!", .{});
try bw.flush();
return e;
},
else => return e,
};
2024-12-21 11:46:53 +00:00
defer tokens.deinit();
// Trace tokens
if (tracing) {
for (tokens.items) |token| {
trace_header();
std.debug.print(
"token: `{s}`, type: `{s}`, start: `{d}` \n",
.{ token.value, @tagName(token.token_type), token.start_pos },
);
}
}
2024-11-19 01:08:42 +00:00
// Print errors
for (error_array.items) |err| {
try stdout.print("Lex error: {s} at pos {d}\n", .{ err.reason, err.start_position });
try bw.flush();
}
// next repl line
2024-12-21 11:46:53 +00:00
}
2024-11-16 01:00:15 +00:00
2024-12-21 11:46:53 +00:00
// var module_ast: syntax.Module = undefined;
// const parsing_error = try alloc.create(errors.ErrorData);
// defer parsing_error.deinit();
// defer alloc.destroy(parsing_error);
//
// try module_ast.init(&tokens, 0, alloc, parsing_error);
2024-12-17 02:00:27 +00:00
try stdout.print("\n\nExecution finished. Bye c:\n", .{});
2024-11-16 10:46:19 +00:00
try bw.flush();
2024-11-16 01:00:15 +00:00
}
2024-12-21 11:46:53 +00:00
inline fn trace_header() void {
std.debug.print(" |TRACE> ", .{});
}