-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
142 lines (130 loc) · 3.87 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
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
#include <iostream>
#include <string>
#include <cstring>
#include "inc/Scanner.hpp"
#include "inc/Parser.hpp"
#include "CompilerConfig.hpp"
const std::string help =
"Usage\n\n"
" <exec_file> [option] <file>\n\n"
"Options\n\n"
" -v, --version - Print current version\n"
" -s, --scanner - Lexer -> print all lexems from <file> to &1\n"
" -p, --parser - Parser -> print syntax trees of statements from <file> to &1\n"
" -e, --parse-expression - Parser -> print syntax tree of expressions from <file> to &1\n"
" -v, --parse-variables - Parser -> print table of variables from <file> to &1\n"
" -f, --parse-functions - Parser -> print table of functions from <file> to &1\n"
" -h, --help - Print 'help'\n\n"
"<file> <- 'test.pas', if not setted.\n";
int main(int argc, char const *argv[]) {
if (argc < 2 || !std::strcmp(argv[1], "--help") || !std::strcmp(argv[1], "-h")) {
std::cout << help << std::endl;
return 0;
}
//PRINT CURRENT VERSION
if (!std::strcmp(argv[1], "--version") || !std::strcmp(argv[1], "-v")) {
std::cout << "Current version "
<< COMPILER_VERSION_MAJOR << '.' << COMPILER_VERSION_MINOR
<< std::endl;
return 0;
}
// SCANNER
if (!std::strcmp(argv[1], "--scanner") || !std::strcmp(argv[1], "-s")) {
compiler::Scanner scanner;
compiler::Lexeme lex;
if (argc > 2)
scanner.open(argv[2]);
else
scanner.open("test.pas");
do {
scanner.next();
lex = scanner.lex();
std::cout << lex.row << ':'
<< lex.column << '\t'
<< lex.tokenName() << '\t'
<< (unsigned long int)lex.tag << '\t'
<< lex.name
<< std::endl;
} while (lex.token != compiler::Token::END_OF_FILE && lex.token != compiler::Token::UNDEFINED);
return 0;
}
//PARSER
if (!std::strcmp(argv[1], "--parser") || !std::strcmp(argv[1], "-p")) {
compiler::Parser parser;
if (argc > 2)
parser.set(argv[2]);
else
parser.set("test.pas");
try {
parser.parse();
std::cout << parser.print();
} catch (std::exception& ex) {
std::cout << ex.what() << std::endl;
}
return 0;
}
//PARSER OF EXPRESSIONS
if (!std::strcmp(argv[1], "--parse-expression") || !std::strcmp(argv[1], "-e")) {
compiler::Parser parser;
if (argc > 2)
parser.set(argv[2]);
else
parser.set("test.pas");
try {
parser.parseExpr();
std::cout << parser.printExprs();
} catch (std::exception& ex) {
std::cout << ex.what() << std::endl;
}
return 0;
}
//PARSER OF VARIABLES
if (!std::strcmp(argv[1], "--parse-variables") || !std::strcmp(argv[1], "-v")) {
compiler::Parser parser;
if (argc > 2)
parser.set(argv[2]);
else
parser.set("test.pas");
try {
parser.parseTable();
std::cout << parser.printVarTable();
} catch (std::exception& ex) {
std::cout << ex.what() << std::endl;
}
return 0;
}
//PARSER OF FUNCTIONS
if (!std::strcmp(argv[1], "--parse-functions") || !std::strcmp(argv[1], "-f")) {
compiler::Parser parser;
if (argc > 2)
parser.set(argv[2]);
else
parser.set("test.pas");
try {
parser.parseTable();
std::cout << parser.printFuncTable();
} catch (std::exception& ex) {
std::cout << ex.what() << std::endl;
}
return 0;
}
//GENERATE ASM
if (!std::strcmp(argv[1], "--generate-asm") || !std::strcmp(argv[1], "-s")) {
compiler::Parser parser;
if (argc > 2)
parser.set(argv[2]);
else
parser.set("test.pas");
try {
parser.parse();
parser.generate();
std::cout << parser.printAsm();
} catch (std::exception& ex) {
std::cout << ex.what() << std::endl;
}
return 0;
}
std::cout << "unknown token: " << argv[1] << std::endl;
std::cout << help << std::endl;
return 0;
}