fabianishere / brainfuck

Brainfuck interpreter written in C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support piped input

mathiasbynens opened this issue · comments

It would be nice if piped content was supported:

echo '.+[.+] prints out all ASCII characters' | brainfuck -c
# Other example: concatenate two brainfuck programs and execute them
cat file1 file2 | brainfuck -c
# Other example: minify brainfuck code using brnfckr and pass the result to `brainfuck` directly
brnfckr '.+[.+] prints out all ASCII characters' | brainfuck -c

Currently, this gives the following error:

error: no code specified!

I'll look into this once I have sorted out some local merge conflicts. (rolls eyes) The irony of git being a git.

Thanks in advance!

Done! :) Will do a pull request for it now.

Hmm, I don’t like having a separate -p option just for piping. What if you want to pipe a file name instead of some code to brainfuck? I would suggest the following:

# piping brainfuck code and executing it, using the `-c` flag, as you normally would
echo '.+[.+] prints out all ASCII characters' | brainfuck -c
# piping a file name and executing the brainfuck code it contains, using the `-f` flag, as you normally would
echo 'foo.b' | brainfuck -f

I’m using echo in these examples, but of course in real-world situations the return value could come from any shell script.

I do the same thing in the brnfckr and luamin binaries, for example. It’s very simple: if stdin is a tty, then handle the shell argument values; if stdin is not a tty, then handle the piped content.

What do you guys think about this?

Yeah, I was reading about checking whether stdin was a tty or not. But I thought I would just implement it quickly for now, as this way took me 3 minutes to do. I will look into implementing it in a better way some time today though! :)

I think I have now implemented it as you have suggested! :)