aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/interpreter.zig4
-rw-r--r--src/main.zig6
-rw-r--r--src/pointer.zig10
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{});