42 // Intepret command-line parameters: |
42 // Intepret command-line parameters: |
43 // -l: list commands |
43 // -l: list commands |
44 // I guess there should be a better way to do this. |
44 // I guess there should be a better way to do this. |
45 if (argc == 2 && String (argv[1]) == "-l") |
45 if (argc == 2 && String (argv[1]) == "-l") |
46 { |
46 { |
47 Print ("Begin list of commands:\n"); |
47 print ("Begin list of commands:\n"); |
48 Print ("------------------------------------------------------\n"); |
48 print ("------------------------------------------------------\n"); |
49 |
49 |
50 BotscriptParser parser; |
50 BotscriptParser parser; |
51 parser.SetReadOnly (true); |
51 parser.setReadOnly (true); |
52 parser.ParseBotscript ("botc_defs.bts"); |
52 parser.parseBotscript ("botc_defs.bts"); |
53 |
53 |
54 for (CommandInfo* comm : GetCommands()) |
54 for (CommandInfo* comm : getCommands()) |
55 Print ("%1\n", comm->GetSignature()); |
55 print ("%1\n", comm->signature()); |
56 |
56 |
57 Print ("------------------------------------------------------\n"); |
57 print ("------------------------------------------------------\n"); |
58 Print ("End of command list\n"); |
58 print ("End of command list\n"); |
59 exit (0); |
59 exit (0); |
60 } |
60 } |
61 |
61 |
62 // Print header |
62 // Print header |
63 String header; |
63 String header; |
64 String headerline; |
64 String headerline; |
65 header = Format (APPNAME " version %1", GetVersionString (true)); |
65 header = format (APPNAME " version %1", versionString (true)); |
66 |
66 |
67 #ifdef DEBUG |
67 #ifdef DEBUG |
68 header += " (debug build)"; |
68 header += " (debug build)"; |
69 #endif |
69 #endif |
70 |
70 |
71 for (int i = 0; i < header.Length() / 2; ++i) |
71 for (int i = 0; i < header.length() / 2; ++i) |
72 headerline += "-="; |
72 headerline += "-="; |
73 |
73 |
74 headerline += '-'; |
74 headerline += '-'; |
75 Print ("%2\n\n%1\n\n%2\n\n", header, headerline); |
75 print ("%2\n\n%1\n\n%2\n\n", header, headerline); |
76 |
76 |
77 if (argc < 2) |
77 if (argc < 2) |
78 { |
78 { |
79 fprintf (stderr, "usage: %s <infile> [outfile] # compiles botscript\n", argv[0]); |
79 fprintf (stderr, "usage: %s <infile> [outfile] # compiles botscript\n", argv[0]); |
80 fprintf (stderr, " %s -l # lists commands\n", argv[0]); |
80 fprintf (stderr, " %s -l # lists commands\n", argv[0]); |
82 } |
82 } |
83 |
83 |
84 String outfile; |
84 String outfile; |
85 |
85 |
86 if (argc < 3) |
86 if (argc < 3) |
87 outfile = MakeObjectFileName (argv[1]); |
87 outfile = makeObjectFileName (argv[1]); |
88 else |
88 else |
89 outfile = argv[2]; |
89 outfile = argv[2]; |
90 |
90 |
91 // Prepare reader and writer |
91 // Prepare reader and writer |
92 BotscriptParser* parser = new BotscriptParser; |
92 BotscriptParser* parser = new BotscriptParser; |
93 |
93 |
94 // We're set, begin parsing :) |
94 // We're set, begin parsing :) |
95 Print ("Parsing script...\n"); |
95 print ("Parsing script...\n"); |
96 parser->ParseBotscript (argv[1]); |
96 parser->parseBotscript (argv[1]); |
97 Print ("Script parsed successfully.\n"); |
97 print ("Script parsed successfully.\n"); |
98 |
98 |
99 // Parse done, print statistics and write to file |
99 // Parse done, print statistics and write to file |
100 int globalcount = parser->GetHighestVarIndex (true) + 1; |
100 int globalcount = parser->getHighestVarIndex (true) + 1; |
101 int statelocalcount = parser->GetHighestVarIndex (false) + 1; |
101 int statelocalcount = parser->getHighestVarIndex (false) + 1; |
102 int stringcount = CountStringsInTable(); |
102 int stringcount = countStringsInTable(); |
103 Print ("%1 / %2 strings\n", stringcount, gMaxStringlistSize); |
103 print ("%1 / %2 strings\n", stringcount, gMaxStringlistSize); |
104 Print ("%1 / %2 global variable indices\n", globalcount, gMaxGlobalVars); |
104 print ("%1 / %2 global variable indices\n", globalcount, gMaxGlobalVars); |
105 Print ("%1 / %2 state variable indices\n", statelocalcount, gMaxGlobalVars); |
105 print ("%1 / %2 state variable indices\n", statelocalcount, gMaxGlobalVars); |
106 Print ("%1 / %2 events\n", parser->GetNumEvents(), gMaxEvents); |
106 print ("%1 / %2 events\n", parser->numEvents(), gMaxEvents); |
107 Print ("%1 state%s1\n", parser->GetNumStates()); |
107 print ("%1 state%s1\n", parser->numStates()); |
108 |
108 |
109 parser->WriteToFile (outfile); |
109 parser->writeToFile (outfile); |
110 delete parser; |
110 delete parser; |
111 return 0; |
111 return 0; |
112 } |
112 } |
113 catch (std::exception& e) |
113 catch (std::exception& e) |
114 { |
114 { |
119 |
119 |
120 // ============================================================================ |
120 // ============================================================================ |
121 // |
121 // |
122 // Mutates given filename to an object filename |
122 // Mutates given filename to an object filename |
123 // |
123 // |
124 String MakeObjectFileName (String s) |
124 String makeObjectFileName (String s) |
125 { |
125 { |
126 // Locate the extension and chop it out |
126 // Locate the extension and chop it out |
127 int extdot = s.LastIndexOf ("."); |
127 int extdot = s.lastIndexOf ("."); |
128 |
128 |
129 if (extdot >= s.Length() - 4) |
129 if (extdot >= s.length() - 4) |
130 s -= (s.Length() - extdot); |
130 s -= (s.length() - extdot); |
131 |
131 |
132 s += ".o"; |
132 s += ".o"; |
133 return s; |
133 return s; |
134 } |
134 } |
135 |
135 |
136 // ============================================================================ |
136 // ============================================================================ |
137 // |
137 // |
138 DataType GetTypeByName (String t) |
138 DataType getTypeByName (String token) |
139 { |
139 { |
140 t = t.ToLowercase(); |
140 token = token.toLowercase(); |
141 return (t == "int") ? TYPE_Int : |
141 return (token == "int") ? TYPE_Int : |
142 (t == "str") ? TYPE_String : |
142 (token == "str") ? TYPE_String : |
143 (t == "void") ? TYPE_Void : |
143 (token == "void") ? TYPE_Void : |
144 (t == "bool") ? TYPE_Bool : |
144 (token == "bool") ? TYPE_Bool : |
145 TYPE_Unknown; |
145 TYPE_Unknown; |
146 } |
146 } |
147 |
147 |
148 |
148 |
149 // ============================================================================ |
149 // ============================================================================ |
150 // |
150 // |
151 // Inverse operation - type name by value |
151 // Inverse operation - type name by value |
152 // |
152 // |
153 String DataTypeName (DataType type) |
153 String dataTypeName (DataType type) |
154 { |
154 { |
155 switch (type) |
155 switch (type) |
156 { |
156 { |
157 case TYPE_Int: return "int"; break; |
157 case TYPE_Int: return "int"; break; |
158 case TYPE_String: return "str"; break; |
158 case TYPE_String: return "str"; break; |
164 return ""; |
164 return ""; |
165 } |
165 } |
166 |
166 |
167 // ============================================================================= |
167 // ============================================================================= |
168 // |
168 // |
169 String MakeVersionString (int major, int minor, int patch) |
169 String makeVersionString (int major, int minor, int patch) |
170 { |
170 { |
171 String ver = Format ("%1.%2", major, minor); |
171 String ver = format ("%1.%2", major, minor); |
172 |
172 |
173 if (patch != 0) |
173 if (patch != 0) |
174 { |
174 { |
175 ver += "."; |
175 ver += "."; |
176 ver += patch; |
176 ver += patch; |
179 return ver; |
179 return ver; |
180 } |
180 } |
181 |
181 |
182 // ============================================================================= |
182 // ============================================================================= |
183 // |
183 // |
184 String GetVersionString (bool longform) |
184 String versionString (bool longform) |
185 { |
185 { |
186 String tag (GIT_DESCRIPTION); |
186 String tag (GIT_DESCRIPTION); |
187 String version = tag; |
187 String version = tag; |
188 |
188 |
189 if (longform && tag.EndsWith ("-pre")) |
189 if (longform && tag.endsWith ("-pre")) |
190 version += "-" + String (GIT_HASH).Mid (0, 8); |
190 version += "-" + String (GIT_HASH).mid (0, 8); |
191 |
191 |
192 return version; |
192 return version; |
193 } |
193 } |