Changeset 571

Show
Ignore:
Timestamp:
06/25/08 12:20:23 (5 months ago)
Author:
weyrick
Message:

playing with an add operator

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/rphp/runtime/var-test.cpp

    r570 r571  
    2929    return os << "php_hash" << std::endl; 
    3030} 
    31  
    3231 
    3332// using the c++ bool as a type seems to cause problems, so we implement php bool 
     
    162161 
    163162 
     163// non destructive cast (makes a copy) 
     164pvar pvar_cast_to_number(const pvar p) { 
     165 
     166    pvar r = p; 
     167    boost::apply_visitor( convert_to_num_visitor(r), r ); 
     168    return r; 
     169     
     170} 
     171 
     172 
     173// define operators for working with pvars 
     174// php hashes would concat, otherwise convert to number 
     175pvar pvar_add (const pvar lhs, const pvar rhs) 
     176{ 
     177    pvar l,r,result; 
     178 
     179    int lhs_type = boost::apply_visitor( type_checker(), lhs ); 
     180    int rhs_type = boost::apply_visitor( type_checker(), rhs ); 
     181    if ( (lhs_type == PVAR_HASH) && (rhs_type == PVAR_HASH) ) { 
     182        std::cout << "fixme: concat hashes" << std::endl; 
     183        result = 0l; 
     184    } 
     185    else { 
     186        // convert to number, then add 
     187        l = pvar_cast_to_number(lhs); 
     188        std::cout << "pvar_add: l is " << l << std::endl; 
     189        r = pvar_cast_to_number(rhs); 
     190        std::cout << "pvar_add: r is " << r << std::endl; 
     191        result = boost::get<long>(l) + boost::get<long>(r); 
     192        std::cout << "pvar_add: result is " << result << std::endl; 
     193    } 
     194 
     195    return result; 
     196} 
     197 
    164198// driver 
    165199int main() 
    166200{ 
    167     pvar u
     201    pvar u,t,r
    168202 
    169203    // string 
     
    236270    std::cout << "final: " << u << std::endl; 
    237271 
    238 
     272    // operators 
     273     
     274    // try adding a long and a numeric string 
     275    u = 10l; 
     276    t = "20"; 
     277    r = pvar_add(u, t); 
     278    std::cout << "number add: " << r << std::endl; 
     279    std::cout << "u is: " << u << std::endl; 
     280    std::cout << "t is: " << t << std::endl; 
     281 
     282