Permalink
Browse files

add and sub vtables, remove print vtables

refactor operators add and sub
print objects via get_string
  • Loading branch information...
1 parent da7f6ab commit 4e6e1316c0905da3f9ebbe17f75467b490ba3918 @NotFound committed May 28, 2012
Showing with 145 additions and 103 deletions.
  1. +4 −0 t/base/features.t
  2. +78 −3 winxedxx.h
  3. +11 −51 winxedxx.winxed
  4. +9 −5 winxedxx_classes.cxx
  5. +19 −11 winxedxx_default.cxx
  6. +3 −2 winxedxx_default.h
  7. +2 −3 winxedxx_integer.h
  8. +12 −10 winxedxx_null.cxx
  9. +2 −2 winxedxx_null.h
  10. +2 −2 winxedxx_object.h
  11. +1 −13 winxedxx_objectptr.cxx
  12. +2 −1 winxedxx_types.h
View
@@ -386,6 +386,10 @@ function main [main] (args)
test.is_string(str + vi1, "2.542", "string + var");
test.is_string(vi1 + str, "422.5", "var + string");
+ test.is(i - 2, 40, "int - int");
+ test.is(i - vi1, 0, "int - var");
+ test.is(vi1 - i, 0, "var - int");
+
string s = "abc";
test.is_string(s * 3, "abcabcabc", "repeat string");
View
@@ -167,14 +167,13 @@ int wxx_print(int i) { std::cout << i; return 0; }
int wxx_print(const char *s) { std::cout << s; return 0; }
int wxx_print(const std::string &s) { std::cout << s; return 0; }
int wxx_print(double n) { std::cout << n; return 0; }
-int wxx_print(WxxObjectPtr obj) { obj.print(); return 0; }
+int wxx_print(WxxObjectPtr obj) { std::cout << obj.get_string(); return 0; }
int wxx_eprint(int i) { std::cerr << i; return 0; }
int wxx_eprint(const char *s) { std::cerr << s; return 0; }
int wxx_eprint(const std::string &s) { std::cerr << s; return 0; }
int wxx_eprint(double n) { std::cerr << n; return 0; }
-// TODO
-int wxx_eprint(WxxObjectPtr obj) { obj.print(); return 0; }
+int wxx_eprint(WxxObjectPtr obj) { std::cerr << obj.get_string(); return 0; }
int wxx_instanceof(WxxObjectPtr &obj, const std::string &type)
{
@@ -291,6 +290,82 @@ int operator <= (int i, const WxxObjectPtr &obj)
return i <= int(obj);
}
+//*********** operator + ****************
+
+int wxx_add(int i1, int i2)
+{
+ return i1 + i2;
+}
+
+double wxx_add(double f1, double f2)
+{
+ return f1 + f2;
+}
+
+double wxx_add(double f1, int i2)
+{
+ return f1 + i2;
+}
+
+double wxx_add(int i1, double f2)
+{
+ return i1 + f2;
+}
+
+std::string wxx_add(int i1, std::string s2)
+{
+ return wxx_int_to_string(i1) + s2;
+}
+
+std::string wxx_add(std::string s1, int i2)
+{
+ return s1 + wxx_int_to_string(i2);
+}
+
+WxxObjectPtr wxx_add(int i1, const WxxObjectPtr &o2)
+{
+ return i1 + int(o2);
+}
+
+WxxObjectPtr wxx_add(const WxxObjectPtr &o1, int i2)
+{
+ return int(o1) + i2;
+}
+
+//*********** operator - ****************
+
+int wxx_sub(int i1, int i2)
+{
+ return i1 - i2;
+}
+
+double wxx_sub(double f1, double f2)
+{
+ return f1 - f2;
+}
+
+double wxx_sub(double f1, int i2)
+{
+ return f1 - i2;
+}
+
+double wxx_sub(int i1, double f2)
+{
+ return i1 - f2;
+}
+
+WxxObjectPtr wxx_sub(int i1, const WxxObjectPtr &o2)
+{
+ return i1 - int(o2);
+}
+
+WxxObjectPtr wxx_sub(const WxxObjectPtr &o1, int i2)
+{
+ return int(o1) - i2;
+}
+
+//*****************************************
+
template <typename NciSig, int nargs>
WxxObjectPtr wxx_dlfunc(WxxObjectPtr lib,
std::string funcname)
View
@@ -562,6 +562,15 @@ function emit_CallBuiltinExpr(out, expr)
//**********************************************************************
+function emit_plainfun2(var out, string name, var arg1, var arg2)
+{
+ out.print(name, "(");
+ emit_expr(out, arg1);
+ out.print(", ");
+ emit_expr(out, arg2);
+ out.print(")");
+}
+
function emit_callargs(out, args)
{
out.print("WinxedXX::WxxObjectArray()");
@@ -859,61 +868,12 @@ function emit_OpMulToExpr(out, assignexpr)
function emit_OpAddExpr(out, expr)
{
- var lexpr = expr.lexpr;
- var rexpr = expr.rexpr;
- string ltype = lexpr.checkresult();
- string rtype = rexpr.checkresult();
- switch {
- case ltype == rtype:
- emit_expr(out, lexpr);
- out.print(" + ");
- emit_expr(out, rexpr);
- break;
- case ltype == REGvar && rtype == REGint:
- out.print(Type_Int + "(");
- emit_expr(out, lexpr);
- out.print(") + ");
- emit_expr(out, rexpr);
- break;
- case ltype == REGint && rtype == REGvar:
- emit_expr(out, lexpr);
- out.print(" + " + Type_Int + "(");
- emit_expr(out, rexpr);
- out.print(")");
- break;
- case ltype == REGvar && rtype == REGstring:
- emit_expr(out, lexpr);
- out.print(".get_string() + ");
- emit_expr(out, rexpr);
- break;
- case ltype == REGstring && rtype == REGvar:
- emit_expr(out, lexpr);
- out.print(" + ");
- emit_expr(out, rexpr);
- out.print(".get_string()");
- break;
- case ltype == REGstring && rtype == REGint:
- emit_expr(out, lexpr);
- out.print(" + wxx_int_to_string(");
- emit_expr(out, rexpr);
- out.print(")");
- break;
- case ltype == REGint && rtype == REGstring:
- out.print("wxx_int_to_string(");
- emit_expr(out, lexpr);
- out.print(") + ");
- emit_expr(out, rexpr);
- break;
- default:
- WxxUnsupported("operator + (" + ltype + ", " + rtype + ")");
- }
+ emit_plainfun2(out, "wxx_add", expr.lexpr, expr.rexpr);
}
function emit_OpSubExpr(out, expr)
{
- emit_expr(out, expr.lexpr);
- out.print(" - ");
- emit_expr(out, expr.rexpr);
+ emit_plainfun2(out, "wxx_sub", expr.lexpr, expr.rexpr);
}
function emit_RepeatString(out, expr)
View
@@ -97,7 +97,15 @@ void WxxInteger::decrement()
--i;
}
-void WxxInteger::print() { std::cout << i; }
+WxxObjectPtr WxxInteger::add(const WxxObjectPtr &value)
+{
+ return new WxxInteger(i + int(value));
+}
+
+WxxObjectPtr WxxInteger::sub(const WxxObjectPtr &value)
+{
+ return new WxxInteger(i - int(value));
+}
//*************************************************************
@@ -134,8 +142,6 @@ WxxObject & WxxFloat::set(double value)
return *this;
}
-void WxxFloat::print() { std::cout << n; }
-
//*************************************************************
WxxString::WxxString(std::string value) : WxxDefault("String")
@@ -167,8 +173,6 @@ WxxObjectPtr WxxString::get_iter()
return WxxObjectPtr(new WxxStringIterator(str));
}
-void WxxString::print() { std::cout << str; }
-
//*************************************************************
WxxArrayBase::WxxArrayBase(const std::string &name) :
View
@@ -7,12 +7,18 @@
#include <iostream>
#include <sstream>
+#include <stdexcept>
namespace WinxedXX
{
//*************************************************************
+WxxDefault::WxxDefault()
+{
+ throw std::runtime_error("Default instantiated");
+}
+
WxxDefault::WxxDefault(const char *name)
{
this->name = name;
@@ -112,9 +118,21 @@ void WxxDefault::decrement()
notimplemented("decrement");
}
+WxxObjectPtr WxxDefault::add(const WxxObjectPtr &value)
+{
+ notimplemented("add");
+ return 0;
+}
+
+WxxObjectPtr WxxDefault::sub(const WxxObjectPtr &value)
+{
+ notimplemented("sub");
+ return 0;
+}
+
std::string WxxDefault::get_string_keyed(int i)
{
- return get_pmc_keyed(i);
+ return get_pmc_keyed(i).get_string();
}
WxxObjectPtr WxxDefault::get_pmc_keyed(int i)
@@ -190,16 +208,6 @@ WxxObjectPtr WxxDefault::call_method(const std::string &methname, WxxObjectArray
return winxedxxnull;
}
-void WxxDefault::print()
-{
- std::cout << this->get_string();
-}
-
-void WxxDefault::print(WxxObjectPtr)
-{
- notimplemented("print");
-}
-
void WxxDefault::notimplemented(const std::string &funcname)
{
throw wxx_error(funcname + " not implemented in " + name);
View
@@ -11,6 +11,7 @@ namespace WinxedXX
class WxxDefault : public WxxObject
{
protected:
+ WxxDefault();
WxxDefault(const char *name);
WxxDefault(const std::string &name);
~WxxDefault();
@@ -31,6 +32,8 @@ class WxxDefault : public WxxObject
WxxObject & set(const std::string &s);
void increment();
void decrement();
+ WxxObjectPtr add(const WxxObjectPtr &value);
+ WxxObjectPtr sub(const WxxObjectPtr &value);
std::string get_string_keyed(int i);
WxxObjectPtr get_pmc_keyed(int i);
WxxObjectPtr get_pmc_keyed(const std::string &s);
@@ -44,8 +47,6 @@ class WxxDefault : public WxxObject
WxxObjectPtr shift_pmc();
WxxObjectPtr operator()(WxxObjectArray &args);
WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args);
- void print();
- void print(WxxObjectPtr);
private:
std::string name;
std::map<std::string, WxxObjectPtr> attributes;
View
@@ -32,8 +32,9 @@ class WxxInteger : public WxxDefault
WxxObject & set(int value);
WxxObject & set(double value);
void increment();
+ WxxObjectPtr add(const WxxObjectPtr &value);
+ WxxObjectPtr sub(const WxxObjectPtr &value);
void decrement();
- void print();
private:
int i;
};
@@ -48,7 +49,6 @@ class WxxFloat : public WxxDefault
std::string get_string();
WxxObject & set(int value);
WxxObject & set(double value);
- void print();
private:
double n;
};
@@ -62,7 +62,6 @@ class WxxString : public WxxDefault
WxxObject & set(const char *s);
WxxObject & set(const std::string &s);
WxxObjectPtr get_iter();
- void print();
private:
std::string str;
};
View
@@ -96,6 +96,18 @@ void WxxNull::decrement()
nullaccess("decrement");
}
+WxxObjectPtr WxxNull::add(const WxxObjectPtr &value)
+{
+ nullaccess("ass");
+ return 0;
+}
+
+WxxObjectPtr WxxNull::sub(const WxxObjectPtr &value)
+{
+ nullaccess("ass");
+ return 0;
+}
+
std::string WxxNull::get_string_keyed(int i)
{
nullaccess("get_pmc_keyed");
@@ -172,16 +184,6 @@ WxxObjectPtr WxxNull::call_method(const std::string &methname, WxxObjectArray &a
return winxedxxnull;
}
-void WxxNull::print()
-{
- nullaccess("print");
-}
-
-void WxxNull::print(WxxObjectPtr)
-{
- nullaccess("print");
-}
-
void WxxNull::nullaccess(const std::string &funcname)
{
throw wxx_error("Null WxxObject in " + funcname);
View
@@ -26,6 +26,8 @@ class WxxNull : public WxxObject
WxxObject & set(const std::string &s);
void increment();
void decrement();
+ WxxObjectPtr add(const WxxObjectPtr &value);
+ WxxObjectPtr sub(const WxxObjectPtr &value);
std::string get_string_keyed(int i);
WxxObjectPtr get_pmc_keyed(int i);
WxxObjectPtr get_pmc_keyed(const std::string &s);
@@ -39,8 +41,6 @@ class WxxNull : public WxxObject
WxxObjectPtr shift_pmc();
WxxObjectPtr operator()(WxxObjectArray &args);
WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args);
- void print();
- void print(WxxObjectPtr);
private:
void nullaccess(const std::string &funcname);
};
Oops, something went wrong.

0 comments on commit 4e6e131

Please sign in to comment.