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_STRINGCONST, /* not the typename but an actual "string" */
56 /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
57 * other error related tokens as well
60 TOKEN_FATAL /* internal error, eg out of memory */
63 static const char *_tokennames[] = {
79 _all_tokennames_added_[
80 ((TOKEN_FATAL - TOKEN_START + 1) ==
81 (sizeof(_tokennames)/sizeof(_tokennames[0])))
93 size_t sline; /* line at the start of a token */
107 MEM_VECTOR_MAKE(frame_macro, frames);
111 MEM_VECTOR_PROTO(lex_file, char, token);
113 lex_file* lex_open (const char *file);
114 void lex_close(lex_file *lex);
115 int lex_do (lex_file *lex);
116 void lex_cleanup(void);
132 unsigned int operands;
140 #define opid2(a,b) ((a<<8)|b)
141 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
143 static const oper_info operators[] = {
144 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
146 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 16, OP_SUFFIX},
147 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 16, OP_SUFFIX},
149 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
150 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
152 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
153 { "~", 1, opid2('~', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
154 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
155 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
156 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
157 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
158 /* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX }, */
160 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
161 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
162 { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0 },
164 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
165 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
167 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0 },
168 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0 },
170 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
171 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
172 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
173 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
175 { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0 },
176 { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0 },
178 { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0 },
180 { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0 },
182 { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0 },
184 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
186 { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0 },
188 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0 },
190 { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0 },
191 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0 },
192 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 2, 0 },
193 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 2, 0 },
194 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 2, 0 },
195 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 2, 0 },
196 { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2, 0 },
197 { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0 },
198 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0 },
199 { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0 },
200 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0 },
202 { ",", 2, opid1(','), ASSOC_LEFT, 1, 0 }
204 static const size_t operator_count = (sizeof(operators) / sizeof(operators[0]));
215 token *tok; /* current token */
217 MEM_VECTOR_MAKE(ast_value*, globals);
220 MEM_VECTOR_PROTO(parse_file, ast_value*, globals);
222 parse_file* parse_open(const char *file);
223 void parse_file_close(parse_file*);
225 bool parse(parse_file*);
227 bool parse_iskey(parse_file *self, const char *ident);
229 void lexerror(lex_file*, const char *fmt, ...);