From dc6b360583da7caf97da4cd7afd816c148dcfc4f Mon Sep 17 00:00:00 2001 From: Nathan Anderson Date: Sun, 13 Jul 2025 21:31:39 -0600 Subject: [PATCH] Reading in all of my files working great, now to lex! --- srv/items.cy => cyo/items.cyo | 0 srv/main.cy => cyo/main.cyo | 0 src/cyo/content.zig | 24 ++++- src/cyo/cyo.zig | 2 +- src/main.zig | 15 ++- src/parser/cyo_parser.zig | 96 ++++++++++++++++++- src/parser/parser.zig | 4 + test/cyo_test_dir/items.cyo | 4 + test/cyo_test_dir/main.cyo | 17 ++++ test/cyo_test_dir/scenes/bathroom.cyo | 1 + .../scenes/planet/planet_comms.cyo | 0 test/cyo_test_dir/scenes/planet/ship/crew.cyo | 0 .../cyo_test_dir/scenes/planet/ship/screw.cyo | 0 13 files changed, 156 insertions(+), 7 deletions(-) rename srv/items.cy => cyo/items.cyo (100%) rename srv/main.cy => cyo/main.cyo (100%) create mode 100644 test/cyo_test_dir/items.cyo create mode 100644 test/cyo_test_dir/main.cyo create mode 100644 test/cyo_test_dir/scenes/bathroom.cyo create mode 100644 test/cyo_test_dir/scenes/planet/planet_comms.cyo create mode 100644 test/cyo_test_dir/scenes/planet/ship/crew.cyo create mode 100644 test/cyo_test_dir/scenes/planet/ship/screw.cyo diff --git a/srv/items.cy b/cyo/items.cyo similarity index 100% rename from srv/items.cy rename to cyo/items.cyo diff --git a/srv/main.cy b/cyo/main.cyo similarity index 100% rename from srv/main.cy rename to cyo/main.cyo diff --git a/src/cyo/content.zig b/src/cyo/content.zig index 5f6dda0..2b9a886 100644 --- a/src/cyo/content.zig +++ b/src/cyo/content.zig @@ -1,3 +1,25 @@ const std = @import("std"); -pub const CyoContent = struct {}; +pub const CyoContent = struct { + allocator: std.mem.Allocator, + files_contents: std.StringHashMap([]const u8), + + pub fn init(alloc: std.mem.Allocator, file_contents: std.StringHashMap([]const u8)) CyoContent { + return .{ + .allocator = alloc, + .file_contents = file_contents, + }; + } + + pub fn deinit(self: *CyoContent) void { + var iter = self.files_contents.keyIterator(); + while (iter.next()) |key| { + const content = self.files_contents.get(key.*); + if (content) |c| { + self.allocator.free(c); + } + self.allocator.free(key.*); + } + self.files_contents.deinit(); + } +}; diff --git a/src/cyo/cyo.zig b/src/cyo/cyo.zig index 0945e20..c5a4061 100644 --- a/src/cyo/cyo.zig +++ b/src/cyo/cyo.zig @@ -1 +1 @@ -pub const content = @import("content.zig"); +pub const content = @import("content.zig").CyoContent; diff --git a/src/main.zig b/src/main.zig index 999c2e1..236789b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,10 +2,23 @@ const std = @import("std"); const parser = @import("parser/parser.zig"); pub fn main() !void { + var gpa = std.heap.DebugAllocator(.{}).init; + var allocator = gpa.allocator(); + + const cyo_dir_path = std.fs.cwd().realpathAlloc(allocator, "./cyo/") catch blk: { + var t = try allocator.alloc(u8, 1); + break :blk t[0..]; + }; + defer allocator.free(cyo_dir_path); + // parser.ActionParser.init(); + _ = try parser.cyo.CyoParser.init( + allocator, + cyo_dir_path, + ); } -test "test" { +test "test all" { _ = @import("parser/parser.zig"); } // test "simple test" { diff --git a/src/parser/cyo_parser.zig b/src/parser/cyo_parser.zig index d3d5ab9..dbd0407 100644 --- a/src/parser/cyo_parser.zig +++ b/src/parser/cyo_parser.zig @@ -1,21 +1,109 @@ const std = @import("std"); -const CyoContent = @import("../cyo/content.zig"); +const CyoContent = @import("../cyo/cyo.zig").content; + +const DEFAULT_CYO_SOURCE_PATH = "cyo"; + +pub const CyoError = error{ BadSource, BadIter }; pub const CyoParser = struct { allocator: std.mem.Allocator, cyo_content: CyoContent, - pub fn init(allocator: std.mem.Allocator, source_path: ?std.fs.path) CyoParser { - var cyo_content = try parseCyoFiles(allocator, source_path); + pub fn init(allocator: std.mem.Allocator, source_path: ?[]const u8) !CyoParser { + const cyo_content = try parseCyoFiles(allocator, source_path); return .{ .allocator = allocator, .cyo_content = cyo_content }; } - fn parseCyoFiles(allocator: std.mem.Allocator, source_path: ?std.fs.path) CyoParser { + pub fn deinit(self: *CyoParser) void { + self.cyo_content.deinit(); + } + + fn parseCyoFiles(allocator: std.mem.Allocator, source_path: ?[]const u8) !CyoContent { + var cyo_source_path: []const u8 = ""; + // var file_exists = + if (source_path != null) { + cyo_source_path = source_path.?; + } else { + const path = try std.fs.cwd().realpathAlloc(allocator, DEFAULT_CYO_SOURCE_PATH); + defer allocator.free(path); + cyo_source_path = path; + } + // 1. get all cyo files + var cyo_dir = std.fs.cwd().openDir(cyo_source_path, .{ .iterate = true }) catch |err| { + std.debug.print("Encountered error: {any}", .{err}); + return CyoError.BadSource; + }; + defer cyo_dir.close(); + var files_contents = std.StringHashMap([]const u8).init(allocator); + try walkDirs(allocator, cyo_dir, 0, &files_contents); + + var iter = files_contents.keyIterator(); + while (iter.next()) |key| { + const content = files_contents.get(key.*); + if (content) |c| { + std.debug.print("Got key: {s}\nContent:{s}\n\n", .{ key.*, c }); + } else { + std.debug.print("Got key: {s}\nContent empty\n\n", .{key.*}); + } + } + + // var path_buf: [128]u8 = undefined; + + // const cyo_dir_path = cyo_dir.realpath(pathname: []const u8, out_buffer: []u8) + // 2. process files // 2a. lexical - validate file syntax // 2b. syntactic parsing // 2c. semantic - create objects and scenes // 2d. evaluate - find missing or cyclical links + + return CyoContent{ .allocator = allocator, .files_contents = files_contents }; + } + + fn walkDirs(allocator: std.mem.Allocator, cyo_dir: std.fs.Dir, depth: u8, files_contents: *std.StringHashMap([]const u8)) !void { + var cyo_iter = cyo_dir.iterate(); + while (cyo_iter.next() catch |err| { + std.debug.print("Failed to iterate: {any}", .{err}); + return CyoError.BadIter; + }) |cyo_entry| { + switch (cyo_entry.kind) { + .file => { + // std.fs.cyo_entry.name; + for (0..depth) |_| { + std.debug.print("\t", .{}); + } + std.debug.print("- File: {s}\n", .{cyo_entry.name}); + const file_path = try cyo_dir.realpathAlloc(allocator, cyo_entry.name); + var cyo_file = try std.fs.openFileAbsolute(file_path, .{ .mode = .read_only }); + + const contents = try cyo_file.readToEndAlloc(allocator, 5243000); // 5mb size limit + try files_contents.*.put(file_path, contents); + }, + .directory => { + for (0..depth) |_| { + std.debug.print("\t", .{}); + } + std.debug.print("Dir: {s}\n", .{cyo_entry.name}); + const dir_path = try cyo_dir.realpathAlloc(allocator, cyo_entry.name); + defer allocator.free(dir_path); + + const new_cyo_dir = try std.fs.openDirAbsolute(dir_path, .{ .iterate = true }); + try walkDirs(allocator, new_cyo_dir, depth + 1, files_contents); + }, + else => { + std.debug.print("ignoring other types...", .{}); + }, + } + } } }; + +test "parse test" { + const cyo_test_dir_path = try std.fs.cwd().realpathAlloc(std.testing.allocator, "./test/cyo_test_dir"); + defer std.testing.allocator.free(cyo_test_dir_path); + var cyo_parser = try CyoParser.init(std.testing.allocator, cyo_test_dir_path); + defer cyo_parser.deinit(); + + try std.testing.expectEqual(6, cyo_parser.cyo_content.files_contents.count()); +} diff --git a/src/parser/parser.zig b/src/parser/parser.zig index f7ec624..55b4b1d 100644 --- a/src/parser/parser.zig +++ b/src/parser/parser.zig @@ -1,2 +1,6 @@ pub const input = @import("input_parser.zig"); pub const cyo = @import("cyo_parser.zig"); + +test "parsers" { + _ = cyo; +} diff --git a/test/cyo_test_dir/items.cyo b/test/cyo_test_dir/items.cyo new file mode 100644 index 0000000..1dec750 --- /dev/null +++ b/test/cyo_test_dir/items.cyo @@ -0,0 +1,4 @@ +[Duct Tape] +d:The tape that does it all. + +[Flash Light] diff --git a/test/cyo_test_dir/main.cyo b/test/cyo_test_dir/main.cyo new file mode 100644 index 0000000..9e95962 --- /dev/null +++ b/test/cyo_test_dir/main.cyo @@ -0,0 +1,17 @@ +[Warehouse] + +Your vision blurs as you fumble for the tablet. The glow of your fingers is nearly gone. The salty tablet is the last +sensation before everything goes dark. + +--- + +You awaken. How long has it been? Thanks to your dull glow, you can read the clock on the wall. 8:36. Only a few hours. +Thank God you brought one with you. But its not a cure, just a band-aid. Time is ticking. You've got 24 hours +to breathe, better get back before then. + +[Warehouse.d] +It might have been an Amazon warehouse. Everything seems sacked. Empty shelves twist in some corporate labrynth before you. +Would have been nice to have something to show for this trip. But information isn't useless. + +[Warehouse.i] +Duct Tape diff --git a/test/cyo_test_dir/scenes/bathroom.cyo b/test/cyo_test_dir/scenes/bathroom.cyo new file mode 100644 index 0000000..14fd67e --- /dev/null +++ b/test/cyo_test_dir/scenes/bathroom.cyo @@ -0,0 +1 @@ +[Bathroom] diff --git a/test/cyo_test_dir/scenes/planet/planet_comms.cyo b/test/cyo_test_dir/scenes/planet/planet_comms.cyo new file mode 100644 index 0000000..e69de29 diff --git a/test/cyo_test_dir/scenes/planet/ship/crew.cyo b/test/cyo_test_dir/scenes/planet/ship/crew.cyo new file mode 100644 index 0000000..e69de29 diff --git a/test/cyo_test_dir/scenes/planet/ship/screw.cyo b/test/cyo_test_dir/scenes/planet/ship/screw.cyo new file mode 100644 index 0000000..e69de29