diff options
author | Julien Dessaux | 2022-08-13 01:23:32 +0200 |
---|---|---|
committer | Julien Dessaux | 2022-08-13 01:23:32 +0200 |
commit | 0f440dfb6fc21c460b6c621571921298d94eb1b0 (patch) | |
tree | 4a7011ce12f4bf611883ea9b645e59a213d301af | |
parent | Implemented the under funge command (diff) | |
download | zigfunge98-0f440dfb6fc21c460b6c621571921298d94eb1b0.tar.gz zigfunge98-0f440dfb6fc21c460b6c621571921298d94eb1b0.tar.bz2 zigfunge98-0f440dfb6fc21c460b6c621571921298d94eb1b0.zip |
Add environment handling
-rw-r--r-- | src/interpreter.zig | 4 | ||||
-rw-r--r-- | src/main.zig | 6 | ||||
-rw-r--r-- | src/pointer.zig | 10 |
3 files changed, 13 insertions, 7 deletions
diff --git a/src/interpreter.zig b/src/interpreter.zig index 05c4516..c378c11 100644 --- a/src/interpreter.zig +++ b/src/interpreter.zig @@ -13,13 +13,13 @@ pub const Interpreter = struct { self.field.deinit(); self.allocator.destroy(self); } - pub fn init(allocator: std.mem.Allocator, fileReader: anytype, args: []const []const u8) !*Interpreter { + pub fn init(allocator: std.mem.Allocator, fileReader: anytype, args: []const []const u8, env: []const [*:0]const u8) !*Interpreter { var i = try allocator.create(Interpreter); errdefer allocator.destroy(i); i.allocator = allocator; i.field = try field.Field.init_from_reader(allocator, fileReader); errdefer i.field.deinit(); - i.pointer = try pointer.Pointer.init(std.testing.allocator, i.field, args); + i.pointer = try pointer.Pointer.init(std.testing.allocator, i.field, args, env); errdefer i.pointer.deinit(); return i; } diff --git a/src/main.zig b/src/main.zig index 32874e5..d1b6ccb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -15,7 +15,8 @@ pub fn main() anyerror!void { var file = try std.fs.cwd().openFile("mycology/sanity.bf", .{}); defer file.close(); - var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), args); + const env: []const [*:0]const u8 = std.os.environ; + var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), args, env[0..]); defer i.deinit(); var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer()); @@ -33,7 +34,8 @@ test "sanity" { defer stdout.deinit(); const expected = "0123456789"; const args = [_][]const u8{"sanity"}; - var i = try interpreter.Interpreter.init(std.testing.allocator, file.reader(), args[0..]); + const env = [_][*:0]const u8{"ENV=TEST"}; + var i = try interpreter.Interpreter.init(std.testing.allocator, file.reader(), args[0..], env[0..]); defer i.deinit(); var ioContext = io.context(stdin.reader(), stdout.writer()); try std.testing.expectEqual(try i.run(&ioContext), 0); diff --git a/src/pointer.zig b/src/pointer.zig index 7578a65..d604518 100644 --- a/src/pointer.zig +++ b/src/pointer.zig @@ -23,6 +23,7 @@ pub const Pointer = struct { stringMode: bool = false, // string mode flags lastCharWasSpace: bool = false, ss: *stackStack.StackStack, + env: []const [*:0]const u8, argv: []const []const u8, rand: *std.rand.Random, @@ -267,13 +268,14 @@ pub const Pointer = struct { self.step(); return result; } - pub fn init(allocator: std.mem.Allocator, f: *field.Field, argv: []const []const u8) !*Pointer { + pub fn init(allocator: std.mem.Allocator, f: *field.Field, argv: []const []const u8, env: []const [*:0]const u8) !*Pointer { var p = try allocator.create(Pointer); errdefer allocator.destroy(p); p.allocator = allocator; p.field = f; p.ss = try stackStack.StackStack.init(allocator); p.argv = argv; + p.env = env; p.x = 0; p.y = 0; p.dx = 1; @@ -356,7 +358,8 @@ test "minimal" { var f = try field.Field.init_from_reader(std.testing.allocator, minimal); defer f.deinit(); const argv = [_][]const u8{"minimal"}; - var p = try Pointer.init(std.testing.allocator, f, argv[0..]); + const env = [_][*:0]const u8{"ENV=TEST"}; + var p = try Pointer.init(std.testing.allocator, f, argv[0..], env[0..]); defer p.deinit(); var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer()); try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{}); @@ -366,7 +369,8 @@ test "almost minimal" { var f = try field.Field.init_from_reader(std.testing.allocator, minimal); defer f.deinit(); const argv = [_][]const u8{"minimal"}; - var p = try Pointer.init(std.testing.allocator, f, argv[0..]); + const env = [_][*:0]const u8{"ENV=TEST"}; + var p = try Pointer.init(std.testing.allocator, f, argv[0..], env[0..]); defer p.deinit(); var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer()); try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{}); |