diff options
author | kdx <kdx@42l.fr> | 2023-01-19 00:23:57 +0100 |
---|---|---|
committer | kdx <kdx@42l.fr> | 2023-01-19 00:25:08 +0100 |
commit | a4ed47d2e35d2e596c06ece8f2513a2716d0e19a (patch) | |
tree | 17bfcd6493bcc845838b02db28b075a9cc620860 | |
parent | 5ca2770f461269f965811af2e32949cecb65745f (diff) | |
download | hare-brainfuck-a4ed47d2e35d2e596c06ece8f2513a2716d0e19a.tar.gz |
need to read input
-rw-r--r-- | drain.ha | 15 | ||||
-rw-r--r-- | main.ha | 6 | ||||
-rw-r--r-- | test.bf | 13 |
3 files changed, 31 insertions, 3 deletions
@@ -1,9 +1,20 @@ +use errors; +use fs; use io; use os; use fmt; -fn drain_stdin() []u8 = { - const buf = match (io::drain(os::stdin)) { +fn drain(path: str) []u8 = { + const file = match (os::open(path)) { + case let file: io::file => + yield file; + case errors::noaccess => + fmt::fatalf("error opening {}: access denied", path); + case let err: fs::error => + fmt::fatalf("error opening {}: {}", path, fs::strerror(err)); + }; + defer io::close(file)!; + const buf = match (io::drain(file)) { case let buf: []u8 => yield buf; case let err: io::error => @@ -1,4 +1,5 @@ use fmt; +use os; use strings; let bf: Brainfuck = Brainfuck { @@ -7,8 +8,11 @@ let bf: Brainfuck = Brainfuck { }; export fn main() void = { - const buf = drain_stdin(); + if (len(os::args) != 2) + fmt::fatal("usage: hare-brainfuck <source>"); + const buf = drain(os::args[1]); defer free(buf); const buf = optimize(buf); + fmt::println("optimized source", strings::fromutf8(buf)!)!; exec(&bf, buf); }; @@ -0,0 +1,13 @@ +jkdwaljdwaljdaw +da +wjd +wadwaj +dwajdkwakdjwadk +jaw +dwajd +wa +j+++ +dawjdwadjwa +A-da_d-wad +AW-d_DW- +,.+,-.+..-.. |