Changeset 641

Show
Ignore:
Timestamp:
08/28/08 12:41:13 (3 months ago)
Author:
weyrick
Message:

some cleanup, organizing, fixing in the runtime.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/rphp/runtime/CMakeLists.txt

    r631 r641  
    99  pHash.cpp 
    1010  pObject.cpp 
    11   pTypes.cpp 
     11  pVarOperators.cpp 
     12  pTypeOperators.cpp 
    1213  pRuntime.cpp 
    1314  pOutputBuffer.cpp 
  • trunk/rphp/runtime/include/pFunctionManager.h

    r621 r641  
    2525#include <boost/multi_index/member.hpp> 
    2626#include <ext/hash_map> 
     27#include "pTypeOperators.h" 
    2728#include "pFunctionSig.h" 
    2829 
  • trunk/rphp/runtime/include/pRuntime.h

    r630 r641  
    2121 
    2222#include "pTypes.h" 
     23#include "pTypeOperators.h" 
     24#include "pVarOperators.h" 
    2325 
    2426namespace rphp { 
  • trunk/rphp/runtime/include/pTypes.h

    r631 r641  
    2020#define RPHP_PTYPES_H_ 
    2121 
    22 #include <boost/lexical_cast.hpp> 
    2322#include <boost/tuple/tuple.hpp> 
    2423 
     24// including this file includes all rphp base types 
    2525#include "pVar.h" 
    2626#include "pHash.h" 
     
    2828#include "pResource.h" 
    2929 
    30 U_NAMESPACE_BEGIN 
    31     // boost hash function for pUString 
    32     std::size_t hash_value(rphp::pUString const& k); 
    33 U_NAMESPACE_END 
    34  
    3530namespace rphp { 
    36  
    37     // types used in the runtime which aren't related to pVar (i.e. that aren't builtin php types) 
    3831 
    3932    // note, pUInt is not a base PHP type (all PHP numbers are signed) 
     
    4639    typedef boost::tuple<const pUString, const pUInt, const pUInt> pSourceStartEndLocation; 
    4740 
    48     // a visitor for determining type of pVar 
    49     class pVarTypeChecker : public boost::static_visitor<pVarType> { 
    50  
    51     public: 
    52  
    53         pVarType operator()(const pTriState &h) const { 
    54             return (pNull(h)) ? pVarNullType : pVarBoolType; 
    55         } 
    56  
    57         pVarType operator()(const pInt &i) const { 
    58             return pVarIntType; 
    59         } 
    60  
    61         pVarType operator()(const pFloat &i) const { 
    62             return pVarFloatType; 
    63         } 
    64  
    65         pVarType operator()(const pBString &str) const { 
    66             return pVarBStringType; 
    67         } 
    68  
    69         pVarType operator()(const pUStringP &str) const { 
    70             return pVarUStringType; 
    71         } 
    72  
    73         pVarType operator()(const pHashP &h) const { 
    74             return pVarHashType; 
    75         } 
    76  
    77         pVarType operator()(const pObjectP &h) const { 
    78             return pVarObjectType; 
    79         } 
    80  
    81         pVarType operator()(const pResourceP &h) const { 
    82             return pVarResourceType; 
    83         } 
    84  
    85         pVarType operator()(const pVarRef &p) const { 
    86             return pVarRefType; 
    87         } 
    88  
    89     }; 
    90  
    91  
    92     // a visitor for converting to a number (long or float) 
    93     class convertToNumber : public boost::static_visitor<void> { 
    94     protected: 
    95         pVar &var; 
    96  
    97     public: 
    98         convertToNumber(pVar &v) : var(v) { } 
    99  
    100         void operator()(pTriState &h) const { 
    101                 (h) ? var = 1l : var = 0l; 
    102         } 
    103  
    104         void operator()(pInt &a) const { 
    105             // nothing, already numeric 
    106         } 
    107  
    108         void operator()(pFloat &i) const { 
    109             // nothing, already numeric 
    110         } 
    111  
    112         void operator()(pBString &a) const { 
    113             // TODO: handle floats 
    114             try { 
    115                 var = boost::lexical_cast<long>(a); 
    116             } catch(boost::bad_lexical_cast &) { 
    117                 var = 0l; 
    118             } 
    119         } 
    120  
    121         void operator()(pUStringP &a) const { 
    122             // TODO: do a real conversion here 
    123             // should handle both integers and floats 
    124             var = 0l; 
    125         } 
    126  
    127         void operator()(pHashP &h) const { 
    128             var = (pInt)h->getSize(); 
    129         } 
    130  
    131         void operator()(pObjectP &h) const { 
    132             var = (pInt)h->getNumProperties(); 
    133         } 
    134  
    135         void operator()(pResourceP &h) const { 
    136             // TODO: return static resource count 
    137             var = 0l; 
    138         } 
    139  
    140         void operator()(pVarRef &r) const { 
    141             // unbox 
    142             //boost::apply_visitor(convertToNumber(*r), *r); 
    143         } 
    144  
    145     }; 
    146  
    147     // a visitor for converting to a string 
    148     class convertToBString : public boost::static_visitor<void> { 
    149     protected: 
    150         pVar &var; 
    151  
    152     public: 
    153         convertToBString(pVar &v) : var(v) { } 
    154  
    155         void operator()(pTriState &h) const { 
    156                 (h) ? var = pBString("1") : var = pBString("0"); 
    157         } 
    158  
    159         void operator()(pInt &a) const { 
    160             // TODO: real conversion 
    161             var = pBString("some pInt"); 
    162         } 
    163  
    164         void operator()(pFloat &i) const { 
    165             // TODO: real conversion 
    166             var = pBString("some pFloat"); 
    167         } 
    168  
    169         void operator()(pBString &a) const { 
    170             // nothing 
    171         } 
    172  
    173         void operator()(pUStringP &a) const { 
    174             // TODO 
    175         } 
    176  
    177         void operator()(pHashP &h) const { 
    178             var = pBString("array"); 
    179         } 
    180  
    181         void operator()(pObjectP &h) const { 
    182             // TODO: toString 
    183             var = pBString("object"); 
    184         } 
    185  
    186         void operator()(pResourceP &h) const { 
    187             var = pBString("resource"); 
    188         } 
    189  
    190         void operator()(pVarRef &r) const { 
    191             // TODO 
    192             // unbox 
    193             //boost::apply_visitor(convertToNumber(*r), *r); 
    194         } 
    195  
    196     }; 
    197  
    198     /* 
    199      * convenience accessors 
    200      * 
    201      */ 
    202  
    203     // convert to number (in place) 
    204     inline pVarType pVar_getType(const pVar &p) { 
    205         return boost::apply_visitor(pVarTypeChecker(), p); 
    206     } 
    207  
    208     // convert to number (in place) 
    209     inline void pVar_convertToNumber(pVar &p) { 
    210         boost::apply_visitor(convertToNumber(p), p); 
    211     } 
    212  
    213     // get the boolean value of a pVar. does NOT convert so pVar 
    214     // must already be a pTriState 
    215     inline pTriState pVar_getVal_pBool(const pVar &p) { 
    216             return boost::get<rphp::pTriState>(p); 
    217     } 
    218  
    219     inline pInt pVar_getVal_pInt(const pVar &p) { 
    220             return boost::get<pInt>(p); 
    221     } 
    222  
    223     inline pBString pVar_getVal_pBString(const pVar &p) { 
    224             return boost::get<pBString>(p); 
    225     } 
    226  
    227     inline pUStringP pVar_getVal_pUString(const pVar &p) { 
    228             return boost::get<pUStringP>(p); 
    229     } 
    230  
    231     inline pVarRef pVar_getVal_pRef(const pVar &p) { 
    232             return boost::get<pVarRef>(p); 
    233     } 
    234  
    235     /* 
    236      * type conversions 
    237      * 
    238      */ 
    239     pVar pVar_castToNumber(const pVar p); 
    240     pVar pVar_castToBString(const pVar p); 
    241  
    242     pVar pVar_add(const pVar lhs, const pVar rhs); 
    243  
    24441} /* namespace rphp */ 
    24542 
  • trunk/rphp/runtime/include/pVar.h

    r631 r641  
    2525#include <boost/shared_ptr.hpp> 
    2626#include <unicode/unistr.h> 
    27 #include <iostream> 
    2827 
    2928namespace rphp { 
    3029 
    31     /* 
    32      * Definition of the main pVar variant 
    33      */ 
     30/* 
     31 * Definition of the main pVar variant, which represents 
     32 * a PHP value in the runtime 
     33 */ 
    3434 
    35     // a boost::tribool represents php true, false and null values 
    36     // pTrue, pFalse and pNull are defined 
    37     typedef boost::logic::tribool pTriState; 
    38     // convenience accesors 
    39     BOOST_TRIBOOL_THIRD_STATE(pNull) 
    40     #define pTrue  pTriState(true) 
    41     #define pFalse pTriState(false) 
     35// a boost::tribool represents php true, false and null values 
     36// pTrue, pFalse and pNull are defined 
     37typedef boost::logic::tribool pTriState; 
     38// convenience accesors 
     39BOOST_TRIBOOL_THIRD_STATE(pNull) 
     40#define pTrue  pTriState(true) 
     41#define pFalse pTriState(false) 
    4242 
    43     // numeric types 
    44     typedef long  pInt; 
    45     typedef double pFloat; 
     43// numeric types 
     44typedef signed long pInt; 
     45typedef double pFloat; 
    4646 
    47     // string types: binary and unicode flavor 
    48     // "binary" strings 
    49     typedef std::string pBString; 
     47// string types: binary and unicode flavor 
     48// "binary" strings 
     49typedef std::string pBString; 
    5050 
    51     // unicode strings, using the ICU library 
    52     typedef UnicodeString pUString; 
    53     typedef boost::shared_ptr<pUString> pUStringP; 
     51// unicode strings, using the ICU library 
     52typedef UnicodeString pUString; 
     53typedef boost::shared_ptr<pUString> pUStringP; 
    5454 
    55     // php hash tables 
    56     class pHash; 
    57     typedef boost::shared_ptr<pHash> pHashP; 
     55// php hash tables 
     56class pHash; 
     57typedef boost::shared_ptr<pHash> pHashP; 
    5858 
    59     // php objects 
    60     class pObject; 
    61     typedef boost::shared_ptr<pObject> pObjectP; 
     59// php objects 
     60class pObject; 
     61typedef boost::shared_ptr<pObject> pObjectP; 
    6262 
    63     // php resources 
    64     class pResource; 
    65     typedef boost::shared_ptr<pResource> pResourceP; 
     63// php resources 
     64class pResource; 
     65typedef boost::shared_ptr<pResource> pResourceP; 
    6666 
    67     // base variant that represents a php variable 
    68     typedef boost::variant< pTriState, pInt, pFloat, pBString, pUStringP, pHashP, pObjectP, pResourceP > pVarBase; 
     67// base variant that represents a php variable 
     68typedef boost::variant< pTriState, pInt, pFloat, pBString, pUStringP, pHashP, pObjectP, pResourceP > pVarBase; 
    6969 
    70     // reference to a pvarBase, i.e. a container for pvar variables 
    71     typedef boost::shared_ptr<pVarBase> pVarRef; 
     70// reference to a pvarBase, i.e. a container for pvar variables 
     71typedef boost::shared_ptr<pVarBase> pVarRef; 
    7272 
    73     // full pvar definition: a variant that can hold a base type or a reference 
    74     typedef boost::variant< pTriState, pInt, pFloat, pBString, pUStringP, pHashP, pObjectP, pResourceP, pVarRef > pVar; 
    75     typedef boost::shared_ptr<pVar> pVarP; 
     73// full pvar definition: a variant that can hold a base type or a reference 
     74typedef boost::variant< pTriState, pInt, pFloat, pBString, pUStringP, pHashP, pObjectP, pResourceP, pVarRef > pVar; 
     75typedef boost::shared_ptr<pVar> pVarP; 
    7676 
    77     // associated enum for checking type 
    78     typedef enum { 
    79         pVarNullType, 
    80         pVarBoolType, 
    81         pVarIntType, 
    82         pVarFloatType, 
    83         pVarBStringType, 
    84         pVarUStringType, 
    85         pVarHashType, 
    86         pVarObjectType, 
    87         pVarResourceType, 
    88         pVarRefType 
    89     } pVarType; 
     77// associated enum for checking type 
     78typedef enum { 
     79    pVarNullType, 
     80    pVarBoolType, 
     81    pVarIntType, 
     82    pVarFloatType, 
     83    pVarBStringType, 
     84    pVarUStringType, 
     85    pVarHashType, 
     86    pVarObjectType, 
     87    pVarResourceType, 
     88    pVarRefType 
     89} pVarType; 
    9090 
    9191} /* namespace rphp */