Sun, 09 Feb 2014 21:27:55 +0200
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
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" | |
37 | ||
38 | int main (int argc, char** argv) | |
39 | { | |
40 | try | |
41 | { | |
42 | // Intepret command-line parameters: | |
43 | // -l: list commands | |
44 | // I guess there should be a better way to do this. | |
45 | if (argc == 2 && String (argv[1]) == "-l") | |
46 | { | |
47 | Print ("Begin list of commands:\n"); | |
48 | Print ("------------------------------------------------------\n"); | |
49 | ||
50 | BotscriptParser parser; | |
51 | parser.SetReadOnly (true); | |
52 | parser.ParseBotscript ("botc_defs.bts"); | |
53 | ||
54 | for (CommandInfo* comm : GetCommands()) | |
55 | Print ("%1\n", comm->GetSignature()); | |
56 | ||
57 | Print ("------------------------------------------------------\n"); | |
58 | Print ("End of command list\n"); | |
59 | exit (0); | |
60 | } | |
61 | ||
62 | // Print header | |
63 | String header; | |
64 | String headerline; | |
65 | header = Format (APPNAME " version %1", GetVersionString (ELongForm)); | |
66 | ||
67 | #ifdef DEBUG | |
68 | header += " (debug build)"; | |
69 | #endif | |
70 | ||
71 | for (int i = 0; i < header.Length() / 2; ++i) | |
72 | headerline += "-="; | |
73 | ||
74 | headerline += '-'; | |
75 | Print ("%2\n\n%1\n\n%2\n\n", header, headerline); | |
76 | ||
77 | if (argc < 2) | |
78 | { | |
79 | fprintf (stderr, "usage: %s <infile> [outfile] # compiles botscript\n", argv[0]); | |
80 | fprintf (stderr, " %s -l # lists commands\n", argv[0]); | |
81 | exit (1); | |
82 | } | |
83 | ||
84 | String outfile; | |
85 | ||
86 | if (argc < 3) | |
87 | outfile = MakeObjectFileName (argv[1]); | |
88 | else | |
89 | outfile = argv[2]; | |
90 | ||
91 | // Prepare reader and writer | |
92 | BotscriptParser* parser = new BotscriptParser; | |
93 | ||
94 | // We're set, begin parsing :) | |
95 | Print ("Parsing script...\n"); | |
96 | parser->ParseBotscript (argv[1]); | |
97 | Print ("Script parsed successfully.\n"); | |
98 | ||
99 | // Parse done, print statistics and write to file | |
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
|
100 | int globalcount = parser->GetScope (0).globalVariables.Size(); |
88 | 101 | 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
|
102 | 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
|
103 | Print ("%1 / %2 global variable indices\n", globalcount, gMaxGlobalVars); |
88 | 104 | Print ("%1 / %2 events\n", parser->GetNumEvents(), gMaxEvents); |
105 | Print ("%1 state%s1\n", parser->GetNumStates()); | |
106 | ||
107 | parser->WriteToFile (outfile); | |
108 | 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
|
109 | return 0; |
88 | 110 | } |
111 | catch (ScriptError& e) | |
112 | { | |
113 | 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
|
114 | return 1; |
88 | 115 | } |
116 | } | |
117 | ||
118 | // ============================================================================ | |
119 | // | |
120 | // Mutates given filename to an object filename | |
121 | // | |
122 | String MakeObjectFileName (String s) | |
123 | { | |
124 | // Locate the extension and chop it out | |
125 | int extdot = s.LastIndexOf ("."); | |
126 | ||
127 | if (extdot >= s.Length() - 4) | |
128 | s -= (s.Length() - extdot); | |
129 | ||
130 | s += ".o"; | |
131 | return s; | |
132 | } | |
133 | ||
134 | // ============================================================================ | |
135 | // | |
136 | EType GetTypeByName (String t) | |
137 | { | |
138 | t = t.ToLowercase(); | |
139 | return (t == "int") ? EIntType : | |
140 | (t == "str") ? EStringType : | |
141 | (t == "void") ? EVoidType : | |
142 | (t == "bool") ? EBoolType : | |
143 | EUnknownType; | |
144 | } | |
145 | ||
146 | ||
147 | // ============================================================================ | |
148 | // | |
149 | // Inverse operation - type name by value | |
150 | // | |
151 | String GetTypeName (EType type) | |
152 | { | |
153 | switch (type) | |
154 | { | |
155 | case EIntType: return "int"; break; | |
156 | case EStringType: return "str"; break; | |
157 | case EVoidType: return "void"; break; | |
158 | case EBoolType: return "bool"; break; | |
159 | case EUnknownType: return "???"; break; | |
160 | } | |
161 | ||
162 | return ""; | |
163 | } | |
164 | ||
165 | // ============================================================================= | |
166 | // | |
167 | String MakeVersionString (int major, int minor, int patch) | |
168 | { | |
169 | String ver = Format ("%1.%2", major, minor); | |
170 | ||
171 | if (patch != 0) | |
172 | { | |
173 | ver += "."; | |
174 | ver += patch; | |
175 | } | |
176 | ||
177 | return ver; | |
178 | } | |
179 | ||
180 | // ============================================================================= | |
181 | // | |
182 | String GetVersionString (EFormLength len) | |
183 | { | |
184 | String tag (GIT_DESCRIPTION); | |
185 | String version = tag; | |
186 | ||
187 | if (len == ELongForm && tag.EndsWith ("-pre")) | |
188 | version += "-" + String (GIT_HASH).Mid (0, 8); | |
189 | ||
190 | return version; | |
191 | } |