-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck.cpp
50 lines (38 loc) · 1.27 KB
/
brainfuck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// A brainfuck interpreter written using unifuck
#include "unifuck.h"
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
using namespace unifuck;
using std::map; using std::string;
using std::ifstream; using std::runtime_error;
using std::cerr; using std::endl;
int main(int argc, char** argv){
// The brainfuck language is defined
map<string, instruction> brainfuck_language;
brainfuck_language[">"] = INCREMENT_POINTER;
brainfuck_language["<"] = DECREMENT_POINTER;
brainfuck_language["+"] = INCREMENT_DATA;
brainfuck_language["-"] = DECREMENT_DATA;
brainfuck_language["."] = OUTPUT_DATA;
brainfuck_language[","] = INPUT_DATA;
brainfuck_language["["] = JUMP_FORWARD;
brainfuck_language["]"] = JUMP_BACKWARD;
interpreter brainfuck_intepreter(brainfuck_language);
if(argc > 1){
for(int i = 1; i < argc; ++i){
ifstream source_file(argv[i]);
if(source_file){
brainfuck_intepreter.run(source_file);
}
else
cerr << "Unable to open file " << argv[i] << endl;
source_file.close();
}
}
else
brainfuck_intepreter.live_run();
return 0;
}