Skip to content

Commit 3cbc7cf

Browse files
prplzJRaspass
authored andcommitted
Add brainfuck language (#79)
* Init brainfuck * Implement brainfuck argv * Add brainfuck version number * Add brainfuck icon
1 parent d184844 commit 3cbc7cf

File tree

11 files changed

+156
-19
lines changed

11 files changed

+156
-19
lines changed

assets/hole.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ main nav a:nth-child(3):before {
258258
#tabs {
259259
clear: both;
260260
display: grid;
261-
grid-template-columns: repeat(13, 1fr);
261+
grid-template-columns: repeat(14, 1fr);
262262
position: relative;
263263
-moz-user-select: none;
264264
-ms-user-select: none;
@@ -287,6 +287,7 @@ main nav a:nth-child(3):before {
287287
}
288288

289289
#tabs a[href="#bash"] { background-image: svg(bash#fff) }
290+
#tabs a[href="#brainfuck"] { background-image: svg(brainfuck#fff) }
290291
#tabs a[href="#c"] { background-image: svg(c#fff) }
291292
#tabs a[href="#haskell"] { background-image: svg(haskell#fff) }
292293
#tabs a[href="#j"] { background-image: svg(j#fff) }

assets/hole.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ onload = function() {
5050
let editors = [];
5151

5252
for (let langName of [
53-
'Bash', 'C', 'Haskell', 'J', 'JavaScript', 'Julia', 'Lisp', 'Lua',
53+
'Bash', 'Brainfuck', 'C', 'Haskell', 'J', 'JavaScript', 'Julia', 'Lisp', 'Lua',
5454
'Perl', 'Perl 6', 'PHP', 'Python', 'Ruby',
5555
]) {
5656
let lang = langName.replace(/ /, '').toLowerCase();
@@ -94,7 +94,7 @@ onload = function() {
9494

9595
( onhashchange = function() {
9696
// Kick 'em to Perl 6 if we don't know the chosen language.
97-
if (!/^#(?:bash|c|haskell|j|javascript|julia|lisp|lua|perl6?|php|python|ruby)$/.exec(location.hash))
97+
if (!/^#(?:bash|brainfuck|c|haskell|j|javascript|julia|lisp|lua|perl6?|php|python|ruby)$/.exec(location.hash))
9898
location.hash = 'perl6';
9999

100100
let lang = location.hash.slice(1);

assets/includes/brainfuck.svg

Lines changed: 1 addition & 0 deletions
Loading

assets/user.css

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,19 @@ tr {
101101
}
102102

103103
#matrix tr:first-child th:nth-child(2) { background-image: svg(bash#222) }
104-
#matrix tr:first-child th:nth-child(3) { background-image: svg(c#222) }
105-
#matrix tr:first-child th:nth-child(4) { background-image: svg(haskell#222) }
106-
#matrix tr:first-child th:nth-child(5) { background-image: svg(j#222) }
107-
#matrix tr:first-child th:nth-child(6) { background-image: svg(javascript#222) }
108-
#matrix tr:first-child th:nth-child(7) { background-image: svg(julia#222) }
109-
#matrix tr:first-child th:nth-child(8) { background-image: svg(lisp#222) }
110-
#matrix tr:first-child th:nth-child(9) { background-image: svg(lua#222) }
111-
#matrix tr:first-child th:nth-child(10) { background-image: svg(perl#222) }
112-
#matrix tr:first-child th:nth-child(11) { background-image: svg(perl6#222) }
113-
#matrix tr:first-child th:nth-child(12) { background-image: svg(php#222) }
114-
#matrix tr:first-child th:nth-child(13) { background-image: svg(python#222) }
115-
#matrix tr:first-child th:nth-child(14) { background-image: svg(ruby#222) }
104+
#matrix tr:first-child th:nth-child(3) { background-image: svg(brainfuck#222) }
105+
#matrix tr:first-child th:nth-child(4) { background-image: svg(c#222) }
106+
#matrix tr:first-child th:nth-child(5) { background-image: svg(haskell#222) }
107+
#matrix tr:first-child th:nth-child(6) { background-image: svg(j#222) }
108+
#matrix tr:first-child th:nth-child(7) { background-image: svg(javascript#222) }
109+
#matrix tr:first-child th:nth-child(8) { background-image: svg(julia#222) }
110+
#matrix tr:first-child th:nth-child(9) { background-image: svg(lisp#222) }
111+
#matrix tr:first-child th:nth-child(10) { background-image: svg(lua#222) }
112+
#matrix tr:first-child th:nth-child(11) { background-image: svg(perl#222) }
113+
#matrix tr:first-child th:nth-child(12) { background-image: svg(perl6#222) }
114+
#matrix tr:first-child th:nth-child(13) { background-image: svg(php#222) }
115+
#matrix tr:first-child th:nth-child(14) { background-image: svg(python#222) }
116+
#matrix tr:first-child th:nth-child(15) { background-image: svg(ruby#222) }
116117

117118
#matrix,
118119
#trophies {

build-langs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare -A urls=(
44
["bash"]="//www.gnu.org/software/bash/"
5+
["brainfuck"]="//github.com/JRaspass/code-golf/tree/master/containers/brainfuck/"
56
["c"]="//bellard.org/tcc/"
67
["haskell"]="//www.haskell.org/ghc/"
78
["j"]="http://jsoftware.com"
@@ -20,7 +21,7 @@ docker pull alpine:edge
2021

2122
cd containers
2223

23-
for name in Bash C Haskell J JavaScript Julia Lisp Lua Perl Perl\ 6 PHP Python Ruby; do
24+
for name in Bash Brainfuck C Haskell J JavaScript Julia Lisp Lua Perl Perl\ 6 PHP Python Ruby; do
2425
lang=${name,,} # lowercase
2526
lang=${lang// /} # trim space
2627

containers/brainfuck/Dockerfile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
FROM alpine:edge as builder
2+
3+
RUN apk add --no-cache curl gcc make musl-dev
4+
5+
RUN curl -L https://download.savannah.nongnu.org/releases/tinycc/tcc-0.9.27.tar.bz2 | tar xjf -
6+
7+
RUN cd tcc-0.9.27 \
8+
&& ./configure \
9+
--config-musl \
10+
--prefix=/usr \
11+
--strip-binaries \
12+
&& make \
13+
&& make install
14+
15+
COPY brainfuck.c /
16+
17+
RUN gcc -s -o brainfuck brainfuck.c
18+
19+
FROM scratch
20+
21+
COPY --from=0 /lib/ld-musl-x86_64.so.1 /lib/
22+
COPY --from=0 /usr/bin/tcc \
23+
/brainfuck /usr/bin/
24+
COPY --from=0 /usr/include/assert.h \
25+
/usr/include/complex.h \
26+
/usr/include/ctype.h \
27+
/usr/include/errno.h \
28+
/usr/include/features.h \
29+
/usr/include/fenv.h \
30+
/usr/include/float.h \
31+
/usr/include/inttypes.h \
32+
/usr/include/iso646.h \
33+
/usr/include/limits.h \
34+
/usr/include/locale.h \
35+
/usr/include/math.h \
36+
/usr/include/setjmp.h \
37+
/usr/include/signal.h \
38+
/usr/include/stdalign.h \
39+
/usr/include/stdarg.h \
40+
/usr/include/stdbool.h \
41+
/usr/include/stddef.h \
42+
/usr/include/stdint.h \
43+
/usr/include/stdio.h \
44+
/usr/include/stdlib.h \
45+
/usr/include/stdnoreturn.h \
46+
/usr/include/string.h \
47+
/usr/include/tgmath.h \
48+
/usr/include/threads.h \
49+
/usr/include/time.h \
50+
/usr/include/uchar.h \
51+
/usr/include/wchar.h \
52+
/usr/include/wctype.h /usr/include/
53+
COPY --from=0 /usr/include/bits /usr/include/bits/
54+
COPY --from=0 /usr/lib/libc.so /usr/lib/
55+
COPY --from=0 /usr/lib/tcc/libtcc1.a /usr/lib/tcc/
56+
57+
ENTRYPOINT ["/usr/bin/brainfuck", "-v"]

containers/brainfuck/brainfuck.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
6+
// Adapted from https://github.com/TryItOnline/brainfuck
7+
8+
int main(int argc, char *argv[]) {
9+
if (argc > 1 && strcmp(argv[1], "-v") == 0) {
10+
puts("1.0.0");
11+
return 0;
12+
}
13+
14+
FILE *fp = fopen("/tmp/code.c", "w");
15+
16+
if (fp == 0)
17+
return 1;
18+
19+
fputs(
20+
"#include <stdint.h>\n"
21+
"#include <stdio.h>\n"
22+
"int main(){\n"
23+
"uint8_t t[65536] = {0};\n"
24+
"uint16_t p = 0;\n"
25+
"size_t i;\n",
26+
fp);
27+
28+
// char in[] = "arg2\0arg3\0argN\0";
29+
fputs("char in[] = \"", fp);
30+
for (int i = 2; i < argc; i++) {
31+
// Write each character of argv[i] using \xNN escape
32+
for (char *a = argv[i]; *a; a++)
33+
fprintf(fp, "\\x%02x", *a & 255);
34+
fputs("\\0",fp);
35+
}
36+
fputs("\";\n", fp);
37+
38+
int ch;
39+
while ((ch = getchar()) != EOF)
40+
switch (ch) {
41+
case '>':
42+
fputs("++p;\n", fp);
43+
break;
44+
case '<':
45+
fputs("--p;\n", fp);
46+
break;
47+
case '+':
48+
fputs("++t[p];\n", fp);
49+
break;
50+
case '-':
51+
fputs("--t[p];\n", fp);
52+
break;
53+
case ',':
54+
// Cell is unchanged when trying to read past the end of stdin
55+
fputs("if(i<sizeof(in)-1)t[p]=in[i++];\n", fp);
56+
break;
57+
case '.':
58+
fputs("putchar(t[p]);\n", fp);
59+
break;
60+
case '[':
61+
fputs("while(t[p]){\n", fp);
62+
break;
63+
case ']':
64+
fputs("}\n", fp);
65+
break;
66+
}
67+
68+
fputs("return 0;}", fp);
69+
70+
fclose(fp);
71+
72+
char *tcc_argv[] = {"/usr/bin/tcc", "-run", "/tmp/code.c", NULL};
73+
execv("/usr/bin/tcc", tcc_argv);
74+
perror("execv");
75+
}

pull-langs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22

3-
for lang in bash haskell j javascript julia lisp lua perl perl6 php python ruby; do
3+
for lang in bash brainfuck c haskell j javascript julia lisp lua perl perl6 php python ruby; do
44
docker pull quay.io/jraspass/$lang-builder
55
done

push-langs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22

3-
for lang in bash haskell j javascript julia lisp lua perl perl6 php python ruby; do
3+
for lang in bash brainfuck c haskell j javascript julia lisp lua perl perl6 php python ruby; do
44
docker push quay.io/jraspass/$lang-builder
55
done

routes/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Hole struct{ Prev, Next, ID, Name, Difficulty, Preamble string }
77

88
var langs = []Lang{
99
{"bash", "Bash"},
10+
{"brainfuck", "Brainfuck"},
1011
{"c", "C"},
1112
{"haskell", "Haskell"},
1213
{"j", "J"},

0 commit comments

Comments
 (0)