Compare commits

..

4 commits

3 changed files with 61 additions and 11 deletions

View file

@ -3,10 +3,8 @@ const std = @import("std");
const plugin = @import("plugin.zig");
pub const ParseError = error{
NoCommandGiven,
UnknownCommand,
OutOfMemory,
ArgRequired,
FloatParseFail,
ParseFail,
};
@ -160,6 +158,11 @@ pub const Lang = struct {
self.keywords.deinit();
}
pub fn reset(self: *Lang) void {
self.has_error = false;
self.line = 0;
}
fn fillKeywords(self: *Lang) !void {
_ = try self.keywords.put("noop", .Noop);
_ = try self.keywords.put("load", .Load);
@ -261,7 +264,7 @@ pub const Lang = struct {
fn expectAny(self: *Lang, count: usize, args: ArgList) !void {
if (args.len != count) {
std.debug.warn("expected {} arguments, found {}\n", count, args.len);
self.doError("expected {} arguments, found {}", count, args.len);
return error.ArgRequired;
}
}
@ -274,7 +277,7 @@ pub const Lang = struct {
var i: usize = 0;
if (args.len != count) {
std.debug.warn("expected {} arguments, found {}\n", count, args.len);
self.doError("expected {} arguments, found {}", count, args.len);
return error.ArgRequired;
}
@ -319,7 +322,7 @@ pub const Lang = struct {
self.has_error = true;
}
pub fn parse(self: *Lang, data: []const u8) !CommandList {
pub fn parse(self: *Lang, data: []const u8) ParseError!CommandList {
var splitted_it = std.mem.separate(data, ";");
try self.fillKeywords();
var cmds = CommandList.init(self.allocator);
@ -359,7 +362,7 @@ pub const Lang = struct {
// construct final Command based on command
var cmd = Command{ .command = ctype, .args = args };
self.validateCommand(cmd) catch |err| {
self.doError("Unknown command '{}' (length {})", command, command.len);
//self.doError("error validating command '{}': {}", command, err);
continue;
};

View file

@ -1,6 +1,7 @@
const std = @import("std");
const langs = @import("lang.zig");
const runners = @import("runner.zig");
const printer = @import("printer.zig");
test "scritcher" {
_ = @import("lang.zig");
@ -8,8 +9,17 @@ test "scritcher" {
}
pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
var stdout_file = try std.io.getStdOut();
const stdout = &stdout_file.outStream().stream;
const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path"));
var file = try std.fs.File.openWrite(scri_path);
defer file.close();
var out = file.outStream();
var stream = &out.stream;
// the thing here is that 'load :0' would load the scri_path, since we
// now have a 'repl' argument right before it. to counteract that, the
// initial repl buffer contains 'load :1' instead.
@ -29,19 +39,33 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
// TODO start a runner and keep it hot
var lang = langs.Lang.init(allocator);
defer lang.deinit();
while (true) {
lang.reset();
try stdout.print("> ");
var buffer = try std.Buffer.init(allocator, ""[0..]);
var line = std.io.readLine(&buffer) catch |err| {
if (err == error.EndOfStream) return;
if (err == error.EndOfStream) break;
return err;
};
// TODO parse the line through langs.parse, then add the command to cmds
if (std.mem.eql(u8, line, "push")) {
try file.seekTo(0);
try printer.printList(cmds, stream);
continue;
} else if (std.mem.eql(u8, line, "list")) {
try printer.printList(cmds, stream);
continue;
}
// TODO save the line to scri_file? or should we have a special `push`
// to do so?
var cmds_parsed = lang.parse(line) catch |err| {
std.debug.warn("repl: error while parsing: {}\n", err);
continue;
};
try cmds.append(cmds_parsed.at(0));
}
}

23
src/printer.zig Normal file
View file

@ -0,0 +1,23 @@
const langs = @import("lang.zig");
pub fn printList(list: langs.CommandList, stream: var) !void {
for (list.toSlice()) |cmd| {
var command = switch (cmd.command) {
.Noop => "noop",
.Load => "load",
.Quicksave => "quicksave",
.RunQS => "runqs",
// TODO rest of commands
else => unreachable,
};
try stream.print("{}", command);
for (cmd.args.toSlice()) |arg| {
try stream.print(" {}", arg);
}
try stream.write(";\n");
}
}