src/main.cc

changeset 88
5def6ff8b466
parent 87
8f65914e7046
child 89
029a330a9bef
equal deleted inserted replaced
87:8f65914e7046 88:5def6ff8b466
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 "data_buffer.h"
35 #include "parser.h"
36 #include "lexer.h"
37 #include "gitinfo.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 && !strcmp (argv[1], "-l"))
47 {
48 printf ("Begin list of commands:\n");
49 printf ("------------------------------------------------------\n");
50
51 for (command_info* comm : get_commands())
52 print ("%1\n", get_command_signature (comm));
53
54 printf ("------------------------------------------------------\n");
55 printf ("End of command list\n");
56 exit (0);
57 }
58
59 // Print header
60 string header;
61 string headerline;
62 header = format (APPNAME " version %1", get_version_string (e_long_form));
63
64 #ifdef DEBUG
65 header += " (debug build)";
66 #endif
67
68 for (int i = 0; i < header.length() / 2; ++i)
69 headerline += "-=";
70
71 headerline += '-';
72 print ("%2\n\n%1\n\n%2\n\n", header, headerline);
73
74 if (argc < 2)
75 {
76 fprintf (stderr, "usage: %s <infile> [outfile] # compiles botscript\n", argv[0]);
77 fprintf (stderr, " %s -l # lists commands\n", argv[0]);
78 exit (1);
79 }
80
81 string outfile;
82
83 if (argc < 3)
84 outfile = make_object_file_name (argv[1]);
85 else
86 outfile = argv[2];
87
88 // Prepare reader and writer
89 botscript_parser* parser = new botscript_parser;
90
91 // We're set, begin parsing :)
92 print ("Parsing script...\n");
93 parser->parse_botscript (argv[1]);
94 print ("Script parsed successfully.\n");
95
96 // Parse done, print statistics and write to file
97 int globalcount = g_GlobalVariables.size();
98 int stringcount = num_strings_in_table();
99 print ("%1 / %2 strings written\n", stringcount, g_max_stringlist_size);
100 print ("%1 / %2 global variables\n", globalcount, g_max_global_vars);
101 print ("%1 / %2 events\n", parser->get_num_events(), g_max_events);
102 print ("%1 state%s1\n", parser->get_num_states());
103
104 parser->write_to_file (outfile);
105
106 // Clear out the junk
107 delete parser;
108
109 // Done!
110 exit (0);
111 }
112 catch (script_error& e)
113 {
114 fprint (stderr, "error: %1\n", e.what());
115 }
116 }
117
118 // ============================================================================
119 //
120 // Mutates given filename to an object filename
121 //
122 string make_object_file_name (string s)
123 {
124 // Locate the extension and chop it out
125 int extdot = s.last (".");
126
127 if (extdot >= s.length() - 4)
128 s -= (s.length() - extdot);
129
130 s += ".o";
131 return s;
132 }
133
134 // ============================================================================
135 //
136 type_e get_type_by_name (string t)
137 {
138 t = t.to_lowercase();
139 return (t == "int") ? e_int_type :
140 (t == "str") ? e_string_type :
141 (t == "void") ? e_void_type :
142 (t == "bool") ? e_bool_type :
143 e_unknown_type;
144 }
145
146
147 // ============================================================================
148 //
149 // Inverse operation - type name by value
150 //
151 string get_type_name (type_e type)
152 {
153 switch (type)
154 {
155 case e_int_type: return "int"; break;
156 case e_string_type: return "str"; break;
157 case e_void_type: return "void"; break;
158 case e_bool_type: return "bool"; break;
159 case e_unknown_type: return "???"; break;
160 }
161
162 return "";
163 }
164
165 // =============================================================================
166 //
167 string make_version_string (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 get_version_string (form_length_e len)
183 {
184 string tag (GIT_DESCRIPTION);
185 string version = tag;
186
187 if (tag.ends_with ("-pre") && len == e_long_form)
188 version += "-" + string (GIT_HASH).mid (0, 8);
189
190 return version;
191 }

mercurial