00001
00002
00003 #include "cppscript"
00004 #include "dynamic/extensions.hpp"
00005 #include <stdlib.h>
00006
00007
00008 static_call( register_pickle_type( cmp_int, 0 ) );
00009 static_call( register_pickle_type( cmp_char, ' ' ) );
00010 static_call( register_pickle_type( cmp_wchar, L' ') );
00011
00012
00013 void dynamic::write_int_to_stream(wostream & os, int i)
00014 {
00015 #if defined(__CYGWIN__)
00016 const int buf_size=40;
00017 char buf[buf_size];
00018 snprintf(buf, buf_size, "%d", i);
00019 for(char *i=buf; *i; ++i) os << wchar_t(*i);
00020 #else
00021
00022 os << i;
00023 #endif
00024 }
00025
00026
00027 namespace
00028 {
00029 void write_to_stream(wostream & os, int i)
00030 {
00031 write_int_to_stream(os, i);
00032 }
00033
00034 template<typename Int>
00035 void write_to_stream(ostream & os, Int i)
00036 {
00037 os << i;
00038 }
00039
00040 template<typename Int>
00041 void write_to_stream(wostream & os, Int i)
00042 {
00043 os << i;
00044 }
00045
00046
00047 template<typename>
00048 struct int_type;
00049
00050 template<> struct int_type<char>
00051 {
00052 static const var_cmp_index value = cmp_char;
00053 static const char * class_name() { return "char"; }
00054 };
00055
00056 template<> struct int_type<wchar_t>
00057 {
00058 static const var_cmp_index value = cmp_wchar;
00059 static const char * class_name() { return "char"; }
00060 };
00061
00062 template<> struct int_type<int>
00063 {
00064 static const var_cmp_index value = cmp_int;
00065 static const char * class_name() { return "int"; }
00066 };
00067 }
00068
00069 namespace dynamic
00070 {
00071 namespace types
00072 {
00074
00076 template<typename Ch>
00077 class int_impl : public dynamic::var_impl
00078 {
00079 int i;
00080 public:
00081 typedef Ch char_type;
00082
00083 int_impl(Ch i) : i(i) { }
00084
00085 void output(ostream & ss)
00086 {
00087 write_to_stream(ss, Ch(i));
00088 }
00089
00090 void output(wostream & os)
00091 {
00092 write_to_stream(os, Ch(i));
00093 }
00094
00095 int as_int()
00096 {
00097 return i;
00098 }
00099
00100 double as_double()
00101 {
00102 return i;
00103 }
00104
00105 var op_mul(const var & v)
00106 {
00107 return i * v.as_int();
00108 }
00109
00110 var op_div(const var & v)
00111 {
00112 return i / v.as_int();
00113 }
00114
00115 var op_mod(const var & v)
00116 {
00117 return i % v.as_int();
00118 }
00119
00120 var_cmp_index comparison_index()
00121 {
00122 return int_type<Ch>::value;
00123 }
00124
00125 var_cmp_result compare2(const var & v)
00126 {
00127 int diff = i - v.as_int();
00128 if(diff<0) return cmp_lt;
00129 if(diff>0) return cmp_gt;
00130 return cmp_equal;
00131 }
00132
00133 void op_inc()
00134 {
00135 ++i;
00136 }
00137
00138 void op_dec()
00139 {
00140 --i;
00141 }
00142
00143 var op_and(const var &a)
00144 {
00145 return i & a.as_int();
00146 }
00147
00148 var op_or(const var & a)
00149 {
00150 return i | a.as_int();
00151 }
00152
00153 var op_xor(const var & a)
00154 {
00155 return i ^ a.as_int();
00156 }
00157
00158 var op_inv()
00159 {
00160 return ~i;
00161 }
00162
00163 bool as_bool()
00164 {
00165 return i!=0;
00166 }
00167
00168 void copy_to(void * dest) const
00169 {
00170 new(dest) int_impl(*this);
00171 }
00172
00173 var op_lshift(const var & a)
00174 {
00175 return i << a.as_int();
00176 }
00177
00178 var op_rshift(const var & a)
00179 {
00180 return i >> a.as_int();
00181 }
00182
00183 var op_neg()
00184 {
00185 return -i;
00186 }
00187
00188 void assign_add(const var &a)
00189 {
00190 int ai = a.as_int();
00191 i += ai;
00192 }
00193
00194 void assign_sub(const var &a)
00195 {
00196 int ai = a.as_int();
00197 i -= ai;
00198 }
00199
00200 void assign_mul(const var &a)
00201 {
00202 int ai = a.as_int();
00203 i *= ai;
00204 }
00205
00206 void assign_div(const var &a)
00207 {
00208 int ai = a.as_int();
00209 i /= ai;
00210 }
00211
00212 void assign_mod(const var &a)
00213 {
00214 int ai = a.as_int();
00215 i %= ai;
00216 }
00217
00218 void assign_lshift(const var &a)
00219 {
00220 int ai = a.as_int();
00221 i <<= ai;
00222 }
00223
00224 void assign_rshift(const var &a)
00225 {
00226 int ai = a.as_int();
00227 i >>= ai;
00228 }
00229
00230 void assign_and(const var &a)
00231 {
00232 int ai = a.as_int();
00233 i &= ai;
00234 }
00235
00236 void assign_or(const var &a)
00237 {
00238 int ai = a.as_int();
00239 i |= ai;
00240 }
00241
00242 void assign_xor(const var &a)
00243 {
00244 int ai = a.as_int();
00245 i ^= ai;
00246 }
00247
00248 void unpickle(unpickler & u)
00249 {
00250 i = u.read_int();
00251 }
00252
00253 std::string class_name()
00254 {
00255 return int_type<Ch>::class_name();
00256 }
00257
00258 var op_add(const var & v)
00259 {
00260 return Ch(i + v.as_int());
00261 }
00262
00263 var op_sub(const var & v)
00264 {
00265 return Ch(i - v.as_int());
00266 }
00267
00268 var as_root()
00269 {
00270 return Ch(i);
00271 }
00272
00273 var as_nonroot()
00274 {
00275 return Ch(i);
00276 }
00277
00278 void pickle(pickler & p)
00279 {
00280 p.write_object_type(int_type<Ch>::value);
00281 p.write_int(i);
00282 }
00283
00284 void get_range(std::size_t container_size, std::pair<std::size_t,std::size_t> & out)
00285 {
00286 out.first = i<0 ? container_size+i : i;
00287 out.second=out.first+1;
00288 }
00289 };
00290 }
00291 }
00292
00293
00295
00296 var::var(int i) : m_variant(types::int_impl<int>(i)) { }
00297
00298 var::var(char ch) : m_variant(types::int_impl<char>(ch)) { }
00299
00300 var::var(wchar_t wc) : m_variant(types::int_impl<wchar_t>(wc)) { }