1 /* |
|
2 * botc source code |
|
3 * Copyright (C) 2012 Santeri `Dusk` Piippo |
|
4 * All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright notice, |
|
10 * this list of conditions and the following disclaimer. |
|
11 * 2. Redistributions in binary form must reproduce the above copyright notice, |
|
12 * this list of conditions and the following disclaimer in the documentation |
|
13 * and/or other materials provided with the distribution. |
|
14 * 3. Neither the name of the developer nor the names of its contributors may |
|
15 * be used to endorse or promote products derived from this software without |
|
16 * specific prior written permission. |
|
17 * 4. Redistributions in any form must be accompanied by information on how to |
|
18 * obtain complete source code for the software and any accompanying |
|
19 * software that uses the software. The source code must either be included |
|
20 * in the distribution or be available for no more than the cost of |
|
21 * distribution plus a nominal fee, and must be freely redistributable |
|
22 * under reasonable conditions. For an executable file, complete source |
|
23 * code means the source code for all modules it contains. It does not |
|
24 * include source code for modules or files that typically accompany the |
|
25 * major components of the operating system on which the executable file |
|
26 * runs. |
|
27 * |
|
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
38 * POSSIBILITY OF SUCH DAMAGE. |
|
39 */ |
|
40 |
|
41 #ifndef __COMMON_H__ |
|
42 #define __COMMON_H__ |
|
43 |
|
44 #include <stdio.h> |
|
45 #include <stdarg.h> |
|
46 #include <stdint.h> |
|
47 #include "bots.h" |
|
48 #include "str.h" |
|
49 |
|
50 // Application name and version |
|
51 #define APPNAME "botc" |
|
52 #define VERSION_MAJOR 0 |
|
53 #define VERSION_MINOR 999 |
|
54 |
|
55 // Use a macro for Write so we can get the function name |
|
56 // This can be pretty crucial in debugging. |
|
57 #ifdef __GNUC__ |
|
58 #define Write(STUFF) DoWrite (__PRETTY_FUNCTION__, STUFF) |
|
59 #else |
|
60 #define Write(STUFF) DoWrite (__func__, STUFF) |
|
61 #endif |
|
62 |
|
63 // On Windows, files are case-insensitive |
|
64 #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__) |
|
65 #define FILE_CASEINSENSITIVE |
|
66 #endif |
|
67 |
|
68 // Parser mode: where is the parser at? |
|
69 enum parsermode_e { |
|
70 MODE_TOPLEVEL, // at top level |
|
71 MODE_EVENT, // inside event definition |
|
72 MODE_MAINLOOP, // inside mainloop |
|
73 MODE_ONENTER, // inside onenter |
|
74 MODE_ONEXIT, // inside onexit |
|
75 }; |
|
76 |
|
77 enum type_e { |
|
78 TYPE_UNKNOWN = 0, |
|
79 TYPE_VOID, |
|
80 TYPE_INT, |
|
81 TYPE_STRING, |
|
82 TYPE_BOOL, |
|
83 }; |
|
84 |
|
85 #define CHECK_FILE(pointer,path,action) \ |
|
86 if (!pointer) { \ |
|
87 error ("couldn't open %s for %s!\n", (char*)path, action); \ |
|
88 exit (1); \ |
|
89 } |
|
90 |
|
91 // Shortcut for formatting |
|
92 #define PERFORM_FORMAT(in, out) \ |
|
93 va_list v; \ |
|
94 va_start (v, in); \ |
|
95 char* out = vdynformat (in, v, 256); \ |
|
96 va_end (v); |
|
97 |
|
98 // Plural expression |
|
99 #define PLURAL(n) (n != 1) ? "s" : "" |
|
100 |
|
101 // Shortcut for zeroing something |
|
102 #define ZERO(obj) memset (&obj, 0, sizeof (obj)); |
|
103 |
|
104 void error (const char* text, ...); |
|
105 char* ObjectFileName (str s); |
|
106 bool fexists (char* path); |
|
107 type_e GetTypeByName (str token); |
|
108 str GetTypeName (type_e type); |
|
109 |
|
110 // Make the parser's variables globally available |
|
111 extern int g_NumStates; |
|
112 extern int g_NumEvents; |
|
113 extern parsermode_e g_CurMode; |
|
114 extern str g_CurState; |
|
115 |
|
116 #define neurosphere if (g_Neurosphere) |
|
117 #define twice for (int repeat_token = 0; repeat_token < 2; repeat_token++) |
|
118 |
|
119 #ifndef __GNUC__ |
|
120 #define __attribute__(X) |
|
121 #endif |
|
122 #define deprecated __attribute__ ((deprecated)) |
|
123 |
|
124 // Power function |
|
125 template<class T> T pow (T a, unsigned int b) { |
|
126 if (!b) |
|
127 return 1; |
|
128 |
|
129 T r = a; |
|
130 while (b > 1) { |
|
131 b--; |
|
132 r = r * a; |
|
133 } |
|
134 |
|
135 return r; |
|
136 } |
|
137 |
|
138 // Whitespace check |
|
139 inline bool IsCharWhitespace (char c) { |
|
140 return (c <= 32 || c == 127 || c == 255); |
|
141 } |
|
142 |
|
143 // Byte datatype |
|
144 typedef int32_t word; |
|
145 typedef unsigned char byte; |
|
146 |
|
147 // Keywords |
|
148 #ifndef __MAIN_CXX__ |
|
149 extern const char** g_Keywords; |
|
150 #endif |
|
151 |
|
152 bool IsKeyword (str s); |
|
153 unsigned int NumKeywords (); |
|
154 |
|
155 // Script mark and reference |
|
156 struct ScriptMark { |
|
157 str name; |
|
158 size_t pos; |
|
159 }; |
|
160 |
|
161 struct ScriptMarkReference { |
|
162 unsigned int num; |
|
163 size_t pos; |
|
164 }; |
|
165 |
|
166 // ==================================================================== |
|
167 // Generic union |
|
168 template <class T> union union_t { |
|
169 T val; |
|
170 byte b[sizeof (T)]; |
|
171 char c[sizeof (T)]; |
|
172 double d; |
|
173 float f; |
|
174 int i; |
|
175 word w; |
|
176 }; |
|
177 |
|
178 // ==================================================================== |
|
179 // Finds a byte in the given value. |
|
180 template <class T> inline unsigned char GetByteIndex (T a, unsigned int b) { |
|
181 union_t<T> uni; |
|
182 uni.val = a; |
|
183 return uni.b[b]; |
|
184 } |
|
185 |
|
186 #endif // __COMMON_H__ |
|