57 if (var->
type == check_type)
64 bool Mission::doBooleanVar(
missionNode *node,
int mode )
66 varInst *var = doVariable( node, mode );
68 bool ok = checkVarType( var,
VAR_BOOL );
70 fatalError( node, mode,
"expected a bool variable - got a different type" );
79 double Mission::doFloatVar(
missionNode *node,
int mode )
81 varInst *var = doVariable( node, mode );
85 fatalError( node, mode,
"expected a float variable - got a different type" );
94 int Mission::doIntVar(
missionNode *node,
int mode )
96 varInst *var = doVariable( node, mode );
98 bool ok = checkVarType( var,
VAR_INT );
100 fatalError( node, mode,
"expected a int variable - got a different type" );
111 varInst *var = doVariable( node, mode );
115 debug( 3, node, mode,
"doObjectVar got variable :" );
116 printVarInst( 3, var );
118 fatalError( node, mode,
"expected a object variable - got a different type" );
131 for (
unsigned int i = 0;
i < cstack->
contexts.size() && defnode == NULL;
i++) {
142 printf(
"ERROR: no local variable with fast lookup\n" );
146 if (defnode2 != defnode)
147 printf(
"ERROR: wrong local variable lookup\n" );
160 if (module_node == NULL) {
161 fatalError( asknode,
SCRIPT_RUN,
"no such module named "+mname );
165 vector< easyDomNode* >::const_iterator siter;
166 for (siter = module_node->
subnodes.begin(); siter != module_node->
subnodes.end(); siter++) {
170 sprintf( buffer,
"FOUND module variable %s in that node", varnode->
script.
name.c_str() );
190 fatalError( module,
SCRIPT_RUN,
"illegal classvar nr." );
195 varInst *var = (*cvmap)[varname];
209 fatalError( asknode,
SCRIPT_RUN,
"illegal classvar nr." );
214 varInst *var = (*cvmap)[varname];
225 varInst *var = lookupModuleVariable( mname, asknode );
242 void Mission::doGlobals(
missionNode *node,
int mode )
247 debug( 3, node, mode,
"doing global variables" );
249 vector< easyDomNode* >::const_iterator siter;
250 for (siter = node->
subnodes.begin(); siter != node->
subnodes.end() && !have_return( mode ); siter++) {
253 doDefVar( snode, mode,
true );
255 fatalError( node, mode,
"only defvars allowed below globals node" );
266 varInst *var = lookupLocalVariable( node );
273 var = lookupModuleVariable( node );
276 var = lookupGlobalVariable( node );
278 fatalError( node, mode,
"did not find variable" );
289 fatalError( node, mode,
"you have to give a variable name" );
295 if (global_var == NULL) {
296 fatalError( node, mode,
"no variable "+node->
script.
name+
" found on the scopestack (dovariable)" );
307 void Mission::doDefVar(
missionNode *node,
int mode,
bool global_var )
313 debug( 0, node, mode,
"defined module variable "+node->
script.
name );
316 debug( 5, node, mode,
"defining context variable "+node->
script.
name );
339 fatalError( node, mode,
"you have to give a variable name" );
342 string value = node->
attr_value(
"initvalue" );
344 debug( 5, node, mode,
"defining variable "+node->
script.
name );
352 if (global_var ==
false) {
353 scope = scope_stack.back();
354 scope_id = scope_stack.size()-1;
368 if ( !value.empty() ) {
369 debug( 4, node, mode,
"setting init for "+node->
script.
name );
371 vi->
float_val = strtod( value.c_str(), NULL );
373 vi->
int_val = atoi( value.c_str() );
375 if (value ==
"true") {
377 }
else if (value ==
"false") {
380 fatalError( node, mode,
"wrong bool value" );
384 fatalError( node, mode,
"this datatype can;t be initialized" );
391 if ( !value.empty() ) {
392 fatalError( node, mode,
"initvalue is not allowed for a local variable" );
402 debug( 3, node, mode,
"defining global variable" );
408 string classvar = node->
attr_value(
"classvar" );
409 if (classvar ==
"true") {
413 fatalError( node, mode,
"no module node with classvars" );
429 debug( 5, scope, mode,
"defined variable in that scope" );
436 void Mission::doSetVar(
missionNode *node,
int mode )
442 fatalError( node, mode,
"you have to give a variable name" );
444 debug( 3, node, mode,
"trying to set variable "+node->
script.
name );
446 fatalError( node, mode,
"setvar takes exactly one argument" );
454 if (global_var == NULL) {
455 fatalError( node, mode,
"no variable "+node->
script.
name+
" found on the scopestack (setvar)" );
461 bool res = checkBoolExpr( expr, mode );
463 double res = checkFloatExpr( expr, mode );
465 float res = checkIntExpr( expr, mode );
467 debug( 3, node, mode,
"setvar object" );
468 varInst *ovi = checkObjectExpr( expr, mode );
471 fatalError( node, mode,
"unsupported type in setvar" );
476 varInst *var_inst = doVariable( node, mode );
477 if (var_inst == NULL) {
478 fatalError( node, mode,
"variable lookup failed for "+node->
script.
name );
483 bool res = checkBoolExpr( expr, mode );
486 double res = checkFloatExpr( expr, mode );
489 int res = checkIntExpr( expr, mode );
492 debug( 3, node, mode,
"setvar object (before)" );
493 varInst *ovi = checkObjectExpr( expr, mode );
494 assignVariable( var_inst, ovi );
495 debug( 3, node, mode,
"setvar object left,right" );
496 printVarInst( 3, var_inst );
497 printVarInst( 3, ovi );
500 fatalError( node, mode,
"unsupported datatype" );
512 string valuestr = node->
attr_value(
"value" );
513 if ( typestr.empty() ) {
514 fatalError( node, mode,
"no valid const declaration" );
517 debug( 5, node, mode,
"parsed const value "+valuestr );
520 if (typestr ==
"float") {
522 vi->
float_val = atof( valuestr.c_str() );
523 }
else if (typestr ==
"int") {
525 vi->
int_val = atoi( valuestr.c_str() );
526 }
else if (typestr ==
"bool") {
528 if (valuestr ==
"true") {
530 }
else if (valuestr ==
"false") {
533 fatalError( node, mode,
"wrong bool value" );
536 }
else if (typestr ==
"object") {
537 string objecttype = node->
attr_value(
"object" );
538 if (objecttype ==
"string") {
539 varInst *svi = call_string_new( node, mode, valuestr );
541 assignVariable( vi, svi );
546 fatalError( node, mode,
"you cant have a const object" );
550 fatalError( node, mode,
"unknown variable type" );
565 fatalError( NULL,
SCRIPT_RUN,
"wrong types in assignvariable" );
566 saveVarInst( v1, cout );
567 saveVarInst( v2, cout );
593 var_type Mission::vartypeFromString(
string type )
596 if (type ==
"float") {
598 }
else if (type ==
"bool") {
600 }
else if (type ==
"int") {
602 }
else if (type ==
"object") {
605 fatalError( NULL,
SCRIPT_PARSE,
"unknown var type "+type );
613 void Mission::saveVariables(
const ostream &out ) {}
617 void Mission::saveVarInst(
varInst *vi, ostream &aa_out )
620 if (vi == NULL) {}
else {
622 sprintf( buffer,
"type=\"bool\" value=\"%d\" ", vi->
bool_val );
624 sprintf( buffer,
"type=\"float\" value=\"%f\" ", vi->
float_val );
626 sprintf( buffer,
"type=\"int\" value=\"%d\" ", vi->
int_val );
629 string *sptr = (
string*) vi->
object;
630 sprintf( buffer,
"type=\"object\" object=\"%s\" value=\"%s\" >\n ", vi->
objectname.c_str(), sptr->c_str() );
632 sprintf( buffer,
"type=\"object\" object=\"%s\" value=\"0x%p\" >\n ", vi->
objectname.c_str(), vi->
object );
636 doCall_toxml( modname, vi );