-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample_runner.zig
51 lines (40 loc) · 1.33 KB
/
example_runner.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const std = @import("std");
const luf = @import("luf");
const log = std.log.scoped(.luf_example);
const example = @import("build_options").example;
pub fn main() !void {
if (example == null)
return log.info(
"No example was provided, use -Dexample=example_name to run an example",
.{},
);
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = &gpa.allocator;
const example_path = try std.fs.path.join(allocator, &[_][]const u8{
"examples",
example.?,
});
defer allocator.free(example_path);
const example_file = try std.mem.concat(allocator, u8, &[_][]const u8{
example_path,
".luf",
});
defer allocator.free(example_file);
const file = std.fs.cwd().openFile(example_file, .{}) catch |_| {
return log.err("Example does not exist", .{});
};
defer file.close();
const size = try file.getEndPos();
const source = try file.readToEndAlloc(allocator, size);
defer allocator.free(source);
const writer = std.io.getStdOut().writer();
var vm = try luf.Vm.init(allocator);
defer vm.deinit();
vm.compileAndRun(source) catch |err| {
try vm.errors.write(source, writer);
return;
};
try vm.peek().print(writer);
try writer.writeAll("\n");
}