| 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->signature()); |
|
| 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", versionString (true)); |
|
| 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 |
|
| 100 int globalcount = parser->getHighestVarIndex (true) + 1; |
|
| 101 int statelocalcount = parser->getHighestVarIndex (false) + 1; |
|
| 102 int stringcount = countStringsInTable(); |
|
| 103 print ("%1 / %2 strings\n", stringcount, gMaxStringlistSize); |
|
| 104 print ("%1 / %2 global variable indices\n", globalcount, gMaxGlobalVars); |
|
| 105 print ("%1 / %2 state variable indices\n", statelocalcount, gMaxGlobalVars); |
|
| 106 print ("%1 / %2 events\n", parser->numEvents(), gMaxEvents); |
|
| 107 print ("%1 state%s1\n", parser->numStates()); |
|
| 108 |
|
| 109 parser->writeToFile (outfile); |
|
| 110 delete parser; |
|
| 111 return 0; |
|
| 112 } |
|
| 113 catch (std::exception& e) |
|
| 114 { |
|
| 115 fprintf (stderr, "error: %s\n", e.what()); |
|
| 116 return 1; |
|
| 117 } |
|
| 118 } |
|
| 119 |
|
| 120 // ============================================================================ |
|
| 121 // |
|
| 122 // Mutates given filename to an object filename |
|
| 123 // |
|
| 124 String makeObjectFileName (String s) |
|
| 125 { |
|
| 126 // Locate the extension and chop it out |
|
| 127 int extdot = s.lastIndexOf ("."); |
|
| 128 |
|
| 129 if (extdot >= s.length() - 4) |
|
| 130 s -= (s.length() - extdot); |
|
| 131 |
|
| 132 s += ".o"; |
|
| 133 return s; |
|
| 134 } |
|
| 135 |
|
| 136 // ============================================================================ |
|
| 137 // |
|
| 138 DataType getTypeByName (String token) |
|
| 139 { |
|
| 140 token = token.toLowercase(); |
|
| 141 return (token == "int") ? TYPE_Int : |
|
| 142 (token == "str") ? TYPE_String : |
|
| 143 (token == "void") ? TYPE_Void : |
|
| 144 (token == "bool") ? TYPE_Bool : |
|
| 145 TYPE_Unknown; |
|
| 146 } |
|
| 147 |
|
| 148 |
|
| 149 // ============================================================================ |
|
| 150 // |
|
| 151 // Inverse operation - type name by value |
|
| 152 // |
|
| 153 String dataTypeName (DataType type) |
|
| 154 { |
|
| 155 switch (type) |
|
| 156 { |
|
| 157 case TYPE_Int: return "int"; break; |
|
| 158 case TYPE_String: return "str"; break; |
|
| 159 case TYPE_Void: return "void"; break; |
|
| 160 case TYPE_Bool: return "bool"; break; |
|
| 161 case TYPE_Unknown: return "???"; break; |
|
| 162 } |
|
| 163 |
|
| 164 return ""; |
|
| 165 } |
|
| 166 |
|
| 167 // ============================================================================= |
|
| 168 // |
|
| 169 String makeVersionString (int major, int minor, int patch) |
|
| 170 { |
|
| 171 String ver = format ("%1.%2", major, minor); |
|
| 172 |
|
| 173 if (patch != 0) |
|
| 174 { |
|
| 175 ver += "."; |
|
| 176 ver += patch; |
|
| 177 } |
|
| 178 |
|
| 179 return ver; |
|
| 180 } |
|
| 181 |
|
| 182 // ============================================================================= |
|
| 183 // |
|
| 184 String versionString (bool longform) |
|
| 185 { |
|
| 186 String tag (GIT_DESCRIPTION); |
|
| 187 String version = tag; |
|
| 188 |
|
| 189 if (longform && tag.endsWith ("-pre")) |
|
| 190 version += "-" + String (GIT_HASH).mid (0, 8); |
|
| 191 |
|
| 192 return version; |
|
| 193 } |
|