]> git.xonotic.org Git - xonotic/gmqcc.git/blob - gmqcc.h
Count file lines
[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;
32         char  peek  [5];
33         char  lastok[8192];
34         
35         int   line;
36         int   last;
37         int   current;
38         int   length;
39         int   size;
40 };
41
42 /*
43  * It's important that this table never exceed 32 keywords, the ascii
44  * table starts at 33 (and we don't want conflicts)
45  */
46 #define TOKEN_DO       0
47 #define TOKEN_ELSE     1
48 #define TOKEN_IF       2
49 #define TOKEN_WHILE    3
50 #define TOKEN_BREAK    4
51 #define TOKEN_CONTINUE 5
52 #define TOKEN_RETURN   6
53 #define TOKEN_GOTO     7
54 #define TOKEN_FOR      8   // extension
55 #define TOKEN_TYPEDEF  9   // extension
56
57 // ensure the token types are out of the
58 // bounds of anyothers that may conflict.
59 #define TOKEN_FLOAT    110
60 #define TOKEN_VECTOR   111
61 #define TOKEN_STRING   112
62 #define TOKEN_ENTITY   113
63 #define TOKEN_VOID     114
64
65 /*
66  * Lexer state constants, these are numbers for where exactly in
67  * the lexing the lexer is at. Or where it decided to stop if a lexer
68  * error occurs.  These numbers must be > where the ascii-table ends
69  * and > the last type token which is TOKEN_VOID
70  */
71 #define LEX_COMMENT    1128 
72 #define LEX_CHRLIT     1129
73 #define LEX_STRLIT     1130
74 #define LEX_IDENT      1131
75
76 int              lex_token(struct lex_file *);
77 void             lex_reset(struct lex_file *);
78 void             lex_close(struct lex_file *);
79 struct lex_file *lex_open (FILE *);
80
81 //===================================================================
82 //========================== error.c ================================
83 //===================================================================
84 #define ERROR_LEX      (SHRT_MAX+0)
85 #define ERROR_PARSE    (SHRT_MAX+1)
86 #define ERROR_INTERNAL (SHRT_MAX+2)
87 #define ERROR_COMPILER (SHRT_MAX+3)
88 #define ERROR_PREPRO   (SHRT_MAX+4)
89 int error(int, const char *, ...);
90
91 //===================================================================
92 //========================== parse.c ================================
93 //===================================================================
94 int parse_tree(struct lex_file *);
95 struct parsenode {
96         struct parsenode *next;
97         int               type; /* some token */
98 };
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 (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
120 #ifdef NOTRACK
121 #       define mem_a(x) malloc(x)
122 #       define mem_d(x) free  (x)
123 #else
124 #       define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
125 #       define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
126 #endif
127
128 #define VECTOR_MAKE(T,N)                                                 \
129     T*     N##_data      = NULL;                                         \
130     long   N##_elements  = 0;                                            \
131     long   N##_allocated = 0;                                            \
132     int    N##_add(T element) {                                          \
133         if (N##_elements == N##_allocated) {                             \
134             if (N##_allocated == 0) {                                    \
135                 N##_allocated = 12;                                      \
136             } else {                                                     \
137                 N##_allocated *= 2;                                      \
138             }                                                            \
139             void *temp = realloc(N##_data, (N##_allocated * sizeof(T))); \
140             if  (!temp) {                                                \
141                 free(temp);                                              \
142                 return -1;                                               \
143             }                                                            \
144             N##_data = (T*)temp;                                         \
145         }                                                                \
146         N##_data[N##_elements] = element;                                \
147         return   N##_elements++;                                         \
148     }
149
150 //===================================================================
151 //=========================== code.c ================================
152 //===================================================================
153 #define TYPE_VOID     0
154 #define TYPE_STRING   1
155 #define TYPE_FLOAT    2
156 #define TYPE_VECTOR   3
157 #define TYPE_ENTITY   4
158 #define TYPE_FIELD    5
159 #define TYPE_FUNCTION 6
160 #define TYPE_POINTER  7
161
162 /*
163  * Each paramater incerements by 3 since vector types hold
164  * 3 components (x,y,z).
165  */
166 #define OFS_NULL      0
167 #define OFS_RETURN    1
168 #define OFS_PARM0     (OFS_RETURN+3)
169 #define OFS_PARM1     (OFS_PARM0 +3)
170 #define OFS_PARM2     (OFS_PARM1 +3)
171 #define OFS_PARM3     (OFS_PARM2 +3)
172 #define OFS_PARM4     (OFS_PARM3 +3)
173 #define OFS_PARM5     (OFS_PARM4 +3)
174 #define OFS_PARM6     (OFS_PARM5 +3)
175 #define OFS_PARM7     (OFS_PARM6 +3)
176
177 /* 
178  * Instructions 
179  * These are the external instructions supported by the interperter
180  * this is what things compile to (from the C code).
181  */
182 enum {
183         INSTR_DONE,
184         INSTR_MUL_F,
185         INSTR_MUL_V,
186         INSTR_MUL_FV,
187         INSTR_MUL_VF,
188         INSTR_DIV_F,
189         INSTR_ADD_F,
190         INSTR_ADD_V,
191         INSTR_SUB_F,
192         INSTR_SUB_V,
193         
194         INSTR_EQ_F,
195         INSTR_EQ_V,
196         INSTR_EQ_S,
197         INSTR_EQ_E,
198         INSTR_EQ_FNC,
199         
200         INSTR_NE_F,
201         INSTR_NE_V,
202         INSTR_NE_S,
203         INSTR_NE_E,
204         INSTR_NE_FNC,
205         
206         INSTR_LE,
207         INSTR_GE,
208         INSTR_LT,
209         INSTR_GT,
210
211         INSTR_LOAD_F,
212         INSTR_LOAD_V,
213         INSTR_LOAD_S,
214         INSTR_LOAD_ENT,
215         INSTR_LOAD_FLD,
216         INSTR_LOAD_FNC,
217
218         INSTR_ADDRESS,
219
220         INSTR_STORE_F,
221         INSTR_STORE_V,
222         INSTR_STORE_S,
223         INSTR_STORE_ENT,
224         INSTR_STORE_FLD,
225         INSTR_STORE_FNC,
226
227         INSTR_STOREP_F,
228         INSTR_STOREP_V,
229         INSTR_STOREP_S,
230         INSTR_STOREP_ENT,
231         INSTR_STOREP_FLD,
232         INSTR_STOREP_FNC,
233
234         INSTR_RETURN,
235         INSTR_NOT_F,
236         INSTR_NOT_V,
237         INSTR_NOT_S,
238         INSTR_NOT_ENT,
239         INSTR_NOT_FNC,
240         INSTR_IF,
241         INSTR_IFNOT,
242         INSTR_CALL0,
243         INSTR_CALL1,
244         INSTR_CALL2,
245         INSTR_CALL3,
246         INSTR_CALL4,
247         INSTR_CALL5,
248         INSTR_CALL6,
249         INSTR_CALL7,
250         INSTR_CALL8,
251         INSTR_STATE,
252         INSTR_GOTO,
253         INSTR_AND,
254         INSTR_OR,
255         
256         INSTR_BITAND,
257         INSTR_BITOR
258 };
259
260
261 void code_write();
262 #endif