2019-07-08 02:03:55 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2019-07-10 19:06:44 +00:00
|
|
|
const plugin = @import("plugin.zig");
|
|
|
|
|
2019-07-08 03:23:12 +00:00
|
|
|
pub const ParseError = error{
|
2019-09-09 02:28:09 +00:00
|
|
|
OutOfMemory,
|
2019-07-09 03:04:01 +00:00
|
|
|
ArgRequired,
|
2019-08-08 23:27:24 +00:00
|
|
|
ParseFail,
|
2019-07-08 03:23:12 +00:00
|
|
|
};
|
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
pub const CommandType = enum {
|
|
|
|
Noop,
|
|
|
|
Load,
|
2019-07-10 19:06:44 +00:00
|
|
|
Quicksave,
|
2019-07-11 00:43:23 +00:00
|
|
|
RunQS,
|
2019-07-10 19:06:44 +00:00
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
Amp,
|
2019-07-10 16:24:03 +00:00
|
|
|
RFlanger,
|
2019-07-10 19:06:44 +00:00
|
|
|
Eq,
|
2019-07-10 19:24:43 +00:00
|
|
|
Phaser,
|
2019-07-10 19:43:30 +00:00
|
|
|
Mbeq,
|
2019-07-10 19:59:15 +00:00
|
|
|
Chorus,
|
2019-07-10 20:31:18 +00:00
|
|
|
PitchScaler,
|
2019-07-10 23:19:40 +00:00
|
|
|
Reverb,
|
2019-07-10 23:41:17 +00:00
|
|
|
Highpass,
|
2019-07-11 13:20:09 +00:00
|
|
|
Delay,
|
2019-07-13 19:46:26 +00:00
|
|
|
Vinyl,
|
2019-07-13 20:20:14 +00:00
|
|
|
RevDelay,
|
2020-01-25 20:47:20 +00:00
|
|
|
Gate,
|
2020-01-25 21:52:45 +00:00
|
|
|
Detune,
|
|
|
|
Overdrive,
|
|
|
|
Degrade,
|
|
|
|
RePsycho,
|
|
|
|
TalkBox,
|
2020-01-26 01:45:37 +00:00
|
|
|
DynComp,
|
2020-01-26 02:32:24 +00:00
|
|
|
ThruZero,
|
2020-01-26 02:52:22 +00:00
|
|
|
Foverdrive,
|
2020-01-26 03:03:15 +00:00
|
|
|
Gverb,
|
2020-01-26 03:13:53 +00:00
|
|
|
Invert,
|
2020-01-26 03:28:59 +00:00
|
|
|
TapeDelay,
|
2020-01-26 05:05:43 +00:00
|
|
|
ModDelay,
|
2020-01-26 17:03:01 +00:00
|
|
|
MultiChorus,
|
2020-01-26 17:50:50 +00:00
|
|
|
Saturator,
|
|
|
|
VintageDelay,
|
2019-07-29 14:51:40 +00:00
|
|
|
|
2019-07-13 20:42:46 +00:00
|
|
|
Noise,
|
2019-07-13 22:55:57 +00:00
|
|
|
WildNoise,
|
2019-07-29 14:51:40 +00:00
|
|
|
Write,
|
2019-10-22 20:08:19 +00:00
|
|
|
Embed,
|
2019-07-22 20:04:43 +00:00
|
|
|
|
|
|
|
Rotate,
|
2019-07-08 02:03:55 +00:00
|
|
|
};
|
|
|
|
|
2020-05-31 18:20:19 +00:00
|
|
|
pub const NewCommandType = enum {
|
2020-05-30 19:57:00 +00:00
|
|
|
/// "LV2 Commands" are commands that receive split, index, and then receive
|
|
|
|
/// any f64 arguments.
|
|
|
|
lv2_command,
|
|
|
|
};
|
|
|
|
|
2020-05-31 19:55:50 +00:00
|
|
|
fn LV2Command(
|
|
|
|
comptime tag: NewCommand.Tag,
|
|
|
|
comptime lv2_url: []const u8,
|
|
|
|
comptime LV2Parameters: type,
|
|
|
|
) type {
|
|
|
|
return struct {
|
|
|
|
pub const base_tag = tag;
|
|
|
|
pub const command_type = NewCommandType.lv2_command;
|
|
|
|
pub const lv2_url = lv2_url;
|
|
|
|
|
|
|
|
base: NewCommand,
|
|
|
|
split: usize,
|
|
|
|
index: usize,
|
|
|
|
parameters: LV2Parameters,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:57:00 +00:00
|
|
|
pub const NewCommand = struct {
|
2020-05-31 01:25:30 +00:00
|
|
|
tag: Tag,
|
|
|
|
|
|
|
|
pub const Tag = enum {
|
|
|
|
noop,
|
|
|
|
load,
|
|
|
|
quicksave,
|
|
|
|
runqs,
|
|
|
|
|
|
|
|
amp,
|
|
|
|
rflanger,
|
|
|
|
eq,
|
|
|
|
phaser,
|
|
|
|
mbeq,
|
|
|
|
chorus,
|
|
|
|
pitchscaler,
|
|
|
|
reverb,
|
|
|
|
highpass,
|
|
|
|
delay,
|
|
|
|
vinyl,
|
|
|
|
revdelay,
|
|
|
|
gate,
|
|
|
|
detune,
|
|
|
|
overdrive,
|
|
|
|
degrade,
|
|
|
|
repsycho,
|
|
|
|
talkbox,
|
|
|
|
dyncomp,
|
|
|
|
thruzero,
|
|
|
|
foverdrive,
|
|
|
|
gverb,
|
|
|
|
invert,
|
|
|
|
tapedelay,
|
|
|
|
moddelay,
|
|
|
|
multichorus,
|
|
|
|
saturator,
|
|
|
|
vintagedelay,
|
|
|
|
|
|
|
|
noise,
|
|
|
|
wildnoise,
|
|
|
|
write,
|
|
|
|
embed,
|
|
|
|
|
|
|
|
rotate,
|
|
|
|
};
|
|
|
|
|
2020-05-31 01:39:45 +00:00
|
|
|
pub fn tagToType(tag: Tag) type {
|
|
|
|
return switch (tag) {
|
|
|
|
.noop => Noop,
|
|
|
|
.load => Load,
|
|
|
|
.quicksave => Quicksave,
|
|
|
|
.runqs => RunQS,
|
|
|
|
|
|
|
|
.amp => Amp,
|
|
|
|
.rflanger => RFlanger,
|
2020-05-31 19:55:50 +00:00
|
|
|
.eq => Eq,
|
|
|
|
.phaser => Phaser,
|
|
|
|
// .mbeq => Mbeq,
|
|
|
|
// .chorus => Chorus,
|
|
|
|
// .pitchscaler => Pitchscaler,
|
|
|
|
// .reverb => Reverb,
|
|
|
|
// .highpass => Highpass,
|
|
|
|
// .delay => Delay,
|
|
|
|
// .vinyl => Vinyl,
|
|
|
|
// .revdelay => Revdelay,
|
|
|
|
// .gate => Gate,
|
|
|
|
// .detune => Detune,
|
|
|
|
// .overdrive => Overdrive,
|
|
|
|
// .degrade => Degrade,
|
|
|
|
// .repsycho => Repsycho,
|
|
|
|
// .talkbox => Talkbox,
|
|
|
|
// .dyncomp => Dyncomp,
|
|
|
|
// .thruzero => Thruzero,
|
|
|
|
// .foverdrive => Foverdrive,
|
|
|
|
// .gverb => Gverb,
|
|
|
|
// .invert => Invert,
|
|
|
|
// .tapedelay => Tapedelay,
|
|
|
|
// .moddelay => Moddelay,
|
|
|
|
// .multichorus => Multichorus,
|
|
|
|
// .saturator => Saturator,
|
|
|
|
// .vintagedelay => Vintagedelay,
|
|
|
|
|
2020-05-31 01:39:45 +00:00
|
|
|
else => @panic("TODO"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-31 01:56:45 +00:00
|
|
|
pub fn cast(base: *const @This(), comptime T: type) ?*const T {
|
2020-05-31 01:25:30 +00:00
|
|
|
if (base.tag != T.base_tag)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return @fieldParentPtr(T, "base", base);
|
|
|
|
}
|
|
|
|
|
2020-05-31 01:39:45 +00:00
|
|
|
pub fn print(base: *const @This()) void {
|
2020-05-31 02:57:34 +00:00
|
|
|
std.debug.warn("tag: {}\n", .{base.tag});
|
2020-05-31 01:39:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 01:25:30 +00:00
|
|
|
pub const Noop = struct {
|
|
|
|
pub const base_tag = Tag.noop;
|
|
|
|
base: NewCommand,
|
|
|
|
};
|
2020-05-30 19:57:00 +00:00
|
|
|
|
|
|
|
pub const Load = struct {
|
2020-05-31 01:25:30 +00:00
|
|
|
pub const base_tag = Tag.load;
|
|
|
|
base: NewCommand,
|
2020-05-30 19:57:00 +00:00
|
|
|
path: []const u8,
|
|
|
|
};
|
|
|
|
|
2020-05-31 01:25:30 +00:00
|
|
|
pub const Quicksave = struct {
|
|
|
|
pub const base_tag = Tag.quicksave;
|
|
|
|
base: NewCommand,
|
|
|
|
};
|
2020-05-30 19:57:00 +00:00
|
|
|
|
|
|
|
pub const RunQS = struct {
|
2020-05-31 01:25:30 +00:00
|
|
|
pub const base_tag = Tag.runqs;
|
2020-05-30 19:57:00 +00:00
|
|
|
program: []const u8,
|
2020-05-31 01:25:30 +00:00
|
|
|
base: NewCommand,
|
2020-05-30 19:57:00 +00:00
|
|
|
};
|
|
|
|
|
2020-05-31 19:55:50 +00:00
|
|
|
pub const Amp = LV2Command(
|
|
|
|
.amp,
|
|
|
|
"http://lv2plug.in/plugins/eg-amp",
|
|
|
|
struct {
|
|
|
|
gain: f32
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
pub const RFlanger = LV2Command(
|
|
|
|
.rflanger,
|
|
|
|
"http://plugin.org.uk/swh-plugins/retroFlange",
|
|
|
|
struct {
|
|
|
|
delay_depth_avg: f32, law_freq: f32
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
pub const Eq = LV2Command(
|
|
|
|
.rflanger,
|
|
|
|
"http://plugin.org.uk/swh-plugins/dj_eq_mono",
|
|
|
|
struct {
|
|
|
|
lo: f32, mid: f32, hi: f32
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
pub const Phaser = LV2Command(
|
|
|
|
.rflanger,
|
|
|
|
"http://plugin.org.uk/swh-plugins/lfoPhaser",
|
|
|
|
struct {
|
|
|
|
lfo_rate: f32, lfo_depth: f32, fb: f32, spread: f32
|
|
|
|
},
|
|
|
|
);
|
2020-05-30 19:57:00 +00:00
|
|
|
};
|
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
pub const Command = struct {
|
|
|
|
command: CommandType,
|
2019-07-08 03:09:34 +00:00
|
|
|
args: ArgList,
|
2019-07-10 23:46:22 +00:00
|
|
|
cur_idx: usize = 0,
|
2019-07-08 02:03:55 +00:00
|
|
|
|
2019-09-03 15:45:17 +00:00
|
|
|
pub fn print(self: Command) void {
|
2020-01-15 01:31:20 +00:00
|
|
|
std.debug.warn("cmd:{}\n", .{self.command});
|
2019-07-08 02:03:55 +00:00
|
|
|
}
|
2019-07-09 01:40:52 +00:00
|
|
|
|
2019-09-03 15:45:17 +00:00
|
|
|
pub fn argAt(self: Command, idx: usize) ![]const u8 {
|
2020-04-10 02:51:33 +00:00
|
|
|
std.debug.warn("{} {}", .{ idx, self.args.items.len });
|
2019-07-09 03:04:01 +00:00
|
|
|
|
2020-04-10 02:51:33 +00:00
|
|
|
if (idx > (self.args.items.len - 1)) {
|
2020-01-15 01:31:20 +00:00
|
|
|
std.debug.warn("Expected argument at index {}\n", .{idx});
|
2019-07-09 03:04:01 +00:00
|
|
|
return ParseError.ArgRequired;
|
|
|
|
}
|
|
|
|
|
2020-04-10 02:51:33 +00:00
|
|
|
return self.args.items[idx];
|
2019-07-09 03:04:01 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 15:45:17 +00:00
|
|
|
pub fn usizeArgAt(self: Command, idx: usize) !usize {
|
2019-07-09 03:04:01 +00:00
|
|
|
var arg = try self.argAt(idx);
|
|
|
|
return try std.fmt.parseInt(usize, arg, 10);
|
|
|
|
}
|
|
|
|
|
2019-07-10 23:46:22 +00:00
|
|
|
pub fn consumePosition(self: *Command) !plugin.Position {
|
|
|
|
self.cur_idx = 2;
|
2019-07-10 19:06:44 +00:00
|
|
|
return plugin.Position{
|
|
|
|
.split = try self.usizeArgAt(0),
|
|
|
|
.index = try self.usizeArgAt(1),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:45:17 +00:00
|
|
|
pub fn intArgAt(self: Command, idx: usize) !i32 {
|
2019-07-09 03:04:01 +00:00
|
|
|
var arg = try self.argAt(idx);
|
2019-07-09 01:40:52 +00:00
|
|
|
return try std.fmt.parseInt(i32, arg, 10);
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:45:17 +00:00
|
|
|
pub fn floatArgAt(self: Command, idx: usize) !f32 {
|
2019-07-09 03:04:01 +00:00
|
|
|
var arg = try self.argAt(idx);
|
2019-07-09 01:40:52 +00:00
|
|
|
return try std.fmt.parseFloat(f32, arg);
|
|
|
|
}
|
2019-07-10 19:43:30 +00:00
|
|
|
|
|
|
|
pub fn floatArgMany(
|
2019-09-03 15:45:17 +00:00
|
|
|
self: Command,
|
2019-07-10 19:43:30 +00:00
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
start_index: usize,
|
|
|
|
elements: usize,
|
|
|
|
default: f32,
|
|
|
|
) ![]const f32 {
|
|
|
|
var i: usize = start_index;
|
|
|
|
var arr = std.ArrayList(f32).init(allocator);
|
|
|
|
|
|
|
|
while (i < elements) : (i += 1) {
|
|
|
|
var value: f32 = self.floatArgAt(i) catch |err| blk: {
|
2020-01-15 01:31:20 +00:00
|
|
|
std.debug.warn("\tdoing default on arg {}\n", .{i});
|
2019-07-10 19:43:30 +00:00
|
|
|
break :blk default;
|
|
|
|
};
|
|
|
|
|
|
|
|
try arr.append(value);
|
|
|
|
}
|
|
|
|
|
2020-05-12 20:26:33 +00:00
|
|
|
return arr.items;
|
2019-07-10 19:43:30 +00:00
|
|
|
}
|
2019-07-10 19:59:15 +00:00
|
|
|
|
|
|
|
pub fn appendParam(
|
2019-07-10 23:46:22 +00:00
|
|
|
self: *Command,
|
2019-07-10 19:59:15 +00:00
|
|
|
params: *plugin.ParamList,
|
|
|
|
symbol: []const u8,
|
|
|
|
) !void {
|
2019-07-10 23:46:22 +00:00
|
|
|
var val = try self.floatArgAt(self.cur_idx);
|
|
|
|
self.cur_idx += 1;
|
|
|
|
|
2019-07-10 19:59:15 +00:00
|
|
|
try params.append(plugin.Param{
|
|
|
|
.sym = symbol,
|
|
|
|
.value = val,
|
|
|
|
});
|
|
|
|
}
|
2019-07-13 21:17:44 +00:00
|
|
|
|
|
|
|
pub fn appendParamMap(
|
|
|
|
self: *Command,
|
|
|
|
map: *plugin.ParamMap,
|
|
|
|
symbol: []const u8,
|
|
|
|
) !void {
|
|
|
|
var val = try self.floatArgAt(self.cur_idx);
|
|
|
|
self.cur_idx += 1;
|
|
|
|
_ = try map.put(symbol, val);
|
|
|
|
}
|
2019-07-08 03:09:34 +00:00
|
|
|
|
2019-09-03 15:45:17 +00:00
|
|
|
pub fn copy(self: Command, allocator: *std.mem.Allocator) !*Command {
|
|
|
|
var cmd = try allocator.create(Command);
|
|
|
|
cmd.* = Command{
|
|
|
|
.command = self.command,
|
|
|
|
.args = self.args,
|
|
|
|
.cur_idx = self.cur_idx,
|
|
|
|
};
|
2019-08-09 21:40:11 +00:00
|
|
|
|
2019-09-03 15:45:17 +00:00
|
|
|
return cmd;
|
2019-08-09 21:40:11 +00:00
|
|
|
}
|
2019-09-03 15:45:17 +00:00
|
|
|
};
|
2019-08-09 21:40:11 +00:00
|
|
|
|
2020-05-31 02:57:42 +00:00
|
|
|
pub const CommandList = std.ArrayList(*NewCommand);
|
2019-09-03 15:45:17 +00:00
|
|
|
pub const ArgList = std.ArrayList([]const u8);
|
2019-08-09 21:40:11 +00:00
|
|
|
|
2019-09-03 15:45:17 +00:00
|
|
|
pub const KeywordMap = std.StringHashMap(CommandType);
|
2019-07-08 02:03:55 +00:00
|
|
|
|
2019-10-06 18:07:30 +00:00
|
|
|
/// A parser.
|
2019-07-08 02:03:55 +00:00
|
|
|
pub const Lang = struct {
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
|
2019-08-09 14:28:40 +00:00
|
|
|
has_error: bool = false,
|
|
|
|
line: usize = 0,
|
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
pub fn init(allocator: *std.mem.Allocator) Lang {
|
2019-07-10 17:44:22 +00:00
|
|
|
return Lang{
|
|
|
|
.allocator = allocator,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-31 19:57:12 +00:00
|
|
|
pub fn deinit(self: *Lang) void {}
|
2019-08-08 00:04:51 +00:00
|
|
|
|
2019-09-10 15:12:10 +00:00
|
|
|
pub fn reset(self: *Lang) void {
|
|
|
|
self.has_error = false;
|
|
|
|
self.line = 0;
|
|
|
|
}
|
2020-01-15 01:31:20 +00:00
|
|
|
fn doError(self: *Lang, comptime fmt: []const u8, args: var) void {
|
|
|
|
std.debug.warn("error at line {}: ", .{self.line});
|
2019-08-09 14:28:40 +00:00
|
|
|
std.debug.warn(fmt, args);
|
2020-01-15 01:31:20 +00:00
|
|
|
std.debug.warn("\n", .{});
|
2019-08-09 14:28:40 +00:00
|
|
|
self.has_error = true;
|
|
|
|
}
|
|
|
|
|
2020-05-31 01:25:30 +00:00
|
|
|
fn parseCommandArguments(
|
|
|
|
self: *@This(),
|
|
|
|
comptime command_struct: type,
|
|
|
|
tok_it: *std.mem.TokenIterator,
|
|
|
|
commands: *CommandList,
|
|
|
|
) !void {
|
|
|
|
// Based on the command struct fields, we can parse the arguments.
|
|
|
|
var cmd = try self.allocator.create(command_struct);
|
2020-05-31 20:08:35 +00:00
|
|
|
const is_lv2_command = switch (command_struct.base_tag) {
|
|
|
|
.noop, .load, .quicksave, .runqs => false,
|
|
|
|
else => command_struct.command_type == .lv2_command,
|
|
|
|
};
|
2020-05-31 01:25:30 +00:00
|
|
|
|
2020-05-31 20:08:35 +00:00
|
|
|
// TODO: crash when no arguments are left but we still need
|
|
|
|
// arguments...
|
2020-05-31 01:25:30 +00:00
|
|
|
|
2020-05-31 20:08:35 +00:00
|
|
|
if (is_lv2_command) {
|
|
|
|
cmd.split = try std.fmt.parseInt(usize, tok_it.next().?, 10);
|
|
|
|
cmd.index = try std.fmt.parseInt(usize, tok_it.next().?, 10);
|
2020-05-31 01:25:30 +00:00
|
|
|
|
2020-05-31 20:08:35 +00:00
|
|
|
inline for (@typeInfo(@typeOf(command_struct.parameters)).Struct.fields) |cmd_field| {
|
|
|
|
const arg = tok_it.next().?;
|
|
|
|
const argument_value = switch (cmd_field.field_type) {
|
|
|
|
f32 => try std.fmt.parseFloat(f32, arg),
|
|
|
|
else => @compileError("LV2 parameter struct can only have f32 fields"),
|
|
|
|
};
|
2020-05-31 02:57:42 +00:00
|
|
|
|
2020-05-31 20:08:35 +00:00
|
|
|
std.debug.warn("parsing {}, arg of type {} => {}\n", .{
|
|
|
|
@typeName(command_struct),
|
|
|
|
@typeName(@TypeOf(argument_value)),
|
|
|
|
argument_value,
|
|
|
|
});
|
|
|
|
|
|
|
|
@field(cmd.params, cmd_field.name) = argument_value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inline for (@typeInfo(command_struct).Struct.fields) |cmd_field| {
|
|
|
|
comptime {
|
|
|
|
if (std.mem.eql(u8, cmd_field.name, "base")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const arg = tok_it.next().?;
|
|
|
|
const argument_value = switch (cmd_field.field_type) {
|
|
|
|
usize => try std.fmt.parseInt(usize, arg, 10),
|
|
|
|
i32 => try std.fmt.parseInt(i32, arg, 10),
|
|
|
|
f32 => try std.fmt.parseFloat(f32, arg),
|
|
|
|
[]const u8 => try self.allocator.dupe(u8, arg),
|
|
|
|
else => @panic("Invalid parameter type (" ++ @typeName(cmd_field.field_type) ++ ") left on command struct " ++ @typeName(command_struct) ++ "."),
|
|
|
|
};
|
|
|
|
|
|
|
|
std.debug.warn("parsing {}, arg of type {} => {}\n", .{
|
|
|
|
@typeName(command_struct),
|
|
|
|
@typeName(@TypeOf(argument_value)),
|
|
|
|
argument_value,
|
|
|
|
});
|
|
|
|
|
|
|
|
@field(cmd.params, cmd_field.name) = argument_value;
|
|
|
|
}
|
2020-05-31 01:25:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 01:39:52 +00:00
|
|
|
cmd.base.tag = command_struct.base_tag;
|
2020-05-31 02:57:42 +00:00
|
|
|
const command = cmd.base.cast(command_struct).?;
|
|
|
|
std.debug.warn("cmd: {}\n", .{command});
|
2020-05-31 01:39:52 +00:00
|
|
|
|
2020-05-31 02:57:42 +00:00
|
|
|
try commands.append(&cmd.base);
|
2020-05-31 01:25:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 01:29:13 +00:00
|
|
|
pub fn parse(self: *Lang, data: []const u8) !CommandList {
|
2020-04-10 02:51:33 +00:00
|
|
|
var splitted_it = std.mem.split(data, ";");
|
2019-07-08 02:03:55 +00:00
|
|
|
var cmds = CommandList.init(self.allocator);
|
|
|
|
|
|
|
|
while (splitted_it.next()) |stmt_orig| {
|
2019-08-09 14:28:40 +00:00
|
|
|
self.line += 1;
|
2019-07-08 02:03:55 +00:00
|
|
|
var stmt = std.mem.trimRight(u8, stmt_orig, "\n");
|
2019-07-08 16:55:54 +00:00
|
|
|
stmt = std.mem.trimLeft(u8, stmt, "\n");
|
2019-07-08 02:03:55 +00:00
|
|
|
|
|
|
|
if (stmt.len == 0) continue;
|
2019-08-13 13:19:39 +00:00
|
|
|
if (std.mem.startsWith(u8, stmt, "#")) continue;
|
2019-07-08 02:03:55 +00:00
|
|
|
|
2020-05-31 01:25:30 +00:00
|
|
|
// TODO better tokenizer instead of just tokenize(" ")...maybe????
|
2019-07-08 03:09:34 +00:00
|
|
|
var tok_it = std.mem.tokenize(stmt, " ");
|
|
|
|
|
2019-07-08 03:23:12 +00:00
|
|
|
var cmd_opt = tok_it.next();
|
2019-08-09 14:28:40 +00:00
|
|
|
if (cmd_opt == null) {
|
2020-01-15 01:31:20 +00:00
|
|
|
self.doError("No command given", .{});
|
2019-08-09 14:28:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-30 20:48:14 +00:00
|
|
|
const command_string = cmd_opt.?;
|
2019-07-10 17:44:22 +00:00
|
|
|
|
2020-05-30 20:48:14 +00:00
|
|
|
var found: bool = false;
|
|
|
|
|
|
|
|
inline for (@typeInfo(NewCommand).Struct.decls) |cmd_struct_decl| {
|
2020-05-31 01:25:30 +00:00
|
|
|
switch (cmd_struct_decl.data) {
|
|
|
|
.Type => |typ| switch (@typeInfo(typ)) {
|
|
|
|
.Struct => {},
|
|
|
|
else => continue,
|
|
|
|
},
|
|
|
|
else => continue,
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:48:14 +00:00
|
|
|
const struct_name = cmd_struct_decl.name;
|
|
|
|
comptime var lowered_command_name = [_]u8{0} ** struct_name.len;
|
|
|
|
comptime {
|
|
|
|
for (struct_name) |c, i| {
|
|
|
|
lowered_command_name[i] = std.ascii.toLower(c);
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 23:13:19 +00:00
|
|
|
|
2020-05-31 01:25:30 +00:00
|
|
|
// if we have a match, we know the proper struct type
|
|
|
|
// to use. this actually works compared to storing command_struct
|
|
|
|
// in a variable because then that variable must be comptime.
|
|
|
|
|
|
|
|
// the drawback of this approach is that our emitted code is basically linear
|
|
|
|
// because we don't use the hashmap anymore. maybe #5359 can help.
|
|
|
|
|
2020-05-30 20:48:14 +00:00
|
|
|
if (std.mem.eql(u8, &lowered_command_name, command_string)) {
|
|
|
|
found = true;
|
2020-05-31 01:25:30 +00:00
|
|
|
const cmd_struct_type = cmd_struct_decl.data.Type;
|
|
|
|
try self.parseCommandArguments(cmd_struct_type, &tok_it, &cmds);
|
2020-05-30 20:48:14 +00:00
|
|
|
}
|
2019-07-08 03:09:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 20:48:14 +00:00
|
|
|
if (!found) {
|
|
|
|
self.doError("Unknown command '{}' ({})", .{ command_string, command_string.len });
|
2019-08-09 14:28:40 +00:00
|
|
|
continue;
|
2020-05-30 20:48:14 +00:00
|
|
|
}
|
2019-08-08 23:13:49 +00:00
|
|
|
|
2020-05-30 20:48:14 +00:00
|
|
|
// try cmds.append(cmd);
|
2019-07-08 02:03:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:28:40 +00:00
|
|
|
if (self.has_error) return ParseError.ParseFail;
|
2019-08-08 23:30:24 +00:00
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
return cmds;
|
|
|
|
}
|
|
|
|
};
|
2019-07-08 03:09:34 +00:00
|
|
|
|
2019-08-08 00:04:51 +00:00
|
|
|
test "noop" {
|
|
|
|
var lang = Lang.init(std.heap.direct_allocator);
|
2019-08-08 00:09:04 +00:00
|
|
|
defer lang.deinit();
|
|
|
|
|
2019-08-08 00:04:51 +00:00
|
|
|
var cmds = try lang.parse("noop;");
|
2019-08-08 00:09:04 +00:00
|
|
|
defer cmds.deinit();
|
|
|
|
|
2019-08-08 00:04:51 +00:00
|
|
|
std.testing.expectEqual(cmds.len, 1);
|
2020-05-12 20:48:15 +00:00
|
|
|
std.testing.expectEqual(cmds.items[0].command, .Noop);
|
2019-08-08 00:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test "load, phaser, quicksave" {
|
|
|
|
var lang = Lang.init(std.heap.direct_allocator);
|
2019-08-08 00:09:04 +00:00
|
|
|
defer lang.deinit();
|
|
|
|
|
2019-08-08 00:04:51 +00:00
|
|
|
const prog =
|
|
|
|
\\load :0;
|
|
|
|
\\phaser 3 1 25 0.25 0 1;
|
|
|
|
\\quicksave;
|
|
|
|
;
|
2019-08-08 00:09:04 +00:00
|
|
|
|
2019-08-08 00:04:51 +00:00
|
|
|
var cmds = try lang.parse(prog);
|
2019-08-08 00:09:04 +00:00
|
|
|
defer cmds.deinit();
|
2019-08-08 00:04:51 +00:00
|
|
|
|
|
|
|
std.testing.expectEqual(cmds.len, 3);
|
2020-05-12 20:48:15 +00:00
|
|
|
std.testing.expectEqual(cmds.items[0].command, .Load);
|
|
|
|
std.testing.expectEqual(cmds.items[1].command, .Phaser);
|
|
|
|
std.testing.expectEqual(cmds.items[2].command, .Quicksave);
|
2019-08-08 00:04:51 +00:00
|
|
|
}
|