]> git.xonotic.org Git - xonotic/gmqcc.git/blob - gmqcc.h
Indentation
[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   last;
36         int   current;
37         int   length;
38         int   size;
39 };
40
41 /*
42  * It's important that this table never exceed 32 keywords, the ascii
43  * table starts at 33 (and we don't want conflicts)
44  */
45 #define TOKEN_DO       0
46 #define TOKEN_ELSE     1
47 #define TOKEN_IF       2
48 #define TOKEN_WHILE    3
49 #define TOKEN_BREAK    4
50 #define TOKEN_CONTINUE 5
51 #define TOKEN_RETURN   6
52 #define TOKEN_GOTO     7
53 #define TOKEN_FOR      8   // extension
54 #define TOKEN_TYPEDEF  9   // extension
55
56 // ensure the token types are out of the
57 // bounds of anyothers that may conflict.
58 #define TOKEN_FLOAT    110
59 #define TOKEN_VECTOR   111
60 #define TOKEN_STRING   112
61 #define TOKEN_ENTITY   113
62 #define TOKEN_VOID     114
63
64 /*
65  * Lexer state constants, these are numbers for where exactly in
66  * the lexing the lexer is at. Or where it decided to stop if a lexer
67  * error occurs.  These numbers must be > where the ascii-table ends
68  * and > the last type token which is TOKEN_VOID
69  */
70 #define LEX_COMMENT    1128 
71 #define LEX_CHRLIT     1129
72 #define LEX_STRLIT     1130
73 #define LEX_IDENT      1131
74
75 int              lex_token(struct lex_file *);
76 void             lex_reset(struct lex_file *);
77 void             lex_close(struct lex_file *);
78 struct lex_file *lex_open (FILE *);
79
80 //===================================================================
81 //========================== error.c ================================
82 //===================================================================
83 #define ERROR_LEX      (SHRT_MAX+0)
84 #define ERROR_PARSE    (SHRT_MAX+1)
85 #define ERROR_INTERNAL (SHRT_MAX+2)
86 #define ERROR_COMPILER (SHRT_MAX+3)
87 #define ERROR_PREPRO   (SHRT_MAX+4)
88 int error(int, const char *, ...);
89
90 //===================================================================
91 //========================== parse.c ================================
92 //===================================================================
93 int parse_tree(struct lex_file *);
94 struct parsenode {
95         struct parsenode *next;
96         int               type; /* some token */
97 };
98
99 //===================================================================
100 //========================== typedef.c ==============================
101 //===================================================================
102 typedef struct typedef_node_t {
103         char      *name;
104 } typedef_node;
105
106 void          typedef_init();
107 void          typedef_clear();
108 typedef_node *typedef_find(const char *);
109 int           typedef_add (const char *, const char *);
110
111
112 //===================================================================
113 //=========================== util.c ================================
114 //===================================================================
115 void *util_memory_a(unsigned int, unsigned int, const char *);
116 void  util_memory_d(void       *, unsigned int, const char *);
117 char *util_strdup  (const char *);
118
119 #ifdef NOTRACK
120 #       define mem_a(x) malloc(x)
121 #       define mem_d(x) free  (x)
122 #else
123 #       define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
124 #       define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
125 #endif
126
127 #define VECTOR_MAKE(T,N)                                                 \
128     T*     N##_data      = NULL;                                         \
129     long   N##_elements  = 0;                                            \
130     long   N##_allocated = 0;                                            \
131     int    N##_add(T element) {                                          \
132         if (N##_elements == N##_allocated) {                             \
133             if (N##_allocated == 0) {                                    \
134                 N##_allocated = 12;                                      \
135             } else {                                                     \
136                 N##_allocated *= 2;                                      \
137             }                                                            \
138             void *temp = realloc(N##_data, (N##_allocated * sizeof(T))); \
139             if  (!temp) {                                                \
140                 free(temp);                                              \
141                 return -1;                                               \
142             }                                                            \
143             N##_data = (T*)temp;                                         \
144         }                                                                \
145         N##_data[N##_elements] = element;                                \
146         return   N##_elements++;                                         \
147     }
148
149 //===================================================================
150 //=========================== code.c ================================
151 //===================================================================
152 #define TYPE_VOID     0
153 #define TYPE_STRING   1
154 #define TYPE_FLOAT    2
155 #define TYPE_VECTOR   3
156 #define TYPE_ENTITY   4
157 #define TYPE_FIELD    5
158 #define TYPE_FUNCTION 6
159 #define TYPE_POINTER  7
160
161 /* 
162  * Instructions 
163  * These are the external instructions supported by the interperter
164  * this is what things compile to (from the C code).
165  */
166 #define INSTR_DONE      0
167 // math
168 #define INSTR_MUL_F     1
169 #define INSTR_MUL_V     2
170 #define INSTR_MUL_FV    3
171 #define INSTR_MUL_VF    4
172 #define INSTR_DIV_F     5
173 #define INSTR_ADD_F     6
174 #define INSTR_ADD_V     7
175 #define INSTR_SUB_F     8
176 #define INSTR_SUB_V     9
177 // compare
178 #define INSTR_EQ_F      10
179 #define INSTR_EQ_V      11
180 #define INSTR_EQ_S      12
181 #define INSTR_EQ_E      13
182 #define INSTR_EQ_FNC    14
183 #define INSTR_NE_F      15
184 #define INSTR_NE_V      16
185 #define INSTR_NE_S      17
186 #define INSTR_NE_E      18
187 #define INSTR_NE_FNC    19
188 // multi compare
189 #define INSTR_LE        20
190 #define INSTR_GE        21
191 #define INSTR_LT        22
192 #define INSTR_GT        23
193 // load and store
194 #define INSTR_LOAD_F    24
195 #define INSTR_LOAD_V    25
196 #define INSTR_LOAD_S    26
197 #define INSTR_LOAD_ENT  27
198 #define INSTR_LOAD_FLD  28
199 #define INSTR_LOAD_FNC  29
200 #define INSTR_STORE_F   31
201 #define INSTR_STORE_V   32
202 #define INSTR_STORE_S   33
203 #define INSTR_STORE_ENT 34
204 #define INSTR_STORE_FLD 35
205 #define INSTR_STORE_FNC 36
206 // others
207 #define INSTR_ADDRESS   30
208 #define INSTR_RETURN    37
209 #define INSTR_NOT_F     38
210 #define INSTR_NOT_V     39
211 #define INSTR_NOT_S     40
212 #define INSTR_NOT_ENT   41
213 #define INSTR_NOT_FNC   42
214 #define INSTR_IF        43
215 #define INSTR_IFNOT     44
216 #define INSTR_CALL0     45
217 #define INSTR_CALL1     46
218 #define INSTR_CALL2     47
219 #define INSTR_CALL3     48
220 #define INSTR_CALL4     49
221 #define INSTR_CALL5     50
222 #define INSTR_CALL6     51
223 #define INSTR_CALL7     52
224 #define INSTR_CALL8     53
225 #define INSTR_STATE     54
226 #define INSTR_GOTO      55
227 #define INSTR_AND       56
228 #define INSTR_OR        57
229 #define INSTR_BITAND    59
230 #define INSTR_BITOR     60
231 #endif