Compare commits
3 commits
6e8ee7831a
...
ac4624ae58
Author | SHA1 | Date | |
---|---|---|---|
ac4624ae58 | |||
be280d44f3 | |||
4bbe980014 |
2 changed files with 114 additions and 0 deletions
82
src/lang.zig
Normal file
82
src/lang.zig
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const ParseError = error{
|
||||||
|
NoCommandGiven,
|
||||||
|
UnknownCommand,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const CommandType = enum {
|
||||||
|
Noop,
|
||||||
|
Load,
|
||||||
|
Amp,
|
||||||
|
Quicksave,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Command = struct {
|
||||||
|
command: CommandType,
|
||||||
|
args: ArgList,
|
||||||
|
|
||||||
|
pub fn print(self: *const Command) void {
|
||||||
|
std.debug.warn("cmd:{}\n", self.command);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const CommandList = std.ArrayList(*Command);
|
||||||
|
pub const ArgList = std.ArrayList([]const u8);
|
||||||
|
|
||||||
|
pub fn commandToCmdType(command: []const u8) ParseError!CommandType {
|
||||||
|
if (std.mem.eql(u8, command, "noop")) {
|
||||||
|
return CommandType.Noop;
|
||||||
|
} else if (std.mem.eql(u8, command, "load")) {
|
||||||
|
return CommandType.Load;
|
||||||
|
} else if (std.mem.eql(u8, command, "amp")) {
|
||||||
|
return CommandType.Amp;
|
||||||
|
} else if (std.mem.eql(u8, command, "quicksave")) {
|
||||||
|
return CommandType.Quicksave;
|
||||||
|
} else {
|
||||||
|
std.debug.warn("Unknown command: '{}'\n", command);
|
||||||
|
return ParseError.UnknownCommand;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Lang = struct {
|
||||||
|
allocator: *std.mem.Allocator,
|
||||||
|
|
||||||
|
pub fn init(allocator: *std.mem.Allocator) Lang {
|
||||||
|
return Lang{ .allocator = allocator };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(self: *Lang, data: []const u8) !CommandList {
|
||||||
|
var splitted_it = std.mem.separate(data, ";");
|
||||||
|
var cmds = CommandList.init(self.allocator);
|
||||||
|
|
||||||
|
while (splitted_it.next()) |stmt_orig| {
|
||||||
|
var stmt = std.mem.trimRight(u8, stmt_orig, "\n");
|
||||||
|
|
||||||
|
if (stmt.len == 0) continue;
|
||||||
|
|
||||||
|
// TODO better tokenizer instead of just tokenize(" ");
|
||||||
|
var tok_it = std.mem.tokenize(stmt, " ");
|
||||||
|
|
||||||
|
var cmd_opt = tok_it.next();
|
||||||
|
if (cmd_opt == null) return ParseError.NoCommandGiven;
|
||||||
|
|
||||||
|
var command = cmd_opt.?;
|
||||||
|
var ctype = try commandToCmdType(command);
|
||||||
|
var args = ArgList.init(self.allocator);
|
||||||
|
|
||||||
|
while (tok_it.next()) |arg| {
|
||||||
|
try args.append(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// construct final Command based on command
|
||||||
|
var cmd_ptr = try self.allocator.create(Command);
|
||||||
|
cmd_ptr.* = Command{ .command = ctype, .args = args };
|
||||||
|
try cmds.append(cmd_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmds;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO tests
|
32
src/main.zig
32
src/main.zig
|
@ -16,6 +16,7 @@
|
||||||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const langs = @import("lang.zig");
|
||||||
|
|
||||||
const c = @cImport({
|
const c = @cImport({
|
||||||
@cInclude("assert.h");
|
@cInclude("assert.h");
|
||||||
|
@ -302,6 +303,37 @@ pub fn main() !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
|
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
|
||||||
const allocator = &arena.allocator;
|
const allocator = &arena.allocator;
|
||||||
|
|
||||||
|
var lang = langs.Lang.init(allocator);
|
||||||
|
//defer lang.deinit();
|
||||||
|
|
||||||
|
var args_it = std.process.args();
|
||||||
|
|
||||||
|
const exe_name = try (args_it.next(allocator) orelse @panic("expected exe name"));
|
||||||
|
|
||||||
|
// args[1] is the path to scri file
|
||||||
|
const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path"));
|
||||||
|
|
||||||
|
std.debug.warn("path: '{}'\n", scri_path);
|
||||||
|
|
||||||
|
var file = try std.fs.File.openRead(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);
|
||||||
|
_ = try file.read(data);
|
||||||
|
|
||||||
|
var cmds = try lang.parse(data);
|
||||||
|
var it = cmds.iterator();
|
||||||
|
while (it.next()) |cmd| {
|
||||||
|
cmd.print();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn oldMain() !void {
|
||||||
|
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
|
||||||
|
const allocator = &arena.allocator;
|
||||||
|
|
||||||
var in_path: ?[]u8 = null;
|
var in_path: ?[]u8 = null;
|
||||||
var out_path: ?[]u8 = null;
|
var out_path: ?[]u8 = null;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue