| |
1 /* |
| |
2 Copyright 2012-2014 Santeri Piippo |
| |
3 All rights reserved. |
| |
4 |
| |
5 Redistribution and use in source and binary forms, with or without |
| |
6 modification, are permitted provided that the following conditions |
| |
7 are met: |
| |
8 |
| |
9 1. Redistributions of source code must retain the above copyright |
| |
10 notice, this list of conditions and the following disclaimer. |
| |
11 2. Redistributions in binary form must reproduce the above copyright |
| |
12 notice, this list of conditions and the following disclaimer in the |
| |
13 documentation and/or other materials provided with the distribution. |
| |
14 3. The name of the author may not be used to endorse or promote products |
| |
15 derived from this software without specific prior written permission. |
| |
16 |
| |
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| |
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| |
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| |
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| |
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| |
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| |
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| |
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| |
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. |
| |
27 */ |
| |
28 |
| |
29 #include "Main.h" |
| |
30 #include "Events.h" |
| |
31 #include "Commands.h" |
| |
32 #include "StringTable.h" |
| |
33 #include "Variables.h" |
| |
34 #include "DataBuffer.h" |
| |
35 #include "Parser.h" |
| |
36 #include "Lexer.h" |
| |
37 #include "GitInformation.h" |
| |
38 |
| |
39 int main (int argc, char** argv) |
| |
40 { |
| |
41 try |
| |
42 { |
| |
43 // Intepret command-line parameters: |
| |
44 // -l: list commands |
| |
45 // I guess there should be a better way to do this. |
| |
46 if (argc == 2 && String (argv[1]) == "-l") |
| |
47 { |
| |
48 Print ("Begin list of commands:\n"); |
| |
49 Print ("------------------------------------------------------\n"); |
| |
50 |
| |
51 BotscriptParser parser; |
| |
52 parser.SetReadOnly (true); |
| |
53 parser.ParseBotscript ("botc_defs.bts"); |
| |
54 |
| |
55 for (CommandInfo* comm : GetCommands()) |
| |
56 Print ("%1\n", comm->GetSignature()); |
| |
57 |
| |
58 Print ("------------------------------------------------------\n"); |
| |
59 Print ("End of command list\n"); |
| |
60 exit (0); |
| |
61 } |
| |
62 |
| |
63 // Print header |
| |
64 String header; |
| |
65 String headerline; |
| |
66 header = Format (APPNAME " version %1", GetVersionString (ELongForm)); |
| |
67 |
| |
68 #ifdef DEBUG |
| |
69 header += " (debug build)"; |
| |
70 #endif |
| |
71 |
| |
72 for (int i = 0; i < header.Length() / 2; ++i) |
| |
73 headerline += "-="; |
| |
74 |
| |
75 headerline += '-'; |
| |
76 Print ("%2\n\n%1\n\n%2\n\n", header, headerline); |
| |
77 |
| |
78 if (argc < 2) |
| |
79 { |
| |
80 fprintf (stderr, "usage: %s <infile> [outfile] # compiles botscript\n", argv[0]); |
| |
81 fprintf (stderr, " %s -l # lists commands\n", argv[0]); |
| |
82 exit (1); |
| |
83 } |
| |
84 |
| |
85 String outfile; |
| |
86 |
| |
87 if (argc < 3) |
| |
88 outfile = MakeObjectFileName (argv[1]); |
| |
89 else |
| |
90 outfile = argv[2]; |
| |
91 |
| |
92 // Prepare reader and writer |
| |
93 BotscriptParser* parser = new BotscriptParser; |
| |
94 |
| |
95 // We're set, begin parsing :) |
| |
96 Print ("Parsing script...\n"); |
| |
97 parser->ParseBotscript (argv[1]); |
| |
98 Print ("Script parsed successfully.\n"); |
| |
99 |
| |
100 // Parse done, print statistics and write to file |
| |
101 int globalcount = g_GlobalVariables.Size(); |
| |
102 int stringcount = CountStringsInTable(); |
| |
103 Print ("%1 / %2 strings written\n", stringcount, gMaxStringlistSize); |
| |
104 Print ("%1 / %2 global variables\n", globalcount, gMaxGlobalVars); |
| |
105 Print ("%1 / %2 events\n", parser->GetNumEvents(), gMaxEvents); |
| |
106 Print ("%1 state%s1\n", parser->GetNumStates()); |
| |
107 |
| |
108 parser->WriteToFile (outfile); |
| |
109 |
| |
110 // Clear out the junk |
| |
111 delete parser; |
| |
112 |
| |
113 // Done! |
| |
114 exit (0); |
| |
115 } |
| |
116 catch (ScriptError& e) |
| |
117 { |
| |
118 PrintTo (stderr, "error: %1\n", e.what()); |
| |
119 } |
| |
120 } |
| |
121 |
| |
122 // ============================================================================ |
| |
123 // |
| |
124 // Mutates given filename to an object filename |
| |
125 // |
| |
126 String MakeObjectFileName (String s) |
| |
127 { |
| |
128 // Locate the extension and chop it out |
| |
129 int extdot = s.LastIndexOf ("."); |
| |
130 |
| |
131 if (extdot >= s.Length() - 4) |
| |
132 s -= (s.Length() - extdot); |
| |
133 |
| |
134 s += ".o"; |
| |
135 return s; |
| |
136 } |
| |
137 |
| |
138 // ============================================================================ |
| |
139 // |
| |
140 EType GetTypeByName (String t) |
| |
141 { |
| |
142 t = t.ToLowercase(); |
| |
143 return (t == "int") ? EIntType : |
| |
144 (t == "str") ? EStringType : |
| |
145 (t == "void") ? EVoidType : |
| |
146 (t == "bool") ? EBoolType : |
| |
147 EUnknownType; |
| |
148 } |
| |
149 |
| |
150 |
| |
151 // ============================================================================ |
| |
152 // |
| |
153 // Inverse operation - type name by value |
| |
154 // |
| |
155 String GetTypeName (EType type) |
| |
156 { |
| |
157 switch (type) |
| |
158 { |
| |
159 case EIntType: return "int"; break; |
| |
160 case EStringType: return "str"; break; |
| |
161 case EVoidType: return "void"; break; |
| |
162 case EBoolType: return "bool"; break; |
| |
163 case EUnknownType: return "???"; break; |
| |
164 } |
| |
165 |
| |
166 return ""; |
| |
167 } |
| |
168 |
| |
169 // ============================================================================= |
| |
170 // |
| |
171 String MakeVersionString (int major, int minor, int patch) |
| |
172 { |
| |
173 String ver = Format ("%1.%2", major, minor); |
| |
174 |
| |
175 if (patch != 0) |
| |
176 { |
| |
177 ver += "."; |
| |
178 ver += patch; |
| |
179 } |
| |
180 |
| |
181 return ver; |
| |
182 } |
| |
183 |
| |
184 // ============================================================================= |
| |
185 // |
| |
186 String GetVersionString (EFormLength len) |
| |
187 { |
| |
188 String tag (GIT_DESCRIPTION); |
| |
189 String version = tag; |
| |
190 |
| |
191 if (len == ELongForm && tag.EndsWith ("-pre")) |
| |
192 version += "-" + String (GIT_HASH).Mid (0, 8); |
| |
193 |
| |
194 return version; |
| |
195 } |