Reading in all of my files working great, now to lex!

This commit is contained in:
Nathan Anderson 2025-07-13 21:31:39 -06:00
parent 126d3ce881
commit dc6b360583
13 changed files with 156 additions and 7 deletions

View File

@ -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();
}
};

View File

@ -1 +1 @@
pub const content = @import("content.zig");
pub const content = @import("content.zig").CyoContent;

View File

@ -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" {

View File

@ -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());
}

View File

@ -1,2 +1,6 @@
pub const input = @import("input_parser.zig");
pub const cyo = @import("cyo_parser.zig");
test "parsers" {
_ = cyo;
}

View File

@ -0,0 +1,4 @@
[Duct Tape]
d:The tape that does it all.
[Flash Light]

View File

@ -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. <p1> But its not a cure, just a band-aid. <p4> Time is ticking. <p1> 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

View File

@ -0,0 +1 @@
[Bathroom]