main.cxx

changeset 71
11f23fabf8a6
parent 70
fc257920ac00
child 72
03e4d9db3fd9
equal deleted inserted replaced
70:fc257920ac00 71:11f23fabf8a6
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 #define __MAIN_CXX__
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include "common.h"
47
48 #include "str.h"
49 #include "scriptreader.h"
50 #include "objwriter.h"
51 #include "events.h"
52 #include "commands.h"
53 #include "stringtable.h"
54 #include "variables.h"
55 #include "array.h"
56 #include "databuffer.h"
57
58 #include "bots.h"
59 #include "botcommands.h"
60
61 // List of keywords
62 const char* g_Keywords[] = {
63 "bool",
64 "break",
65 "case",
66 "continue",
67 "const",
68 "default",
69 "do",
70 "else",
71 "event",
72 "for",
73 "goto",
74 "if",
75 "int",
76 "mainloop",
77 "onenter",
78 "onexit",
79 "state",
80 "switch",
81 "str"
82 "void",
83 "while",
84
85 // These ones aren't implemented yet but I plan to do so, thus they are
86 // reserved. Also serves as a to-do list of sorts for me. >:F
87 "enum", // Would enum actually be useful? I think so.
88 "func", // Would function support need external support from zandronum?
89 "return",
90 };
91
92 // databuffer global variable
93 int g_NextMark = 0;
94
95 int main (int argc, char** argv) {
96 // Intepret command-line parameters:
97 // -l: list commands
98 // I guess there should be a better way to do this.
99 if (argc == 2 && !strcmp (argv[1], "-l")) {
100 ReadCommands ();
101 printf ("Begin list of commands:\n");
102 printf ("------------------------------------------------------\n");
103
104 CommandDef* comm;
105 ITERATE_COMMANDS (comm)
106 printf ("%s\n", GetCommandPrototype (comm).chars());
107
108 printf ("------------------------------------------------------\n");
109 printf ("End of command list\n");
110 exit (0);
111 }
112
113 // Print header
114 str header;
115 str headerline = "-=";
116 header.appendformat ("%s version %d.%d", APPNAME, VERSION_MAJOR, VERSION_MINOR);
117
118 headerline *= (header.len() / 2) - 1;
119 headerline += '-';
120 printf ("%s\n%s\n", header.chars(), headerline.chars());
121
122 if (argc < 2) {
123 fprintf (stderr, "usage: %s <infile> [outfile] # compiles botscript\n", argv[0]);
124 fprintf (stderr, " %s -l # lists commands\n", argv[0]);
125 exit (1);
126 }
127
128 // A word should always be exactly 4 bytes. The above list command
129 // doesn't need it, but the rest of the program does.
130 if (sizeof (word) != 4)
131 error ("%s expects a word (uint32_t) to be 4 bytes in size, is %d\n",
132 APPNAME, sizeof (word));
133
134 str outfile;
135 if (argc < 3)
136 outfile = ObjectFileName (argv[1]);
137 else
138 outfile = argv[2];
139
140 // If we'd end up writing into an existing file,
141 // ask the user if we want to overwrite it
142 if (fexists (outfile)) {
143 // Additional warning if the paths are the same
144 str warning;
145 #ifdef FILE_CASEINSENSITIVE
146 if (!outfile.icompare (argv[1]))
147 #else
148 if (!outfile.compare (argv[1]))
149 #endif
150 {
151 warning = "\nWARNING: Output file is the same as the input file. ";
152 warning += "Answering yes here will destroy the source!\n";
153 warning += "Continue nevertheless?";
154 }
155 printf ("output file `%s` already exists! overwrite?%s (y/n) ", outfile.chars(), warning.chars());
156
157 char ans;
158 fgets (&ans, 2, stdin);
159 if (ans != 'y') {
160 printf ("abort\n");
161 exit (1);
162 }
163 }
164
165 // Read definitions
166 printf ("Reading definitions...\n");
167 ReadEvents ();
168 ReadCommands ();
169
170 // Init stuff
171 InitStringTable ();
172
173 // Prepare reader and writer
174 ScriptReader* r = new ScriptReader (argv[1]);
175 ObjWriter* w = new ObjWriter (outfile);
176
177 // We're set, begin parsing :)
178 printf ("Parsing script...\n");
179 r->ParseBotScript (w);
180 printf ("Script parsed successfully.\n");
181
182 // Parse done, print statistics and write to file
183 unsigned int globalcount = g_GlobalVariables.size();
184 unsigned int stringcount = CountStringTable ();
185 int NumMarks = w->MainBuffer->CountMarks ();
186 int NumRefs = w->MainBuffer->CountReferences ();
187 printf ("%u / %u strings written\n", stringcount, MAX_LIST_STRINGS);
188 printf ("%u / %u global variables\n", globalcount, MAX_SCRIPT_VARIABLES);
189 printf ("%d / %d bytecode marks\n", NumMarks, MAX_MARKS);
190 printf ("%d / %d bytecode references\n", NumRefs, MAX_MARKS);
191 printf ("%d / %d events\n", g_NumEvents, MAX_NUM_EVENTS);
192 printf ("%d state%s\n", g_NumStates, PLURAL (g_NumStates));
193
194 w->WriteToFile ();
195
196 // Clear out the junk
197 delete r;
198 delete w;
199
200 // Done!
201 exit (0);
202 }
203
204 // ============================================================================
205 // Utility functions
206
207 // ============================================================================
208 // Does the given file exist?
209 bool fexists (char* path) {
210 if (FILE* test = fopen (path, "r")) {
211 fclose (test);
212 return true;
213 }
214 return false;
215 }
216
217 // ============================================================================
218 // Generic error
219 void error (const char* text, ...) {
220 PERFORM_FORMAT (text, c);
221 fprintf (stderr, "error: %s", c);
222 exit (1);
223 }
224
225 // ============================================================================
226 // Mutates given filename to an object filename
227 char* ObjectFileName (str s) {
228 // Locate the extension and chop it out
229 unsigned int extdot = s.last (".");
230 if (extdot >= s.len()-4)
231 s -= (s.len() - extdot);
232
233 s += ".o";
234 return s.chars();
235 }
236
237 // ============================================================================
238 // Is the given argument a reserved keyword?
239 bool IsKeyword (str s) {
240 for (unsigned int u = 0; u < NumKeywords (); u++)
241 if (!s.icompare (g_Keywords[u]))
242 return true;
243 return false;
244 }
245
246 unsigned int NumKeywords () {
247 return sizeof (g_Keywords) / sizeof (const char*);
248 }
249
250 // ============================================================================
251 type_e GetTypeByName (str t) {
252 t = t.tolower();
253 return (t == "int") ? TYPE_INT :
254 (t == "str") ? TYPE_STRING :
255 (t == "void") ? TYPE_VOID :
256 (t == "bool") ? TYPE_BOOL :
257 TYPE_UNKNOWN;
258 }
259
260
261 // ============================================================================
262 // Inverse operation - type name by value
263 str GetTypeName (type_e type) {
264 switch (type) {
265 case TYPE_INT: return "int"; break;
266 case TYPE_STRING: return "str"; break;
267 case TYPE_VOID: return "void"; break;
268 case TYPE_BOOL: return "bool"; break;
269 case TYPE_UNKNOWN: return "???"; break;
270 }
271
272 return "";
273 }

mercurial