fix other things related to reading scri file on repl
This commit is contained in:
parent
66b262beb2
commit
80036c4c0d
2 changed files with 24 additions and 21 deletions
|
@ -140,6 +140,7 @@ pub const ArgList = std.ArrayList([]const u8);
|
||||||
|
|
||||||
pub const KeywordMap = std.StringHashMap(CommandType);
|
pub const KeywordMap = std.StringHashMap(CommandType);
|
||||||
|
|
||||||
|
/// A parser.
|
||||||
pub const Lang = struct {
|
pub const Lang = struct {
|
||||||
allocator: *std.mem.Allocator,
|
allocator: *std.mem.Allocator,
|
||||||
keywords: KeywordMap,
|
keywords: KeywordMap,
|
||||||
|
|
44
src/main.zig
44
src/main.zig
|
@ -25,45 +25,49 @@ fn wrapInCmdList(allocator: *std.mem.Allocator, cmd: langs.Command) !langs.Comma
|
||||||
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();
|
var stdout_file = try std.io.getStdOut();
|
||||||
const stdout = &stdout_file.outStream().stream;
|
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_read = try std.fs.File.openRead(scri_path);
|
||||||
|
const total_bytes = try file_read.getEndPos();
|
||||||
|
|
||||||
// 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.
|
||||||
|
|
||||||
var loadargs = langs.ArgList.init(allocator);
|
// TODO load should -1 the index if its on repl
|
||||||
defer loadargs.deinit();
|
|
||||||
try loadargs.append(":1");
|
|
||||||
|
|
||||||
var cmds = try wrapInCmdList(allocator, langs.Command{
|
|
||||||
.command = .Load,
|
|
||||||
.args = loadargs,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
var cmds = langs.CommandList.init(allocator);
|
||||||
defer cmds.deinit();
|
defer cmds.deinit();
|
||||||
|
|
||||||
// if the scri file already had commands, we add them up on the cmd list
|
var lang = langs.Lang.init(allocator);
|
||||||
var file_read = try std.fs.openReead(scri_path);
|
defer lang.deinit();
|
||||||
const total_bytes = try file_read.getEndPos();
|
|
||||||
|
|
||||||
if (total_bytes > 0) {
|
if (total_bytes > 0) {
|
||||||
|
// those MUST BE long lived, we can't defer them here
|
||||||
var scri_existing = try allocator.alloc(u8, total_bytes);
|
var scri_existing = try allocator.alloc(u8, total_bytes);
|
||||||
defer allocator.free(scri_existing);
|
_ = try file_read.read(scri_existing);
|
||||||
|
|
||||||
_ = try file.read(slice);
|
|
||||||
file_read.close();
|
|
||||||
|
|
||||||
var existing_cmds = try lang.parse(scri_existing);
|
var existing_cmds = try lang.parse(scri_existing);
|
||||||
defer existing_cmds.deinit();
|
|
||||||
|
|
||||||
// copy the existing command list into the repl's command list
|
// copy the existing command list into the repl's command list
|
||||||
var it = existing_cmds.iterator();
|
var it = existing_cmds.iterator();
|
||||||
while (it.next()) |existing_cmd| {
|
while (it.next()) |existing_cmd| {
|
||||||
try cmds.append(existing_cmd);
|
try cmds.append(existing_cmd);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// if there isn't any commands on the file, we load our default
|
||||||
|
// 'load :1' command
|
||||||
|
var loadargs = langs.ArgList.init(allocator);
|
||||||
|
// defer loadargs.deinit();
|
||||||
|
try loadargs.append(":1");
|
||||||
|
|
||||||
|
try cmds.append(langs.Command{
|
||||||
|
.command = .Load,
|
||||||
|
.args = loadargs,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file_read.close();
|
||||||
|
|
||||||
var file = try std.fs.File.openWrite(scri_path);
|
var file = try std.fs.File.openWrite(scri_path);
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
||||||
|
@ -72,6 +76,7 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
|
||||||
|
|
||||||
// since we opened the file for writing, it becomes empty, so, to ensure
|
// since we opened the file for writing, it becomes empty, so, to ensure
|
||||||
// we don't fuck up later on, we print cmds before starting the repl
|
// we don't fuck up later on, we print cmds before starting the repl
|
||||||
|
try printer.printList(cmds, stdout);
|
||||||
try printer.printList(cmds, stream);
|
try printer.printList(cmds, stream);
|
||||||
|
|
||||||
// we keep
|
// we keep
|
||||||
|
@ -83,9 +88,6 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
|
||||||
// - one runner that gets copied from the original on every new
|
// - one runner that gets copied from the original on every new
|
||||||
// command the user issues
|
// command the user issues
|
||||||
|
|
||||||
var lang = langs.Lang.init(allocator);
|
|
||||||
defer lang.deinit();
|
|
||||||
|
|
||||||
var current: langs.Command = undefined;
|
var current: langs.Command = undefined;
|
||||||
|
|
||||||
var runner = runners.Runner.init(allocator);
|
var runner = runners.Runner.init(allocator);
|
||||||
|
|
Loading…
Reference in a new issue