diff --git a/src/image.zig b/src/image.zig index 0e9c775..3e38903 100644 --- a/src/image.zig +++ b/src/image.zig @@ -131,6 +131,7 @@ pub const Image = struct { /// Open a BMP image for later. pub fn open(allocator: *std.mem.Allocator, path: []const u8) !*Image { var in_fmt = mkSfInfo(); + var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt); var image = try allocator.create(Image); @@ -148,26 +149,6 @@ pub const Image = struct { return image; } - pub fn clone(self: *Image) !*Image { - var in_fmt = mkSfInfo(); - // clone sndfile - var sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt); - var image = try self.allocator.create(Image); - - std.debug.assert(in_fmt.frames > i64(0)); - std.debug.assert(in_fmt.seekable == i32(1)); - - image.* = Image{ - .allocator = self.allocator, - .sndfile = sndfile, - .path = self.path, - .curpath = self.curpath, - .frames = @intCast(usize, in_fmt.frames), - }; - - return image; - } - pub fn close(self: *Image) void { //self.allocator.free(self.path); //self.allocator.free(self.curpath); diff --git a/src/lang.zig b/src/lang.zig index 9724dfb..3af632c 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -3,8 +3,10 @@ const std = @import("std"); const plugin = @import("plugin.zig"); pub const ParseError = error{ - OutOfMemory, + NoCommandGiven, + UnknownCommand, ArgRequired, + FloatParseFail, ParseFail, }; @@ -158,11 +160,6 @@ 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); @@ -264,7 +261,7 @@ pub const Lang = struct { fn expectAny(self: *Lang, count: usize, args: ArgList) !void { 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; } } @@ -277,7 +274,7 @@ pub const Lang = struct { var i: usize = 0; 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; } @@ -322,7 +319,7 @@ pub const Lang = struct { 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, ";"); try self.fillKeywords(); var cmds = CommandList.init(self.allocator); @@ -362,7 +359,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("error validating command '{}': {}", command, err); + self.doError("Unknown command '{}' (length {})", command, command.len); continue; }; diff --git a/src/main.zig b/src/main.zig index be034f9..156855e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,122 +1,12 @@ const std = @import("std"); const langs = @import("lang.zig"); const runners = @import("runner.zig"); -const printer = @import("printer.zig"); test "scritcher" { _ = @import("lang.zig"); _ = @import("runner.zig"); } -fn wrapInCmdList(allocator: *std.mem.Allocator, cmd: langs.Command) !langs.CommandList { - var cmds = langs.CommandList.init(allocator); - try cmds.append(cmd); - return cmds; -} - -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. - - var loadargs = langs.ArgList.init(allocator); - defer loadargs.deinit(); - try loadargs.append(":1"); - - var cmds = try wrapInCmdList(allocator, langs.Command{ - .command = .Load, - .args = loadargs, - }); - - defer cmds.deinit(); - - // we keep - // - a CommandList with the full commands we have right now - // - a Command with the current last typed successful command - - // - one runner that contains the current full state of the image - // as if the current cmds was ran over it (TODO better wording) - // - one runner that gets copied from the original on every new - // command the user issues - - var lang = langs.Lang.init(allocator); - defer lang.deinit(); - - var current: langs.Command = undefined; - - var runner = runners.Runner.init(allocator); - defer runner.deinit(); - - // run the load command - try runner.runCommands(cmds, true); - - var runqs_args = langs.ArgList.init(allocator); - defer runqs_args.deinit(); - - // TODO change the runqs command to something given in an env var - try runqs_args.append("ristretto"); - - 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) break; - return err; - }; - - if (std.mem.eql(u8, line, "push")) { - try cmds.append(current); - - // run the current added command to main cmds list - // with the main parent runner - var cmds_wrapped = try wrapInCmdList(allocator, current); - defer cmds_wrapped.deinit(); - try runner.runCommands(cmds_wrapped, true); - - continue; - } else if (std.mem.eql(u8, line, "list")) { - try printer.printList(cmds, stdout); - continue; - } else if (std.mem.eql(u8, line, "save")) { - try file.seekTo(0); - try printer.printList(cmds, stream); - continue; - } - - var cmds_parsed = lang.parse(line) catch |err| { - std.debug.warn("repl: error while parsing: {}\n", err); - continue; - }; - current = cmds_parsed.at(0); - - // by cloning the parent runner, we can iteratively write - // whatever command we want and only commit the good results - // back to the parent runner - var runner_clone = try runner.clone(); - defer runner_clone.deinit(); - - try cmds_parsed.append(langs.Command{ - .command = .RunQS, - .args = runqs_args, - }); - - try runner_clone.runCommands(cmds_parsed, true); - } -} - pub fn main() !void { const allocator = std.heap.direct_allocator; @@ -128,15 +18,9 @@ pub fn main() !void { var args_it = std.process.args(); - // TODO print help - _ = try (args_it.next(allocator) orelse @panic("expected exe name")); const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path")); - if (std.mem.eql(u8, scri_path, "repl")) { - return try doRepl(allocator, &args_it); - } - var file = try std.fs.File.openRead(scri_path); defer file.close(); diff --git a/src/printer.zig b/src/printer.zig deleted file mode 100644 index 2a30f8e..0000000 --- a/src/printer.zig +++ /dev/null @@ -1,39 +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", - - .Amp => "amp", - .RFlanger => "rflanger", - .Eq => "eq", - .Phaser => "phaser", - .Mbeq => "mbeq", - .Chorus => "chorus", - .PitchScaler => "pitchscaler", - .Reverb => "reverb", - .Highpass => "highpass", - .Delay => "delay", - .Vinyl => "vinyl", - .RevDelay => "revdelay", - - .Noise => "noise", - .WildNoise => "wildnoise", - .Write => "write", - - .Rotate => "rotate", - }; - - try stream.print("{}", command); - - for (cmd.args.toSlice()) |arg| { - try stream.print(" {}", arg); - } - - try stream.write(";\n"); - } -} diff --git a/src/runner.zig b/src/runner.zig index 072e38b..a348f22 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -33,11 +33,6 @@ pub const Runner = struct { } } - pub fn clone(self: *Runner) !Runner { - var cloned_image = if (self.image) |image| try image.clone() else null; - return Runner{ .allocator = self.allocator, .image = cloned_image }; - } - fn resolveArg(self: *Runner, load_path: []const u8) ![]const u8 { if (load_path[0] == ':') { // parse the index from 1 to end @@ -178,7 +173,7 @@ pub const Runner = struct { ); defer proc.deinit(); - std.debug.warn("running '{} {}'\n", program, out_path); + std.debug.warn("running '{} {}'", program, out_path); _ = try proc.spawnAndWait(); }