]> git.xonotic.org Git - xonotic/gmqcc.git/blob - gmqcc.h
Include now works
[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_include(struct lex_file *, char *);
83 struct lex_file *lex_open   (FILE *);
84
85 //===================================================================
86 //========================== error.c ================================
87 //===================================================================
88 #define ERROR_LEX      (SHRT_MAX+0)
89 #define ERROR_PARSE    (SHRT_MAX+1)
90 #define ERROR_INTERNAL (SHRT_MAX+2)
91 #define ERROR_COMPILER (SHRT_MAX+3)
92 #define ERROR_PREPRO   (SHRT_MAX+4)
93 int error(int, const char *, ...);
94
95 //===================================================================
96 //========================== parse.c ================================
97 //===================================================================
98 int parse_tree(struct lex_file *);
99 struct parsenode {
100         struct parsenode *next;
101         int               type; /* some token */
102 };
103
104 //===================================================================
105 //========================== typedef.c ==============================
106 //===================================================================
107 typedef struct typedef_node_t {
108         char      *name;
109 } typedef_node;
110
111 void          typedef_init();
112 void          typedef_clear();
113 typedef_node *typedef_find(const char *);
114 int           typedef_add (const char *, const char *);
115
116
117 //===================================================================
118 //=========================== util.c ================================
119 //===================================================================
120 void *util_memory_a(unsigned int, unsigned int, const char *);
121 void  util_memory_d(void       *, unsigned int, const char *);
122 char *util_strdup  (const char *);
123
124 #ifdef NOTRACK
125 #       define mem_a(x) malloc(x)
126 #       define mem_d(x) free  (x)
127 #else
128 #       define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
129 #       define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
130 #endif
131
132 #define VECTOR_MAKE(T,N)                                                 \
133     T*     N##_data      = NULL;                                         \
134     long   N##_elements  = 0;                                            \
135     long   N##_allocated = 0;                                            \
136     int    N##_add(T element) {                                          \
137         if (N##_elements == N##_allocated) {                             \
138             if (N##_allocated == 0) {                                    \
139                 N##_allocated = 12;                                      \
140             } else {                                                     \
141                 N##_allocated *= 2;                                      \
142             }                                                            \
143             void *temp = realloc(N##_data, (N##_allocated * sizeof(T))); \
144             if  (!temp) {                                                \
145                 free(temp);                                              \
146                 return -1;                                               \
147             }                                                            \
148             N##_data = (T*)temp;                                         \
149         }                                                                \
150         N##_data[N##_elements] = element;                                \
151         return   N##_elements++;                                         \
152     }
153
154 //===================================================================
155 //=========================== code.c ================================
156 //===================================================================
157 #define TYPE_VOID     0
158 #define TYPE_STRING   1
159 #define TYPE_FLOAT    2
160 #define TYPE_VECTOR   3
161 #define TYPE_ENTITY   4
162 #define TYPE_FIELD    5
163 #define TYPE_FUNCTION 6
164 #define TYPE_POINTER  7
165
166 /*
167  * Each paramater incerements by 3 since vector types hold
168  * 3 components (x,y,z).
169  */
170 #define OFS_NULL      0
171 #define OFS_RETURN    1
172 #define OFS_PARM0     (OFS_RETURN+3)
173 #define OFS_PARM1     (OFS_PARM0 +3)
174 #define OFS_PARM2     (OFS_PARM1 +3)
175 #define OFS_PARM3     (OFS_PARM2 +3)
176 #define OFS_PARM4     (OFS_PARM3 +3)
177 #define OFS_PARM5     (OFS_PARM4 +3)
178 #define OFS_PARM6     (OFS_PARM5 +3)
179 #define OFS_PARM7     (OFS_PARM6 +3)
180
181 /* 
182  * Instructions 
183  * These are the external instructions supported by the interperter
184  * this is what things compile to (from the C code).
185  */
186 enum {
187         INSTR_DONE,
188         INSTR_MUL_F,
189         INSTR_MUL_V,
190         INSTR_MUL_FV,
191         INSTR_MUL_VF,
192         INSTR_DIV_F,
193         INSTR_ADD_F,
194         INSTR_ADD_V,
195         INSTR_SUB_F,
196         INSTR_SUB_V,
197         
198         INSTR_EQ_F,
199         INSTR_EQ_V,
200         INSTR_EQ_S,
201         INSTR_EQ_E,
202         INSTR_EQ_FNC,
203         
204         INSTR_NE_F,
205         INSTR_NE_V,
206         INSTR_NE_S,
207         INSTR_NE_E,
208         INSTR_NE_FNC,
209         
210         INSTR_LE,
211         INSTR_GE,
212         INSTR_LT,
213         INSTR_GT,
214
215         INSTR_LOAD_F,
216         INSTR_LOAD_V,
217         INSTR_LOAD_S,
218         INSTR_LOAD_ENT,
219         INSTR_LOAD_FLD,
220         INSTR_LOAD_FNC,
221
222         INSTR_ADDRESS,
223
224         INSTR_STORE_F,
225         INSTR_STORE_V,
226         INSTR_STORE_S,
227         INSTR_STORE_ENT,
228         INSTR_STORE_FLD,
229         INSTR_STORE_FNC,
230
231         INSTR_STOREP_F,
232         INSTR_STOREP_V,
233         INSTR_STOREP_S,
234         INSTR_STOREP_ENT,
235         INSTR_STOREP_FLD,
236         INSTR_STOREP_FNC,
237
238         INSTR_RETURN,
239         INSTR_NOT_F,
240         INSTR_NOT_V,
241         INSTR_NOT_S,
242         INSTR_NOT_ENT,
243         INSTR_NOT_FNC,
244         INSTR_IF,
245         INSTR_IFNOT,
246         INSTR_CALL0,
247         INSTR_CALL1,
248         INSTR_CALL2,
249         INSTR_CALL3,
250         INSTR_CALL4,
251         INSTR_CALL5,
252         INSTR_CALL6,
253         INSTR_CALL7,
254         INSTR_CALL8,
255         INSTR_STATE,
256         INSTR_GOTO,
257         INSTR_AND,
258         INSTR_OR,
259         
260         INSTR_BITAND,
261         INSTR_BITOR
262 };
263
264
265 void code_write();
266 #endif