-
Notifications
You must be signed in to change notification settings - Fork 3
/
tab.cc
254 lines (184 loc) · 7.37 KB
/
tab.cc
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <time.h>
#include "tab.h"
std::istream& file_or_stdin(const std::string& file) {
if (file.empty())
return std::cin;
static std::ifstream ret;
ret.open(file);
if (!ret)
throw std::runtime_error("Could not open input file: " + file);
return ret;
}
#ifdef _REENTRANT
#include "threaded.h"
#endif
template <bool SORTED>
void run(size_t seed, const std::string& program, const std::string& infile, unsigned int debuglevel) {
tab::API<SORTED> api;
api.init(seed);
static const tab::Type intype(tab::Type::SEQ, { tab::Type(tab::Type::STRING) });
typename tab::API<SORTED>::compiled_t code;
api.compile(program.begin(), program.end(), intype, code, debuglevel);
tab::obj::Object* input = new tab::funcs::SeqFile(file_or_stdin(infile));
tab::obj::Object* output = api.run(code, input);
tab::obj::Printer p;
output->print(p);
// https://github.com/ivan-tkatchev/tab/issues/4
if (!p.null) {
p.nl();
}
}
void show_help(const std::string& help_section) {
const char* help = get_help(help_section);
if (help) {
std::cout << help << std::endl;
return;
}
std::cout <<
"Usage: tab [-i inputdata_file] [-f expression_file] [-t N] [-r random seed] [-s] [-v|-vv|-vvv] [-h section] "
<< "<expressions...>"
<< std::endl
<< " -V, --version: show version." << std::endl
<< " -i: read data from this file instead of stdin." << std::endl
<< " -f: prepend code from this file to <expressions...>" << std::endl
<< " -p: use this code as the prelude; this code will be prepended to code from file and command line args." << std::endl
<< " -r: use a specific random seed." << std::endl
<< " -s: use maps with keys in sorted order instead of the unsorted default." << std::endl
#ifdef _REENTRANT
<< " -t: use N parallel threads for evaluating the expression." << std::endl
#endif
<< " (use '-->' to separate scatter and gather subexpressions; see tab -h 'threads')" << std::endl
<< " -v: verbosity flag -- print type of the result." << std::endl
<< " -vv: verbosity flag -- print type of the result and VM instructions." << std::endl
<< " -vvv: verbosity flag -- print type of the result, VM instructions and parse tree." << std::endl
<< " -h: show help from given section." << std::endl
<< std::endl
<< "Note:" << std::endl
<< " Expressions from '-p' come first, then expressions from '-f', and finally those from the command line." << std::endl
<< " If '-p' is set, the default prelude will be overwritten."
<< " (The default prelude is 'def $ index.@')" << std::endl
<< std::endl
<< "Help sections:" << std::endl
<< " 'overview' -- show an overview of types and concepts." << std::endl
<< " 'syntax' -- show a syntax reference." << std::endl
<< " 'examples' -- show some example tab programs." << std::endl
<< " 'threads' -- show examples of multithreaded programs." << std::endl
<< " 'functions' -- show a complete list of built-in functions." << std::endl
<< " <function name> -- explain the given built-in function." << std::endl;
}
bool getopt(unsigned char opt, int argc, char** argv, int& i, std::string& out, bool required = true) {
if (argv[i][0] == '-' && argv[i][1] == opt) {
if (argv[i][2] == '\0') {
if (i == argc - 1 || argv[i + 1][0] == '-') {
if (!required) return true;
std::runtime_error("The '-" + std::string(opt, 1) +"' command line argument expects an argument.");
}
++i;
out = argv[i];
} else {
out = argv[i] + 2;
}
return true;
}
return false;
}
int main(int argc, char** argv) {
try {
if (argc < 2) {
show_help("");
return 1;
}
std::string prelude = "def $ index.@";
unsigned int debuglevel = 0;
bool sorted = false;
std::string program;
std::string infile;
std::string programfile;
size_t seed = ::time(NULL);
bool help = false;
bool has_program = false;
std::string help_section;
size_t nthreads = 0;
for (int i = 1; i < argc; ++i) {
std::string arg(argv[i]);
std::string out;
if (arg == "-v" || getopt('v', argc, argv, i, out, false)) {
if (out == "vv") {
debuglevel = 3;
} else if (out == "v") {
debuglevel = 2;
} else {
debuglevel = 1;
}
} else if (getopt('r', argc, argv, i, out)) {
seed = std::stoul(out);
} else if (arg == "-s") {
sorted = true;
} else if (getopt('p', argc, argv, i, prelude)) {
} else if (getopt('f', argc, argv, i, programfile)) {
} else if (getopt('i', argc, argv, i, infile)) {
//
} else if (getopt('h', argc, argv, i, help_section, false)) {
help = true;
#ifdef _REENTRANT
} else if (getopt('t', argc, argv, i, out)) {
nthreads = std::stoul(out);
#endif
} else if (arg == "-V" || arg == "--version") {
std::cout << "tab 9.3" << std::endl << " →→→ https://github.com/ivan-tkatchev/tab/" << std::endl;
return 0;
} else {
has_program = true;
if (program.size() > 0) {
program += ' ';
}
program += arg;
}
}
if (programfile.size() > 0) {
has_program = true;
std::ifstream f;
f.open(programfile);
if (!f)
throw std::runtime_error("Could not open input program file: " + programfile);
programfile.assign(std::istreambuf_iterator<char>(f),
std::istreambuf_iterator<char>());
if (program.empty()) {
program = programfile;
} else {
program = programfile + "," + program;
}
}
// //
if (help || !has_program) {
show_help(help_section);
return 1;
}
if (!prelude.empty()) {
program = prelude + "," + program;
}
// //
#ifdef _REENTRANT
if (nthreads > 0) {
if (sorted) {
run_threaded<true>(seed, program, nthreads, infile, debuglevel);
} else {
run_threaded<false>(seed, program, nthreads, infile, debuglevel);
}
return 0;
}
#endif
if (sorted) {
run<true>(seed, program, infile, debuglevel);
} else {
run<false>(seed, program, infile, debuglevel);
}
} catch (std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "UNKNOWN ERROR." << std::endl;
return 1;
}
return 0;
}