feat: loading source files
This commit is contained in:
parent
af48211483
commit
94ccd1999d
2 changed files with 100 additions and 36 deletions
105
src/main.zig
105
src/main.zig
|
|
@ -1,27 +1,98 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const ink = @import("ink");
|
const ink = @import("ink");
|
||||||
|
const fatal = std.process.fatal;
|
||||||
|
|
||||||
|
var stdin_buffer: [4096]u8 align(std.heap.page_size_min) = undefined;
|
||||||
|
var stdout_buffer: [4096]u8 align(std.heap.page_size_min) = undefined;
|
||||||
|
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
|
||||||
|
const max_src_size = std.math.maxInt(u32);
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
// Prints to stderr, ignoring potential errors.
|
const gpa = debug_allocator.allocator();
|
||||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
defer _ = debug_allocator.deinit();
|
||||||
try ink.bufferedPrint();
|
|
||||||
|
var arena_allocator = std.heap.ArenaAllocator.init(gpa);
|
||||||
|
defer arena_allocator.deinit();
|
||||||
|
|
||||||
|
const arena = arena_allocator.allocator();
|
||||||
|
const args = try std.process.argsAlloc(gpa);
|
||||||
|
defer std.process.argsFree(gpa, args);
|
||||||
|
|
||||||
|
if (args.len < 2) {
|
||||||
|
fatal("Not enough arguments!", .{});
|
||||||
|
}
|
||||||
|
return mainArgs(gpa, arena, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "simple test" {
|
fn mainArgs(
|
||||||
const gpa = std.testing.allocator;
|
gpa: std.mem.Allocator,
|
||||||
var list: std.ArrayList(i32) = .empty;
|
arena: std.mem.Allocator,
|
||||||
defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
|
args_list: []const [:0]const u8,
|
||||||
try list.append(gpa, 42);
|
) !void {
|
||||||
try std.testing.expectEqual(@as(i32, 42), list.pop());
|
var source_path: ?[]const u8 = null;
|
||||||
|
var arg_index: usize = 1;
|
||||||
|
|
||||||
|
while (arg_index < args_list.len) : (arg_index += 1) {
|
||||||
|
const arg = args_list[arg_index];
|
||||||
|
if (std.mem.startsWith(u8, arg, "-")) {
|
||||||
|
// TODO: Parse CLI options.
|
||||||
|
} else if (source_path == null) {
|
||||||
|
source_path = arg;
|
||||||
|
} else {
|
||||||
|
fatal("invalid positional argument: '{s}'", .{arg});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test "fuzz example" {
|
const filename = source_path orelse "<STDIN>";
|
||||||
const Context = struct {
|
const source_bytes: [:0]const u8 = s: {
|
||||||
fn testOne(context: @This(), input: []const u8) anyerror!void {
|
var f = if (source_path) |p| file: {
|
||||||
_ = context;
|
break :file std.fs.cwd().openFile(p, .{}) catch |err| {
|
||||||
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
|
fatal("unable to open file '{s}': {s}", .{ p, @errorName(err) });
|
||||||
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
try std.testing.fuzz(Context{}, Context.testOne, .{});
|
} else std.fs.File.stdin();
|
||||||
|
defer if (source_path != null) f.close();
|
||||||
|
|
||||||
|
var file_reader: std.fs.File.Reader = f.reader(&stdin_buffer);
|
||||||
|
break :s readSourceFile(arena, &file_reader) catch |err| {
|
||||||
|
fatal("unable to load file '{s}': {s}", .{ filename, @errorName(err) });
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var story = try ink.Story.loadFromString(gpa, source_bytes, .{});
|
||||||
|
defer story.deinit();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn readSourceFile(gpa: std.mem.Allocator, file_reader: *std.fs.File.Reader) ![:0]u8 {
|
||||||
|
var buffer: std.ArrayListUnmanaged(u8) = .empty;
|
||||||
|
defer buffer.deinit(gpa);
|
||||||
|
|
||||||
|
if (file_reader.getSize()) |size| {
|
||||||
|
const casted_size = std.math.cast(u32, size) orelse return error.StreamTooLong;
|
||||||
|
try buffer.ensureTotalCapacityPrecise(gpa, casted_size + 1);
|
||||||
|
} else |_| {}
|
||||||
|
|
||||||
|
try file_reader.interface.appendRemaining(gpa, &buffer, .limited(max_src_size));
|
||||||
|
|
||||||
|
const unsupported_boms = [_][]const u8{
|
||||||
|
"\xff\xfe\x00\x00", // UTF-32 little endian
|
||||||
|
"\xfe\xff\x00\x00", // UTF-32 big endian
|
||||||
|
"\xfe\xff", // UTF-16 big endian
|
||||||
|
};
|
||||||
|
for (unsupported_boms) |bom| {
|
||||||
|
if (std.mem.startsWith(u8, buffer.items, bom)) {
|
||||||
|
return error.UnsupportedEncoding;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (std.mem.startsWith(u8, buffer.items, "\xff\xfe")) {
|
||||||
|
if (buffer.items.len % 2 != 0) {
|
||||||
|
return error.InvalidEncoding;
|
||||||
|
}
|
||||||
|
return std.unicode.utf16LeToUtf8AllocZ(gpa, @ptrCast(@alignCast(buffer.items))) catch |err| switch (err) {
|
||||||
|
error.DanglingSurrogateHalf => error.UnsupportedEncoding,
|
||||||
|
error.ExpectedSecondSurrogateHalf => error.UnsupportedEncoding,
|
||||||
|
error.UnexpectedSecondSurrogateHalf => error.UnsupportedEncoding,
|
||||||
|
else => |e| return e,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return buffer.toOwnedSliceSentinel(gpa, 0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
29
src/root.zig
29
src/root.zig
|
|
@ -1,23 +1,16 @@
|
||||||
//! By convention, root.zig is the root source file when making a library.
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn bufferedPrint() !void {
|
pub const Story = struct {
|
||||||
// Stdout is for the actual output of your application, for example if you
|
pub const LoadOptions = struct {};
|
||||||
// are implementing gzip, then only the compressed bytes should be sent to
|
|
||||||
// stdout, not any debugging messages.
|
|
||||||
var stdout_buffer: [1024]u8 = undefined;
|
|
||||||
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
|
|
||||||
const stdout = &stdout_writer.interface;
|
|
||||||
|
|
||||||
try stdout.print("Run `zig build test` to run the tests.\n", .{});
|
pub fn loadFromString(
|
||||||
|
_: std.mem.Allocator,
|
||||||
try stdout.flush(); // Don't forget to flush!
|
source_bytes: [:0]const u8,
|
||||||
|
_: LoadOptions,
|
||||||
|
) !Story {
|
||||||
|
std.debug.print("{s}\n", .{source_bytes});
|
||||||
|
return .{};
|
||||||
}
|
}
|
||||||
|
pub fn deinit(_: *Story) void {}
|
||||||
|
};
|
||||||
|
|
||||||
pub fn add(a: i32, b: i32) i32 {
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
test "basic add functionality" {
|
|
||||||
try std.testing.expect(add(3, 7) == 10);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue