Compare commits
No commits in common. "058bf8deb90c21c3d318df79af173a2b527de439" and "34d3b12c56a6d49a9cd30e344769af2c6e5f0d7a" have entirely different histories.
058bf8deb9
...
34d3b12c56
3 changed files with 11 additions and 61 deletions
17
src/lang.zig
17
src/lang.zig
|
@ -3,8 +3,10 @@ const std = @import("std");
|
||||||
const plugin = @import("plugin.zig");
|
const plugin = @import("plugin.zig");
|
||||||
|
|
||||||
pub const ParseError = error{
|
pub const ParseError = error{
|
||||||
OutOfMemory,
|
NoCommandGiven,
|
||||||
|
UnknownCommand,
|
||||||
ArgRequired,
|
ArgRequired,
|
||||||
|
FloatParseFail,
|
||||||
ParseFail,
|
ParseFail,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -158,11 +160,6 @@ pub const Lang = struct {
|
||||||
self.keywords.deinit();
|
self.keywords.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(self: *Lang) void {
|
|
||||||
self.has_error = false;
|
|
||||||
self.line = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fillKeywords(self: *Lang) !void {
|
fn fillKeywords(self: *Lang) !void {
|
||||||
_ = try self.keywords.put("noop", .Noop);
|
_ = try self.keywords.put("noop", .Noop);
|
||||||
_ = try self.keywords.put("load", .Load);
|
_ = try self.keywords.put("load", .Load);
|
||||||
|
@ -264,7 +261,7 @@ pub const Lang = struct {
|
||||||
|
|
||||||
fn expectAny(self: *Lang, count: usize, args: ArgList) !void {
|
fn expectAny(self: *Lang, count: usize, args: ArgList) !void {
|
||||||
if (args.len != count) {
|
if (args.len != count) {
|
||||||
self.doError("expected {} arguments, found {}", count, args.len);
|
std.debug.warn("expected {} arguments, found {}\n", count, args.len);
|
||||||
return error.ArgRequired;
|
return error.ArgRequired;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,7 +274,7 @@ pub const Lang = struct {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
|
|
||||||
if (args.len != count) {
|
if (args.len != count) {
|
||||||
self.doError("expected {} arguments, found {}", count, args.len);
|
std.debug.warn("expected {} arguments, found {}\n", count, args.len);
|
||||||
return error.ArgRequired;
|
return error.ArgRequired;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,7 +319,7 @@ pub const Lang = struct {
|
||||||
self.has_error = true;
|
self.has_error = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(self: *Lang, data: []const u8) ParseError!CommandList {
|
pub fn parse(self: *Lang, data: []const u8) !CommandList {
|
||||||
var splitted_it = std.mem.separate(data, ";");
|
var splitted_it = std.mem.separate(data, ";");
|
||||||
try self.fillKeywords();
|
try self.fillKeywords();
|
||||||
var cmds = CommandList.init(self.allocator);
|
var cmds = CommandList.init(self.allocator);
|
||||||
|
@ -362,7 +359,7 @@ pub const Lang = struct {
|
||||||
// construct final Command based on command
|
// construct final Command based on command
|
||||||
var cmd = Command{ .command = ctype, .args = args };
|
var cmd = Command{ .command = ctype, .args = args };
|
||||||
self.validateCommand(cmd) catch |err| {
|
self.validateCommand(cmd) catch |err| {
|
||||||
//self.doError("error validating command '{}': {}", command, err);
|
self.doError("Unknown command '{}' (length {})", command, command.len);
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
32
src/main.zig
32
src/main.zig
|
@ -1,7 +1,6 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const langs = @import("lang.zig");
|
const langs = @import("lang.zig");
|
||||||
const runners = @import("runner.zig");
|
const runners = @import("runner.zig");
|
||||||
const printer = @import("printer.zig");
|
|
||||||
|
|
||||||
test "scritcher" {
|
test "scritcher" {
|
||||||
_ = @import("lang.zig");
|
_ = @import("lang.zig");
|
||||||
|
@ -9,17 +8,8 @@ test "scritcher" {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
|
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"));
|
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
|
// 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
|
// now have a 'repl' argument right before it. to counteract that, the
|
||||||
// initial repl buffer contains 'load :1' instead.
|
// initial repl buffer contains 'load :1' instead.
|
||||||
|
@ -39,33 +29,19 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
|
||||||
|
|
||||||
// TODO start a runner and keep it hot
|
// TODO start a runner and keep it hot
|
||||||
|
|
||||||
var lang = langs.Lang.init(allocator);
|
|
||||||
defer lang.deinit();
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
lang.reset();
|
|
||||||
try stdout.print("> ");
|
try stdout.print("> ");
|
||||||
|
|
||||||
var buffer = try std.Buffer.init(allocator, ""[0..]);
|
var buffer = try std.Buffer.init(allocator, ""[0..]);
|
||||||
var line = std.io.readLine(&buffer) catch |err| {
|
var line = std.io.readLine(&buffer) catch |err| {
|
||||||
if (err == error.EndOfStream) break;
|
if (err == error.EndOfStream) return;
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (std.mem.eql(u8, line, "push")) {
|
// TODO parse the line through langs.parse, then add the command to cmds
|
||||||
try file.seekTo(0);
|
|
||||||
try printer.printList(cmds, stream);
|
|
||||||
continue;
|
|
||||||
} else if (std.mem.eql(u8, line, "list")) {
|
|
||||||
try printer.printList(cmds, stream);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var cmds_parsed = lang.parse(line) catch |err| {
|
// TODO save the line to scri_file? or should we have a special `push`
|
||||||
std.debug.warn("repl: error while parsing: {}\n", err);
|
// to do so?
|
||||||
continue;
|
|
||||||
};
|
|
||||||
try cmds.append(cmds_parsed.at(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue