commands.cxx

changeset 36
a8838b5f1213
parent 34
0a9a5902beaa
child 38
e4bbd540663b
equal deleted inserted replaced
35:3d3f6ed40171 36:a8838b5f1213
45 #include "common.h" 45 #include "common.h"
46 #include "scriptreader.h" 46 #include "scriptreader.h"
47 #include "str.h" 47 #include "str.h"
48 #include "commands.h" 48 #include "commands.h"
49 49
50 // ============================================================================
51 // Reads command definitions from commands.def and stores them to memory.
50 void ReadCommands () { 52 void ReadCommands () {
51 ScriptReader* r = new ScriptReader ("commands.def"); 53 ScriptReader* r = new ScriptReader ("commands.def");
52 g_CommDef = NULL; 54 g_CommDef = NULL;
53 CommandDef* curdef = g_CommDef; 55 CommandDef* curdef = g_CommDef;
54 unsigned int numCommDefs = 0; 56 unsigned int numCommDefs = 0;
64 r->MustNext (":"); 66 r->MustNext (":");
65 67
66 // Name 68 // Name
67 r->MustNext (); 69 r->MustNext ();
68 comm->name = r->token; 70 comm->name = r->token;
71 if (IsKeyword (comm->name))
72 r->ParserError ("command name `%s` conflicts with keyword", comm->name.chars());
69 73
70 r->MustNext (":"); 74 r->MustNext (":");
71 75
72 // Return value 76 // Return value
73 r->MustNext (); 77 r->MustNext ();
140 curdef = comm; 144 curdef = comm;
141 } 145 }
142 numCommDefs++; 146 numCommDefs++;
143 } 147 }
144 148
149 if (!numCommDefs)
150 r->ParserError ("no commands defined!\n");
151
145 r->CloseFile (); 152 r->CloseFile ();
146 delete r; 153 delete r;
147
148 if (!numCommDefs)
149 error ("no commands defined!\n");
150 printf ("%d command definitions read.\n", numCommDefs); 154 printf ("%d command definitions read.\n", numCommDefs);
151 } 155 }
152 156
157 // ============================================================================
158 // Get command type by name
153 int GetCommandType (str t) { 159 int GetCommandType (str t) {
154 // "float" is for now just int. 160 // "float" is for now just int.
155 // TODO: find out how BotScript floats work 161 // TODO: find out how BotScript floats work
156 // (are they real floats or fixed? how are they 162 // (are they real floats or fixed? how are they
157 // stored?) and add proper floating point support. 163 // stored?) and add proper floating point number support.
158 // NOTE: Also, shouldn't use RETURNVAL for data types..
159 t = t.tolower(); 164 t = t.tolower();
160 return !t.compare ("int") ? TYPE_INT : 165 return !t.compare ("int") ? TYPE_INT :
161 !t.compare ("float") ? TYPE_INT : 166 !t.compare ("float") ? TYPE_INT :
162 !t.compare ("str") ? TYPE_STRING : 167 !t.compare ("str") ? TYPE_STRING :
163 !t.compare ("void") ? TYPE_VOID : 168 !t.compare ("void") ? TYPE_VOID :
164 !t.compare ("bool") ? TYPE_INT : -1; 169 !t.compare ("bool") ? TYPE_INT : -1;
165 } 170 }
166 171
167 // Inverse operation 172 // ============================================================================
173 // Inverse operation - type name by value
168 str GetReturnTypeName (int r) { 174 str GetReturnTypeName (int r) {
169 switch (r) { 175 switch (r) {
170 case RETURNVAL_INT: return "int"; break; 176 case RETURNVAL_INT: return "int"; break;
171 case RETURNVAL_STRING: return "str"; break; 177 case RETURNVAL_STRING: return "str"; break;
172 case RETURNVAL_VOID: return "void"; break; 178 case RETURNVAL_VOID: return "void"; break;
174 } 180 }
175 181
176 return ""; 182 return "";
177 } 183 }
178 184
185 // ============================================================================
186 // Finds a command by name
179 CommandDef* FindCommand (str fname) { 187 CommandDef* FindCommand (str fname) {
180 CommandDef* comm; 188 CommandDef* comm;
181 ITERATE_COMMANDS (comm) { 189 ITERATE_COMMANDS (comm) {
182 if (!fname.icompare (comm->name)) 190 if (!fname.icompare (comm->name))
183 return comm; 191 return comm;
184 } 192 }
185 193
186 return NULL; 194 return NULL;
187 } 195 }
188 196
197 // ============================================================================
198 // Returns the prototype of the command
189 str GetCommandPrototype (CommandDef* comm) { 199 str GetCommandPrototype (CommandDef* comm) {
190 str text; 200 str text;
191 text += GetReturnTypeName (comm->returnvalue); 201 text += GetReturnTypeName (comm->returnvalue);
192 text += ' '; 202 text += ' ';
193 text += comm->name; 203 text += comm->name;

mercurial