Updated to Zig 15.2 & fixed loop recursion, added beer
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
[ 99 Bottles of Beer in Brainf*** by Eric Bock - ebock@uswest.net ]
|
||||
|
||||
+++++++++>+++++++++>>>++++++[<+++>-]+++++++++>+++++++++>>>>>>+++++++++++++[<+++
|
||||
+++>-]>+++++++++++[<++++++++++>-]<+>>++++++++[<++++>-]>+++++++++++[<++++++++++>
|
||||
-]<->>+++++++++++[<++++++++++>-]<+>>>+++++[<++++>-]<-[<++++++>-]>++++++++++[<++
|
||||
++++++++>-]<+>>>+++++++[<+++++++>-]>>>++++++++[<++++>-]>++++++++[<++++>-]>+++++
|
||||
++++++[<+++++++++>-]<->>++++++++[<++++>-]>+++++++++++[<++++++++++>-]<+>>+++++++
|
||||
+[<++++>-]>>+++++++[<++++>-]<+[<++++>-]>++++++++[<++++>-]>>+++++++[<++++>-]<+[<
|
||||
++++>-]>>++++++++++++[<+++++++++>-]++++++++++>>++++++++++[<++++++++++>-]<+>>+++
|
||||
+++++++++[<+++++++++>-]>>++++++++++++[<+++++++++>-]>>++++++[<++++>-]<-[<+++++>-
|
||||
]>++++++++++++[<++++++++>-]<+>>>>++++[<++++>-]<+[<+++++++>-]>++++++++[<++++>-]>
|
||||
++++++++[<++++>-]>+++++++++++[<++++++++++>-]<+>>++++++++++[<++++++++++>-]<+>>>+
|
||||
+++[<++++>-]<+[<++++++>-]>+++++++++++++[<++++++++>-]>++++++++[<++++>-]>>+++++++
|
||||
[<++++>-]<+[<++++>-]>+++++++++++[<+++++++++>-]<->>++++++++[<++++>-]>++++++++++[
|
||||
<++++++++++>-]<+>>+++++++++++[<++++++++++>-]>++++++++++[<++++++++++>-]<+>>+++++
|
||||
++++++[<++++++++++>-]<+>>>+++++[<++++>-]<-[<++++++>-]>++++++++[<++++>-]>+++++++
|
||||
+++>>>>++++++++++++[<+++++++>-]++++++++++>>++++++++++++[<++++++++>-]<+>>+++++++
|
||||
+++[<++++++++++>-]>++++++++++++[<+++++++++>-]<->>+++++++++++[<++++++++++>-]>+++
|
||||
+++++++[<++++++++++>-]<+>>+++++++++++++[<+++++++++>-]>++++++++[<++++>-]>+++++++
|
||||
++++[<++++++++++>-]<+>>>>+++++[<++++>-]<-[<++++++>-]>+++++++++++[<++++++++++>-]
|
||||
<+>>++++++++++++[<++++++++>-]<+>>+++++++++++[<++++++++++>-]>++++++++[<++++>-]>+
|
||||
+++++++++[<++++++++++>-]<+>>>+++++++[<++++>-]<+[<++++>-]>>>+++++[<+++>-]<[<++++
|
||||
+++>-]>>+++++[<+++>-]<[<+++++++>-]>++++++++[<++++>-]>>+++++++[<++++>-]<+[<++++>
|
||||
-]>>++++++[<++++>-]<-[<+++++>-]>>>++++++[<++++>-]<-[<+++++>-]>++++++++[<++++>-]
|
||||
>++++++++++++[<++++++++>-]<+>>++++++++++[<++++++++++>-]>>++++[<++++>-]<[<++++++
|
||||
+>-]>+++++++++++[<++++++++++>-]<+>>++++++++[<++++>-]>>++++[<++++>-]<+[<+++++++>
|
||||
-]>++++++++++[<++++++++++>-]>+++++++++++[<++++++++++>-]>+++++++++++[<++++++++++
|
||||
>-]>++++++++[<++++>-]>++++++++++++[<++++++++>-]<+[<]<[<]<[<]<[<]<<<<[<]<[<]<[<]
|
||||
<[<]<<<<[<]<<<<<<[>>[<<[>>>+>+<<<<-]>>>[<<<+>>>-]<[>+<-]>>-[>-<[-]]>+[<+>-]<<<]
|
||||
>>[<<<<[-]>>>>>>>[>]>.>>>[.>>]>[>]>>[.>>]<[.<<]<[<]<<.>>>[.>>]>[>]>>[.>>]>.>>>[
|
||||
.>>]>[>]>>[.>>]>>[.>>]<[.<<]<<<<[<]<[<]<[<]<[<]<<<<[<]>[.>]>>>>[.>>]>>[.>>]>>[.
|
||||
>>]<[.<<]<[<]<<<<[<]<<-]<<<<[[-]>>>[<+>-]<<[>>+>+<<<-]>>>[<<<+>>>-]<[<<<+++++++
|
||||
+[>++++++<-]>>>[-]]++++++++[<++++++>-]<<[.>.>]>[.>>]>>>[>]>>>>[.>>]>>[.>>]>>[.>
|
||||
>]<[.<<]<[<]<<<<[<]<<<<<[.>.>]>[.>>]<<<[-]>[-]>>>>>[>]>>>>[.>>]>>[.>>]>>[.>>]>.
|
||||
>>>[.>>]>>[.>>]>[>]>>[.>>]<[.<<]<<<<[<]<[<]<[<]<[<]<<<<[<]<<<<<<]<<[>+>+<<-]>>[
|
||||
<<+>>-]<[>-<[-]]>+[<+>-]<[<++++++++++<->>-]<<[>>+>+>+<<<<-]>>[<<+>>-]<-[>+>+>>+
|
||||
<<<<-]>[<+>-]>>>[<<[>>>+>+<<<<-]>>>[<<<+>>>-]<[>+<-]>>-[>-<[-]]>+[<+>-]<<<]>>[<
|
||||
<<<[-]>>>>>>>[>]>.>>>[.>>]>[>]>>[.>>]<[.<<]<[<]<<<<[<]<<-]<<<<[[-]>>>[<+>-]<<[>
|
||||
>+>+<<<-]>>>[<<<+>>>-]<[<<<++++++++[>++++++<-]>>>[-]]++++++++[<++++++>-]<<[.>.>
|
||||
]>[.>>]<<<[-]>[-]>>>>>[>]>>>>[.>>]>>[.>>]>>[.>>]<[.<<]<[<]<<<<[<]<<<<<<]+++++++
|
||||
+++.[-]<<<[>>+>+>+<<<<-]>>[<<+>>-]<[>+>+>>+<<<<-]>[<+>-]>]
|
||||
|
||||
+2
-75
@@ -1,86 +1,13 @@
|
||||
.{
|
||||
// This is the default name used by packages depending on this one. For
|
||||
// example, when a user runs `zig fetch --save <url>`, this field is used
|
||||
// as the key in the `dependencies` table. Although the user can choose a
|
||||
// different name, most users will stick with this provided value.
|
||||
//
|
||||
// It is redundant to include "zig" in this name because it is already
|
||||
// within the Zig package namespace.
|
||||
.name = .bfz,
|
||||
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
// In a future version of Zig it will be used for package deduplication.
|
||||
.version = "0.0.0",
|
||||
|
||||
// Together with name, this represents a globally unique package
|
||||
// identifier. This field is generated by the Zig toolchain when the
|
||||
// package is first created, and then *never changes*. This allows
|
||||
// unambiguous detection of one package being an updated version of
|
||||
// another.
|
||||
//
|
||||
// When forking a Zig project, this id should be regenerated (delete the
|
||||
// field and run `zig build`) if the upstream project is still maintained.
|
||||
// Otherwise, the fork is *hostile*, attempting to take control over the
|
||||
// original project's identity. Thus it is recommended to leave the comment
|
||||
// on the following line intact, so that it shows up in code reviews that
|
||||
// modify the field.
|
||||
.version = "0.0.1",
|
||||
.fingerprint = 0x3765925f0c6b2cc2, // Changing this has security and trust implications.
|
||||
|
||||
// Tracks the earliest Zig version that the package considers to be a
|
||||
// supported use case.
|
||||
.minimum_zig_version = "0.14.1",
|
||||
|
||||
// This field is optional.
|
||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||
// Once all dependencies are fetched, `zig build` no longer requires
|
||||
// internet connectivity.
|
||||
.minimum_zig_version = "0.15.2",
|
||||
.dependencies = .{
|
||||
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
||||
//.example = .{
|
||||
// // When updating this field to a new URL, be sure to delete the corresponding
|
||||
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||
// // the new URL. If the contents of a URL change this will result in a hash mismatch
|
||||
// // which will prevent zig from using it.
|
||||
// .url = "https://example.com/foo.tar.gz",
|
||||
//
|
||||
// // This is computed from the file contents of the directory of files that is
|
||||
// // obtained after fetching `url` and applying the inclusion rules given by
|
||||
// // `paths`.
|
||||
// //
|
||||
// // This field is the source of truth; packages do not come from a `url`; they
|
||||
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
||||
// // obtain a package matching this `hash`.
|
||||
// //
|
||||
// // Uses the [multihash](https://multiformats.io/multihash/) format.
|
||||
// .hash = "...",
|
||||
//
|
||||
// // When this is provided, the package is found in a directory relative to the
|
||||
// // build root. In this case the package's hash is irrelevant and therefore not
|
||||
// // computed. This field and `url` are mutually exclusive.
|
||||
// .path = "foo",
|
||||
//
|
||||
// // When this is set to `true`, a package is declared to be lazily
|
||||
// // fetched. This makes the dependency only get fetched if it is
|
||||
// // actually used.
|
||||
// .lazy = false,
|
||||
//},
|
||||
},
|
||||
|
||||
// Specifies the set of files and directories that are included in this package.
|
||||
// Only files and directories listed here are included in the `hash` that
|
||||
// is computed for this package. Only files listed here will remain on disk
|
||||
// when using the zig package manager. As a rule of thumb, one should list
|
||||
// files required for compilation plus any license(s).
|
||||
// Paths are relative to the build root. Use the empty string (`""`) to refer to
|
||||
// the build root itself.
|
||||
// A directory listed here means that all files within, recursively, are included.
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
// For example...
|
||||
//"LICENSE",
|
||||
//"README.md",
|
||||
},
|
||||
}
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
99 bottles in 1752 brainfuck instructions
|
||||
by jim crawford (http://www (dot) goombas (dot) org/)
|
||||
>++++++++++[<++++++++++>-]<->>>>>+++[>+++>+++<<-]<<<<+<[>[>+
|
||||
>+<<-]>>[-<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]>>
|
||||
>>>>[[-]<++++++++++<->>]<-[>+>+<<-]>[<+>-]+>[[-]<->]<<<<<<<<
|
||||
<->>]<[>+>+<<-]>>[-<<+>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<
|
||||
<[>>+>+<<<-]>>>[-<<<+>>>]++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<
|
||||
<[>+<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<<[>>+>+<<<-]>>>[-<<<+>>>]+
|
||||
+++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<
|
||||
<+>>[-]]<<[[-]>>>++++++++[>>++++++<<-]>[<++++++++[>++++++<-]
|
||||
>.<++++++++[>------<-]>[<<+>>-]]>.<<++++++++[>>------<<-]<[-
|
||||
>>+<<]<++++++++[<++++>-]<.>+++++++[>+++++++++<-]>+++.<+++++[
|
||||
>+++++++++<-]>.+++++..--------.-------.++++++++++++++>>[>>>+
|
||||
>+<<<<-]>>>>[-<<<<+>>>>]>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<<
|
||||
[>>>+>+<<<<-]>>>>[-<<<<+>>>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>
|
||||
>]<<<[>>+<<[-]]>[>+<[-]]++>>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<
|
||||
+<[[-]>-<]>[<<<<<<<.>>>>>>>[-]]<<<<<<<<<.>>----.---------.<<
|
||||
.>>----.+++..+++++++++++++.[-]<<[-]]<[>+>+<<-]>>[-<<+>>]+>+<
|
||||
[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++
|
||||
>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<<+>
|
||||
>[-]]<<[[-]>++++++++[<++++>-]<.>++++++++++[>+++++++++++<-]>+
|
||||
.-.<<.>>++++++.------------.---.<<.>++++++[>+++<-]>.<++++++[
|
||||
>----<-]>++.+++++++++++..[-]<<[-]++++++++++.[-]]<[>+>+<<-]>>
|
||||
[-<<+>>]+++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.
|
||||
>+++++++++[>+++++++++<-]>+++.+++++++++++++.++++++++++.------
|
||||
.<++++++++[>>++++<<-]>>.<++++++++++.-.---------.>.<-.+++++++
|
||||
++++.++++++++.---------.>.<-------------.+++++++++++++.-----
|
||||
-----.>.<++++++++++++.---------------.<+++[>++++++<-]>..>.<-
|
||||
---------.+++++++++++.>.<<+++[>------<-]>-.+++++++++++++++++
|
||||
.---.++++++.-------.----------.[-]>[-]<<<.[-]]<[>+>+<<-]>>[-
|
||||
<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.[
|
||||
-]<[-]>]<+<]
|
||||
|
||||
|
||||
+36
-30
@@ -19,12 +19,13 @@
|
||||
// THE SOFTWARE.
|
||||
|
||||
const std = @import("std");
|
||||
const stdin = std.io.getStdIn().reader();
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
var stdout_buffer: [256]u8 = undefined;
|
||||
var stdin_buffer: [256]u8 = undefined;
|
||||
|
||||
const bf_machine = struct {
|
||||
const Self = @This();
|
||||
const size:usize = 5000;
|
||||
const size:usize = 30000;
|
||||
const exeParam = struct {
|
||||
program_pos:usize = 0,
|
||||
loop:bool = false,
|
||||
@@ -32,56 +33,54 @@ const bf_machine = struct {
|
||||
};
|
||||
const Fail = error {
|
||||
Odd_Brackets,
|
||||
Odd_Brackets_Overflow
|
||||
};
|
||||
mem:[]u8,
|
||||
pos:usize,
|
||||
program:[]const u8,
|
||||
allocator:std.mem.Allocator,
|
||||
stdout:*std.io.Writer,
|
||||
stdin:*std.io.Reader,
|
||||
|
||||
pub fn init(allocator:std.mem.Allocator, program:[]const u8) !Self {
|
||||
pub fn init(allocator:std.mem.Allocator, program:[]const u8, stdin:*std.io.Reader, stdout:*std.io.Writer) !Self {
|
||||
const mem = try allocator.alloc(u8, size);
|
||||
for(mem) |*x| { // allocator doesn't make sure any of this is actually zero
|
||||
x.* = 0;
|
||||
}
|
||||
@memset(mem, 0);
|
||||
return .{
|
||||
.program = program,
|
||||
.mem = mem,
|
||||
.allocator = allocator,
|
||||
.pos = 0,
|
||||
.stdin = stdin,
|
||||
.stdout = stdout,
|
||||
};
|
||||
}
|
||||
|
||||
// pub fn bracket_check(program:[]const u8) bool {
|
||||
// var pos = 0;
|
||||
// var open = 0;
|
||||
// var closed = 0;
|
||||
// while(pos < program.len-1) {
|
||||
// switch(program[pos]) {
|
||||
// '[' => {open += 1;},
|
||||
// ']' => {closed += 1;},
|
||||
// else => {},
|
||||
// }
|
||||
// }
|
||||
// if(open == closed) return false else return true;
|
||||
// }
|
||||
|
||||
pub fn execute(self:*Self, param:exeParam) !usize {
|
||||
var program_pos = param.program_pos;
|
||||
while(program_pos < self.program.len-1) {
|
||||
switch(self.program[program_pos]) {
|
||||
'>' => {self.pos += 1; program_pos += 1;},
|
||||
'<' => {self.pos -= 1; program_pos += 1;},
|
||||
'+' => {self.mem[self.pos] = @addWithOverflow(self.mem[self.pos], 1)[0]; program_pos += 1;},
|
||||
'-' => {self.mem[self.pos] = @subWithOverflow(self.mem[self.pos], 1)[0]; program_pos += 1;},
|
||||
'.' => {try stdout.print("{c}", .{self.mem[self.pos]}); program_pos += 1;},
|
||||
'+' => {self.mem[self.pos] +%= 1; program_pos += 1;},
|
||||
'-' => {self.mem[self.pos] -%= 1; program_pos += 1;},
|
||||
'.' => {try self.stdout.print("{c}", .{self.mem[self.pos]}); try self.stdout.flush(); program_pos += 1;},
|
||||
',' => {
|
||||
const i:u8 = try stdin.readByte();
|
||||
const i:u8 = try self.stdin.takeByte();
|
||||
self.mem[self.pos] = i;
|
||||
program_pos += 1;
|
||||
},
|
||||
'[' => {
|
||||
program_pos += 1;
|
||||
program_pos = try self.execute(.{.program_pos = program_pos, .loop = true, .loop_start = program_pos});
|
||||
if(self.mem[self.pos] == 0) {
|
||||
var nested:usize = 1;
|
||||
program_pos += 1;
|
||||
while(nested > 0 and program_pos < self.program.len-1) : (program_pos += 1) {
|
||||
if(self.program[program_pos] == ']') nested -= 1 else if (self.program[program_pos] == '[') nested += 1;
|
||||
}
|
||||
if(nested != 0) return Fail.Odd_Brackets_Overflow;
|
||||
} else {
|
||||
program_pos += 1;
|
||||
program_pos = try self.execute(.{.program_pos = program_pos, .loop = true, .loop_start = program_pos});
|
||||
}
|
||||
},
|
||||
']' => {
|
||||
if(param.loop == true) {
|
||||
@@ -99,7 +98,6 @@ const bf_machine = struct {
|
||||
pub fn deinit(self:*Self) void {
|
||||
self.allocator.free(self.mem);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
@@ -108,8 +106,14 @@ pub fn main() !void {
|
||||
const args = try std.process.argsAlloc(allocator);
|
||||
defer std.process.argsFree(allocator, args);
|
||||
|
||||
var stdout_writer_wrapper = std.fs.File.stdout().writer(&stdout_buffer);
|
||||
const stdout: *std.io.Writer = &stdout_writer_wrapper.interface;
|
||||
|
||||
var stdin_reader_wrapper = std.fs.File.stdin().reader(&stdin_buffer);
|
||||
const stdin: *std.io.Reader = &stdin_reader_wrapper.interface;
|
||||
|
||||
if(args.len != 2) {
|
||||
try stdout.print("Filename as first argument required!\n", .{});
|
||||
std.debug.print("Filename as first argument required!\n", .{});
|
||||
} else {
|
||||
var f = try std.fs.cwd().openFile(args[1], .{});
|
||||
defer f.close();
|
||||
@@ -122,8 +126,10 @@ pub fn main() !void {
|
||||
return;
|
||||
}
|
||||
|
||||
var bf = try bf_machine.init(allocator, d);
|
||||
var bf = try bf_machine.init(allocator, d, stdin, stdout);
|
||||
defer bf.deinit();
|
||||
_ = try bf.execute(.{});
|
||||
}
|
||||
|
||||
try stdout.flush();
|
||||
}
|
||||
Reference in New Issue
Block a user