]> git.xonotic.org Git - xonotic/gmqcc.git/blob - gmqcc.h
More parsing stuff (still totally broken)
[xonotic/gmqcc.git] / gmqcc.h
1 /*
2  * Copyright (C) 2012 
3  *      Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #ifndef GMQCC_HDR
24 #define GMQCC_HDR
25 #include <stdio.h>
26
27 //===================================================================
28 //============================ lex.c ================================
29 //===================================================================
30 struct lex_file {
31         FILE *file;        /* file handler */
32         char *name;        /* name of file */
33         char  peek  [5];  
34         char  lastok[8192];
35         
36         
37         int   last;    /* last token                   */
38         int   current; /* current token                */
39         
40         int   length;  /* bytes left to parse          */
41         int   size;    /* never changes (size of file) */
42         int   line;    /* what line are we on?         */
43 };
44
45 /*
46  * It's important that this table never exceed 32 keywords, the ascii
47  * table starts at 33 (and we don't want conflicts)
48  */
49 #define TOKEN_DO       0
50 #define TOKEN_ELSE     1
51 #define TOKEN_IF       2
52 #define TOKEN_WHILE    3
53 #define TOKEN_BREAK    4
54 #define TOKEN_CONTINUE 5
55 #define TOKEN_RETURN   6
56 #define TOKEN_GOTO     7
57 #define TOKEN_FOR      8   // extension
58 #define TOKEN_TYPEDEF  9   // extension
59
60 // ensure the token types are out of the
61 // bounds of anyothers that may conflict.
62 #define TOKEN_FLOAT    110
63 #define TOKEN_VECTOR   111
64 #define TOKEN_STRING   112
65 #define TOKEN_ENTITY   113
66 #define TOKEN_VOID     114
67
68 /*
69  * Lexer state constants, these are numbers for where exactly in
70  * the lexing the lexer is at. Or where it decided to stop if a lexer
71  * error occurs.  These numbers must be > where the ascii-table ends
72  * and > the last type token which is TOKEN_VOID
73  */
74 #define LEX_COMMENT    1128 
75 #define LEX_CHRLIT     1129
76 #define LEX_STRLIT     1130
77 #define LEX_IDENT      1131
78
79 int              lex_token(struct lex_file *);
80 void             lex_reset(struct lex_file *);
81 void             lex_close(struct lex_file *);
82 struct lex_file *lex_open (FILE *);
83
84 //===================================================================
85 //========================== error.c ================================
86 //===================================================================
87 #define ERROR_LEX      (SHRT_MAX+0)
88 #define ERROR_PARSE    (SHRT_MAX+1)
89 #define ERROR_INTERNAL (SHRT_MAX+2)
90 #define ERROR_COMPILER (SHRT_MAX+3)
91 #define ERROR_PREPRO   (SHRT_MAX+4)
92 int error(int, const char *, ...);
93
94 //===================================================================
95 //========================== parse.c ================================
96 //===================================================================
97 int parse_tree(struct lex_file *);
98 struct parsenode {
99         struct parsenode *next;
100         int               type; /* some token */
101 };
102
103 //===================================================================
104 //========================== typedef.c ==============================
105 //===================================================================
106 typedef struct typedef_node_t {
107         char      *name;
108 } typedef_node;
109
110 void          typedef_init();
111 void          typedef_clear();
112 typedef_node *typedef_find(const char *);
113 int           typedef_add (const char *, const char *);
114
115
116 //===================================================================
117 //=========================== util.c ================================
118 //===================================================================
119 void *util_memory_a(unsigned int, unsigned int, const char *);
120 void  util_memory_d(void       *, unsigned int, const char *);
121 char *util_strdup  (const char *);
122
123 #ifdef NOTRACK
124 #       define mem_a(x) malloc(x)
125 #       define mem_d(x) free  (x)
126 #else
127 #       define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
128 #       define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
129 #endif
130
131 #define VECTOR_MAKE(T,N)                                                 \
132     T*     N##_data      = NULL;                                         \
133     long   N##_elements  = 0;                                            \
134     long   N##_allocated = 0;                                            \
135     int    N##_add(T element) {                                          \
136         if (N##_elements == N##_allocated) {                             \
137             if (N##_allocated == 0) {                                    \
138                 N##_allocated = 12;                                      \
139             } else {                                                     \
140                 N##_allocated *= 2;                                      \
141             }                                                            \
142             void *temp = realloc(N##_data, (N##_allocated * sizeof(T))); \
143             if  (!temp) {                                                \
144                 free(temp);                                              \
145                 return -1;                                               \
146             }                                                            \
147             N##_data = (T*)temp;                                         \
148         }                                                                \
149         N##_data[N##_elements] = element;                                \
150         return   N##_elements++;                                         \
151     }
152
153 //===================================================================
154 //=========================== code.c ================================
155 //===================================================================
156 #define TYPE_VOID     0
157 #define TYPE_STRING   1
158 #define TYPE_FLOAT    2
159 #define TYPE_VECTOR   3
160 #define TYPE_ENTITY   4
161 #define TYPE_FIELD    5
162 #define TYPE_FUNCTION 6
163 #define TYPE_POINTER  7
164
165 /*
166  * Each paramater incerements by 3 since vector types hold
167  * 3 components (x,y,z).
168  */
169 #define OFS_NULL      0
170 #define OFS_RETURN    1
171 #define OFS_PARM0     (OFS_RETURN+3)
172 #define OFS_PARM1     (OFS_PARM0 +3)
173 #define OFS_PARM2     (OFS_PARM1 +3)
174 #define OFS_PARM3     (OFS_PARM2 +3)
175 #define OFS_PARM4     (OFS_PARM3 +3)
176 #define OFS_PARM5     (OFS_PARM4 +3)
177 #define OFS_PARM6     (OFS_PARM5 +3)
178 #define OFS_PARM7     (OFS_PARM6 +3)
179
180 /* 
181  * Instructions 
182  * These are the external instructions supported by the interperter
183  * this is what things compile to (from the C code).
184  */
185 enum {
186         INSTR_DONE,
187         INSTR_MUL_F,
188         INSTR_MUL_V,
189         INSTR_MUL_FV,
190         INSTR_MUL_VF,
191         INSTR_DIV_F,
192         INSTR_ADD_F,
193         INSTR_ADD_V,
194         INSTR_SUB_F,
195         INSTR_SUB_V,
196         
197         INSTR_EQ_F,
198         INSTR_EQ_V,
199         INSTR_EQ_S,
200         INSTR_EQ_E,
201         INSTR_EQ_FNC,
202         
203         INSTR_NE_F,
204         INSTR_NE_V,
205         INSTR_NE_S,
206         INSTR_NE_E,
207         INSTR_NE_FNC,
208         
209         INSTR_LE,
210         INSTR_GE,
211         INSTR_LT,
212         INSTR_GT,
213
214         INSTR_LOAD_F,
215         INSTR_LOAD_V,
216         INSTR_LOAD_S,
217         INSTR_LOAD_ENT,
218         INSTR_LOAD_FLD,
219         INSTR_LOAD_FNC,
220
221         INSTR_ADDRESS,
222
223         INSTR_STORE_F,
224         INSTR_STORE_V,
225         INSTR_STORE_S,
226         INSTR_STORE_ENT,
227         INSTR_STORE_FLD,
228         INSTR_STORE_FNC,
229
230         INSTR_STOREP_F,
231         INSTR_STOREP_V,
232         INSTR_STOREP_S,
233         INSTR_STOREP_ENT,
234         INSTR_STOREP_FLD,
235         INSTR_STOREP_FNC,
236
237         INSTR_RETURN,
238         INSTR_NOT_F,
239         INSTR_NOT_V,
240         INSTR_NOT_S,
241         INSTR_NOT_ENT,
242         INSTR_NOT_FNC,
243         INSTR_IF,
244         INSTR_IFNOT,
245         INSTR_CALL0,
246         INSTR_CALL1,
247         INSTR_CALL2,
248         INSTR_CALL3,
249         INSTR_CALL4,
250         INSTR_CALL5,
251         INSTR_CALL6,
252         INSTR_CALL7,
253         INSTR_CALL8,
254         INSTR_STATE,
255         INSTR_GOTO,
256         INSTR_AND,
257         INSTR_OR,
258         
259         INSTR_BITAND,
260         INSTR_BITOR
261 };
262
263
264 void code_write();
265 #endif