-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler2.5-3.cpp
287 lines (258 loc) · 10.3 KB
/
compiler2.5-3.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <iostream>
#include <vector>
#include <memory>
#include <string>
#include "./glb/message.hpp"
#include "./compiler.hpp"
/*+++
============================
TODO: ERROR->
test("LOL", arg, arg2, arg3
)
causes:
%%%%%%%%%%%%%%%%%% ATL MESSAGE %%%%%%%%%%%%%%%%%%
MESSAGE CODE: <17-9>
MESSAGE: Unknown identifier.
GIVEN SP_MESSAGE: <Invalid argument in unknown identifier fixer. Expected a declaration or a function call, got: 'arg3' from line: 12 column: 29 of type: 4 and subtype: 6>
%%%%%%%%%%%%%%%%%% END MESSAGE %%%%%%%%%%%%%%%%%%
because of the whitespace after the last argument
Type: 2 Subtype: 3 Value: (
Type: 3 Subtype: 2 Value: "LOL"
Type: 2 Subtype: 7 Value: ,
Type: 4 Subtype: 6 Value: arg
Type: 2 Subtype: 7 Value: ,
Type: 4 Subtype: 6 Value: arg2
Type: 2 Subtype: 7 Value: ,
Type: 4 Subtype: 6 Value: arg3
Type: 7 Subtype: 3 Value:
Type: 2 Subtype: 4 Value: )
============================
does something for:
type 4: subtype: 6 || IDENTIFIER_UNKNOWN
it can be atleast:
- function declaration # // DONE
- variable declaration # // DONE
- variable assignment # // DONE
- function call # // DONE
- macro definition # // DONE
- import statement # // DONE
- compiler directive # // DONE
- struct declaration # // DONE
- enum declaration # // DONE
- function argument name # // DONE
syntax:
Function declaration:
<keyword> <identifier> (<identifier> <identifier> <identifier> ... <identifier>)
Function call:
<identifier>
<identifier>()
<identifier>(<expression>)
Function example:
void print() {
print("Hello, World!");
}
print <- This can be done if no args are needed
print() <- Also valid
print("Hello, World!") <- If the function takes an argument, then of course it/they must be passed
-> Hello, World!
-u-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Variable declaration:
<identifier> <identifier>
<identifier> <identifier> = <expression>
Variable assignment:
<identifier> = <expression>
Variable example:
int x
int y = 5
x <- These will be interpeted as function calls, which will cause errors
y <- These will be interpeted as function calls, which will cause errors
-> Error
-u-
---*/
void IDENTIFIER_UNKNOWN_fix(std::vector<Token> &tokens){
for (size_t i = 0; i < tokens.size(); i++) {
if(tokens[i].type != Tokens::IDENTIFIER ||
tokens[i].subtype != IDENTIFIER_UNKNOWN) continue;
if(i - 1 < 0 || i + 1 >= tokens.size()) continue; // TODO: What if the first token is an unknown identifier or parenthesisless function call?
#ifdef DEBUG
std::cout << "UNKNOWN IDENTIFIER: " << tokens[i].value << " INFO: " <<
tokens[i+1].type << " "
<< tokens[i+1].subtype << " " <<
tokens[i+1].value <<
" <=== next | previous ===>" <<
tokens[i-1].type << " "
<< tokens[i-1].subtype << " " <<
tokens[i-1].value << std::endl;
#endif
// For declarations such as
// function, variable, struct, enum, etc.
if(tokens[i-1].type == Tokens::KEYWORD){
if(
// Function declaration
// can be: INT, FLOAT, CHAR, BOOL, VOID
// TODO: CHECK -> // NOTE: TABS ARE NOT ALLOWED?
(
tokens[i-1].subtype == KEYWORD_INT
|| tokens[i-1].subtype == KEYWORD_FLOAT
|| tokens[i-1].subtype == KEYWORD_CHAR
|| tokens[i-1].subtype == KEYWORD_BOOL
|| tokens[i-1].subtype == KEYWORD_VOID
|| tokens[i-1].subtype == KEYWORD_STRING
)
&&
(
tokens[i+1].type == Tokens::SEPARATOR
&& tokens[i+1].subtype == Separator_Tokens::LPAREN
)
)
{
tokens[i].subtype = IDENTIFIER_FUNC_DECL;
}
else if(
// Variable declaration
(
tokens[i-1].subtype == KEYWORD_INT
|| tokens[i-1].subtype == KEYWORD_FLOAT
|| tokens[i-1].subtype == KEYWORD_CHAR
|| tokens[i-1].subtype == KEYWORD_BOOL
|| tokens[i-1].subtype == KEYWORD_STRING
)
&&
tokens[i+1].type == Tokens::OPERATOR
&& tokens[i+1].subtype == Operator_Tokens::ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::ADD_ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::SUB_ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::MUL_ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::DIV_ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::MOD_ASSIGN
)
{
tokens[i].subtype = IDENTIFIER_VAR_DECL;
}
else if(
// Macro definition
tokens[i-1].subtype == KEYWORD_MACRO
)
{
tokens[i].subtype = IDENTIFIER_MACRO;
}
else if(
// Import statement
tokens[i-1].subtype == KEYWORD_IMPORT
)
{
tokens[i].subtype = IDENTIFIER_IMPORT;
}
else if(
// Compiler directive
tokens[i-1].subtype == KEYWORD_COMPILER
)
{
tokens[i].subtype = IDENTIFIER_COMPILER;
}
else if(
// Struct declaration
tokens[i-1].subtype == KEYWORD_STRUCT
)
{
tokens[i].subtype = IDENTIFIER_STRUCT;
}
else if(
// Enum declaration
tokens[i-1].subtype == KEYWORD_ENUM
)
{
tokens[i].subtype = IDENTIFIER_ENUM;
}
else if(
// Argument definitions in function declarations
// TODO: Only support for basic types, add more?
tokens[i-2].type == Tokens::SEPARATOR
&& tokens[i-2].subtype == Separator_Tokens::COMMA
|| tokens[i-2].subtype == Separator_Tokens::LPAREN
&& tokens[i-1].type == Tokens::KEYWORD
&& tokens[i-1].subtype == KEYWORD_INT
|| tokens[i-1].subtype == KEYWORD_FLOAT
|| tokens[i-1].subtype == KEYWORD_CHAR
|| tokens[i-1].subtype == KEYWORD_BOOL
|| tokens[i-1].subtype == KEYWORD_STRING
// Check for (string test) or (int test, int test2, ...)
&& tokens[i+1].type == Tokens::SEPARATOR
&& tokens[i+1].subtype == Separator_Tokens::COMMA
|| tokens[i+1].subtype == Separator_Tokens::RPAREN
)
{
tokens[i].subtype = IDENTIFIER_ARGUMENT;
}
//
else {
message(
UNKNOW_IDENTIFIER,
FL_compiler253,
"Invalid argument in unknown identifier fixer. Expected a declaration, got: '" + tokens[i].value +
"' from line: " + std::to_string(tokens[i].line) + " column: " + std::to_string(tokens[i].column)
+ " of type: " + std::to_string(tokens[i].type) + " and subtype: " + std::to_string(tokens[i].subtype)
,
true
);
}
}
// For function calls and variable assignments
else {
if(
// Variable assignment
tokens[i+1].type == Tokens::OPERATOR
&& tokens[i+1].subtype == Operator_Tokens::ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::ADD_ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::SUB_ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::MUL_ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::DIV_ASSIGN
|| tokens[i+1].subtype == Operator_Tokens::MOD_ASSIGN
)
{
tokens[i].subtype = IDENTIFIER_VAR_ASSIGN;
}
else if(
// Function call
// Just in case previous and next tokens are whitespace
// if it is parenthless function call
// If not, then the next token must be a left parenthesis else: error
// NOTE: This is a very bad way to check for function calls
// Needs more testing?
tokens[i-1].type == Tokens::WHITESPACE
&& tokens[i+1].type == Tokens::WHITESPACE
|| tokens[i-1].type == Tokens::WHITESPACE
&& tokens[i+1].type == Tokens::SEPARATOR
|| tokens[i+1].type == Separator_Tokens::LPAREN
)
{
tokens[i].subtype = IDENTIFIER_FUNC_CALL;
}
else if(
// Argument names in function calls
tokens[i-1].type == Tokens::SEPARATOR
&& tokens[i-1].subtype == Separator_Tokens::COMMA
|| tokens[i-1].subtype == Separator_Tokens::LPAREN
// Check for (test) or (test, test2, ...)
&& tokens[i+1].type == Tokens::SEPARATOR
&& tokens[i+1].subtype == Separator_Tokens::COMMA
|| tokens[i+1].subtype == Separator_Tokens::RPAREN
)
{
tokens[i].subtype = IDENTIFIER_ARGUMENT;
}
else {
message(
UNKNOW_IDENTIFIER,
FL_compiler253,
"Invalid argument in unknown identifier fixer. Expected a declaration or a function call, got: '" + tokens[i].value +
"' from line: " + std::to_string(tokens[i].line) + " column: " + std::to_string(tokens[i].column)
+ " of type: " + std::to_string(tokens[i].type) + " and subtype: " + std::to_string(tokens[i].subtype)
,
true
);
}
}
}
}