Skip to content

Commit cc5508a

Browse files
committed
force add files
0 parents  commit cc5508a

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.vscode
2+
a.out.js
3+
a.out.wasm
4+
bli

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Leandro Ferreira
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CC=gcc
2+
CFLAGS=-Wall -Werror
3+
4+
all: bli.c
5+
$(CC) $(CFLAGS) bli.c -o bli
6+
7+
clean: bli
8+
rm -f bli
9+
10+
web: bli.html
11+
emcc -O3 -s WASM=1 bli.c

README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Simple and lazy Brainf*ck interpreter
2+
3+
4+
### Why?
5+
6+
Because i'm learning about compilers and interpreters
7+
8+
9+
## Does it work?
10+
11+
Yes, all you need is GCC and make ( in fact you just need gcc, but typing make is faster )
12+
13+
```shell
14+
$ make
15+
gcc -Wall -Werror bli.c -o bli
16+
$ ./bli "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
17+
Hello World!
18+
```
19+
20+
And that's it!!
21+
22+
### Why Brainf*ck?
23+
24+
As much as Brainfuck can be a little bit intimidating at first look, it's a very simple esoteric language , it has a VERY compact instruction set ( 8 to be exact ) and it's functioning is kinda funny.
25+
26+
27+
### How it works?
28+
29+
it starting by reading the first parameter passed throug the command line, than it loop through it char by char and do a action on the memory tape.
30+
31+
Also, the tape is a 30000 array with 0 on every position, the instruction set move ou update the value of a position on this tape that's later converted to it's equivalent char in the ASCII table, talking about tables, that's one containing all possible brainfuck instructions:
32+
33+
| Instruction | Utility |
34+
|------|-------|
35+
| "." (dot) | print the current cell value |
36+
| "+" and "-" | increment or decrement the value of the current cell |
37+
| "," | read a char from the user and set at the pointer position |
38+
| "[" and "]" | start and end a loop when the current cell is not zero |
39+
| "<" and ">" | move the point to the right or left |
40+
41+
### Visual explaining: TODO
42+
43+
Add a visual run through the hello world example

bli.c

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <emscripten.h>
5+
6+
char tape[30000], *ptr = tape;
7+
8+
char brainfuck(char *code, size_t code_len) {
9+
char output[30000] = {0};
10+
int output_index = 0;
11+
int code_index = 0;
12+
13+
while (code_index < code_len) {
14+
char c = code[code_index];
15+
16+
switch (c) {
17+
case '>':
18+
ptr++;
19+
break;
20+
case '<':
21+
ptr--;
22+
break;
23+
case '+':
24+
(*ptr)++;
25+
break;
26+
case '-':
27+
(*ptr)--;
28+
break;
29+
case '.':
30+
output[output_index] = *ptr;
31+
output_index++;
32+
break;
33+
case ',':
34+
*ptr = getchar();
35+
break;
36+
case '[':
37+
if (*ptr == 0) {
38+
int balance = 1;
39+
while (balance) {
40+
code_index++;
41+
char next_c = code[code_index];
42+
if (next_c == '[') {
43+
balance++;
44+
} else if (next_c == ']') {
45+
balance--;
46+
}
47+
}
48+
}
49+
break;
50+
case ']':
51+
if (*ptr != 0) {
52+
int balance = 1;
53+
while (balance) {
54+
code_index--;
55+
char next_c = code[code_index];
56+
if (next_c == ']') {
57+
balance++;
58+
} else if (next_c == '[') {
59+
balance--;
60+
}
61+
}
62+
}
63+
break;
64+
default:
65+
break;
66+
}
67+
68+
code_index++;
69+
}
70+
71+
printf("%s", output);
72+
return 0;
73+
}
74+
75+
int main(int argc, char *argv[]) {
76+
if (argc < 2) {
77+
fprintf(stderr, "Usage: %s \"brainfuck-code\"\n", argv[0]);
78+
exit(EXIT_FAILURE);
79+
}
80+
81+
char *code = argv[1];
82+
size_t code_len = strlen(code);
83+
84+
brainfuck(code, code_len);
85+
86+
return 0;
87+
}

bli.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script src="a.out.js"></script>
2+
<script>
3+
Module.onRuntimeInitialized = async (_) => {
4+
const brainfuck = {
5+
version: Module.cwrap('main', 'number', ['string']),
6+
};
7+
console.log(brainfuck("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."));
8+
};
9+
</script>

0 commit comments

Comments
 (0)