Wed, 12 Feb 2014 06:33:16 +0200
- added named enumerations to botc source, this should help with debugging
88 | 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 "DataBuffer.h" | |
34 | #include "Parser.h" | |
35 | #include "Lexer.h" | |
36 | #include "GitInformation.h" | |
109
6572803cd0ca
- added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
37 | #include "EnumStrings.h" |
88 | 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; | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
66 | header = Format (APPNAME " version %1", GetVersionString (true)); |
88 | 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 | |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
101 | int globalcount = parser->GetHighestVarIndex (true) + 1; |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
102 | int statelocalcount = parser->GetHighestVarIndex (false) + 1; |
88 | 103 | int stringcount = CountStringsInTable(); |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
104 | Print ("%1 / %2 strings\n", stringcount, gMaxStringlistSize); |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
105 | Print ("%1 / %2 global variable indices\n", globalcount, gMaxGlobalVars); |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
106 | Print ("%1 / %2 state variable indices\n", statelocalcount, gMaxGlobalVars); |
88 | 107 | Print ("%1 / %2 events\n", parser->GetNumEvents(), gMaxEvents); |
108 | Print ("%1 state%s1\n", parser->GetNumStates()); | |
109 | ||
110 | parser->WriteToFile (outfile); | |
111 | delete parser; | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
112 | return 0; |
88 | 113 | } |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
114 | catch (std::exception& e) |
88 | 115 | { |
116 | PrintTo (stderr, "error: %1\n", e.what()); | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
117 | return 1; |
88 | 118 | } |
119 | } | |
120 | ||
121 | // ============================================================================ | |
122 | // | |
123 | // Mutates given filename to an object filename | |
124 | // | |
125 | String MakeObjectFileName (String s) | |
126 | { | |
127 | // Locate the extension and chop it out | |
128 | int extdot = s.LastIndexOf ("."); | |
129 | ||
130 | if (extdot >= s.Length() - 4) | |
131 | s -= (s.Length() - extdot); | |
132 | ||
133 | s += ".o"; | |
134 | return s; | |
135 | } | |
136 | ||
137 | // ============================================================================ | |
138 | // | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
139 | DataType GetTypeByName (String t) |
88 | 140 | { |
141 | t = t.ToLowercase(); | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
142 | return (t == "int") ? TYPE_Int : |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
143 | (t == "str") ? TYPE_String : |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
144 | (t == "void") ? TYPE_Void : |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
145 | (t == "bool") ? TYPE_Bool : |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
146 | TYPE_Unknown; |
88 | 147 | } |
148 | ||
149 | ||
150 | // ============================================================================ | |
151 | // | |
152 | // Inverse operation - type name by value | |
153 | // | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
154 | String GetTypeName (DataType type) |
88 | 155 | { |
156 | switch (type) | |
157 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
158 | case TYPE_Int: return "int"; break; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
159 | case TYPE_String: return "str"; break; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
160 | case TYPE_Void: return "void"; break; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
161 | case TYPE_Bool: return "bool"; break; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
162 | case TYPE_Unknown: return "???"; break; |
88 | 163 | } |
164 | ||
165 | return ""; | |
166 | } | |
167 | ||
168 | // ============================================================================= | |
169 | // | |
170 | String MakeVersionString (int major, int minor, int patch) | |
171 | { | |
172 | String ver = Format ("%1.%2", major, minor); | |
173 | ||
174 | if (patch != 0) | |
175 | { | |
176 | ver += "."; | |
177 | ver += patch; | |
178 | } | |
179 | ||
180 | return ver; | |
181 | } | |
182 | ||
183 | // ============================================================================= | |
184 | // | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
185 | String GetVersionString (bool longform) |
88 | 186 | { |
187 | String tag (GIT_DESCRIPTION); | |
188 | String version = tag; | |
189 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
190 | if (longform && tag.EndsWith ("-pre")) |
88 | 191 | version += "-" + String (GIT_HASH).Mid (0, 8); |
192 | ||
193 | return version; | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
194 | } |