From 6bd4484fb0abdbe1ac074ce00a0ed9e8333316fa Mon Sep 17 00:00:00 2001 From: 1computer1 Date: Wed, 22 May 2019 22:26:55 -0400 Subject: [PATCH] Add Brainfuck --- README.md | 1 + docker/brainfuck/Dockerfile | 12 ++++ docker/brainfuck/bf.cpp | 109 ++++++++++++++++++++++++++++++++++++ docker/brainfuck/run.sh | 1 + src/languages/brainfuck.js | 12 ++++ 5 files changed, 135 insertions(+) create mode 100644 docker/brainfuck/Dockerfile create mode 100644 docker/brainfuck/bf.cpp create mode 100644 docker/brainfuck/run.sh create mode 100644 src/languages/brainfuck.js diff --git a/README.md b/README.md index 7aa8c7b..692c5b0 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ One of the following language codes is set in `lang`. Options are optionally set in `options`, which is a semicolon-delimited list of `flag` or `flag=value`. - `bash` Bash +- `bf` Brainfuck - `c` C (GCC) - `clj` Clojure - `cpp` C++ (G++) diff --git a/docker/brainfuck/Dockerfile b/docker/brainfuck/Dockerfile new file mode 100644 index 0000000..067f601 --- /dev/null +++ b/docker/brainfuck/Dockerfile @@ -0,0 +1,12 @@ +FROM alpine AS build + +RUN apk update && apk add g++ +COPY bf.cpp . +RUN g++ bf.cpp -o bf + +FROM alpine +LABEL author="1Computer1" + +RUN apk update && apk add libstdc++ +COPY --from=build bf /usr/local/bin/ +COPY run.sh /var/run/ diff --git a/docker/brainfuck/bf.cpp b/docker/brainfuck/bf.cpp new file mode 100644 index 0000000..d67de52 --- /dev/null +++ b/docker/brainfuck/bf.cpp @@ -0,0 +1,109 @@ +#include +#include +#include + +int main(int argc, char **argv) { + if (argc == 1) { + std::cerr << "No input given"; + return 1; + } + + char* ops = argv[1]; + std::vector tape = { 0 }; + int oix = 0; + int tix = 0; + int len = strlen(ops); + while (oix < len) { + switch (ops[oix]) { + case '>': + tix++; + if (tix >= tape.size()) { + tape.push_back(0); + } + + oix++; + break; + case '<': + tix--; + if (tix < 0) { + std::cerr << "Out of bounds"; + return 1; + } + + oix++; + break; + case '+': + tape[tix]++; + oix++; + break; + case '-': + tape[tix]--; + oix++; + break; + case '.': + std::cout << tape[tix]; + oix++; + break; + case ',': + std::cin >> tape[tix]; + oix++; + break; + case '[': + if (tape[tix] == 0) { + int ls = 0; + int rs = 0; + for (int i = oix; i < len; i++) { + switch (ops[i]) { + case '[': + ls++; + break; + case ']': + rs++; + break; + default: + break; + } + + if (ls == rs) { + oix = i + 1; + break; + } + } + } else { + oix++; + } + + break; + case ']': + if (tape[tix] != 0) { + int ls = 0; + int rs = 0; + for (int i = oix; i >= 0; i--) { + switch (ops[i]) { + case '[': + ls++; + break; + case ']': + rs++; + break; + default: + break; + } + + if (ls == rs) { + oix = i + 1; + break; + } + } + } else { + oix++; + } + + break; + default: + oix++; + } + } + + return 0; +} diff --git a/docker/brainfuck/run.sh b/docker/brainfuck/run.sh new file mode 100644 index 0000000..ef1d0ee --- /dev/null +++ b/docker/brainfuck/run.sh @@ -0,0 +1 @@ +bf "$1" diff --git a/src/languages/brainfuck.js b/src/languages/brainfuck.js new file mode 100644 index 0000000..d8e7b55 --- /dev/null +++ b/src/languages/brainfuck.js @@ -0,0 +1,12 @@ +const Language = require('../struct/Language'); + +class Brainfuck extends Language { + constructor() { + super('brainfuck', { + name: 'Brainfuck', + aliases: ['brainfuck', 'bf'] + }); + } +} + +module.exports = Brainfuck;