-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
195 lines (159 loc) · 3.22 KB
/
parser.h
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
#pragma once
#include "main.h"
#include "files.h"
extern const char* asttext[];
extern const char* tokentext[];
enum TOKENTYPE {
T_NL = '\n',
T_RETURN = 'r',
T_CONTINUE = 'c',
T_BREAK = 'b',
T_IF = 'i',
T_ELIF = 'l',
T_ELSE = 'e',
T_FOR = 'f',
T_WHILE = 'w',
T_IDENTIFIER = '0',
T_NUMBER = '1',
T_FLOAT = '2',
T_STRING = '3',
T_MSTRING = '4',
T_ADD = '+',
T_SUB = '-',
T_MUL = '*',
T_DIV = '/',
T_MOD = '%',
T_AND = '&',
T_OR = '|',
T_XOR = '^',
T_LESS = '<',
T_GREAT = '>',
T_NOT = '!',
T_EQ = '=',
T_ORB = '(',
T_CRB = ')',
T_OCB = '{',
T_CCB = '}',
T_OSB = '[',
T_CSB = ']',
T_DOT = '.',
T_COMMA = ',',
T_COLON = ':',
T_SCOLON = ';',
T_IMPORT = '#',
T_EXTERNAL = '~',
T_TERNARY = '?',
T_AT = '@',
T_THIS = '$',
T_CATNL = '\\',
T_ADDEQ = 'A',
T_SUBEQ = 'S',
T_MULEQ = 'M',
T_DIVEQ = 'D',
T_MODEQ = 'O',
T_ANDEQ = 'N',
T_OREQ = 'R',
T_XOREQ = 'X',
T_LESSEQ = 'L',
T_GREATEQ = 'G',
T_NOTEQ = 'T',
T_EQEQ = 'E',
};
// LEXER STRUCTS
struct token {
TOKENTYPE type;
const char* begin;
const char* end;
const char* line;
};
struct token_list {
token t;
token_list* next;
};
// PARSER STRUCTS
enum VARIABLETYPE {
V_UNKNOWN,
V_OBJECT,
V_FUNCTION,
V_NUMBER,
V_FLOAT,
V_STRING,
V_ARRAY
};
struct variable {
VARTYPE type;
char* name;
bool constant;
void* value;
};
struct variable_list {
variable v;
variable_list* next;
};
struct Scope {
union {
Scope* parent;
//node* parentnode;
};
//std::vector<variable> variables;
variable_list* variables;
variable_list* returns;
};
enum NODETYPE {
AST_UNKNOWN,
AST_STATEMENT,
AST_BSTATEMENT, // b for block
AST_JSTATEMENT, // j for jump
AST_CSTATEMENT, // c for conditional
AST_ISTATEMENT, // i for iteration
AST_EXPRESSION,
AST_BEXPRESSION, // b for block
AST_AEXPRESSION, // a for assignment
AST_FUNCTION,
AST_IDENTIFIER,
AST_ARRAY,
AST_OBJECT
// Add more node types as needed
};
struct node_list;
#define EXPRTYPE TOKENTYPE
struct node {
NODETYPE type;
Scope* scope;
linked_list<node_list> nodes;
EXPRTYPE exprtype;
union {
void* vptr;
//const char* strptr;
node_list* nlptr;
token_list* tptr;
};
};
struct node_list {
node n;
node_list* next;
};
struct Parser {
node* root;
token_list* tokens;
union {
//node* node;
Scope* scope;
};
//std::vector<std::pair<char*, node>> ids;
//std::vector<char*> imports;
//std::vector<char*> exports;
};
#define RTYPEL node_list*
token_list* tokenize(SFile file);
void parse(Parser& parser, token_list* tokens);
RTYPEL lparse(Parser& parser);
RTYPEL rparse(Parser& parser);
RTYPEL assignmentparse(Parser& parser);
RTYPEL comparisonparse(Parser& parser);
RTYPEL notparse(Parser& parser);
RTYPEL addparse(Parser& parser);
RTYPEL mulparse(Parser& parser);
RTYPEL bracketsparse(Parser& parser);
RTYPEL dotparse(Parser& parser);
RTYPEL idparse(Parser& parser);