Skip to content

Commit 7bc8ed4

Browse files
committed
Create initial interpreter.
0 parents  commit 7bc8ed4

6 files changed

Lines changed: 294 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin/

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.PHONY: all hello mandelbrot clean
2+
3+
CXX = clang++ -Wall -std=c++11
4+
5+
all:
6+
mkdir -p bin
7+
$(CXX) -O3 -o bin/zero zero.cpp
8+
9+
dbg:
10+
mkdir -p bin
11+
$(CXX) -O0 -g -o bin/zero-debug zero.cpp
12+
13+
hello: all
14+
./bin/zero ./test/helloworld.b
15+
16+
mandelbrot: all
17+
./bin/zero ./test/mandelbrot.b
18+
19+
clean:
20+
rm -r bin
21+

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# zero.bf
2+
3+
A project that intends to run a Brainfuck version of Mandelbrot in <1ms.
4+
This project solely targets aarch64 macOS binaries.
5+
6+
## Building
7+
8+
Use `make`, the default target will build an optimized binary.
9+
A debug build can be generated using `make dbg`.
10+
11+
## Running
12+
13+
Run with `./bin/zero path/to/file.b`.

test/helloworld.b

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
>++++++++[<+++++++++>-]<.>++++[<+++++++>-]<+.+++++++..+++.>>++++++[<+++++++>-]<++.------------.>++++++[<+++++++++>-]<+.<.+++.------.--------.>>>++++[<++++++++>-]<+.

test/mandelbrot.b

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[
2+
>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>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<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>
142+
>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++
143+
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
144+
<<<<<]]>>>]

zero.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <fstream>
2+
#include <iostream>
3+
#include <stack>
4+
#include <stdexcept>
5+
#include <string>
6+
#include <unordered_map>
7+
#include <vector>
8+
9+
constexpr size_t MEMORY_SIZE = 30000;
10+
11+
void getInstructions(char*, std::vector<char>&, std::unordered_map<size_t, size_t>&);
12+
13+
int main(int argc, char** argv) {
14+
// We expect most of the time the program is provided.
15+
if (__builtin_expect(argc < 2, 0)) {
16+
std::cerr << "zero: please provide the input file" << std::endl;
17+
return 1;
18+
}
19+
// Keep track of the jumping points;
20+
std::vector<char> instructions;
21+
std::unordered_map<size_t, size_t> jump;
22+
try {
23+
getInstructions(argv[1], instructions, jump);
24+
} catch (std::runtime_error &e) {
25+
std::cerr << e.what() << std::endl;
26+
return 1;
27+
}
28+
// Program metadata.
29+
size_t instructionLength = instructions.size();
30+
size_t pc = 0;
31+
int32_t memory[30000];
32+
size_t pointer;
33+
while (pc < instructionLength) {
34+
switch (instructions[pc]) {
35+
case '+':
36+
memory[pointer]++;
37+
break;
38+
case '-':
39+
memory[pointer]--;
40+
break;
41+
// Memory pointer: wrap around.
42+
case '<':
43+
if (__builtin_expect(pointer == 0, 0)) {
44+
pointer = MEMORY_SIZE - 1;
45+
} else {
46+
pointer--;
47+
}
48+
break;
49+
case '>':
50+
if (__builtin_expect(pointer == MEMORY_SIZE - 1, 0)) {
51+
pointer = 0;
52+
} else {
53+
pointer++;
54+
}
55+
break;
56+
// IO: Delegate entirely to C++.
57+
case '.':
58+
std::cout << static_cast<char>(memory[pointer]);
59+
break;
60+
case ',':
61+
std::cin >> memory[pointer];
62+
break;
63+
// Jumps: continue so we don't increment program counter.
64+
case '[':
65+
if (memory[pointer] == 0) {
66+
pc = jump[pc];
67+
continue;
68+
}
69+
break;
70+
case ']':
71+
if (memory[pointer] != 0) {
72+
pc = jump[pc];
73+
continue;
74+
}
75+
break;
76+
default:
77+
std::string errorMessage = "unknown instruction: ";
78+
errorMessage.push_back(instructions[pc]);
79+
throw std::runtime_error(errorMessage);
80+
}
81+
pc++;
82+
}
83+
std::cout << std::endl;
84+
}
85+
86+
// Reads the instructions from file. If it doesn't exist, it does nothing.
87+
// Also populates the jump table.
88+
void getInstructions(char* fileName, std::vector<char> &instructions, std::unordered_map<size_t, size_t> &jump) {
89+
char ch;
90+
std::fstream file(fileName, std::fstream::in);
91+
std::stack<size_t> stack;
92+
size_t pc = 0;
93+
while (file >> ch) {
94+
instructions.push_back(ch);
95+
switch (ch) {
96+
case '[':
97+
stack.push(pc);
98+
break;
99+
case ']':
100+
if (__builtin_expect(stack.size() == 0, 0)) {
101+
throw std::runtime_error("program parse error: expected [");
102+
}
103+
size_t begin = stack.top();
104+
stack.pop();
105+
jump[begin] = pc;
106+
jump[pc] = begin;
107+
break;
108+
}
109+
pc++;
110+
}
111+
if (!__builtin_expect(stack.empty(), 1)) {
112+
throw std::runtime_error("program parse error: expected ]");
113+
}
114+
}

0 commit comments

Comments
 (0)