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