Changeset 269

Show
Ignore:
Timestamp:
11/28/09 23:58:23 (2 years ago)
Author:
walter
Message:

bugzilla 3481, only

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/expression.c

    r257 r269  
    124124    precedence[TOKdiv] = PREC_mul; 
    125125    precedence[TOKmod] = PREC_mul; 
     126    precedence[TOKpow] = PREC_mul; 
    126127 
    127128    precedence[TOKadd] = PREC_add; 
     
    183184    precedence[TOKdivass] = PREC_assign; 
    184185    precedence[TOKmodass] = PREC_assign; 
     186    //precedence[TOKpowass] = PREC_assign; 
    185187    precedence[TOKshlass] = PREC_assign; 
    186188    precedence[TOKshrass] = PREC_assign; 
     
    97729774} 
    97739775 
     9776PowExp::PowExp(Loc loc, Expression *e1, Expression *e2) 
     9777    : BinExp(loc, TOKpow, sizeof(PowExp), e1, e2) 
     9778{ 
     9779} 
     9780 
     9781Expression *PowExp::semantic(Scope *sc) 
     9782{   Expression *e; 
     9783 
     9784    if (type) 
     9785    return this; 
     9786 
     9787    BinExp::semanticp(sc); 
     9788    e = op_overload(sc); 
     9789    if (e) 
     9790    return e; 
     9791 
     9792    static int importMathChecked = 0; 
     9793    if (!importMathChecked) 
     9794    { 
     9795    importMathChecked = 1; 
     9796    for (int i = 0; i < Module::amodules.dim; i++) 
     9797    {   Module *mi = (Module *)Module::amodules.data[i]; 
     9798        //printf("\t[%d] %s\n", i, mi->toChars()); 
     9799        if (mi->ident == Id::math && 
     9800        mi->parent->ident == Id::std && 
     9801        !mi->parent->parent) 
     9802        goto L1; 
     9803    } 
     9804    error("must import std.math to use ^^ operator"); 
     9805 
     9806     L1: ; 
     9807    } 
     9808 
     9809    assert(e1->type && e2->type); 
     9810    if ( (e1->type->isintegral() || e1->type->isfloating()) && 
     9811     (e2->type->isintegral() || e2->type->isfloating())) 
     9812    { 
     9813    // For built-in numeric types, there are three cases: 
     9814    // x ^^ 1   ----> x 
     9815    // x ^^ 0.5 ----> sqrt(x) 
     9816    // x ^^ y   ----> pow(x, y) 
     9817    // TODO: backend support, especially for  e1 ^^ 2. 
     9818    bool wantSqrt = false;   
     9819    e2 = e2->optimize(0); 
     9820    if ((e2->op == TOKfloat64 && e2->toReal() == 1.0) || 
     9821        (e2->op == TOKint64 && e2->toInteger() == 1)) 
     9822    { 
     9823        return e1;  // Replace x ^^ 1 with x. 
     9824    } 
     9825 
     9826    e = new IdentifierExp(loc, Id::empty); 
     9827    e = new DotIdExp(loc, e, Id::std); 
     9828    e = new DotIdExp(loc, e, Id::math); 
     9829    if (e2->op == TOKfloat64 && e2->toReal() == 0.5) 
     9830    {   // Replace e1 ^^ 0.5 with .std.math.sqrt(x) 
     9831        e = new CallExp(loc, new DotIdExp(loc, e, Id::_sqrt), e1); 
     9832    } 
     9833    else  
     9834    {   // Replace e1 ^^ e2 with .std.math.pow(e1, e2) 
     9835        e = new CallExp(loc, new DotIdExp(loc, e, Id::_pow), e1, e2);    
     9836    }    
     9837    e = e->semantic(sc); 
     9838    return e; 
     9839    } 
     9840    error("%s ^^ %s is not supported", e1->type->toChars(), e2->type->toChars() ); 
     9841    return new ErrorExp(); 
     9842} 
     9843 
    97749844/************************************************************/ 
    97759845 
  • trunk/src/expression.h

    r254 r269  
    12991299}; 
    13001300 
     1301#if DMDV2 
     1302struct PowExp : BinExp 
     1303{ 
     1304    PowExp(Loc loc, Expression *e1, Expression *e2); 
     1305    Expression *semantic(Scope *sc); 
     1306 
     1307    // For operator overloading 
     1308    Identifier *opId(); 
     1309    Identifier *opId_r(); 
     1310}; 
     1311#endif 
     1312 
    13011313struct ShlExp : BinExp 
    13021314{ 
  • trunk/src/idgen.c

    r268 r269  
    212212    { "opDispatch" }, 
    213213    { "opImplicitCast" }, 
     214    { "pow", "opPow" }, 
     215    { "pow_r", "opPow_r" }, 
     216    //{ "powass", "opPowAssign" }, 
    214217 
    215218    { "classNew", "new" }, 
     
    272275    { "tan" }, 
    273276    { "_sqrt", "sqrt" }, 
     277    { "_pow", "pow" }, 
    274278    { "fabs" }, 
    275279 
  • trunk/src/lexer.c

    r249 r269  
    11521152        return; 
    11531153 
     1154#if DMDV2 
     1155        case '^': 
     1156        p++; 
     1157        if (*p == '^') 
     1158        {   p++; 
     1159#if 0 
     1160            if (*p == '=') 
     1161            {   p++; 
     1162            t->value = TOKpowass;  // ^^= 
     1163            } 
     1164            else 
     1165#endif 
     1166            t->value = TOKpow;     // ^^ 
     1167        } 
     1168        else if (*p == '=') 
     1169        {   p++; 
     1170            t->value = TOKxorass;    // ^= 
     1171        } 
     1172        else 
     1173            t->value = TOKxor;       // ^ 
     1174        return; 
     1175#endif 
     1176 
    11541177#define SINGLE(c,tok) case c: p++; t->value = tok; return; 
    11551178 
     
    11821205        DOUBLE('*', TOKmul, '=', TOKmulass) 
    11831206        DOUBLE('%', TOKmod, '=', TOKmodass) 
     1207#if DMDV1 
    11841208        DOUBLE('^', TOKxor, '=', TOKxorass) 
    1185  
     1209#endif 
    11861210#undef DOUBLE 
    11871211 
     
    30853109    Token::tochars[TOKidentifier]   = "identifier"; 
    30863110    Token::tochars[TOKat]       = "@"; 
     3111    Token::tochars[TOKpow]      = "^^"; 
     3112    //Token::tochars[TOKpowass]     = "^^="; 
    30873113 
    30883114     // For debugging 
  • trunk/src/lexer.h

    r249 r269  
    3333    &   |   ^   &=  |=  ^= 
    3434    =   !   ~   @ 
     35    ^^  ^^= 
    3536    ++  -- 
    3637    .   ->  :   , 
     
    163164    TOKshared, 
    164165    TOKat, 
     166    TOKpow, 
     167    //TOKpowass, 
    165168#endif 
    166169 
  • trunk/src/opover.c

    r253 r269  
    105105Identifier *ModExp::opId()   { return Id::mod; } 
    106106Identifier *ModExp::opId_r() { return Id::mod_r; } 
     107 
     108#if DMDV2 
     109Identifier *PowExp::opId()   { return Id::pow; } 
     110Identifier *PowExp::opId_r() { return Id::pow_r; } 
     111#endif 
    107112 
    108113Identifier *ShlExp::opId()   { return Id::shl; } 
     
    143148Identifier *UshrAssignExp::opId()  { return Id::ushrass; } 
    144149Identifier * CatAssignExp::opId()  { return Id::catass;  } 
     150//Identifier * PowAssignExp::opId()  { return Id::powass;  } 
    145151 
    146152int EqualExp::isCommutative()  { return TRUE; } 
  • trunk/src/parse.c

    r257 r269  
    56035603    { 
    56045604        case TOKmul: nextToken(); e2 = parseUnaryExp(); e = new MulExp(loc,e,e2); continue; 
    5605         case TOKdiv:   nextToken(); e2 = parseUnaryExp(); e = new DivExp(loc,e,e2); continue; 
    5606         case TOKmod:  nextToken(); e2 = parseUnaryExp(); e = new ModExp(loc,e,e2); continue; 
     5605        case TOKdiv: nextToken(); e2 = parseUnaryExp(); e = new DivExp(loc,e,e2); continue; 
     5606        case TOKmod: nextToken(); e2 = parseUnaryExp(); e = new ModExp(loc,e,e2); continue; 
     5607        case TOKpow: nextToken(); e2 = parseUnaryExp(); e = new PowExp(loc,e,e2); continue; 
    56075608 
    56085609        default: 
     
    59495950        X(TOKdivass,    DivAssignExp); 
    59505951        X(TOKmodass,    ModAssignExp); 
     5952        //X(TOKpowass,    PowAssignExp); 
    59515953        X(TOKandass,    AndAssignExp); 
    59525954        X(TOKorass,     OrAssignExp);