1 #ifndef GMQCC_LEXER_HDR_
2 #define GMQCC_LEXER_HDR_
4 typedef struct token_s token;
11 MEM_VECTOR_MAKE(char, value);
27 void token_delete(token*);
28 token* token_copy(const token *cp);
29 void token_delete_all(token *t);
30 token* token_copy_all(const token *cp);
36 /* Other tokens which we can return: */
46 TOKEN_KEYWORD, /* loop */
48 TOKEN_DOTS, /* 3 dots, ... */
50 TOKEN_STRINGCONST, /* not the typename but an actual "string" */
58 /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
59 * other error related tokens as well
62 TOKEN_FATAL /* internal error, eg out of memory */
65 static const char *_tokennames[] = {
82 _all_tokennames_added_[
83 ((TOKEN_FATAL - TOKEN_START + 1) ==
84 (sizeof(_tokennames)/sizeof(_tokennames[0])))
96 size_t sline; /* line at the start of a token */
110 MEM_VECTOR_MAKE(frame_macro, frames);
114 MEM_VECTOR_PROTO(lex_file, char, token);
116 lex_file* lex_open (const char *file);
117 void lex_close(lex_file *lex);
118 int lex_do (lex_file *lex);
119 void lex_cleanup(void);
135 unsigned int operands;
143 #define opid2(a,b) ((a<<8)|b)
144 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
146 static const oper_info c_operators[] = {
147 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
149 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 16, OP_SUFFIX},
150 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 16, OP_SUFFIX},
152 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
153 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
155 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
156 { "~", 1, opid2('~', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
157 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
158 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
159 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
160 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
161 /* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX }, */
163 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
164 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
165 { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0 },
167 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
168 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
170 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0 },
171 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0 },
173 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
174 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
175 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
176 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
178 { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0 },
179 { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0 },
181 { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0 },
183 { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0 },
185 { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0 },
187 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
189 { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0 },
191 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0 },
193 { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0 },
194 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0 },
195 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 2, 0 },
196 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 2, 0 },
197 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 2, 0 },
198 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 2, 0 },
199 { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2, 0 },
200 { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0 },
201 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0 },
202 { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0 },
203 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0 },
205 { ",", 2, opid1(','), ASSOC_LEFT, 1, 0 }
207 static const size_t c_operator_count = (sizeof(c_operators) / sizeof(c_operators[0]));
209 static const oper_info qcc_operators[] = {
210 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
212 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
213 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
215 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
216 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
217 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
219 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
220 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
221 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0 },
222 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0 },
224 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
225 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
227 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
228 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
229 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
230 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
231 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0 },
232 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0 },
234 { "=", 2, opid1('='), ASSOC_RIGHT, 8, 0 },
235 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 8, 0 },
236 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 8, 0 },
237 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 8, 0 },
238 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 8, 0 },
239 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0 },
240 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0 },
241 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0 },
243 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
244 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0 },
246 { ",", 2, opid1(','), ASSOC_LEFT, 1, 0 }
248 static const size_t qcc_operator_count = (sizeof(qcc_operators) / sizeof(qcc_operators[0]));
250 extern const oper_info *operators;
251 extern size_t operator_count;
262 token *tok; /* current token */
264 MEM_VECTOR_MAKE(ast_value*, globals);
267 MEM_VECTOR_PROTO(parse_file, ast_value*, globals);
269 parse_file* parse_open(const char *file);
270 void parse_file_close(parse_file*);
272 bool parse(parse_file*);
274 bool parse_iskey(parse_file *self, const char *ident);
276 void lexerror(lex_file*, const char *fmt, ...);