-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.h
261 lines (231 loc) · 6.37 KB
/
token.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
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
/**
* Implementace překladače imperativního jazyka IFJ22
*
* @file token.h
* @author Josef Kuchař ([email protected])
* @author Matej Sirovatka ([email protected])
* @author Tomáš Běhal ([email protected])
* @author Šimon Benčík ([email protected])
* @brief Declaration of token functions
*/
#ifndef __TOKEN_H__
#define __TOKEN_H__
#include "str.h"
/**
* @brief All token types
*/
typedef enum {
TOK_MULTIPLY, // *
TOK_DIVIDE, // /
TOK_PLUS, // +
TOK_MINUS, // -
TOK_DOT, // .
TOK_LESS, // <
TOK_LESS_E, // <=
TOK_GREATER, // >
TOK_GREATER_E, // >=
TOK_EQUALS, // ===
TOK_NEQUALS, // !==
TOK_LPAREN, // (
TOK_RPAREN, // )
TOK_VAR, // Identifier (eg. $foo)
TOK_INT_LIT, // Integer literal (eg. 0)
TOK_FLOAT_LIT, // Float literal (eg. 0.0)
TOK_STR_LIT, // String literal (eg. "foo")
TOK_NULL, // Null
TOK_DOLLAR, // BOTTOM OF THE STACK (INTERNAL TO EXPR PARSING)
TOK_HANDLE_START, // HANDLE_START (INTERNAL TO EXPR PARSING)
TOK_EXP_END, // Expression end (Internal to expr parsing)
TOK_E, // expression (Internal to expr parsing)
TOK_EOF, // End of file
TOK_BOOL_LIT, // Bool literal (eg. true)
TOK_FUN_NAME, // Function name (eg. write)
TOK_COMMA, // ,
TOK_COLON, // :
TOK_SEMICOLON, // ;
TOK_ASSIGN, // =
TOK_LBRACE, // {
TOK_RBRACE, // }
/* KEYWORDS */
TOK_ELSE, // else
TOK_FLOAT, // float
TOK_FUNCTION, // function
TOK_IF, // if
TOK_INT, // int
TOK_RETURN, // return
TOK_STRING, // string
TOK_VOID, // void
TOK_WHILE, // while
TOK_FOR, // for
TOK_BREAK, // break
TOK_CONTINUE // continue
} token_type_t;
/**
* @brief Token attribute
*/
typedef union {
bool val_b; // Bool value (for TOK_BOOL_LIT)
int val_i; // Integer value (for TOK_INT_LIT)
double val_f; // Float value (for TOK_FLOAT_LIT)
str_t val_s; // String value (for TOK_STR_LIT, TOK_ID, TOK_FUN_NAME)
} token_attribute_t;
/**
* @brief Token struct
*/
typedef struct {
token_type_t type;
token_attribute_t attr;
size_t line_nr;
size_t col_nr;
} token_t;
/**
* @brief Print token in pretty format
*
* @param token Token to be printed
*/
void token_print(token_t* token);
/**
* @brief Create new token
*
* @param type Token type
* @return New token
*/
token_t token_new(token_type_t type, size_t line_nr, size_t col_nr);
/**
* @brief Create new token with supplied string AND CONSUME THE STRING
*
* @param type Token type
* @param str String
* @return New token with string
*/
token_t token_new_with_string(token_type_t type, str_t* str, size_t line_nr, size_t col_nr);
/**
* @brief Create new token with supplied string literal AND CONSUME THE STRING
*
* @param type Token type
* @param str String
* @return New token with string
*/
token_t token_new_with_string_literal(token_type_t type, str_t* str, size_t line_nr, size_t col_nr);
/**
* @brief Create new token with supplied string value of an int AND CONSUME THE STRING
*
* @param type Token type
* @param str String
* @return New token with int
*/
token_t token_new_with_int(token_type_t type, str_t* str, size_t line_nr, size_t col_nr);
/**
* @brief Create new token with supplied string value of a float AND CONSUME THE STRING
*
* @param type Token type
* @param str String
* @return New token with float
*/
token_t token_new_with_float(token_type_t type, str_t* str, size_t line_nr, size_t col_nr);
/**
* @brief Create new token with supplied bool
*
* @param type Token type
* @param val Bool value
* @return New token with bool
*/
token_t token_new_with_bool(token_type_t type, bool val, size_t line_nr, size_t col_nr);
/**
* @brief Check if token is a data type (eg. int, float, string)
*
* @param token Token
* @return true if token is a data type
*/
bool token_is_datatype(token_t* token);
/**
* @brief Check if token is a literal (eg. int literal etc.)
*
* @param token Token
* @return true if token is a literal
*/
bool token_is_literal(token_t* token);
/**
* @brief Check if token is a valid expression start
*
* @param token Token
* @return true if token can be an expression start
*/
bool token_is_expression(token_t* token);
/**
* @brief Free existing token
*
* @param token Token
*/
void token_free(token_t* token);
/**
* @brief Print enum value as a string
*
* @param token_type type
* @return String representation of the enum value
*/
char* token_to_string(token_type_t type);
/**
* @brief Checks if type is a number type (eg. float, int etc.)
*
* @param token_type Type
* @return true if token is a number type
*/
bool type_is_number(token_type_t type);
/**
* @brief Checks if type is an arithmetic operator (eg. plus, minus etc.)
*
* @param token Token
* @return true if token is a number type
*/
bool token_is_ar_operator(token_t* token);
/**
* @brief Checks if type is a logical operator (eg. less than, not equals etc.)
*
* @param token Token
* @return true if token is a number type
*/
bool token_is_comparator(token_t* token);
/**
* @brief Checks if type is data type (eg. int, float, string etc.)
*
* @param type Token type
* @return true if token type is a data type
*/
bool type_is_datatype(token_type_t type);
/**
* @brief Checks if type is literal (eg. "abc", 123)
*
* @param type Token type
* @return true if toke type n is a literal
*/
bool type_is_literal(token_type_t type);
/**
* @brief Checks if type is a valid expression start
*
* @param type Token type
* @return true if token can be an expression start
*/
bool type_is_expression(token_type_t type);
/**
* @brief Checks if type is operator (eg. +, -)
*
* @param type Token type
* @return true if token type is an operator
*/
bool type_is_ar_operator(token_type_t type);
/**
* @brief Checks if type is comparator (eg. ===, >=)
*
* @param type Token type
* @return true if token type is a comparator
*/
bool type_is_comparator(token_type_t type);
/**
* @brief Checks if type is return type (eg. int, float, string etc.)
*
* @param type Token type
* @return true if token type is a return type
*/
bool token_is_returntype(token_t* token);
#endif // __TOKEN_H__