Mixing floats and ints is possible now!

functions
Justyna Ilczuk 2012-12-22 11:40:57 +01:00
parent f0bc0e235b
commit b75236096e
1 changed files with 141 additions and 108 deletions

View File

@ -30,63 +30,63 @@ std::string SenchaObject::repr()
std::string representation = ""; std::string representation = "";
switch(type){ switch(type){
case string_literal: case string_literal:
representation = "type: string\n"; representation = "type: string\n";
representation += this->text; representation += this->text;
break; break;
case integer_number: case integer_number:
representation = "type: integer\n"; representation = "type: integer\n";
representation += to_string(this->integer); representation += to_string(this->integer);
break; break;
case float_number: case float_number:
representation = "type: float\n"; representation = "type: float\n";
representation += to_string(this->number); representation += to_string(this->number);
break; break;
case null: case null:
representation = "type: null\n"; representation = "type: null\n";
representation += "null"; representation += "null";
break; break;
case symbol: case symbol:
representation = "type: symbol\n"; representation = "type: symbol\n";
representation += this->text; representation += this->text;
break; break;
case invalid: case invalid:
representation = "type: invalid\n"; representation = "type: invalid\n";
representation += "some crap"; representation += "some crap";
break; break;
case boolean: case boolean:
representation = "type: boolean\n"; representation = "type: boolean\n";
representation += to_string(this->truthy); representation += to_string(this->truthy);
break; break;
} }
return representation; return representation;
} }
std::string SenchaObject::str() std::string SenchaObject::str()
{ {
std::string representation = ""; std::string representation = "";
switch(type){ switch(type){
case string_literal: case string_literal:
representation += this->text; representation += this->text;
break; break;
case integer_number: case integer_number:
representation += to_string(this->integer); representation += to_string(this->integer);
break; break;
case float_number: case float_number:
representation += to_string(this->number); representation += to_string(this->number);
break; break;
case null: case null:
representation += "null"; representation += "null";
break; break;
case symbol: case symbol:
representation += this->text; representation += this->text;
break; break;
case invalid: case invalid:
representation += "some crap"; representation += "some crap";
break; break;
case boolean: case boolean:
representation += to_string(this->truthy); representation += to_string(this->truthy);
break; break;
} }
return representation; return representation;
} }
SenchaObject SenchaObject::operator+(const SenchaObject& right)const SenchaObject SenchaObject::operator+(const SenchaObject& right)const
@ -106,6 +106,14 @@ SenchaObject SenchaObject::operator+(const SenchaObject& right)const
break; break;
} }
} }
else if(type == float_number && right.type == integer_number )
{
result.set_value(this->number + right.integer);
}
else if(type == integer_number && right.type == float_number)
{
result.set_value(this->integer + right.number);
}
else else
{ {
result.type = invalid; result.type = invalid;
@ -118,51 +126,67 @@ SenchaObject SenchaObject::operator+(const SenchaObject& right)const
SenchaObject SenchaObject::operator-(const SenchaObject& right)const SenchaObject SenchaObject::operator-(const SenchaObject& right)const
{ {
SenchaObject result; SenchaObject result;
if(type == right.type) if(type == right.type)
{ {
switch(type){ switch(type){
case string_literal: case string_literal:
result.type = invalid;
break;
case integer_number:
result.set_value(this->integer - right.integer);
break;
case float_number:
result.set_value(this->number - right.number);
break;
}
}
else
{
result.type = invalid; result.type = invalid;
break;
case integer_number:
result.set_value(this->integer - right.integer);
break;
case float_number:
result.set_value(this->number - right.number);
break;
} }
}
else if(type == float_number && right.type == integer_number )
{
result.set_value(this->number - right.integer);
}
else if(type == integer_number && right.type == float_number)
{
result.set_value(this->integer - right.number);
}
else
{
result.type = invalid;
}
return result; return result;
} }
SenchaObject SenchaObject::operator*(const SenchaObject& right)const SenchaObject SenchaObject::operator*(const SenchaObject& right)const
{ {
SenchaObject result; SenchaObject result;
if(type == right.type) if(type == right.type)
{ {
switch(type){ switch(type){
case string_literal: case string_literal:
result.type = invalid; result.type = invalid;
break; break;
case integer_number: case integer_number:
result.set_value(this->integer * right.integer); result.set_value(this->integer * right.integer);
break; break;
case float_number: case float_number:
result.set_value(this->number * right.number); result.set_value(this->number * right.number);
break; break;
} }
} }
else else if(type == float_number && right.type == integer_number )
{ {
result.type = invalid; result.set_value(this->number * right.integer);
} }
else if(type == integer_number && right.type == float_number)
{
result.set_value(this->integer * right.number);
}
else
{
result.type = invalid;
}
return result; return result;
} }
SenchaObject SenchaObject::operator/(const SenchaObject& right)const SenchaObject SenchaObject::operator/(const SenchaObject& right)const
@ -182,6 +206,14 @@ SenchaObject SenchaObject::operator/(const SenchaObject& right)const
break; break;
} }
} }
else if(type == float_number && right.type == integer_number )
{
result.set_value(this->number / right.integer);
}
else if(type == integer_number && right.type == float_number)
{
result.set_value(this->integer / right.number);
}
else else
{ {
result.type = invalid; result.type = invalid;
@ -191,40 +223,41 @@ SenchaObject SenchaObject::operator/(const SenchaObject& right)const
} }
SenchaObject SenchaObject::operator==(const SenchaObject& right)const SenchaObject SenchaObject::operator==(const SenchaObject& right)const
{ {
SenchaObject result; SenchaObject result;
bool value = false; bool value = false;
result.type = boolean; result.type = boolean;
if(type == right.type) if(type == right.type)
{ {
switch(type){ switch(type){
case string_literal: case string_literal:
if(this->text == right.text) if(this->text == right.text)
{ {
value = true; value = true;
}
break;
case integer_number:
if(this->integer == right.integer)
{
value = true;
}
break;
case float_number:
if(this->number == right.number)
{
value = true;
}
break;
} }
break;
case integer_number:
if(this->integer == right.integer)
{
value = true;
}
break;
case float_number:
if(this->number == right.number)
{
value = true;
}
break;
}
}
result.truthy = value;
return result;
} }
result.truthy = value;
return result;
}
SenchaObject SenchaObject::operator!=(const SenchaObject& right)const SenchaObject SenchaObject::operator!=(const SenchaObject& right)const
{ {
SenchaObject result; SenchaObject result;
result.type = boolean; result.type = boolean;
@ -234,6 +267,6 @@ SenchaObject SenchaObject::operator!=(const SenchaObject& right)const
else result.truthy = true; else result.truthy = true;
return result; return result;
} }
//TODO change code above to something more generic //TODO change code above to something more generic