00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#ifndef __KSCRIPT_VALUE_H
00021
#define __KSCRIPT_VALUE_H
00022
00023
#include <qstring.h>
00024
#include <qvaluelist.h>
00025
#include <qmap.h>
00026
#include <qshared.h>
00027
#include <qdatetime.h>
00028
00029
#include "koscript_ptr.h"
00030
#include "koscript_types.h"
00031
00032
class KSFunction;
00033
class KSMethod;
00034
class KSContext;
00035
class KSProperty;
00036
class KSModule;
00037
class KSStruct;
00038
class KSStructClass;
00039
00040
typedef bool (KSStruct::*KSStructBuiltinMethod)( KSContext&,
const QString& );
00041
00047 class KSValue :
public QShared
00048 {
00049
public:
00050
typedef KSSharedPtr<KSValue> Ptr;
00051
00052
enum Type {
00053 Empty,
00054 StringType,
00055 IntType,
00056 BoolType,
00057 DoubleType,
00058 ListType,
00059 MapType,
00060 CharType,
00061 CharRefType,
00062 FunctionType,
00063 MethodType,
00064 PropertyType,
00065 ModuleType,
00066 StructType,
00067 StructClassType,
00068 StructBuiltinMethodType,
00069 DateType,
00070 TimeType,
00071 NTypes
00072 };
00073
00074
enum Mode {
00075 LeftExpr,
00076 Constant,
00077 Temp
00078 };
00079
00080
KSValue();
00081
KSValue( Type );
00082
KSValue(
const KSValue& );
00083
virtual ~
KSValue();
00084
00085
KSValue(
const QString& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00086
KSValue(
const QValueList<Ptr>& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00087
KSValue(
const QMap<QString,Ptr>& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00088
KSValue( KScript::Long _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00089
KSValue(
int _v ) { m_mode = Temp; typ = Empty; setValue( (KScript::Long)_v ); }
00090
KSValue( KScript::Boolean _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00091
KSValue( KScript::Double _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00092
KSValue(
const KScript::Char& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00093
KSValue(
const KScript::CharRef& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00094
KSValue( KSFunction* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00095
KSValue(
KSMethod* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00096
KSValue( KSProperty* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00097
KSValue(
KSModule* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00098
KSValue( KSStruct* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00099
KSValue( KSStructClass* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00100
KSValue( KSStructBuiltinMethod _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00101
KSValue(
const QTime& t ) { m_mode = Temp; typ = Empty; setValue( t ); }
00102
KSValue(
const QDate& d ) { m_mode = Temp; typ = Empty; setValue( d ); }
00103
00104
KSValue& operator= (
const KSValue& );
00105
00106
void setValue(
const QString& );
00107
void setValue(
const QValueList<Ptr>& );
00108
void setValue(
const QMap<QString,Ptr>& );
00109
void setValue(
int _v ) { setValue( (KScript::Long)_v ); }
00110
void setValue( KScript::Long );
00111
void setValue( KScript::Boolean );
00112
void setValue( KScript::Double );
00113
void setValue(
const KScript::Char& );
00114
void setValue(
const KScript::CharRef& );
00115
void setValue( KSFunction* );
00116
void setValue(
KSMethod* );
00117
void setValue( KSProperty* );
00118
void setValue(
KSModule* );
00119
void setValue( KSStruct* );
00120
void setValue( KSStructClass* );
00121
void setValue( KSStructBuiltinMethod );
00122
void setValue(
const QDate& );
00123
void setValue(
const QTime& );
00124
00125
void suck(
KSValue* );
00126
00127 Mode mode()
const {
return m_mode; }
00128
void setMode( Mode m ) { m_mode = m; }
00129
00130 Type type()
const {
return typ; }
00131
virtual QString typeName()
const;
00132
00133
bool isEmpty()
const {
return ( typ == Empty ); }
00134
00135
const QDate& dateValue()
const { Q_ASSERT( typ == DateType );
return *((
QDate*)val.ptr); }
00136
QDate& dateValue() { Q_ASSERT( typ == DateType );
return *((
QDate*)val.ptr); }
00137
00138
const QTime& timeValue()
const { Q_ASSERT( typ == TimeType );
return *((
QTime*)val.ptr); }
00139
QTime& timeValue() { Q_ASSERT( typ == TimeType );
return *((
QTime*)val.ptr); }
00140
00141
const QString& stringValue()
const { Q_ASSERT( typ == StringType );
return *((
QString*)val.ptr); }
00142
QString& stringValue() { Q_ASSERT( typ == StringType );
return *((
QString*)val.ptr); }
00143
const QValueList<Ptr>& listValue()
const { Q_ASSERT( typ == ListType );
return *((
QValueList<Ptr>*)val.ptr); }
00144
QValueList<Ptr>& listValue() { Q_ASSERT( typ == ListType );
return *((
QValueList<Ptr>*)val.ptr); }
00145
const QMap<QString,Ptr>& mapValue()
const { Q_ASSERT( typ == MapType );
return *((
QMap<QString,Ptr>*)val.ptr); }
00146
QMap<QString,Ptr>& mapValue() { Q_ASSERT( typ == MapType );
return *((
QMap<QString,Ptr>*)val.ptr); }
00147 KScript::Long intValue()
const { Q_ASSERT( typ == IntType || typ == DoubleType );
if ( typ == IntType )
return val.i;
return (
int)val.d; }
00148 KScript::Boolean boolValue()
const { Q_ASSERT( typ == IntType || typ == DoubleType || typ == BoolType || typ == StringType );
00149
if ( typ == BoolType )
return val.b;
if( typ == DoubleType )
return ( doubleValue() != 0 );
00150
if ( typ == IntType )
return intValue() != 0;
return !stringValue().isEmpty(); }
00151 KScript::Double doubleValue()
const { Q_ASSERT( typ == DoubleType || typ == IntType );
if ( typ == DoubleType )
return val.d;
00152
return (
double)val.i; }
00153
KScript::Char charValue()
const {
if ( typ == CharRefType )
return *((KScript::CharRef*)val.ptr);
00154 Q_ASSERT( typ == CharType );
return QChar( val.c ); }
00155 KScript::CharRef& charRefValue() { Q_ASSERT( typ == CharRefType );
return *((KScript::CharRef*)val.ptr); }
00156
const KScript::CharRef& charRefValue()
const { Q_ASSERT( typ == CharRefType );
return *((KScript::CharRef*)val.ptr); }
00157 KSFunction* functionValue() { Q_ASSERT( typ == FunctionType );
return ((KSFunction*)val.ptr); }
00158
const KSFunction* functionValue()
const { Q_ASSERT( typ == FunctionType );
return ((KSFunction*)val.ptr); }
00159
KSMethod* methodValue() { Q_ASSERT( typ == MethodType );
return ((
KSMethod*)val.ptr); }
00160
const KSMethod* methodValue()
const { Q_ASSERT( typ == MethodType );
return ((
KSMethod*)val.ptr); }
00161 KSProperty* propertyValue() { Q_ASSERT( typ == PropertyType );
return ((KSProperty*)val.ptr); }
00162
const KSProperty* propertyValue()
const { Q_ASSERT( typ == PropertyType );
return ((KSProperty*)val.ptr); }
00163
KSModule* moduleValue() { Q_ASSERT( typ == ModuleType );
return ((
KSModule*)val.ptr); }
00164
const KSModule* moduleValue()
const { Q_ASSERT( typ == ModuleType );
return ((
KSModule*)val.ptr); }
00165 KSStructClass* structClassValue() { Q_ASSERT( typ == StructClassType );
return ((KSStructClass*)val.ptr); }
00166
const KSStructClass* structClassValue()
const { Q_ASSERT( typ == StructClassType );
return ((KSStructClass*)val.ptr); }
00167 KSStruct* structValue() { Q_ASSERT( typ == StructType );
return ((KSStruct*)val.ptr); }
00168
const KSStruct* structValue()
const { Q_ASSERT( typ == StructType );
return ((KSStruct*)val.ptr); }
00169 KSStructBuiltinMethod structBuiltinMethodValue() { Q_ASSERT( typ == StructBuiltinMethodType );
return val.sm; }
00170
00174
bool cast( Type );
00175
00180
bool implicitCast( Type typ )
const;
00181
00182
QString toString( KSContext& context );
00183
00184
bool operator==(
const KSValue& v )
const;
00185
00186
bool cmp(
const KSValue& v )
const;
00187
00191
void clear();
00192
00193
static QString typeToName( Type _typ );
00197
static Type
nameToType(
const QString& _name );
00198
00203 static KSValue*
null() {
if ( !s_null ) s_null =
new KSValue; s_null->ref();
return s_null; }
00204
00205
protected:
00206
00207 Mode m_mode;
00208 Type typ;
00209
union
00210
{
00211 KScript::Long i;
00212 KScript::Boolean b;
00213 KScript::Double d;
00214 ushort c;
00215
void *ptr;
00216 KSStructBuiltinMethod sm;
00217
QDate* date;
00218
QTime* time;
00219 } val;
00220
00221
private:
00222
static void initTypeNameMap();
00223
static KSValue* s_null;
00224 };
00225
00226
#endif
00227