-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler3.cpp
139 lines (128 loc) · 5.88 KB
/
compiler3.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
#include "compiler.hpp"
#include "./glb/message.hpp"
#include "./funcs/funcs.hpp"
#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <optional>
std::shared_ptr<Token> look_ahead(std::vector<Token> &tokens, size_t &amount, const size_t &forward = 1){
return std::make_shared<Token>(tokens[amount + forward]);
}
std::shared_ptr<Token> look_behind(std::vector<Token> &tokens, size_t &amount, const size_t &behind = 1){
return std::make_shared<Token>(tokens[amount - behind]);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void syntaxanalysis(std::vector<Token> &tokens) {
std::vector<std::shared_ptr<Parsed_Token>> parsetree;
size_t cur_inda = 0;
#ifdef DEBUG
for(size_t i = 0; i < tokens.size(); i++){
std::cout << "Type: '" << tokens[i].type << "' Subtype: '" << tokens[i].subtype << "' Value: '" << tokens[i].value << "'" << std::endl;
}
#endif
for(size_t cur_ind = 0; cur_ind < tokens.size(); cur_ind++){
switch (tokens[cur_ind].type){
case Tokens::KEYWORD:
switch (tokens[cur_ind].subtype){
case Keyword_Tokens::KEYWORD_EXIT:
{
const std::shared_ptr<Parsed_Token> _exit = parse_common_function(tokens, cur_ind);
parsetree.push_back(_exit);
}
break;
case Keyword_Tokens::KEYWORD_IF: break;
case Keyword_Tokens::KEYWORD_ELSE: break;
case Keyword_Tokens::KEYWORD_WHILE: break;
case Keyword_Tokens::KEYWORD_FOR: break;
case Keyword_Tokens::KEYWORD_RETURN: break;
case Keyword_Tokens::KEYWORD_INT: break;
case Keyword_Tokens::KEYWORD_STRING: break;
case Keyword_Tokens::KEYWORD_CHAR: break;
case Keyword_Tokens::KEYWORD_FLOAT: break;
case Keyword_Tokens::KEYWORD_DOUBLE: break;
case Keyword_Tokens::KEYWORD_BOOL: break;
case Keyword_Tokens::KEYWORD_TRUE: break;
case Keyword_Tokens::KEYWORD_FALSE: break;
case Keyword_Tokens::KEYWORD_FUNCTION: break;
case Keyword_Tokens::KEYWORD_STRUCT: break;
case Keyword_Tokens::KEYWORD_ENUM: break;
case Keyword_Tokens::KEYWORD_IMPORT: break;
case Keyword_Tokens::KEYWORD_MACRO: break;
case Keyword_Tokens::KEYWORD_COMPILER: break;
case Keyword_Tokens::KEYWORD_VOID: break;
default:
message(UNKNOWN_TOKEN, FL_compiler3,
"Unknown keyword subtype: " +
std::to_string(tokens[cur_ind].subtype) +
" value: " +
tokens[cur_ind].value +
" at line: " +
std::to_string(tokens[cur_ind].line) +
" column: " +
std::to_string(tokens[cur_ind].column),
true);
break;
}
break;
case Tokens::IDENTIFIER:
switch (tokens[cur_ind].subtype) {
case Identifier_Tokens::IDENTIFIER_VARIABLE: break;
case Identifier_Tokens::IDENTIFIER_FUNCTION: break;
case Identifier_Tokens::IDENTIFIER_STRUCT: break;
case Identifier_Tokens::IDENTIFIER_ENUM: break;
case Identifier_Tokens::IDENTIFIER_MACRO: break;
case Identifier_Tokens::IDENTIFIER_COMPILER: break;
case Identifier_Tokens::IDENTIFIER_FUNC_CALL: break;
case Identifier_Tokens::IDENTIFIER_VAR_DECL: break;
case Identifier_Tokens::IDENTIFIER_VAR_ASSIGN: break;
case Identifier_Tokens::IDENTIFIER_IMPORT: break;
case Identifier_Tokens::IDENTIFIER_ARGUMENT: break;
case Identifier_Tokens::IDENTIFIER_UNKNOWN:
message(UNKNOW_IDENTIFIER, FL_compiler3,
"Unknown identifier found '" +
tokens[cur_ind].value +
"' at line: " +
std::to_string(tokens[cur_ind].line) +
" column: " +
std::to_string(tokens[cur_ind].column),
true);
break;
default:
message(UNKNOWN_TOKEN, FL_compiler3,
"Unknown identifier subtype: " +
std::to_string(tokens[cur_ind].subtype) +
" value: " +
tokens[cur_ind].value +
" at line: " +
std::to_string(tokens[cur_ind].line) +
" column: " +
std::to_string(tokens[cur_ind].column),
true);
break;
}
break;
default:
message(UNKNOWN_TOKEN, FL_compiler3,
"Unknown token type: " +
std::to_string(tokens[cur_ind].type) +
+ " value: " +
tokens[cur_ind].value +
" at line: " +
std::to_string(tokens[cur_ind].line) +
" column: " +
std::to_string(tokens[cur_ind].column),
true);
break;
}
}
}