-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.hpp
288 lines (249 loc) · 8.08 KB
/
compiler.hpp
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
288
#pragma once
#include <string>
#include <vector>
#include <memory> // shared_ptr
#if defined(LINUX) || defined(__linux__)
#include <cstring> // strcmp
#include <algorithm> // std::find_if
#endif
#define VERSION_MAJOR 0
#define VERSION_MINOR 1
#define VERSION_PATCH 0
#define VERSION_BUILD 1
#define OUTPUT 0 // POSITION OF OUTPUT IN S_ARGUMENTS
#define INPUT 1 // POSITION OF INPUT IN S_ARGUMENTS
#define S_ARG_SIZE 2 // SIZE OF S_ARGUMENTS
extern std::vector<std::string> s_arguments;
/*+++
Tokenizer enums and structs
---*/
enum Tokens {
KEYWORD,
OPERATOR,
SEPARATOR,
LITERAL,
IDENTIFIER,
COMMENT,
PREPROCESSOR,
WHITESPACE,
};
enum Keyword_Tokens {
KEYWORD_EXIT, // exit
KEYWORD_IF, // if
KEYWORD_ELSE, // else
KEYWORD_WHILE, // while
KEYWORD_FOR, // for
KEYWORD_RETURN, // return
KEYWORD_INT, // int
KEYWORD_STRING, // string
KEYWORD_CHAR, // char
KEYWORD_FLOAT, // float
KEYWORD_DOUBLE, // double
KEYWORD_BOOL, // bool
KEYWORD_TRUE, // true
KEYWORD_FALSE, // false
KEYWORD_FUNCTION, // function
KEYWORD_STRUCT, // struct
KEYWORD_ENUM, // enum
KEYWORD_IMPORT, // include | import
KEYWORD_MACRO, // define | macro
KEYWORD_COMPILER, // compiler |pragma
KEYWORD_VOID // void
/*
TODO ADD:
KEYWORD_BREAK
KEYWORD_CONTINUE
KEYWORD_SWITCH
KEYWORD_CASE
KEYWORD_DEFAULT
KEYWORD_DO
KEYWORD_GOTO
KEYWORD_SIZEOF
KEYWORD_TYPEOF
KEYWORD_EXPORT
*/
};
enum Operator_Tokens {
ADD, // +
SUB, // -
MUL, // *
DIV, // /
ASSIGN, // =
ADD_ASSIGN, // +=
SUB_ASSIGN, // -=
EQ, // ==
NEQ, // !=
GT, // >
LT, // <
GTE, // >=
LTE, // <=
AND, // &&
OR, // ||
NOT, // !
BIT_AND, // &
BIT_OR, // |
BIT_XOR, // ^
BIT_NOT, // ~
LSHIFT, // <<
RSHIFT, // >>
MOD, // %
INC, // ++
DEC, // --
MUL_ASSIGN, // *=
DIV_ASSIGN, // /=
MOD_ASSIGN, // %=
};
enum Separator_Tokens {
SEMICOLON, // ;
LBRACE, // {
RBRACE, // }
LPAREN, // (
RPAREN, // )
LBRACKET, // [
RBRACKET, // ]
COMMA, // ,
DOT, // .
COLON, // :
};
enum Literal_Tokens {
INT_LITERAL, // 1
FLOAT_LITERAL, // 1.0
STRING_LITERAL, // "string"
CHAR_LITERAL, // 'c'
BOOL_LITERAL, // true, false
VOID_LITERAL, // void
HEX_LITERAL, // 0x1
};
// Identifier names are stored in the value field
// of the token struct.
// more accurate information will be gathered in syntaxanalysis
enum Identifier_Tokens {
IDENTIFIER_VARIABLE, // variable
IDENTIFIER_FUNCTION, // function
IDENTIFIER_STRUCT, // struct
IDENTIFIER_ENUM, // enum
IDENTIFIER_MACRO, // macro
IDENTIFIER_COMPILER, // compiler
IDENTIFIER_UNKNOWN, // unknown, i'll use this in lexanalysis.
//i can't know if it is a variable or a function before syntaxanalysis
IDENTIFIER_FUNC_CALL, // function call
IDENTIFIER_FUNC_DECL, // function declaration
IDENTIFIER_VAR_DECL, // variable declaration
IDENTIFIER_VAR_ASSIGN, // variable assignment
IDENTIFIER_IMPORT, // include | import
IDENTIFIER_ARGUMENT, // argument
};
enum Comment_Tokens {
LINE_COMMENT, // //
BLOCK_COMMENT // /* */
};
/*+++
Preprocessor_Tokens is not used in the lexer -
since they are all keywords too and will be -
treated as such
enum Preprocessor_Tokens {
PREPROCESSOR_IMPORT, // #include
PREPROCESSOR_MACRO, // #define
PREPROCESSOR_COMPILER // #pragma
};
---*/
#define mcr_WHITESPACE_SPACE 0x20
#define mcr_WHITESPACE_TAB 0x09
#define mcr_WHITESPACE_NEWLINE 0x0A
#define mcr_WHITESPACE_RETURN 0x0D
enum Whitespace_Tokens {
WHITESPACE_SPACE, // ' ' 0x20
WHITESPACE_TAB, // '\t' 0x09
WHITESPACE_NEWLINE, // '\n' 0x0A
WHITESPACE_RETURN // '\r' 0x0D. Should be ['0x0D', '0x0A']. But I'll treat it as 0x0D since switch case is easier
};
struct Token {
Tokens type;
int subtype;
std::string value;
int line;
int column;
Token(Tokens t, int st, std::string val, int l, int c) : type(t), subtype(st), value(val), line(l), column(c)
{}
};
std::vector<Token>* lexanalysis(std::vector<std::string> &s_arguments);
void IDENTIFIER_UNKNOWN_fix(std::vector<Token> &tokens);
/*+++
Syntax analysis and parser struct and enums
---*/
void syntaxanalysis(std::vector<Token> &tokens);
enum class Expression_Types {
Literal,
Identifier, // Variable
Operator, // Operator
Expression, // Subexpression
Term, // Term
Factor // Factor
};
/*+++
Parser structs in order of appearance -
lowest to highest
Parse_Factor -> Parse_Term -> Parse_Expression
---*/
// Factor is the lowest level of the parser
// it is a number or a subexpression
struct Parse_Factor {
Expression_Types type;
std::string value;
std::shared_ptr<struct Parse_Expression> subexpression; // Optional subexpression
// Constructor for literals (numbers and strings)
Parse_Factor(Expression_Types t, std::string val, std::shared_ptr<Parse_Expression> subexpr = nullptr)
: type(t), value(val), subexpression(subexpr) {}
// Constructor for expressions
Parse_Factor(Expression_Types t, std::shared_ptr<Parse_Expression> subexpr)
: type(t), value(""), subexpression(subexpr) {}
};
// Term is a collection of factors and operators
// it is a multiplication or division of factors
struct Parse_Term {
std::vector<Parse_Factor> factors;
std::vector<char> operators; // Operators between factors
Parse_Term(const std::vector<Parse_Factor>& f, const std::vector<char>& ops)
: factors(f), operators(ops) {}
};
// Expression is a collection of terms and operators
// it is an addition or subtraction of terms
struct Parse_Expression {
std::vector<Parse_Term> terms;
std::vector<char> operators; // Operators between terms
Parse_Expression(const std::vector<Parse_Term>& t, const std::vector<char>& ops)
: terms(t), operators(ops) {}
};
// Statement is a value and an expression
// it is a variable assignment or a function call
struct Parse_Statement {
std::string value;
std::shared_ptr<Parse_Expression> expression;
Parse_Statement(std::string val, std::shared_ptr<Parse_Expression> exp)
: value(val), expression(exp) {}
};
// Function is a collection of statements
// it is a function definition
struct Parse_Function {
std::string name;
std::vector<Parse_Statement> statements;
Parse_Function(std::string n, const std::vector<Parse_Statement>& s)
: name(n), statements(s) {}
};
// thanks to https://stackoverflow.com/a/217605
// trim from start (in place)
inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}
inline void trim(std::string &s) {
rtrim(s);
ltrim(s);
}