Changeset 76 in dev for BasicCompiler32/NumOpe.cpp
- Timestamp:
- Mar 21, 2007, 9:26:56 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
BasicCompiler32/NumOpe.cpp
r75 r76 67 67 } 68 68 69 void NewStringObject( LPSTR lpszText){69 void NewStringObject( const char *str ){ 70 70 /////////////////////////////////////////////////////// 71 71 // lpszTextを元にStringオブジェクトを生成し、 … … 73 73 /////////////////////////////////////////////////////// 74 74 75 char *parameter = (char *)malloc( lstrlen( lpszText) + 32 );76 sprintf( parameter, "\"%s\"%c%c*Char", lpszText, 1, ESC_AS );75 char *parameter = (char *)malloc( lstrlen( str ) + 32 ); 76 sprintf( parameter, "\"%s\"%c%c*Char", str, 1, ESC_AS ); 77 77 SetStringQuotes( parameter ); 78 78 79 79 extern CClass *pobj_StringClass; 80 Type baseTypeInfo = { DEF_OBJECT, (LONG_PTR)pobj_StringClass }; 81 Operator_New( *pobj_StringClass, "", parameter, baseTypeInfo ); 80 Operator_New( *pobj_StringClass, "", parameter, Type( DEF_OBJECT, *pobj_StringClass ) ); 82 81 83 82 free( parameter ); … … 85 84 86 85 87 int NumOpe(const char *Command,int BaseType,LONG_PTR lpBaseIndex,LONG_PTR *plpIndex,BOOL *pbUseHeap){ 88 extern HANDLE hHeap; 86 bool NumOpe( const char *expression, 87 const Type &baseType, 88 Type &resultType, 89 BOOL *pbUseHeap ){ 90 89 91 int i,i2,i3,i4; 90 char temporary[ 8192],temp2[1024],temp3[1024];91 92 if( Command[0]=='\0'){92 char temporary[1024],temp2[1024],temp3[1024]; 93 94 if(expression[0]=='\0'){ 93 95 SetError(1,NULL,cp); 94 return -1;95 } 96 97 if( Command[0]==1&& Command[1]==ESC_NEW ){96 return false; 97 } 98 99 if(expression[0]==1&& expression[1]==ESC_NEW ){ 98 100 //New演算子(オブジェクト生成) 99 Type baseTypeInfo = { BaseType, lpBaseIndex }; 100 int resultType = Operator_New(Command+2,plpIndex, baseTypeInfo ); 101 102 return resultType; 101 102 if( !Operator_New( expression+2, baseType, resultType ) ){ 103 return false; 104 } 105 106 return true; 103 107 } 104 108 … … 112 116 long stack[255]; 113 117 int pnum; 114 if(!GetNumOpeElements( Command,&pnum,values,calc,stack)){118 if(!GetNumOpeElements(expression,&pnum,values,calc,stack)){ 115 119 for(i=0;i<pnum;i++){ 116 120 if(values[i]) HeapDefaultFree(values[i]); 117 121 } 118 return 0;122 return false; 119 123 } 120 124 … … 146 150 double dbl; 147 151 int sp; 148 int type [255];152 int type_stack[255]; 149 153 LONG_PTR index_stack[255]; 150 154 BOOL bUseHeap[255]; … … 155 159 156 160 if(idCalc){ 157 if(type [sp-2]==DEF_OBJECT){161 if(type_stack[sp-2]==DEF_OBJECT){ 158 162 //オーバーロードされたオペレータを呼び出す 159 Type BaseTypeInfo={BaseType,lpBaseIndex}; 160 i2=CallOperatorProc(idCalc,&BaseTypeInfo,type,index_stack,bUseHeap,sp); 163 i2=CallOperatorProc(idCalc,baseType,type_stack,index_stack,bUseHeap,sp); 161 164 if(i2==0){ 162 165 if(idCalc==CALC_EQUAL) lstrcpy(temp2,"=="); … … 171 174 } 172 175 173 if(!CheckCalcType(idCalc,type ,sp)) goto error;176 if(!CheckCalcType(idCalc,type_stack,sp)) goto error; 174 177 } 175 178 … … 192 195 StrLiteral: 193 196 194 if(BaseType==DEF_OBJECT){ 195 CClass *pobj_Class; 196 pobj_Class=(CClass *)lpBaseIndex; 197 Type BaseTypeInfo = {BaseType,lpBaseIndex}; 198 if(IsStringObjectType(BaseTypeInfo)){ 199 //要求タイプがオブジェクトであり、Stringの受け入れが可能な場合 197 if( baseType.IsObject() ){ 198 if( baseType.IsStringObject() ){ 199 //要求タイプがStringのとき 200 200 201 201 //String型オブジェクトを生成 … … 203 203 204 204 extern CClass *pobj_StringClass; 205 type [sp]=DEF_OBJECT;205 type_stack[sp]=DEF_OBJECT; 206 206 index_stack[sp]=(LONG_PTR)pobj_StringClass; 207 207 bLiteralCalculation=0; … … 213 213 214 214 215 type[sp]=typeOfPtrChar; 216 index_stack[sp]=LITERAL_STRING; 215 type_stack[sp]=typeOfPtrChar; 217 216 bLiteralCalculation=0; 218 217 … … 252 251 i4=GetStringInPare_RemovePare(temp2,term+i2+1); 253 252 254 int idProc;255 253 void *pInfo; 256 idProc=GetProc(temporary,&pInfo); 257 254 int idProc=GetProc(temporary,(void **)&pInfo); 255 256 Type resultType; 258 257 if(idProc){ 259 258 //閉じカッコ")"に続く文字がNULLでないとき … … 272 271 //////////////// 273 272 274 i2=CallProc(idProc,pInfo,temporary,temp2,&index_stack[sp]);275 if( i2==-1){273 CallProc(idProc,pInfo,temporary,temp2,resultType); 274 if(resultType.IsNull()){ 276 275 //戻り値が存在しないとき 277 276 for(i2=2;;i2++){ … … 292 291 293 292 //大きな型への暗黙の変換 294 type[sp]=AutoBigCast(BaseType,i2); 293 type_stack[sp]=AutoBigCast(baseType.GetBasicType(),resultType.GetBasicType()); 294 index_stack[sp] = resultType.GetIndex(); 295 295 bLiteralCalculation=0; 296 296 297 297 //スタックへプッシュ 298 PushReturnValue(i2); 299 300 if(Is64Type(type[sp])&&IsWholeNumberType(i2)&&GetTypeSize(i2,-1)<=sizeof(long)){ 301 //必要に応じて64ビット拡張 302 ExtendStackTo64(i2); 298 PushReturnValue( resultType.GetBasicType() ); 299 300 if( Is64Type(type_stack[sp]) 301 && resultType.IsWhole() 302 && resultType.GetBasicSize() <= sizeof(long) ){ 303 //必要に応じて64ビット拡張 304 ExtendStackTo64( resultType.GetBasicType() ); 303 305 } 304 306 305 if( i2==DEF_STRUCT){307 if( resultType.IsStruct() ){ 306 308 //構造体が戻ったときはヒープ領域にインスタンスが格納されている 307 309 //※後にfreeする必要あり … … 321 323 322 324 //マクロ関数の場合 323 type[sp]=NumOpe(temp3,0,0,&index_stack[sp]);324 325 if(!IS_LITERAL( index_stack[sp])){325 NumOpe(temp3,Type(),resultType); 326 327 if(!IS_LITERAL(resultType.GetIndex())){ 326 328 //リテラル値ではなかったとき 327 329 bLiteralCalculation=0; 328 330 } 329 331 332 type_stack[sp] = resultType.GetBasicType(); 333 index_stack[sp] = resultType.GetIndex(); 334 330 335 sp++; 331 336 break; … … 335 340 336 341 337 342 //インデクサ(getアクセサ) 338 343 char variable[VN_SIZE],array_element[VN_SIZE]; 339 CClass *pobj_c;340 344 GetArrayElement(term,variable,array_element); 341 345 if(array_element[0]){ 342 i2=GetVarType(variable,(LONG_PTR *)&pobj_c,0);343 if(i2==DEF_OBJECT){344 Type RetTypeInfo;345 CallIndexerGetterProc( pobj_c,variable,array_element,RetTypeInfo);346 type [sp]=RetTypeInfo.type;347 index_stack[sp]= RetTypeInfo.u.lpIndex;346 Type resultType; 347 GetVarType(variable,resultType,0); 348 if( resultType.IsObject() ){ 349 CallIndexerGetterProc(&resultType.GetClass(),variable,array_element,resultType); 350 type_stack[sp]=resultType.GetBasicType(); 351 index_stack[sp]=resultType.GetIndex(); 348 352 bLiteralCalculation=0; 349 353 … … 359 363 // Nothing 360 364 if( lstrcmp( term, "Nothing" ) == 0 ){ 361 type [sp] = DEF_OBJECT;362 if( BaseType == DEF_OBJECT){363 index_stack[sp] = lpBaseIndex;365 type_stack[sp] = DEF_OBJECT; 366 if( baseType.IsObject() ){ 367 index_stack[sp] = baseType.GetIndex(); 364 368 } 365 369 else{ … … 377 381 378 382 383 if( (string)term=="value"){ 384 int test=0; 385 } 386 379 387 380 388 RELATIVE_VAR RelativeVar; 389 Type varType; 381 390 if(GetVarOffset( 382 391 false, //エラー表示あり 383 392 false, //読み込み専用 384 term,&i2,&RelativeVar,&index_stack[sp])){ 393 term, 394 &RelativeVar,varType)){ 385 395 ////////// 386 396 // 変数 … … 388 398 389 399 //大きな型への暗黙の変換 390 type[sp]=AutoBigCast(BaseType,i2); 400 type_stack[sp]=AutoBigCast(baseType.GetBasicType(),varType.GetBasicType()); 401 index_stack[sp] = varType.GetIndex(); 391 402 bLiteralCalculation=0; 392 403 393 if( i2&FLAG_PTR){404 if(varType.GetBasicType()&FLAG_PTR){ 394 405 //配列ポインタ 395 type [sp]=GetPtrType(i2^FLAG_PTR,index_stack[sp]);406 type_stack[sp]=GetPtrType(varType.GetBasicType()^FLAG_PTR); 396 407 397 408 SetVarPtrToEax(&RelativeVar); … … 400 411 op_push(REG_EAX); 401 412 } 402 else if(i2==DEF_DOUBLE|| 403 i2==DEF_INT64|| 404 i2==DEF_QWORD){ 413 else if( varType.IsStruct() ){ 414 //構造体ポインタをeaxへ格納(構造体は値型) 415 SetVarPtrToEax(&RelativeVar); 416 417 //push eax 418 op_push(REG_EAX); 419 } 420 else if( varType.GetBasicSize() == sizeof(_int64) ){ 405 421 //64ビット型 406 422 PushDoubleVariable(&RelativeVar); 407 423 } 408 else if(i2==DEF_LONG||i2==DEF_DWORD||i2==DEF_SINGLE|| 409 IsPtrType(i2) || i2==DEF_OBJECT){ 424 else if( varType.GetBasicSize() == sizeof(long) ){ 410 425 //32ビット型 411 426 PushLongVariable(&RelativeVar); 412 427 } 413 else if( i2==DEF_INTEGER || (isUnicode&&i2==DEF_CHAR)){428 else if( varType.IsInteger() ){ 414 429 PushIntegerVariable(&RelativeVar); 415 430 } 416 else if( i2==DEF_WORD){431 else if( varType.IsWord() ){ 417 432 PushWordVariable(&RelativeVar); 418 433 } 419 else if( i2==DEF_SBYTE || (isUnicode==false&&i2==DEF_CHAR)){434 else if( varType.IsSByte() ){ 420 435 PushCharVariable(&RelativeVar); 421 436 } 422 else if( i2==DEF_BYTE||i2==DEF_BOOLEAN){437 else if( varType.IsByte() || varType.IsBoolean() ){ 423 438 PushByteVariable(&RelativeVar); 424 439 } 425 else if(i2==DEF_STRUCT){426 //構造体ポインタをeaxへ格納(構造体は値型)427 SetVarPtrToEax(&RelativeVar);428 429 //push eax430 op_push(REG_EAX);431 }432 440 else SetError(11,term,cp); 433 441 434 if(Is64Type(type[sp])&&IsWholeNumberType(i2)&&GetTypeSize(i2,-1)<=sizeof(long)){ 442 if( Is64Type(type_stack[sp]) 443 && varType.IsWhole() 444 && varType.GetBasicSize()<=sizeof(long)){ 435 445 //必要に応じて64ビット拡張 436 ExtendStackTo64(i2);446 ExtendStackTo64( varType.GetBasicType() ); 437 447 } 438 448 … … 448 458 i3 = CDBConst::obj.GetType(term); 449 459 if(i3){ 450 type [sp]=i3;460 type_stack[sp]=i3; 451 461 if(IsRealNumberType(i3)){ 452 462 //実数 … … 483 493 i3=GetTypeFixed(term,&lp); 484 494 if(i3!=-1){ 485 type [sp]=i3|FLAG_CAST;495 type_stack[sp]=i3|FLAG_CAST; 486 496 index_stack[sp]=lp; 487 497 sp++; … … 500 510 501 511 if(GetSubHash(VarName,0)){ 502 Type RetTypeInfo;503 CallPropertyMethod(term,NULL, &RetTypeInfo);512 Type resultType; 513 CallPropertyMethod(term,NULL,resultType); 504 514 505 515 //大きな型への暗黙の変換 506 type[sp]=AutoBigCast(BaseType,RetTypeInfo.type); 507 508 index_stack[sp]=RetTypeInfo.u.lpIndex; 516 type_stack[sp]=AutoBigCast(baseType.GetBasicType(),resultType.GetBasicType()); 517 index_stack[sp]=resultType.GetIndex(); 509 518 bLiteralCalculation=0; 510 519 511 520 //スタックへプッシュ 512 PushReturnValue( RetTypeInfo.type);513 514 if(type [sp]==DEF_STRUCT){521 PushReturnValue( resultType.GetBasicType() ); 522 523 if(type_stack[sp]==DEF_STRUCT){ 515 524 //構造体が戻ったときはヒープ領域にインスタンスが格納されている 516 525 //※後にfreeする必要あり … … 527 536 bError=1; 528 537 SetError(3,term,cp); 529 type [sp]=DEF_DOUBLE;538 type_stack[sp]=DEF_DOUBLE; 530 539 } 531 540 else{ 532 541 //リテラル値 533 type [sp]=GetLiteralValue(term,&i64data,BaseType);542 type_stack[sp]=GetLiteralValue(term,&i64data,baseType.GetBasicType()); 534 543 Literal: 535 if(type [sp]==DEF_INT64||536 type [sp]==DEF_QWORD||537 type [sp]==DEF_DOUBLE){544 if(type_stack[sp]==DEF_INT64|| 545 type_stack[sp]==DEF_QWORD|| 546 type_stack[sp]==DEF_DOUBLE){ 538 547 //64ビット(符号有り整数/実数) 539 548 … … 544 553 op_push_V(*(long *)(&i64data)); 545 554 } 546 else if(type [sp]==DEF_SINGLE){555 else if(type_stack[sp]==DEF_SINGLE){ 547 556 //single実数 548 557 … … 566 575 567 576 //リテラル値の種類 568 if(Is64Type(type [sp])==0&&IsRealNumberType(type[sp])==0){577 if(Is64Type(type_stack[sp])==0&&IsRealNumberType(type_stack[sp])==0){ 569 578 //整数(符号有り/無し) 570 579 … … 579 588 //value[sp-2] xor= value[sp-1] 580 589 //xor演算 581 if(!Calc_Xor(type ,index_stack,&sp)) goto error;590 if(!Calc_Xor(type_stack,index_stack,&sp)) goto error; 582 591 break; 583 592 case CALC_OR: 584 593 //value[sp-2] or= value[sp-1] 585 594 //or演算 586 if(!Calc_Or(type ,index_stack,&sp)) goto error;595 if(!Calc_Or(type_stack,index_stack,&sp)) goto error; 587 596 break; 588 597 case CALC_AND: 589 598 //value[sp-2] and= value[sp-1] 590 599 //and演算 591 if(!Calc_And(type ,index_stack,&sp)) goto error;600 if(!Calc_And(type_stack,index_stack,&sp)) goto error; 592 601 break; 593 602 case CALC_NOT: 594 603 //value[sp-1]=Not value[sp-1] 595 604 //NOT演算子 596 if(!Calc_Not(type ,sp)) goto error;605 if(!Calc_Not(type_stack,sp)) goto error; 597 606 break; 598 607 … … 600 609 case CALC_PE: 601 610 //value[sp-2]<=value[sp-1] 602 if(!Calc_Relation_PE(type ,index_stack,&sp)) goto error;611 if(!Calc_Relation_PE(type_stack,index_stack,&sp)) goto error; 603 612 break; 604 613 case CALC_QE: 605 614 //value[sp-2]>=value[sp-1] 606 if(!Calc_Relation_QE(type ,index_stack,&sp)) goto error;615 if(!Calc_Relation_QE(type_stack,index_stack,&sp)) goto error; 607 616 break; 608 617 case CALC_P: 609 618 //value[sp-2]<value[sp-1] 610 if(!Calc_Relation_P(type ,index_stack,&sp)) goto error;619 if(!Calc_Relation_P(type_stack,index_stack,&sp)) goto error; 611 620 break; 612 621 case CALC_Q: 613 622 //value[sp-2]>value[sp-1] 614 if(!Calc_Relation_Q(type ,index_stack,&sp)) goto error;623 if(!Calc_Relation_Q(type_stack,index_stack,&sp)) goto error; 615 624 break; 616 625 case CALC_NOTEQUAL: 617 626 //value[sp-2]<>value[sp-1] 618 if(!Calc_Relation_NotEqual(type ,&sp)) goto error;627 if(!Calc_Relation_NotEqual(type_stack,&sp)) goto error; 619 628 break; 620 629 case CALC_EQUAL: 621 630 //value[sp-2]=value[sp-1] 622 if(!Calc_Relation_Equal(type ,&sp)) goto error;631 if(!Calc_Relation_Equal(type_stack,&sp)) goto error; 623 632 break; 624 633 … … 626 635 case CALC_SHL: 627 636 //value[sp-2]=value[sp-2]<<value[sp-1] 628 if(!Calc_SHL(type ,&sp)) goto error;637 if(!Calc_SHL(type_stack,&sp)) goto error; 629 638 break; 630 639 case CALC_SHR: 631 640 //value[sp-2]=value[sp-2]>>value[sp-1] 632 if(!Calc_SHR(type ,&sp)) goto error;641 if(!Calc_SHR(type_stack,&sp)) goto error; 633 642 break; 634 643 … … 637 646 case CALC_SUBTRACTION: 638 647 case CALC_PRODUCT: 639 if(!CalcTwoTerm_Arithmetic(idCalc,type ,index_stack,&sp)) goto error;648 if(!CalcTwoTerm_Arithmetic(idCalc,type_stack,index_stack,&sp)) goto error; 640 649 break; 641 650 … … 643 652 //value[sp-2]%=value[sp-1] 644 653 //剰余演算 645 if(!Calc_Mod(type ,&sp)) goto error;654 if(!Calc_Mod(type_stack,&sp)) goto error; 646 655 break; 647 656 case CALC_QUOTIENT: 648 657 //value[sp-2]/=value[sp-1]; 649 658 //除算 650 if(!Calc_Divide(type ,&sp,BaseType)) goto error;659 if(!Calc_Divide(type_stack,&sp,baseType.GetBasicType())) goto error; 651 660 break; 652 661 case CALC_INTQUOTIENT: 653 662 //value[sp-2]/=value[sp-1] 654 663 //整数除算 655 if(!Calc_IntDivide(type ,index_stack,&sp)) goto error;664 if(!Calc_IntDivide(type_stack,index_stack,&sp)) goto error; 656 665 break; 657 666 case CALC_MINUSMARK: 658 667 //value[sp-1]=-value[sp-1] 659 668 //符号反転 660 if(!Calc_MinusMark(type ,sp)) goto error;669 if(!Calc_MinusMark(type_stack,sp)) goto error; 661 670 index_stack[sp-1]=-1; 662 671 break; 663 672 case CALC_POWER: 664 673 //べき乗演算(浮動小数点演算のみ) 665 if(!Calc_Power(type ,&sp)) goto error;674 if(!Calc_Power(type_stack,&sp)) goto error; 666 675 break; 667 676 case CALC_AS: 668 677 //キャスト 669 if(!Calc_Cast(type ,index_stack,&sp)) goto error;678 if(!Calc_Cast(type_stack,index_stack,&sp)) goto error; 670 679 break; 671 680 672 681 case CALC_BYVAL: 673 682 //ポインタ型→参照型 674 if( PTR_LEVEL( type [sp-1] ) <= 0 ){683 if( PTR_LEVEL( type_stack[sp-1] ) <= 0 ){ 675 684 //ポインタ型ではないとき 676 685 SetError( 3, NULL, cp ); … … 678 687 } 679 688 680 type [sp-1] = PTR_LEVEL_DOWN( type[sp-1] );689 type_stack[sp-1] = PTR_LEVEL_DOWN( type_stack[sp-1] ); 681 690 682 691 break; … … 697 706 if(bLiteralCalculation){ 698 707 //右辺値が数値の定数式の場合 699 LONG_PTR lpClassIndex;700 i2=StaticCalculation(true, Command,BaseType,&i64data,&lpClassIndex);708 Type resultType; 709 StaticCalculation(true, expression,baseType.GetBasicType(),&i64data,resultType); 701 710 702 711 obp=BeforeObp; … … 705 714 pobj_Reloc->copy(pobj_BackReloc); 706 715 707 if(i2==DEF_INT64|| 708 i2==DEF_QWORD|| 709 i2==DEF_DOUBLE){ 716 if( resultType.GetBasicSize() == sizeof(_int64) ){ 710 717 //64ビット(符号有り整数/実数) 711 718 … … 716 723 op_push_V(*(long *)(&i64data)); 717 724 } 718 else if( i2==DEF_SINGLE){725 else if( resultType.IsSingle() ){ 719 726 //single実数 720 727 … … 733 740 i3=(long)i64data; 734 741 735 if( i2==DEF_SBYTE||i2==DEF_BYTE||i2==DEF_BOOLEAN || (isUnicode==false&&i2==DEF_CHAR)) i3=i3&0x000000FF;736 if( i2==DEF_INTEGER||i2==DEF_WORD || (isUnicode&&i2==DEF_CHAR)) i3=i3&0x0000FFFF;742 if(resultType.GetBasicSize()==sizeof(char)) i3=i3&0x000000FF; 743 if(resultType.GetBasicSize()==sizeof(short)) i3=i3&0x0000FFFF; 737 744 738 745 //push term … … 740 747 } 741 748 742 type [0]=i2;743 index_stack[0]= lpClassIndex;749 type_stack[0]=resultType.GetBasicType(); 750 index_stack[0]=resultType.GetIndex(); 744 751 } 745 752 else{ … … 748 755 } 749 756 750 if(plpIndex) *plpIndex=index_stack[0];751 757 if(pbUseHeap) *pbUseHeap=bUseHeap[0]; 752 758 753 int RetType; 754 RetType=type[0]; 759 resultType.SetType( type_stack[0], index_stack[0] ); 760 761 bool isSuccessful = true; 755 762 goto finish; 756 763 757 764 758 765 error: 759 RetType=-1;766 isSuccessful = false; 760 767 goto finish; 761 768 … … 770 777 delete pobj_BackReloc; 771 778 772 return RetType;779 return isSuccessful; 773 780 }
Note:
See TracChangeset
for help on using the changeset viewer.