-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
85 lines (72 loc) · 1.95 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
#include <iostream>
#include <fstream>
#include <vector>
#include "parser.h"
#include "interpreter.h"
bool check_file (std::string name);
void options_parse (std::string, struct options_struct&);
struct options_struct
{
options_struct() : out(true), plain(false) {}
bool out;
bool plain;
};
int main(int argc, char* argv[])
{
std::vector<char> inst;
struct options_struct options;
if (argc<2)
{
printf ("Type Brainfuck code: ");
std::string code; getline(std::cin, code);
if (!parser_string(code, inst)) { printf ("Loop brackets don't match\n"); exit(3); }
}
else if (argc==2)
{
if (check_file(argv[1]))
{
std::ifstream ifile(argv[1]);
if (!parser_file(ifile, inst)) { printf ("Loop brackets don't match\n"); exit(3); }
}
else if (argv[1][0]=='-')
{
options_parse(argv[1], options);
printf ("Type Brainfuck code: ");
std::string code; getline(std::cin, code);
if (!parser_string(code, inst)) { printf ("Loop brackets don't match\n"); exit(3); }
}
else { printf("File does not exist\n"); exit(1); }
}
else if (argc>2)
{
options_parse(argv[1], options);
if (check_file(argv[2]))
{
std::ifstream ifile(argv[2]);
if (!parser_file(ifile, inst)) { printf ("Loop brackets don't match\n"); exit(3); }
}
else { printf("File does not exist\n"); exit(1); }
}
interpreter(inst, options.out, options.plain);
}
bool check_file (std::string name) {
std::fstream file(name.c_str());
if (file.fail()) { return false; }
else { file.close(); return true; }
}
void options_parse (std::string opt, struct options_struct& opt_s)
{
if (opt[0]=='-' && opt.length()>1)
{
for (unsigned i=1; i<opt.length(); i++)
{
switch (opt[i])
{
case 'n' : opt_s.out=false; break;
case 'p' : opt_s.plain=true; break;
default : printf("Uknown option\n"); exit(2);
}
}
}
else { printf("Uknown option\n"); exit(2); }
}