-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
113 lines (92 loc) · 2.85 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <fstream>
#include <sstream>
#include <stack>
#include <vector>
#include <string>
#include "help-usage.hpp"
#include "fmt/core.h"
#include "fmt/printf.h"
#include "fmt/color.h"
#include "types.hpp"
#include "transpiler.hpp"
#include "interpret_unsafe_unoptimised.hpp"
#include "interpret_unsafe_optimised.hpp"
#include "interpret_safe_unoptimised.hpp"
#include "interpret_safe_optimised.hpp"
int main(int argc, char** argv)
{
std::string program;
bool safe = false;
bool counter = false;
bool optimise = false;
bool transpile = false;
if(argc > 1)
{
if(argv[1] == std::string("-h"))
{
print_help();
return 0;
}
std::ifstream f;
f.open(argv[1]);
if(!f)
{
fmt::print("{0}: invalid file path! Try the option `-h`\n",
fmt::styled("bfntp" , fmt::fg(fmt::color::red) | fmt::emphasis::bold)
);
return -1;
}
for(std::string opt : std::vector<std::string>(argv + 1, argv + argc))
{
if(opt == "-s") { safe = true; }
if(opt == "-c") { counter = true; }
if(opt == "-o") { optimise = true; }
if(opt == "-t") { transpile = true; }
}
if((not safe) and counter) { counter = false; }
if(optimise and counter) { counter = false; }
std::stringstream ss;
ss << f.rdbuf();
program = ss.str();
} else {
fmt::print(std::string("Please supply the path of the brainfuck source as the first commandline argument\n") +
"If you don't know how to use this program, the commandline argument `-h` will give you" +
" usage notes.\n");
return 1;
}
std::vector<uint16_t> mem_tape; mem_tape.resize(32 * 1024); // standard brainfuck 30k cells
std::stack<size_t> bracket_insn_pointers;
bracket_insn_pointers.push(0);
size_t brackets_to_skip = 0;
int64_t insn_num = 0;
bool overflow = false;
uint32_t temp = 0;
if(transpile)
{
transpiler(program, optimise, safe);
return 0;
}
if(!optimise)
{
if(!safe)
interpret_raw_unsafe(program, mem_tape, bracket_insn_pointers);
else
interpret_raw_safe(program, mem_tape, bracket_insn_pointers, overflow, counter, insn_num);
}
else
{
std::vector<insn_t> opt_program;
optim(program, opt_program);
if(!safe)
interpret_opt_unsafe(opt_program, mem_tape, bracket_insn_pointers);
else
interpret_opt_safe(opt_program, mem_tape, bracket_insn_pointers);
}
if(counter)
{
fmt::print("the program finished in {0} instructions and {1}\n", insn_num,
overflow?"overflowed":"didn't overflow");
}
fmt::print("\n");
return 0;
}