Began implementing a funge stack
This commit is contained in:
parent
b2b4f7be41
commit
eb73b96d9b
4 changed files with 131 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
zig-cache
|
||||
zig-out
|
34
build.zig
Normal file
34
build.zig
Normal file
|
@ -0,0 +1,34 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
// Standard target options allows the person running `zig build` to choose
|
||||
// what target to build for. Here we do not override the defaults, which
|
||||
// means any target is allowed, and the default is native. Other options
|
||||
// for restricting supported target set are available.
|
||||
const target = b.standardTargetOptions(.{});
|
||||
|
||||
// Standard release options allow the person running `zig build` to select
|
||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
const exe = b.addExecutable("zigfunge98", "src/main.zig");
|
||||
exe.setTarget(target);
|
||||
exe.setBuildMode(mode);
|
||||
exe.install();
|
||||
|
||||
const run_cmd = exe.run();
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
const exe_tests = b.addTest("src/main.zig");
|
||||
exe_tests.setTarget(target);
|
||||
exe_tests.setBuildMode(mode);
|
||||
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&exe_tests.step);
|
||||
}
|
10
src/main.zig
Normal file
10
src/main.zig
Normal file
|
@ -0,0 +1,10 @@
|
|||
const std = @import("std");
|
||||
const stack = @import("stack.zig");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
std.log.info("All your codebase are belong to us.", .{});
|
||||
}
|
||||
|
||||
test "all" {
|
||||
std.testing.refAllDecls(@This());
|
||||
}
|
85
src/stack.zig
Normal file
85
src/stack.zig
Normal file
|
@ -0,0 +1,85 @@
|
|||
const std = @import("std");
|
||||
const vector = std.meta.Vector(2, u64);
|
||||
|
||||
pub const Stack = struct {
|
||||
data: std.ArrayList(u64),
|
||||
pub fn clear(self: *Stack) void {
|
||||
self.data.clearRetainingCapacity();
|
||||
}
|
||||
pub fn deinit(self: *Stack) void {
|
||||
self.data.deinit();
|
||||
}
|
||||
pub fn duplicate(self: *Stack) !void {
|
||||
if (self.data.items.len > 0) {
|
||||
try self.push(self.data.items[self.data.items.len - 1]);
|
||||
}
|
||||
}
|
||||
pub fn init(comptime allocator: std.mem.Allocator) Stack {
|
||||
return Stack{
|
||||
.data = std.ArrayList(u64).init(allocator),
|
||||
};
|
||||
}
|
||||
pub fn pop(self: *Stack) u64 {
|
||||
return if (self.data.popOrNull()) |v| v else 0;
|
||||
}
|
||||
pub fn popVector(self: *Stack) [2]u64 {
|
||||
const b = if (self.data.popOrNull()) |v| v else 0;
|
||||
const a = if (self.data.popOrNull()) |v| v else 0;
|
||||
return [2]u64{ a, b };
|
||||
}
|
||||
pub fn push(self: *Stack, n: u64) !void {
|
||||
try self.data.append(n);
|
||||
}
|
||||
pub fn pushVector(self: *Stack, v: [2]u64) !void {
|
||||
try self.data.appendSlice(v[0..2]);
|
||||
}
|
||||
pub fn swap(self: *Stack) !void {
|
||||
const v = self.popVector();
|
||||
try self.pushVector([2]u64{ v[1], v[0] });
|
||||
}
|
||||
};
|
||||
|
||||
test "clear" {
|
||||
var s = Stack.init(std.testing.allocator);
|
||||
defer s.deinit();
|
||||
s.clear();
|
||||
try std.testing.expect(s.pop() == 0);
|
||||
try s.pushVector([2]u64{ 1, 2 });
|
||||
s.clear();
|
||||
try std.testing.expect(s.pop() == 0);
|
||||
}
|
||||
test "duplicate" {
|
||||
var s = Stack.init(std.testing.allocator);
|
||||
defer s.deinit();
|
||||
try s.duplicate();
|
||||
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 0, 0 }));
|
||||
try s.pushVector([2]u64{ 1, 2 });
|
||||
try s.duplicate();
|
||||
try s.duplicate();
|
||||
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 2, 2 }));
|
||||
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 1, 2 }));
|
||||
}
|
||||
test "push and pop" {
|
||||
var s = Stack.init(std.testing.allocator);
|
||||
defer s.deinit();
|
||||
try std.testing.expect(s.pop() == 0);
|
||||
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 0, 0 }));
|
||||
try s.push(1);
|
||||
try std.testing.expect(s.pop() == 1);
|
||||
try std.testing.expect(s.pop() == 0);
|
||||
try s.pushVector([2]u64{ 2, 3 });
|
||||
try std.testing.expect(s.pop() == 3);
|
||||
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 0, 2 }));
|
||||
}
|
||||
test "swap" {
|
||||
var s = Stack.init(std.testing.allocator);
|
||||
defer s.deinit();
|
||||
try s.swap();
|
||||
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 0, 0 }));
|
||||
try s.push(1);
|
||||
try s.swap();
|
||||
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 1, 0 }));
|
||||
try s.push(2);
|
||||
try s.swap();
|
||||
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 2, 0 }));
|
||||
}
|
Loading…
Add table
Reference in a new issue