25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include "main.h" |
29 #include "main.h" |
30 #include "object_writer.h" |
|
31 #include "events.h" |
30 #include "events.h" |
32 #include "commands.h" |
31 #include "commands.h" |
33 #include "stringtable.h" |
32 #include "stringtable.h" |
34 #include "variables.h" |
33 #include "variables.h" |
35 #include "data_buffer.h" |
34 #include "data_buffer.h" |
36 #include "object_writer.h" |
|
37 #include "parser.h" |
35 #include "parser.h" |
38 #include "lexer.h" |
36 #include "lexer.h" |
39 #include "gitinfo.h" |
37 #include "gitinfo.h" |
40 |
|
41 // List of keywords |
|
42 const string_list g_Keywords = |
|
43 { |
|
44 "bool", |
|
45 "break", |
|
46 "case", |
|
47 "continue", |
|
48 "const", |
|
49 "default", |
|
50 "do", |
|
51 "else", |
|
52 "event", |
|
53 "for", |
|
54 "goto", |
|
55 "if", |
|
56 "int", |
|
57 "mainloop", |
|
58 "onenter", |
|
59 "onexit", |
|
60 "state", |
|
61 "switch", |
|
62 "str" |
|
63 "void", |
|
64 "while", |
|
65 |
|
66 // These ones aren't implemented yet but I plan to do so, thus they are |
|
67 // reserved. Also serves as a to-do list of sorts for me. >:F |
|
68 "enum", // Would enum actually be useful? I think so. |
|
69 "func", // Would function support need external support from zandronum? |
|
70 "return", |
|
71 }; |
|
72 |
|
73 // databuffer global variable |
|
74 int g_NextMark = 0; |
|
75 |
38 |
76 int main (int argc, char** argv) |
39 int main (int argc, char** argv) |
77 { |
40 { |
78 try |
41 try |
79 { |
42 { |
120 if (argc < 3) |
83 if (argc < 3) |
121 outfile = ObjectFileName (argv[1]); |
84 outfile = ObjectFileName (argv[1]); |
122 else |
85 else |
123 outfile = argv[2]; |
86 outfile = argv[2]; |
124 |
87 |
125 // If we'd end up writing into an existing file, |
|
126 // ask the user if we want to overwrite it |
|
127 if (fexists (outfile)) |
|
128 { |
|
129 // Additional warning if the paths are the same |
|
130 string warning; |
|
131 #ifdef FILE_CASEINSENSITIVE |
|
132 |
|
133 if (+outfile == +string (argv[1])) |
|
134 #else |
|
135 if (outfile == argv[1]) |
|
136 #endif |
|
137 { |
|
138 warning = "\nWARNING: Output file is the same as the input file. "; |
|
139 warning += "Answering yes here will destroy the source!\n"; |
|
140 warning += "Continue nevertheless?"; |
|
141 } |
|
142 |
|
143 printf ("output file `%s` already exists! overwrite?%s (y/n) ", outfile.chars(), warning.chars()); |
|
144 |
|
145 char ans; |
|
146 fgets (&ans, 1, stdin); |
|
147 |
|
148 if (ans != 'y') |
|
149 { |
|
150 printf ("abort\n"); |
|
151 exit (1); |
|
152 } |
|
153 } |
|
154 |
|
155 // Prepare reader and writer |
88 // Prepare reader and writer |
156 botscript_parser* r = new botscript_parser; |
89 botscript_parser* parser = new botscript_parser; |
157 object_writer* w = new object_writer; |
|
158 |
90 |
159 // We're set, begin parsing :) |
91 // We're set, begin parsing :) |
160 printf ("Parsing script...\n"); |
92 print ("Parsing script...\n"); |
161 r->parse_botscript (argv[1], w); |
93 parser->parse_botscript (argv[1]); |
162 printf ("Script parsed successfully.\n"); |
94 print ("Script parsed successfully.\n"); |
163 |
95 |
164 // Parse done, print statistics and write to file |
96 // Parse done, print statistics and write to file |
165 int globalcount = g_GlobalVariables.size(); |
97 int globalcount = g_GlobalVariables.size(); |
166 int stringcount = num_strings_in_table(); |
98 int stringcount = num_strings_in_table(); |
167 int NumMarks = w->MainBuffer->count_marks(); |
|
168 int NumRefs = w->MainBuffer->count_references(); |
|
169 print ("%1 / %2 strings written\n", stringcount, g_max_stringlist_size); |
99 print ("%1 / %2 strings written\n", stringcount, g_max_stringlist_size); |
170 print ("%1 / %2 global variables\n", globalcount, g_max_global_vars); |
100 print ("%1 / %2 global variables\n", globalcount, g_max_global_vars); |
171 print ("%1 / %2 bytecode marks\n", NumMarks, MAX_MARKS); // TODO: nuke |
101 print ("%1 / %2 events\n", parser->get_num_events(), g_max_events); |
172 print ("%1 / %2 bytecode references\n", NumRefs, MAX_MARKS); // TODO: nuke |
102 print ("%1 state%s1\n", parser->get_num_states()); |
173 print ("%1 / %2 events\n", g_NumEvents, g_max_events); |
103 |
174 print ("%1 state%s1\n", g_NumStates); |
104 parser->write_to_file (outfile); |
175 |
|
176 w->write_to_file (outfile); |
|
177 |
105 |
178 // Clear out the junk |
106 // Clear out the junk |
179 delete r; |
107 delete parser; |
180 delete w; |
|
181 |
108 |
182 // Done! |
109 // Done! |
183 exit (0); |
110 exit (0); |
184 } |
111 } |
185 catch (script_error& e) |
112 catch (script_error& e) |
209 string ObjectFileName (string s) |
136 string ObjectFileName (string s) |
210 { |
137 { |
211 // Locate the extension and chop it out |
138 // Locate the extension and chop it out |
212 int extdot = s.last ("."); |
139 int extdot = s.last ("."); |
213 |
140 |
214 if (extdot >= s.len() - 4) |
141 if (extdot >= s.length() - 4) |
215 s -= (s.len() - extdot); |
142 s -= (s.length() - extdot); |
216 |
143 |
217 s += ".o"; |
144 s += ".o"; |
218 return s; |
145 return s; |
219 } |
|
220 |
|
221 // ============================================================================ |
|
222 // Is the given argument a reserved keyword? |
|
223 bool IsKeyword (string s) |
|
224 { |
|
225 for (int u = 0; u < NumKeywords(); u++) |
|
226 if (s.to_uppercase() == g_Keywords[u].to_uppercase()) |
|
227 return true; |
|
228 |
|
229 return false; |
|
230 } |
|
231 |
|
232 int NumKeywords() |
|
233 { |
|
234 return sizeof (g_Keywords) / sizeof (const char*); |
|
235 } |
146 } |
236 |
147 |
237 // ============================================================================ |
148 // ============================================================================ |
238 type_e GetTypeByName (string t) |
149 type_e GetTypeByName (string t) |
239 { |
150 { |