From 767881893a6582840a5420266cfc7b50ef5c22f1 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 30 Mar 2025 14:55:48 -0400 Subject: [PATCH] --- src/main.zig | 85 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 11 deletions(-) diff --git a/src/main.zig b/src/main.zig index e029011..d675e38 100644 --- a/src/main.zig +++ b/src/main.zig @@ -9,6 +9,13 @@ pub fn main() !void { var ln = Linenoise.init(allocator); defer ln.deinit(); + var prng = Random.DefaultPrng.init(blk: { + var seed: u64 = undefined; + try std.posix.getrandom(std.mem.asBytes(&seed)); + break :blk seed; + }); + const rand = prng.random(); + const p = try getPrompt(allocator); defer allocator.free(p); @@ -18,15 +25,75 @@ pub fn main() !void { try ln.history.add(input); const command = try tokenizeCommand(input, allocator); defer allocator.free(command); - _ = runCommand(command, allocator) catch |err| switch (err) { - error.FileNotFound => print("mash: {s}: command not found\n", .{command[0]}), - error.AccessDenied => print("mash: {s}: Permission denied\n", .{command[0]}), - else => print("Unkown error: {}\n", .{err}), - }; + if (mathTest(rand, 100)) { + _ = runCommand(command, allocator) catch |err| switch (err) { + error.FileNotFound => print("mash: {s}: command not found\n", .{command[0]}), + error.AccessDenied => print("mash: {s}: Permission denied\n", .{command[0]}), + else => print("Unkown error: {}\n", .{err}), + }; + } else { + print( + \\\n + \\How can you expect to execute commands without knowing any math???\n + \\\n + , .{}); + } } } } +const Operators = enum { + add, + subtract, + multiply, + divide, + + const Self = @This(); + + fn toString(s: Self) []const u8 { + return switch (s) { + .add => "+", + .subtract => "-", + .multiply => "*", + .divide => "/", + }; + } + + fn apply(s: Self, a: i32, b: i32) i32 { + return switch (s) { + .add => a + b, + .subtract => a - b, + .multiply => a * b, + .divide => @divTrunc(a, b), + }; + } +}; + +/// return true if the user passed, false if the user did not. +fn mathTest(rand: Random, limit: i32) bool { + + // const operators = comptime .{ + // .{ "+", add }, + // .{ "-", subtract }, + // .{ "*", multiply }, + // .{ "/", divide }, + // }; + // const ops = StaticStringMap(BinaryOperator).initComptime(operators); + // _ = ops; + const op = rand.enumValue(Operators); + const num1 = rand.intRangeAtMost(i32, 1, limit); + const num2 = rand.intRangeAtMost(i32, 1, limit); + const answer = op.apply(num1, num2); + print("{d} {s} {d} = {d}\n", .{ num1, op.toString(), num2, answer }); + return true; +} + +fn runCommand(command: [][]const u8, allocator: Allocator) !u8 { + var child = process.Child.init(command, allocator); + const result = try child.spawnAndWait(); + return result.Exited; +} + fn getPrompt(allocator: Allocator) ![]const u8 { const script = @embedFile("./get_prompt.bash"); const result = try process.Child.run(.{ @@ -37,12 +104,6 @@ fn getPrompt(allocator: Allocator) ![]const u8 { return result.stdout; } -fn runCommand(command: [][]const u8, allocator: Allocator) !u8 { - var child = process.Child.init(command, allocator); - const result = try child.spawnAndWait(); - return result.Exited; -} - fn tokenizeCommand(command: []const u8, allocator: Allocator) ![][]const u8 { var argv_array_list = ArrayList([]const u8).init(allocator); defer argv_array_list.deinit(); @@ -68,4 +129,6 @@ const tokenizeAny = std.mem.tokenizeAny; const process = std.process; const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; +const StaticStringMap = std.static_string_map.StaticStringMap; +const Random = std.Random; const Linenoise = @import("linenoise").Linenoise;