================ idCompiler::ParseVariableDef ================ */
| 2305 | ================ |
| 2306 | */ |
| 2307 | void idCompiler::ParseVariableDef( idTypeDef *type, const char *name ) { |
| 2308 | idVarDef *def, *def2; |
| 2309 | bool negate; |
| 2310 | |
| 2311 | def = gameLocal.program.GetDef( type, name, scope ); |
| 2312 | if ( def ) { |
| 2313 | Error( "%s redeclared", name ); |
| 2314 | } |
| 2315 | |
| 2316 | def = gameLocal.program.AllocDef( type, name, scope, false ); |
| 2317 | |
| 2318 | // check for an initialization |
| 2319 | if ( CheckToken( "=" ) ) { |
| 2320 | // if a local variable in a function then write out interpreter code to initialize variable |
| 2321 | if ( scope->Type() == ev_function ) { |
| 2322 | def2 = GetExpression( TOP_PRIORITY ); |
| 2323 | if ( ( type == &type_float ) && ( def2->TypeDef() == &type_float ) ) { |
| 2324 | EmitOpcode( OP_STORE_F, def2, def ); |
| 2325 | } else if ( ( type == &type_vector ) && ( def2->TypeDef() == &type_vector ) ) { |
| 2326 | EmitOpcode( OP_STORE_V, def2, def ); |
| 2327 | } else if ( ( type == &type_string ) && ( def2->TypeDef() == &type_string ) ) { |
| 2328 | EmitOpcode( OP_STORE_S, def2, def ); |
| 2329 | } else if ( ( type == &type_entity ) && ( ( def2->TypeDef() == &type_entity ) || ( def2->TypeDef()->Inherits( &type_object ) ) ) ) { |
| 2330 | EmitOpcode( OP_STORE_ENT, def2, def ); |
| 2331 | } else if ( ( type->Inherits( &type_object ) ) && ( def2->TypeDef() == &type_entity ) ) { |
| 2332 | EmitOpcode( OP_STORE_OBJENT, def2, def ); |
| 2333 | } else if ( ( type->Inherits( &type_object ) ) && ( def2->TypeDef()->Inherits( type ) ) ) { |
| 2334 | EmitOpcode( OP_STORE_OBJ, def2, def ); |
| 2335 | } else if ( ( type == &type_boolean ) && ( def2->TypeDef() == &type_boolean ) ) { |
| 2336 | EmitOpcode( OP_STORE_BOOL, def2, def ); |
| 2337 | } else if ( ( type == &type_string ) && ( def2->TypeDef() == &type_float ) ) { |
| 2338 | EmitOpcode( OP_STORE_FTOS, def2, def ); |
| 2339 | } else if ( ( type == &type_string ) && ( def2->TypeDef() == &type_boolean ) ) { |
| 2340 | EmitOpcode( OP_STORE_BTOS, def2, def ); |
| 2341 | } else if ( ( type == &type_string ) && ( def2->TypeDef() == &type_vector ) ) { |
| 2342 | EmitOpcode( OP_STORE_VTOS, def2, def ); |
| 2343 | } else if ( ( type == &type_boolean ) && ( def2->TypeDef() == &type_float ) ) { |
| 2344 | EmitOpcode( OP_STORE_FTOBOOL, def2, def ); |
| 2345 | } else if ( ( type == &type_float ) && ( def2->TypeDef() == &type_boolean ) ) { |
| 2346 | EmitOpcode( OP_STORE_BOOLTOF, def2, def ); |
| 2347 | } else { |
| 2348 | Error( "bad initialization for '%s'", name ); |
| 2349 | } |
| 2350 | } else { |
| 2351 | // global variables can only be initialized with immediate values |
| 2352 | negate = false; |
| 2353 | if ( token.type == TT_PUNCTUATION && token == "-" ) { |
| 2354 | negate = true; |
| 2355 | NextToken(); |
| 2356 | if ( immediateType != &type_float ) { |
| 2357 | Error( "wrong immediate type for '-' on variable '%s'", name ); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | if ( immediateType != type ) { |
| 2362 | Error( "wrong immediate type for '%s'", name ); |
| 2363 | } |
| 2364 |