1
0
Fork 0

Add environment handling

This commit is contained in:
Julien Dessaux 2022-08-13 01:23:32 +02:00
parent 362fdc1118
commit 0f440dfb6f
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 13 additions and 7 deletions

View file

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

View file

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

View file

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