Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

celx::Value Class Reference

#include <value.h>

Collaboration diagram for celx::Value:

Collaboration graph
List of all members.

Public Member Functions

bool booleanValue (bool &) const
bool functionValue (Function *&) const
Type getType () const
bool numberValue (double &) const
bool operator!= (const Value &) const
Valueoperator= (const Value &)
bool operator== (const Value &) const
void output (std::ostream &) const
bool stringValue (std::string &) const
bool toBoolean () const
double toNumber () const
std::string toString () const
 Value (Function *)
 Value (bool)
 Value (const std::string &)
 Value (double)
 Value (const Value &)
 Value ()
 ~Value ()

Private Attributes

Type type
union {
   bool   boolVal
   Function *   funcVal
   double   numVal
   const std::string *   strVal
val

Constructor & Destructor Documentation

Value::Value  ) 
 

Definition at line 19 of file value.cpp.

References celx::NilType.

00019              :
00020     type(NilType)
00021 {
00022 }

Value::Value const Value  ) 
 

Definition at line 67 of file value.cpp.

References celx::NilType.

00067                            :
00068     type(NilType)
00069 {
00070     *this = v;
00071 }

Value::Value double   ) 
 

Definition at line 38 of file value.cpp.

References celx::NumberType, and val.

00038                      :
00039     type(NumberType)
00040 {
00041     val.numVal = x;
00042 }

Value::Value const std::string  ) 
 

Definition at line 45 of file value.cpp.

References celx::StringType, and val.

00045                             :
00046     type(StringType)
00047 {
00048     val.strVal = new string(x);
00049 }

Value::Value bool   ) 
 

Definition at line 52 of file value.cpp.

References celx::BooleanType, and val.

00052                    :
00053     type(BooleanType)
00054 {
00055     val.boolVal = x;
00056 }

Value::Value Function  ) 
 

Definition at line 59 of file value.cpp.

References celx::FunctionType, and val.

00059                         :
00060     type(FunctionType)
00061 {
00062     val.funcVal = new Function(*f);
00063 }

Value::~Value  ) 
 

Definition at line 25 of file value.cpp.

References celx::FunctionType, celx::StringType, type, and val.

00026 {
00027     if (type == StringType)
00028     {
00029         delete val.strVal;
00030     }
00031     else if (type == FunctionType)
00032     {
00033         delete val.funcVal;
00034     }
00035 }


Member Function Documentation

bool celx::Value::booleanValue bool &   )  const [inline]
 

Definition at line 69 of file value.h.

00070 {
00071     if (type != BooleanType)
00072     {
00073         return false;
00074     }
00075     else
00076     {
00077         x = val.boolVal;
00078         return true;
00079     }
00080 }

bool celx::Value::functionValue Function *&   )  const [inline]
 

Definition at line 108 of file value.h.

References celx::FunctionType.

Referenced by celx::FunctionCallExpression::eval().

00109 {
00110     if (type != FunctionType)
00111     {
00112         return false;
00113     }
00114     else
00115     {
00116         f = val.funcVal;
00117         return true;
00118     }
00119 }

Type celx::Value::getType  )  const [inline]
 

Definition at line 64 of file value.h.

00065 {
00066     return type;
00067 }

bool celx::Value::numberValue double &   )  const [inline]
 

Definition at line 82 of file value.h.

Referenced by AddFunc(), DivideFunc(), MultiplyFunc(), and SubtractFunc().

00083 {
00084     if (type != NumberType)
00085     {
00086         return false;
00087     }
00088     else
00089     {
00090         x = val.numVal;
00091         return true;
00092     }
00093 }

bool Value::operator!= const Value  )  const
 

Definition at line 141 of file value.cpp.

00142 {
00143     return !(*this == v);
00144 }

Value & Value::operator= const Value  ) 
 

Definition at line 74 of file value.cpp.

References celx::BooleanType, celx::FunctionType, funcVal, celx::NilType, celx::NumberType, celx::StringType, strVal, type, and val.

00075 {
00076     if (this != &v)
00077     {
00078         // Clean up the current contents first
00079         switch (type)
00080         {
00081         case StringType:
00082             delete val.strVal;
00083             break;
00084         case FunctionType:
00085             delete val.funcVal;
00086             break;
00087         default:
00088             break;
00089         }
00090 
00091         type = v.type;
00092         switch (type)
00093         {
00094         case NumberType:
00095             val.numVal = v.val.numVal;
00096             break;
00097         case StringType:
00098             val.strVal = new string(*v.val.strVal);
00099             break;
00100         case BooleanType:
00101             val.boolVal = v.val.boolVal;
00102             break;
00103         case NilType:
00104             break;
00105         case FunctionType:
00106             val.funcVal = new Function(*v.val.funcVal);
00107             break;
00108         default:
00109             assert(0);
00110             break;
00111         }
00112     }
00113 
00114     return *this;
00115 }

bool Value::operator== const Value  )  const
 

Definition at line 118 of file value.cpp.

References celx::BooleanType, celx::FunctionType, celx::NilType, celx::NumberType, celx::StringType, type, and val.

00119 {
00120     if (v.type != type)
00121         return false;
00122 
00123     switch (type)
00124     {
00125     case BooleanType:
00126         return v.val.boolVal == val.boolVal;
00127     case NumberType:
00128         return v.val.numVal == val.numVal;
00129     case StringType:
00130         return *v.val.strVal == *val.strVal;
00131     case FunctionType:
00132         return v.val.funcVal == val.funcVal;
00133     case NilType:
00134         return true;
00135     default:
00136         return false;
00137     }
00138 }

void Value::output std::ostream &   )  const
 

Definition at line 207 of file value.cpp.

References celx::BooleanType, celx::FunctionType, celx::NilType, celx::NumberType, celx::StringType, type, and val.

00208 {
00209     switch (type)
00210     {
00211     case NilType:
00212         out << "null";
00213         break;
00214     case BooleanType:
00215         if (val.boolVal)
00216             out << "true";
00217         else
00218             out << "false";
00219         break;
00220     case NumberType:
00221         out << val.numVal;
00222         break;
00223     case StringType:
00224         out << '"' << *val.strVal << '"';
00225         break;
00226     case FunctionType:
00227         out << "[function]";
00228         break;
00229     default:
00230         out << "#unknown";
00231         break;
00232     }
00233 }

bool celx::Value::stringValue std::string  )  const [inline]
 

Definition at line 95 of file value.h.

00096 {
00097     if (type != StringType)
00098     {
00099         return false;
00100     }
00101     else
00102     {
00103         x = *val.strVal;
00104         return true;
00105     }
00106 }

bool Value::toBoolean  )  const
 

Definition at line 147 of file value.cpp.

References celx::BooleanType, celx::NumberType, celx::StringType, type, and val.

Referenced by celx::WhileStatement::execute(), and celx::IfStatement::execute().

00148 {
00149     switch (type)
00150     {
00151     case BooleanType:
00152         return val.boolVal;
00153     case NumberType:
00154         return val.numVal != 0.0;
00155     case StringType:
00156         return !val.strVal->empty();
00157     default:
00158         return false;
00159     }
00160 }

double Value::toNumber  )  const
 

Definition at line 163 of file value.cpp.

References celx::BooleanType, celx::NumberType, celx::StringType, type, and val.

00164 {
00165     switch (type)
00166     {
00167     case BooleanType:
00168         return val.boolVal ? 1.0 : 0.0;
00169     case NumberType:
00170         return val.numVal;
00171     case StringType:
00172         return 0.0; // TODO: attempt conversion to number
00173     default:
00174         return 0.0;
00175     }
00176 }

string Value::toString  )  const
 

Definition at line 179 of file value.cpp.

References celx::BooleanType, celx::NilType, celx::NumberType, celx::StringType, type, and val.

00180 {
00181     switch (type)
00182     {
00183     case BooleanType:
00184         if (val.boolVal)
00185             return string("true");
00186         else
00187             return string("false");
00188     case NumberType:
00189         {
00190             char buf[256];
00191             sprintf(buf, "%f", val.numVal);
00192             return string(buf);
00193         }
00194 
00195     case StringType:
00196         return *val.strVal;
00197 
00198     case NilType:
00199         return string("null");
00200 
00201     default:
00202         return string("");
00203     }
00204 }


Member Data Documentation

bool celx::Value::boolVal [private]
 

Definition at line 57 of file value.h.

Function* celx::Value::funcVal [private]
 

Definition at line 60 of file value.h.

Referenced by operator=().

double celx::Value::numVal [private]
 

Definition at line 58 of file value.h.

const std::string* celx::Value::strVal [private]
 

Definition at line 59 of file value.h.

Referenced by operator=().

Type celx::Value::type [private]
 

Definition at line 54 of file value.h.

Referenced by operator=(), operator==(), output(), toBoolean(), toNumber(), toString(), and ~Value().

union { ... } celx::Value::val [private]
 

Referenced by operator=(), operator==(), output(), toBoolean(), toNumber(), toString(), Value(), and ~Value().


The documentation for this class was generated from the following files:
Generated on Sat Jan 14 22:33:45 2006 for Celestia by  doxygen 1.4.1