add basic lang parse code

This commit is contained in:
Luna 2019-07-07 23:03:55 -03:00
parent 6e8ee7831a
commit 4bbe980014
2 changed files with 75 additions and 0 deletions

43
src/lang.zig Normal file
View File

@ -0,0 +1,43 @@
const std = @import("std");
pub const CommandType = enum {
Noop,
Load,
Amp,
Quicksave,
};
pub const Command = struct {
command: CommandType,
args: []const u8,
pub fn print(self: *const Command) void {
std.debug.warn("cmd:{} args:{}\n", self.command, self.args);
}
};
pub const CommandList = std.ArrayList(Command);
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 parse stmt
std.debug.warn("full cmd: '{}'\n", stmt);
}
return cmds;
}
};

View File

@ -16,6 +16,7 @@
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
const std = @import("std");
const langs = @import("lang.zig");
const c = @cImport({
@cInclude("assert.h");
@ -302,6 +303,37 @@ pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.c_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 = 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 out_path: ?[]u8 = null;