thp-zig/src/main.zig

44 lines
1.3 KiB
Zig
Raw Normal View History

2024-11-16 01:00:15 +00:00
const std = @import("std");
2024-11-16 11:02:56 +00:00
const lexic = @import("./01_lexic/root.zig");
2024-11-16 10:46:19 +00:00
const thp_version: []const u8 = "0.0.0";
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
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
2024-11-16 10:46:19 +00:00
}
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-11-16 10:46:19 +00:00
try stdout.print("The THP REPL, v{s}\n", .{thp_version});
2024-11-16 11:02:56 +00:00
try stdout.print("Enter expressions to evaluate. Enter CTRL-D to exit.\n\n", .{});
try bw.flush();
const stdin = std.io.getStdIn().reader();
try stdout.print("thp => ", .{});
try bw.flush();
const user_input = try stdin.readUntilDelimiterAlloc(std.heap.page_allocator, '\n', 8192);
defer std.heap.page_allocator.free(user_input);
2024-11-16 01:00:15 +00:00
2024-11-16 11:02:56 +00:00
try stdout.print("got: `{s}`\n", .{user_input});
2024-11-16 10:46:19 +00:00
try bw.flush();
2024-11-16 01:00:15 +00:00
}
test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}
2024-11-16 11:02:56 +00:00
test "new test" {
const res = lexic.stub();
try std.testing.expectEqual(@as(i32, 322), res);
}