]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
ir_values which are members of a vector should know that, so that liferange calc...
[xonotic/gmqcc.git] / lexer.h
1 #ifndef GMQCC_LEXER_HDR_
2 #define GMQCC_LEXER_HDR_
3
4 typedef struct token_s token;
5
6 #include "ast.h"
7
8 struct token_s {
9         int ttype;
10
11         MEM_VECTOR_MAKE(char, value);
12
13         union {
14                 vector v;
15                 int    i;
16                 double f;
17                 int    t; /* type */
18         } constval;
19
20         struct token_s *next;
21         struct token_s *prev;
22
23         lex_ctx ctx;
24 };
25
26 token* token_new();
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);
31
32 /* Lexer
33  *
34  */
35 enum {
36     /* Other tokens which we can return: */
37     TOKEN_NONE = 0,
38     TOKEN_START = 128,
39
40     TOKEN_IDENT,
41
42     TOKEN_TYPENAME,
43
44     TOKEN_OPERATOR,
45
46     TOKEN_KEYWORD, /* loop */
47
48     TOKEN_STRINGCONST, /* not the typename but an actual "string" */
49     TOKEN_CHARCONST,
50     TOKEN_VECTORCONST,
51     TOKEN_INTCONST,
52     TOKEN_FLOATCONST,
53
54     TOKEN_EOF,
55
56     /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
57      * other error related tokens as well
58      */
59     TOKEN_ERROR,
60     TOKEN_FATAL /* internal error, eg out of memory */
61 };
62
63 static const char *_tokennames[] = {
64     "TOKEN_START",
65     "TOKEN_IDENT",
66     "TOKEN_TYPENAME",
67     "TOKEN_OPERATOR",
68     "TOKEN_KEYWORD",
69     "TOKEN_STRINGCONST",
70     "TOKEN_CHARCONST",
71     "TOKEN_VECTORCONST",
72     "TOKEN_INTCONST",
73     "TOKEN_FLOATCONST",
74     "TOKEN_EOF",
75     "TOKEN_ERROR",
76     "TOKEN_FATAL",
77 };
78 typedef int
79 _all_tokennames_added_[
80         ((TOKEN_FATAL - TOKEN_START + 1) ==
81          (sizeof(_tokennames)/sizeof(_tokennames[0])))
82         ? 1 : -1];
83
84 typedef struct {
85     char *name;
86     int   value;
87 } frame_macro;
88
89 typedef struct {
90         FILE   *file;
91         char   *name;
92         size_t  line;
93         size_t  sline; /* line at the start of a token */
94
95         char    peek[256];
96         size_t  peekpos;
97
98         token  *tok;
99
100         struct {
101             bool noops;
102         } flags;
103
104     int framevalue;
105         MEM_VECTOR_MAKE(frame_macro, frames);
106         char *modelname;
107 } lex_file;
108
109 MEM_VECTOR_PROTO(lex_file, char, token);
110
111 lex_file* lex_open (const char *file);
112 void      lex_close(lex_file   *lex);
113 int       lex_do   (lex_file   *lex);
114 void      lex_cleanup(void);
115
116 /* Parser
117  *
118  */
119
120 enum {
121     ASSOC_LEFT,
122     ASSOC_RIGHT
123 };
124
125 #define OP_SUFFIX 1
126 #define OP_PREFIX 2
127
128 typedef struct {
129     const char   *op;
130     unsigned int operands;
131     unsigned int id;
132     unsigned int assoc;
133     unsigned int prec;
134     unsigned int flags;
135 } oper_info;
136
137 #define opid1(a) (a)
138 #define opid2(a,b) ((a<<8)|b)
139 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
140
141 static const oper_info operators[] = {
142     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
143
144     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  16, OP_SUFFIX},
145     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  16, OP_SUFFIX},
146
147     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
148     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
149
150     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
151     { "~",   1, opid2('~', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
152     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
153     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
154     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
155     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
156 /*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX }, */
157
158     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
159     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
160     { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0 },
161
162     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
163     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
164
165     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
166     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
167
168     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
169     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
170     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
171     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
172
173     { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0 },
174     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0 },
175
176     { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0 },
177
178     { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0 },
179
180     { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0 },
181
182     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
183
184     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0 },
185
186     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0 },
187
188     { "=",   2, opid1('='),         ASSOC_RIGHT, 2,  0 },
189     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 2,  0 },
190     { "-=",  2, opid2('-','='),     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, opid3('>','>','='), ASSOC_RIGHT, 2,  0 },
195     { "<<=", 2, opid3('<','<','='), 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
200     { ",",   2, opid1(','),         ASSOC_LEFT,  1,  0 }
201 };
202 static const size_t operator_count = (sizeof(operators) / sizeof(operators[0]));
203
204 typedef struct
205 {
206         lex_file *lex;
207         int      error;
208         lex_ctx  ctx;
209
210         token    *tokens;
211         token    *lastok;
212
213         token    *tok; /* current token */
214
215         MEM_VECTOR_MAKE(ast_value*, globals);
216 } parse_file;
217
218 MEM_VECTOR_PROTO(parse_file, ast_value*, globals);
219
220 parse_file* parse_open(const char *file);
221 void        parse_file_close(parse_file*);
222
223 bool        parse(parse_file*);
224
225 bool        parse_iskey(parse_file *self, const char *ident);
226
227 void lexerror(lex_file*, const char *fmt, ...);
228
229 #endif