-
Notifications
You must be signed in to change notification settings - Fork 3
/
bf_main.cpp
executable file
·151 lines (130 loc) · 4.26 KB
/
bf_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2014 Brian Quinlan
// See "LICENSE" file for details.
//
// Executes Brainfuck code stored in a file. Flags control the execution mode.
// See http://en.wikipedia.org/wiki/Brainfuck.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include <string>
#include <vector>
#include "bf_runner.h"
#include "bf_compile_and_go.h"
#include "bf_interpreter.h"
#include "bf_jit.h"
using std::string;
using std::unique_ptr;
using std::vector;
const size_t kBrainfuckMemorySize = 1024 * 1024;
const char USAGE[] = "Usage: %s [options] <Brainfuck file>\n"
"Execute the Brainfuck code in the given file e.g.\n"
"%s examples/hello.b\n"
"\n"
"Options:\n"
"--mode=cag : Run using a compiler\n"
"--mode=i : Run using an interpreter\n"
"--mode=jit : Run using a Just-In-Time compiler\n";
// Passed to BrainfuckRunner->run(...) to provide output functionality for
// the "." command.
static bool bf_write(void*, char c) {
return putchar(c) != EOF;
}
// Passed to BrainfuckRunner->run(...) to provide input functionality for
// the "," command.
static char bf_read(void*) {
int c = getchar();
if (c == EOF) {
return 0;
} else {
return c;
}
}
int run_brainfuck_program(BrainfuckRunner* runner,
const string& source_file_path) {
FILE *bf_source_file = fopen(source_file_path.c_str(), "rb");
if (bf_source_file == NULL) {
fprintf(stderr, "Could not open file \"%s\": %s\n",
source_file_path.c_str(), strerror(errno));
return 1;
}
if (fseek(bf_source_file, 0, SEEK_END)) {
fprintf(stderr, "Could not seek in \"%s\": %s\n",
source_file_path.c_str(), strerror(ferror(bf_source_file)));
return 1;
}
long source_size = ftell(bf_source_file);
if (source_size == -1) {
fprintf(stderr, "Could not tell in \"%s\": %s\n",
source_file_path.c_str(), strerror(errno));
return 1;
}
rewind(bf_source_file);
char* source_buffer = reinterpret_cast<char *>(malloc(source_size));
size_t amount_read = fread(source_buffer, 1, source_size, bf_source_file);
fclose(bf_source_file);
if (amount_read != static_cast<size_t>(source_size)) {
fprintf(stderr, "Error reading file \"%s\": %s\n",
source_file_path.c_str(), strerror(errno));
return 1;
}
void* memory = calloc(kBrainfuckMemorySize, 1);
if (memory == NULL) {
fprintf(stderr,
"Unable to allocate memory %ld bytes for Brainfuck memory\n",
static_cast<unsigned long>(kBrainfuckMemorySize));
}
const string source(source_buffer, source_size);
if (!runner->init(source.begin(), source.end())) {
return 1;
}
runner->run(bf_read, NULL, bf_write, NULL, memory);
return 0;
}
int main(int argc, char *argv[]) {
unique_ptr<BrainfuckRunner> bf(new BrainfuckInterpreter());
string bf_file;
for (int i = 1; i < argc; ++i) {
if (argv[i] == string("-h") ||
argv[i] == string("-help") ||
argv[i] == string("--help") ||
argv[i] == string("-?")) {
printf(USAGE, argv[0], argv[0]);
return 0;
}
}
vector<string> files;
for (int i = 1; i < argc; ++i) {
string arg(argv[i]);
if (arg.find("--") == 0) {
if (arg.find("--mode=") == 0) {
if (arg == "--mode=cag") {
unique_ptr<BrainfuckRunner> compile_and_go(
new BrainfuckCompileAndGo());
bf = std::move(compile_and_go);
} else if (arg == "--mode=i") {
unique_ptr<BrainfuckRunner> interpreter(new BrainfuckInterpreter());
bf = std::move(interpreter);
} else if (arg == "--mode=jit") {
unique_ptr<BrainfuckRunner> jit(new BrainfuckJIT());
bf = std::move(jit);
} else {
fprintf(stderr, "Unexpected mode: %s\n", arg.c_str());
return 1;
}
} else {
fprintf(stderr, "Unexpected argument: %s\n", arg.c_str());
return 1;
}
} else {
files.push_back(arg);
}
}
if (files.size() != 1) {
fputs("You need to specify exactly one Brainfuck file\n", stderr);
printf(USAGE, argv[0], argv[0]);
return 1;
}
return run_brainfuck_program(bf.get(), files[0]);
}