-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.zig
More file actions
46 lines (35 loc) · 1.26 KB
/
Copy pathmain.zig
File metadata and controls
46 lines (35 loc) · 1.26 KB
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
const std = @import("std");
const Io = std.Io;
const zigline = @import("zigline");
const terminal = zigline.terminal;
pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
const io = init.io;
var stdout_buffer: [1024]u8 = undefined;
var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
const out = &stdout_file_writer.interface;
try out.writeAll("zigline demo REPL\n\n");
try out.flush();
var editor = zigline.Line.init(gpa, io, .{ .prompt = "zigline> " });
defer editor.deinit();
var raw = try terminal.RawMode.enable();
defer raw.disable();
while (try editor.readLine()) |line| {
defer gpa.free(line);
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const tokens = zigline.tokenize(arena.allocator(), line) catch |err| {
try out.print("parse error: {s}\n", .{@errorName(err)});
try out.flush();
continue;
};
try out.print("{d} token(s):\n", .{tokens.len});
for (tokens, 0..) |tok, i| {
try out.print(" [{d}] '{s}'\n", .{ i, tok });
}
try out.flush();
try editor.historyAdd(line);
}
try out.writeAll("noniin!\n");
try out.flush();
}