Compare commits

..

11 commits

5 changed files with 191 additions and 9 deletions

View file

@ -131,7 +131,6 @@ pub const Image = struct {
/// Open a BMP image for later. /// Open a BMP image for later.
pub fn open(allocator: *std.mem.Allocator, path: []const u8) !*Image { pub fn open(allocator: *std.mem.Allocator, path: []const u8) !*Image {
var in_fmt = mkSfInfo(); var in_fmt = mkSfInfo();
var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt); var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt);
var image = try allocator.create(Image); var image = try allocator.create(Image);
@ -149,6 +148,26 @@ pub const Image = struct {
return image; 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 { pub fn close(self: *Image) void {
//self.allocator.free(self.path); //self.allocator.free(self.path);
//self.allocator.free(self.curpath); //self.allocator.free(self.curpath);

View file

@ -3,10 +3,8 @@ const std = @import("std");
const plugin = @import("plugin.zig"); const plugin = @import("plugin.zig");
pub const ParseError = error{ pub const ParseError = error{
NoCommandGiven, OutOfMemory,
UnknownCommand,
ArgRequired, ArgRequired,
FloatParseFail,
ParseFail, ParseFail,
}; };
@ -160,6 +158,11 @@ 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);
@ -261,7 +264,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) {
std.debug.warn("expected {} arguments, found {}\n", count, args.len); self.doError("expected {} arguments, found {}", count, args.len);
return error.ArgRequired; return error.ArgRequired;
} }
} }
@ -274,7 +277,7 @@ pub const Lang = struct {
var i: usize = 0; var i: usize = 0;
if (args.len != count) { 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; return error.ArgRequired;
} }
@ -319,7 +322,7 @@ pub const Lang = struct {
self.has_error = true; 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, ";"); 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);
@ -359,7 +362,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("Unknown command '{}' (length {})", command, command.len); //self.doError("error validating command '{}': {}", command, err);
continue; continue;
}; };

View file

@ -1,12 +1,122 @@
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");
_ = @import("runner.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 { pub fn main() !void {
const allocator = std.heap.direct_allocator; const allocator = std.heap.direct_allocator;
@ -18,9 +128,15 @@ pub fn main() !void {
var args_it = std.process.args(); var args_it = std.process.args();
// TODO print help
_ = try (args_it.next(allocator) orelse @panic("expected exe name")); _ = try (args_it.next(allocator) orelse @panic("expected exe name"));
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"));
if (std.mem.eql(u8, scri_path, "repl")) {
return try doRepl(allocator, &args_it);
}
var file = try std.fs.File.openRead(scri_path); var file = try std.fs.File.openRead(scri_path);
defer file.close(); defer file.close();

39
src/printer.zig Normal file
View file

@ -0,0 +1,39 @@
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");
}
}

View file

@ -33,6 +33,11 @@ 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 { fn resolveArg(self: *Runner, load_path: []const u8) ![]const u8 {
if (load_path[0] == ':') { if (load_path[0] == ':') {
// parse the index from 1 to end // parse the index from 1 to end
@ -173,7 +178,7 @@ pub const Runner = struct {
); );
defer proc.deinit(); defer proc.deinit();
std.debug.warn("running '{} {}'", program, out_path); std.debug.warn("running '{} {}'\n", program, out_path);
_ = try proc.spawnAndWait(); _ = try proc.spawnAndWait();
} }