Add environment handling
This commit is contained in:
parent
362fdc1118
commit
0f440dfb6f
3 changed files with 13 additions and 7 deletions
|
@ -13,13 +13,13 @@ pub const Interpreter = struct {
|
||||||
self.field.deinit();
|
self.field.deinit();
|
||||||
self.allocator.destroy(self);
|
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);
|
var i = try allocator.create(Interpreter);
|
||||||
errdefer allocator.destroy(i);
|
errdefer allocator.destroy(i);
|
||||||
i.allocator = allocator;
|
i.allocator = allocator;
|
||||||
i.field = try field.Field.init_from_reader(allocator, fileReader);
|
i.field = try field.Field.init_from_reader(allocator, fileReader);
|
||||||
errdefer i.field.deinit();
|
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();
|
errdefer i.pointer.deinit();
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,8 @@ pub fn main() anyerror!void {
|
||||||
var file = try std.fs.cwd().openFile("mycology/sanity.bf", .{});
|
var file = try std.fs.cwd().openFile("mycology/sanity.bf", .{});
|
||||||
defer file.close();
|
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();
|
defer i.deinit();
|
||||||
|
|
||||||
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
|
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
|
||||||
|
@ -33,7 +34,8 @@ test "sanity" {
|
||||||
defer stdout.deinit();
|
defer stdout.deinit();
|
||||||
const expected = "0123456789";
|
const expected = "0123456789";
|
||||||
const args = [_][]const u8{"sanity"};
|
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();
|
defer i.deinit();
|
||||||
var ioContext = io.context(stdin.reader(), stdout.writer());
|
var ioContext = io.context(stdin.reader(), stdout.writer());
|
||||||
try std.testing.expectEqual(try i.run(&ioContext), 0);
|
try std.testing.expectEqual(try i.run(&ioContext), 0);
|
||||||
|
|
|
@ -23,6 +23,7 @@ pub const Pointer = struct {
|
||||||
stringMode: bool = false, // string mode flags
|
stringMode: bool = false, // string mode flags
|
||||||
lastCharWasSpace: bool = false,
|
lastCharWasSpace: bool = false,
|
||||||
ss: *stackStack.StackStack,
|
ss: *stackStack.StackStack,
|
||||||
|
env: []const [*:0]const u8,
|
||||||
argv: []const []const u8,
|
argv: []const []const u8,
|
||||||
rand: *std.rand.Random,
|
rand: *std.rand.Random,
|
||||||
|
|
||||||
|
@ -267,13 +268,14 @@ pub const Pointer = struct {
|
||||||
self.step();
|
self.step();
|
||||||
return result;
|
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);
|
var p = try allocator.create(Pointer);
|
||||||
errdefer allocator.destroy(p);
|
errdefer allocator.destroy(p);
|
||||||
p.allocator = allocator;
|
p.allocator = allocator;
|
||||||
p.field = f;
|
p.field = f;
|
||||||
p.ss = try stackStack.StackStack.init(allocator);
|
p.ss = try stackStack.StackStack.init(allocator);
|
||||||
p.argv = argv;
|
p.argv = argv;
|
||||||
|
p.env = env;
|
||||||
p.x = 0;
|
p.x = 0;
|
||||||
p.y = 0;
|
p.y = 0;
|
||||||
p.dx = 1;
|
p.dx = 1;
|
||||||
|
@ -356,7 +358,8 @@ test "minimal" {
|
||||||
var f = try field.Field.init_from_reader(std.testing.allocator, minimal);
|
var f = try field.Field.init_from_reader(std.testing.allocator, minimal);
|
||||||
defer f.deinit();
|
defer f.deinit();
|
||||||
const argv = [_][]const u8{"minimal"};
|
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();
|
defer p.deinit();
|
||||||
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
|
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
|
||||||
try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{});
|
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);
|
var f = try field.Field.init_from_reader(std.testing.allocator, minimal);
|
||||||
defer f.deinit();
|
defer f.deinit();
|
||||||
const argv = [_][]const u8{"minimal"};
|
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();
|
defer p.deinit();
|
||||||
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
|
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
|
||||||
try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{});
|
try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{});
|
||||||
|
|
Loading…
Add table
Reference in a new issue