commands.cxx

changeset 34
0a9a5902beaa
parent 33
fd35f6cb5f28
child 36
a8838b5f1213
equal deleted inserted replaced
33:fd35f6cb5f28 34:0a9a5902beaa
49 49
50 void ReadCommands () { 50 void ReadCommands () {
51 ScriptReader* r = new ScriptReader ("commands.def"); 51 ScriptReader* r = new ScriptReader ("commands.def");
52 g_CommDef = NULL; 52 g_CommDef = NULL;
53 CommandDef* curdef = g_CommDef; 53 CommandDef* curdef = g_CommDef;
54 unsigned int numCommDefs = 0; 54 unsigned int numCommDefs = 0;
55 55
56 while (r->PeekNext().len()) { 56 while (r->PeekNext().len()) {
57 CommandDef* comm = new CommandDef; 57 CommandDef* comm = new CommandDef;
58 comm->next = NULL; 58 comm->next = NULL;
59 59
69 69
70 r->MustNext (":"); 70 r->MustNext (":");
71 71
72 // Return value 72 // Return value
73 r->MustNext (); 73 r->MustNext ();
74 comm->returnvalue = GetReturnTypeByString (r->token); 74 comm->returnvalue = GetCommandType (r->token);
75 if (comm->returnvalue == -1) 75 if (comm->returnvalue == -1)
76 r->ParserError ("bad return value type `%s`", r->token.chars()); 76 r->ParserError ("bad return value type `%s`", r->token.chars());
77 77
78 r->MustNext (":"); 78 r->MustNext (":");
79 79
94 int curarg = 0; 94 int curarg = 0;
95 while (curarg < comm->maxargs) { 95 while (curarg < comm->maxargs) {
96 r->MustNext (":"); 96 r->MustNext (":");
97 r->MustNext (); 97 r->MustNext ();
98 98
99 int type = GetReturnTypeByString (r->token); 99 int type = GetCommandType (r->token);
100 if (type == -1) 100 if (type == -1)
101 r->ParserError ("bad argument %d type `%s`", curarg, r->token.chars()); 101 r->ParserError ("bad argument %d type `%s`", curarg, r->token.chars());
102 if (type == RETURNVAL_VOID) 102 if (type == RETURNVAL_VOID)
103 r->ParserError ("void is not a valid argument type!"); 103 r->ParserError ("void is not a valid argument type!");
104 comm->argtypes[curarg] = type; 104 comm->argtypes[curarg] = type;
107 r->MustNext (); 107 r->MustNext ();
108 108
109 // - 1 because of terminating null character 109 // - 1 because of terminating null character
110 if (r->token.len() > MAX_ARGNAMELEN - 1) 110 if (r->token.len() > MAX_ARGNAMELEN - 1)
111 r->ParserWarning ("argument name is too long (%d, max is %d)", 111 r->ParserWarning ("argument name is too long (%d, max is %d)",
112 r->token.len(), MAX_ARGNAMELEN-1); 112 r->token.len(), MAX_ARGNAMELEN - 1);
113 113
114 strncpy (comm->argnames[curarg], r->token.chars(), MAX_ARGNAMELEN); 114 strncpy (comm->argnames[curarg], r->token.chars(), MAX_ARGNAMELEN);
115 comm->argnames[curarg][MAX_ARGNAMELEN-1] = 0; 115 comm->argnames[curarg][MAX_ARGNAMELEN-1] = 0;
116 116
117 // If this is an optional parameter, we need the default value. 117 // If this is an optional parameter, we need the default value.
144 144
145 r->CloseFile (); 145 r->CloseFile ();
146 delete r; 146 delete r;
147 147
148 if (!numCommDefs) 148 if (!numCommDefs)
149 error ("error: no commands defined!\n"); 149 error ("no commands defined!\n");
150 printf ("%d command definitions read.\n", numCommDefs); 150 printf ("%d command definitions read.\n", numCommDefs);
151 } 151 }
152 152
153 // urgh long name 153 int GetCommandType (str t) {
154 int GetReturnTypeByString (str t) {
155 // "float" is for now just int. 154 // "float" is for now just int.
156 // TODO: find out how BotScript floats work 155 // TODO: find out how BotScript floats work
157 // (are they real floats or fixed? how are they 156 // (are they real floats or fixed? how are they
158 // stored?) and add proper floating point support. 157 // stored?) and add proper floating point support.
159 // NOTE: Also, shouldn't use RETURNVAL for data types.. 158 // NOTE: Also, shouldn't use RETURNVAL for data types..
160 t = t.tolower(); 159 t = t.tolower();
161 return !t.compare ("int") ? RETURNVAL_INT : 160 return !t.compare ("int") ? TYPE_INT :
162 !t.compare ("float") ? RETURNVAL_INT : 161 !t.compare ("float") ? TYPE_INT :
163 !t.compare ("str") ? RETURNVAL_STRING : 162 !t.compare ("str") ? TYPE_STRING :
164 !t.compare ("void") ? RETURNVAL_VOID : 163 !t.compare ("void") ? TYPE_VOID :
165 !t.compare ("bool") ? RETURNVAL_BOOLEAN : -1; 164 !t.compare ("bool") ? TYPE_INT : -1;
166 } 165 }
167 166
168 // Inverse operation 167 // Inverse operation
169 str GetReturnTypeName (int r) { 168 str GetReturnTypeName (int r) {
170 switch (r) { 169 switch (r) {
175 } 174 }
176 175
177 return ""; 176 return "";
178 } 177 }
179 178
180 CommandDef* GetCommandByName (str fname) { 179 CommandDef* FindCommand (str fname) {
181 CommandDef* comm; 180 CommandDef* comm;
182 ITERATE_COMMANDS (comm) { 181 ITERATE_COMMANDS (comm) {
183 if (!fname.icompare (comm->name)) 182 if (!fname.icompare (comm->name))
184 return comm; 183 return comm;
185 } 184 }

mercurial