2019-07-05 19:59:45 +00:00
|
|
|
const std = @import("std");
|
2019-07-08 02:03:55 +00:00
|
|
|
const langs = @import("lang.zig");
|
2019-07-08 15:38:16 +00:00
|
|
|
const runners = @import("runner.zig");
|
2019-09-10 14:45:04 +00:00
|
|
|
const printer = @import("printer.zig");
|
2019-07-05 19:59:45 +00:00
|
|
|
|
2022-04-27 23:01:09 +00:00
|
|
|
const log = std.log.scoped(.scritcher);
|
|
|
|
|
2019-08-08 00:04:51 +00:00
|
|
|
test "scritcher" {
|
|
|
|
_ = @import("lang.zig");
|
2019-08-08 19:48:31 +00:00
|
|
|
_ = @import("runner.zig");
|
2019-08-08 00:04:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 13:53:09 +00:00
|
|
|
const readline = @cImport({
|
|
|
|
@cInclude("stdio.h");
|
|
|
|
@cInclude("stdlib.h");
|
|
|
|
|
|
|
|
@cInclude("readline/readline.h");
|
|
|
|
@cInclude("readline/history.h");
|
|
|
|
});
|
|
|
|
|
2022-04-27 23:01:12 +00:00
|
|
|
fn wrapInCmdList(allocator: std.mem.Allocator, cmd: *langs.Command) !langs.CommandList {
|
2019-09-11 01:00:07 +00:00
|
|
|
var cmds = langs.CommandList.init(allocator);
|
2019-09-11 00:31:23 +00:00
|
|
|
try cmds.append(cmd);
|
|
|
|
return cmds;
|
|
|
|
}
|
|
|
|
|
2022-04-27 23:01:12 +00:00
|
|
|
fn copyCommandToHeap(allocator: std.mem.Allocator, command: langs.Command, comptime tag: langs.Command.Tag) !*langs.Command {
|
2020-06-02 02:10:30 +00:00
|
|
|
const CommandStruct = langs.Command.tagToType(tag);
|
|
|
|
const casted = command.cast(CommandStruct).?;
|
|
|
|
var heap_cmd = try allocator.create(CommandStruct);
|
|
|
|
|
|
|
|
@memcpy(
|
|
|
|
@ptrCast([*]u8, &heap_cmd),
|
|
|
|
@ptrCast([*]const u8, &casted),
|
|
|
|
@sizeOf(CommandStruct),
|
|
|
|
);
|
|
|
|
|
|
|
|
return &heap_cmd.base;
|
|
|
|
}
|
|
|
|
|
2022-04-27 23:01:12 +00:00
|
|
|
pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
|
2019-12-08 15:14:31 +00:00
|
|
|
var stdout_file = std.io.getStdOut();
|
2021-04-04 01:16:04 +00:00
|
|
|
const stdout = &stdout_file.writer();
|
2022-04-28 03:08:52 +00:00
|
|
|
const scri_path = (args_it.next() orelse @panic("expected scri path"));
|
2022-04-27 23:01:00 +00:00
|
|
|
errdefer allocator.free(scri_path);
|
|
|
|
defer allocator.free(scri_path);
|
2019-09-08 20:06:19 +00:00
|
|
|
|
2020-03-26 19:35:58 +00:00
|
|
|
var file_read_opt: ?std.fs.File = std.fs.cwd().openFile(scri_path, .{}) catch |err| blk: {
|
2019-10-20 14:37:11 +00:00
|
|
|
if (err == error.FileNotFound) break :blk null;
|
|
|
|
return err;
|
|
|
|
};
|
|
|
|
const total_bytes = if (file_read_opt) |file_read| try file_read.getEndPos() else 0;
|
2019-10-06 18:07:30 +00:00
|
|
|
|
|
|
|
var cmds = langs.CommandList.init(allocator);
|
2019-09-11 00:31:23 +00:00
|
|
|
defer cmds.deinit();
|
2019-09-08 20:27:59 +00:00
|
|
|
|
2019-10-06 18:07:30 +00:00
|
|
|
var lang = langs.Lang.init(allocator);
|
|
|
|
defer lang.deinit();
|
2019-10-06 17:39:15 +00:00
|
|
|
|
|
|
|
if (total_bytes > 0) {
|
|
|
|
var scri_existing = try allocator.alloc(u8, total_bytes);
|
2019-10-20 14:37:11 +00:00
|
|
|
_ = try file_read_opt.?.read(scri_existing);
|
2022-04-27 23:01:00 +00:00
|
|
|
defer allocator.free(scri_existing);
|
2019-10-06 18:12:19 +00:00
|
|
|
|
2020-08-18 23:41:57 +00:00
|
|
|
// we can't defer this directly because we copy the
|
|
|
|
// Command pointers to the cmds list. running deinit() directly
|
|
|
|
// would cause those pointers to be freed.
|
2019-10-06 17:39:15 +00:00
|
|
|
var existing_cmds = try lang.parse(scri_existing);
|
2020-08-18 23:41:57 +00:00
|
|
|
defer existing_cmds.list.deinit();
|
2019-10-06 17:39:15 +00:00
|
|
|
|
|
|
|
// copy the existing command list into the repl's command list
|
2020-08-18 23:40:06 +00:00
|
|
|
for (existing_cmds.list.items) |existing_cmd| {
|
2019-10-06 17:39:15 +00:00
|
|
|
try cmds.append(existing_cmd);
|
|
|
|
}
|
2019-10-06 18:07:30 +00:00
|
|
|
} else {
|
|
|
|
// if there isn't any commands on the file, we load our default
|
2019-10-06 18:16:41 +00:00
|
|
|
// 'load :0' command
|
2019-10-06 18:07:30 +00:00
|
|
|
|
2020-06-02 01:34:56 +00:00
|
|
|
// TODO: deliberate memleak here. we only allocate this
|
|
|
|
// command once, for the start of the file, so.
|
|
|
|
var load_cmd = try allocator.create(langs.Command.Load);
|
2020-08-18 23:53:54 +00:00
|
|
|
load_cmd.path = ":0";
|
2020-06-02 01:34:56 +00:00
|
|
|
load_cmd.base.tag = langs.Command.Tag.load;
|
|
|
|
|
|
|
|
// taking address is fine, because load_cmd lives in the lifetime
|
|
|
|
// of the allocator.
|
|
|
|
try cmds.append(&load_cmd.base);
|
2019-10-06 17:39:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 14:37:11 +00:00
|
|
|
if (file_read_opt) |file_read| {
|
|
|
|
file_read.close();
|
|
|
|
}
|
2019-10-06 18:07:30 +00:00
|
|
|
|
2022-04-27 23:01:06 +00:00
|
|
|
var file = try std.fs.cwd().createFile(scri_path, .{
|
2020-03-26 19:35:58 +00:00
|
|
|
.read = false,
|
2022-04-27 23:01:06 +00:00
|
|
|
.truncate = true,
|
2020-03-26 19:35:58 +00:00
|
|
|
});
|
2019-10-06 17:39:15 +00:00
|
|
|
defer file.close();
|
|
|
|
|
2021-04-04 01:16:04 +00:00
|
|
|
var out = file.writer();
|
2020-03-26 19:35:58 +00:00
|
|
|
var stream = &out;
|
2019-10-06 17:39:15 +00:00
|
|
|
|
|
|
|
// 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
|
2019-10-06 18:07:30 +00:00
|
|
|
try printer.printList(cmds, stdout);
|
2019-10-06 17:39:15 +00:00
|
|
|
try printer.printList(cmds, stream);
|
|
|
|
|
2019-09-10 18:44:10 +00:00
|
|
|
// we keep
|
|
|
|
// - a CommandList with the full commands we have right now
|
|
|
|
// - a Command with the current last typed successful command
|
|
|
|
|
2019-09-11 00:31:23 +00:00
|
|
|
// - 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
|
|
|
|
|
2019-09-10 18:44:10 +00:00
|
|
|
var current: langs.Command = undefined;
|
|
|
|
|
2019-10-06 18:16:41 +00:00
|
|
|
var runner = runners.Runner.init(allocator, true);
|
2019-09-11 00:31:23 +00:00
|
|
|
defer runner.deinit();
|
|
|
|
|
|
|
|
// run the load command
|
|
|
|
try runner.runCommands(cmds, true);
|
|
|
|
|
2020-04-10 03:01:23 +00:00
|
|
|
const wanted_runner: []const u8 = std.os.getenv("SCRITCHER_RUNNER") orelse "ristretto";
|
2020-06-02 02:10:30 +00:00
|
|
|
|
|
|
|
var runqs_cmd = langs.Command.RunQS{
|
|
|
|
.base = langs.Command{ .tag = langs.Command.Tag.runqs },
|
|
|
|
.program = wanted_runner,
|
|
|
|
};
|
2019-09-11 01:00:07 +00:00
|
|
|
|
2019-09-08 20:27:59 +00:00
|
|
|
while (true) {
|
2019-09-10 17:51:41 +00:00
|
|
|
lang.reset();
|
2019-09-08 20:27:59 +00:00
|
|
|
|
2019-12-08 15:14:31 +00:00
|
|
|
var rd_line = readline.readline("> ");
|
2019-10-20 16:13:32 +00:00
|
|
|
if (rd_line == null) {
|
2022-04-27 23:01:09 +00:00
|
|
|
log.debug("leaving from eof\n", .{});
|
2019-10-20 16:13:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-10-06 13:53:09 +00:00
|
|
|
readline.add_history(rd_line);
|
2019-10-20 14:59:15 +00:00
|
|
|
//defer std.heap.c_allocator.destroy(rd_line);
|
2019-10-06 13:53:09 +00:00
|
|
|
|
2020-03-26 19:35:58 +00:00
|
|
|
var line = rd_line[0..std.mem.len(rd_line)];
|
2019-09-08 20:27:59 +00:00
|
|
|
|
2019-09-10 17:51:41 +00:00
|
|
|
if (std.mem.eql(u8, line, "push")) {
|
2020-06-02 02:10:30 +00:00
|
|
|
const heap_cmd = switch (current.tag) {
|
|
|
|
.noop => try copyCommandToHeap(allocator, current, .noop),
|
|
|
|
.load => try copyCommandToHeap(allocator, current, .load),
|
|
|
|
.quicksave => try copyCommandToHeap(allocator, current, .quicksave),
|
|
|
|
.runqs => try copyCommandToHeap(allocator, current, .runqs),
|
|
|
|
.amp => try copyCommandToHeap(allocator, current, .amp),
|
|
|
|
.rflanger => try copyCommandToHeap(allocator, current, .rflanger),
|
|
|
|
.eq => try copyCommandToHeap(allocator, current, .eq),
|
|
|
|
.phaser => try copyCommandToHeap(allocator, current, .phaser),
|
|
|
|
.mbeq => try copyCommandToHeap(allocator, current, .mbeq),
|
|
|
|
.chorus => try copyCommandToHeap(allocator, current, .chorus),
|
|
|
|
.pitchscaler => try copyCommandToHeap(allocator, current, .pitchscaler),
|
|
|
|
.reverb => try copyCommandToHeap(allocator, current, .reverb),
|
|
|
|
.highpass => try copyCommandToHeap(allocator, current, .highpass),
|
|
|
|
.delay => try copyCommandToHeap(allocator, current, .delay),
|
|
|
|
.vinyl => try copyCommandToHeap(allocator, current, .vinyl),
|
|
|
|
.revdelay => try copyCommandToHeap(allocator, current, .revdelay),
|
|
|
|
.gate => try copyCommandToHeap(allocator, current, .gate),
|
|
|
|
.detune => try copyCommandToHeap(allocator, current, .detune),
|
|
|
|
.overdrive => try copyCommandToHeap(allocator, current, .overdrive),
|
|
|
|
.degrade => try copyCommandToHeap(allocator, current, .degrade),
|
|
|
|
.repsycho => try copyCommandToHeap(allocator, current, .repsycho),
|
|
|
|
.talkbox => try copyCommandToHeap(allocator, current, .talkbox),
|
|
|
|
.dyncomp => try copyCommandToHeap(allocator, current, .dyncomp),
|
|
|
|
.thruzero => try copyCommandToHeap(allocator, current, .thruzero),
|
|
|
|
.foverdrive => try copyCommandToHeap(allocator, current, .foverdrive),
|
|
|
|
.gverb => try copyCommandToHeap(allocator, current, .gverb),
|
|
|
|
.invert => try copyCommandToHeap(allocator, current, .invert),
|
|
|
|
.tapedelay => try copyCommandToHeap(allocator, current, .tapedelay),
|
|
|
|
.moddelay => try copyCommandToHeap(allocator, current, .moddelay),
|
|
|
|
.multichorus => try copyCommandToHeap(allocator, current, .multichorus),
|
|
|
|
.saturator => try copyCommandToHeap(allocator, current, .saturator),
|
|
|
|
.vintagedelay => try copyCommandToHeap(allocator, current, .vintagedelay),
|
|
|
|
.noise => try copyCommandToHeap(allocator, current, .noise),
|
|
|
|
.wildnoise => try copyCommandToHeap(allocator, current, .wildnoise),
|
|
|
|
.write => try copyCommandToHeap(allocator, current, .write),
|
|
|
|
.embed => try copyCommandToHeap(allocator, current, .embed),
|
|
|
|
.rotate => try copyCommandToHeap(allocator, current, .rotate),
|
|
|
|
};
|
|
|
|
|
|
|
|
try cmds.append(heap_cmd);
|
2019-09-11 00:35:22 +00:00
|
|
|
|
|
|
|
// run the current added command to main cmds list
|
|
|
|
// with the main parent runner
|
2020-06-02 02:10:30 +00:00
|
|
|
var cmds_wrapped = try wrapInCmdList(allocator, heap_cmd);
|
2019-09-11 00:31:23 +00:00
|
|
|
defer cmds_wrapped.deinit();
|
|
|
|
try runner.runCommands(cmds_wrapped, true);
|
2019-09-11 00:35:22 +00:00
|
|
|
|
2019-09-10 17:51:41 +00:00
|
|
|
continue;
|
|
|
|
} else if (std.mem.eql(u8, line, "list")) {
|
2019-09-10 18:44:10 +00:00
|
|
|
try printer.printList(cmds, stdout);
|
|
|
|
continue;
|
|
|
|
} else if (std.mem.eql(u8, line, "save")) {
|
2019-10-06 17:39:15 +00:00
|
|
|
// seek to 0 instead of appending the new command
|
|
|
|
// NOTE appending single command might be faster
|
2019-09-10 18:44:10 +00:00
|
|
|
try file.seekTo(0);
|
2019-09-10 17:51:41 +00:00
|
|
|
try printer.printList(cmds, stream);
|
|
|
|
continue;
|
2019-10-06 00:44:44 +00:00
|
|
|
} else if (std.mem.eql(u8, line, "quit") or std.mem.eql(u8, line, "q")) {
|
2022-04-27 23:01:09 +00:00
|
|
|
log.debug("leaving\n", .{});
|
2019-10-06 00:44:44 +00:00
|
|
|
break;
|
2019-10-20 16:12:49 +00:00
|
|
|
} else if (std.mem.startsWith(u8, line, "#")) {
|
|
|
|
continue;
|
2019-09-10 17:51:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 02:28:09 +00:00
|
|
|
var cmds_parsed = lang.parse(line) catch |err| {
|
2022-04-27 23:01:09 +00:00
|
|
|
log.debug("repl: error while parsing: {}\n", .{err});
|
2019-09-09 02:28:09 +00:00
|
|
|
continue;
|
|
|
|
};
|
2020-06-02 19:16:43 +00:00
|
|
|
|
|
|
|
// no command? ignore!
|
2020-08-18 23:40:06 +00:00
|
|
|
if (cmds_parsed.list.items.len == 0) continue;
|
|
|
|
current = cmds_parsed.list.items[0].*;
|
2019-09-11 00:31:23 +00:00
|
|
|
|
2019-09-11 00:35:22 +00:00
|
|
|
// by cloning the parent runner, we can iteratively write
|
|
|
|
// whatever command we want and only commit the good results
|
|
|
|
// back to the parent runner
|
2019-09-11 00:31:23 +00:00
|
|
|
var runner_clone = try runner.clone();
|
|
|
|
defer runner_clone.deinit();
|
|
|
|
|
2020-06-02 02:10:30 +00:00
|
|
|
// taking address is fine, because runqs_cmd lives in the lifetime
|
|
|
|
// of this function.
|
2020-06-02 20:25:03 +00:00
|
|
|
try cmds_parsed.append(&runqs_cmd.base);
|
2019-09-11 01:00:07 +00:00
|
|
|
|
2020-06-02 20:25:03 +00:00
|
|
|
try runner_clone.runCommands(cmds_parsed, true);
|
2020-03-26 19:35:58 +00:00
|
|
|
_ = try stdout.write("\n");
|
2019-09-08 20:27:59 +00:00
|
|
|
}
|
2019-09-08 20:06:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 02:20:17 +00:00
|
|
|
fn doHelp() void {
|
2022-04-27 23:01:09 +00:00
|
|
|
log.debug("scritcher!\n", .{});
|
|
|
|
log.debug("usage: scritcher [run|help|repl]\n", .{});
|
|
|
|
log.debug("\tscritcher run path_to_script.scri path_to_input_file.bmp\n", .{});
|
|
|
|
log.debug("\tscritcher repl path_to_script.scri path_to_input_file.bmp\n", .{});
|
2021-04-04 02:20:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 23:01:12 +00:00
|
|
|
fn doRun(allocator: std.mem.Allocator, args_it: anytype) !void {
|
2021-04-04 02:21:50 +00:00
|
|
|
var lang = langs.Lang.init(allocator);
|
|
|
|
defer lang.deinit();
|
|
|
|
|
|
|
|
var runner = runners.Runner.init(allocator, false);
|
|
|
|
defer runner.deinit();
|
|
|
|
|
2022-04-28 03:08:52 +00:00
|
|
|
const scri_path = (args_it.next() orelse @panic("run: expected scri path"));
|
2021-04-04 02:21:50 +00:00
|
|
|
|
|
|
|
var file = try std.fs.cwd().openFile(scri_path, .{});
|
|
|
|
defer file.close();
|
|
|
|
|
|
|
|
// sadly, we read it all into memory. such is life
|
|
|
|
const total_bytes = try file.getEndPos();
|
|
|
|
|
|
|
|
var data = try allocator.alloc(u8, total_bytes);
|
|
|
|
defer allocator.free(data);
|
|
|
|
_ = try file.read(data);
|
|
|
|
|
|
|
|
var cmds = try lang.parse(data);
|
|
|
|
defer cmds.deinit();
|
|
|
|
|
|
|
|
try runner.runCommands(cmds, true);
|
|
|
|
}
|
|
|
|
|
2019-07-07 05:26:05 +00:00
|
|
|
pub fn main() !void {
|
2020-08-19 00:00:16 +00:00
|
|
|
var allocator_instance = std.heap.GeneralPurposeAllocator(.{}){};
|
|
|
|
defer {
|
|
|
|
_ = allocator_instance.deinit();
|
|
|
|
}
|
2022-04-28 03:08:52 +00:00
|
|
|
const allocator = allocator_instance.allocator();
|
2019-07-08 02:03:55 +00:00
|
|
|
|
2022-04-28 03:08:52 +00:00
|
|
|
var args_it = try std.process.argsWithAllocator(allocator);
|
|
|
|
defer args_it.deinit();
|
2019-07-08 02:03:55 +00:00
|
|
|
|
2020-08-18 23:54:04 +00:00
|
|
|
_ = args_it.skip();
|
2022-04-28 03:08:52 +00:00
|
|
|
const cli_command_opt = args_it.next();
|
2021-04-04 02:20:17 +00:00
|
|
|
if (cli_command_opt == null) {
|
|
|
|
return doHelp();
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:08:52 +00:00
|
|
|
const cli_command = cli_command_opt.?;
|
2019-07-08 02:03:55 +00:00
|
|
|
|
2021-04-04 02:05:56 +00:00
|
|
|
if (std.mem.eql(u8, cli_command, "help")) {
|
2021-04-04 02:20:17 +00:00
|
|
|
return doHelp();
|
2021-04-04 02:05:56 +00:00
|
|
|
} else if (std.mem.eql(u8, cli_command, "repl")) {
|
2020-06-02 01:35:07 +00:00
|
|
|
return try doRepl(allocator, &args_it);
|
2021-04-04 02:05:56 +00:00
|
|
|
} else if (std.mem.eql(u8, cli_command, "run")) {
|
2021-04-04 02:21:50 +00:00
|
|
|
return try doRun(allocator, &args_it);
|
2021-04-04 02:05:56 +00:00
|
|
|
} else {
|
2022-04-27 23:01:09 +00:00
|
|
|
log.debug("unknown command: '{s}'\n", .{cli_command});
|
2021-04-04 02:20:17 +00:00
|
|
|
return error.UnknownCommand;
|
2021-04-04 02:05:56 +00:00
|
|
|
}
|
2019-07-08 02:03:55 +00:00
|
|
|
}
|