239 lines
4.3 KiB
C++
239 lines
4.3 KiB
C++
/*
|
|
* SenchaObject.cpp
|
|
*
|
|
* Created on: Dec 5, 2012
|
|
* Author: attero
|
|
*/
|
|
|
|
#include "SenchaObject.h"
|
|
|
|
SenchaObject::SenchaObject() {
|
|
set_null_value();
|
|
}
|
|
|
|
SenchaObject::~SenchaObject() {
|
|
// TODO Auto-generated destructor stub
|
|
}
|
|
|
|
bool SenchaObject::is_true()
|
|
{
|
|
if(type == invalid || type == null)
|
|
{
|
|
return false;
|
|
}
|
|
if(type == boolean) return truthy;
|
|
else return true;
|
|
|
|
}
|
|
std::string SenchaObject::repr()
|
|
{
|
|
std::string representation = "";
|
|
switch(type){
|
|
case string_literal:
|
|
representation = "type: string\n";
|
|
representation += this->text;
|
|
break;
|
|
case integer_number:
|
|
representation = "type: integer\n";
|
|
representation += to_string(this->integer);
|
|
break;
|
|
case float_number:
|
|
representation = "type: float\n";
|
|
representation += to_string(this->number);
|
|
break;
|
|
case null:
|
|
representation = "type: null\n";
|
|
representation += "null";
|
|
break;
|
|
case symbol:
|
|
representation = "type: symbol\n";
|
|
representation += this->text;
|
|
break;
|
|
case invalid:
|
|
representation = "type: invalid\n";
|
|
representation += "some crap";
|
|
break;
|
|
case boolean:
|
|
representation = "type: boolean\n";
|
|
representation += to_string(this->truthy);
|
|
break;
|
|
}
|
|
return representation;
|
|
}
|
|
std::string SenchaObject::str()
|
|
{
|
|
std::string representation = "";
|
|
switch(type){
|
|
case string_literal:
|
|
representation += this->text;
|
|
break;
|
|
case integer_number:
|
|
representation += to_string(this->integer);
|
|
break;
|
|
case float_number:
|
|
representation += to_string(this->number);
|
|
break;
|
|
case null:
|
|
representation += "null";
|
|
break;
|
|
case symbol:
|
|
representation += this->text;
|
|
break;
|
|
case invalid:
|
|
representation += "some crap";
|
|
break;
|
|
case boolean:
|
|
representation += to_string(this->truthy);
|
|
break;
|
|
}
|
|
return representation;
|
|
}
|
|
|
|
SenchaObject SenchaObject::operator+(const SenchaObject& right)const
|
|
{
|
|
SenchaObject result;
|
|
if(type == right.type)
|
|
{
|
|
switch(type){
|
|
case string_literal:
|
|
result.set_new_string(this->text + right.text);
|
|
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;
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
SenchaObject SenchaObject::operator-(const SenchaObject& right)const
|
|
{
|
|
SenchaObject result;
|
|
if(type == right.type)
|
|
{
|
|
switch(type){
|
|
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;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
SenchaObject SenchaObject::operator*(const SenchaObject& right)const
|
|
{
|
|
SenchaObject result;
|
|
if(type == right.type)
|
|
{
|
|
switch(type){
|
|
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;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
SenchaObject SenchaObject::operator/(const SenchaObject& right)const
|
|
{
|
|
SenchaObject result;
|
|
if(type == right.type)
|
|
{
|
|
switch(type){
|
|
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;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
SenchaObject SenchaObject::operator==(const SenchaObject& right)const
|
|
{
|
|
SenchaObject result;
|
|
bool value = false;
|
|
result.type = boolean;
|
|
|
|
if(type == right.type)
|
|
{
|
|
switch(type){
|
|
case string_literal:
|
|
if(this->text == right.text)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
result.truthy = value;
|
|
return result;
|
|
}
|
|
|
|
SenchaObject SenchaObject::operator!=(const SenchaObject& right)const
|
|
{
|
|
SenchaObject result;
|
|
result.type = boolean;
|
|
|
|
result = (*this == right);
|
|
if(result.truthy)
|
|
result.truthy = false;
|
|
else result.truthy = true;
|
|
|
|
return result;
|
|
}
|
|
|
|
//TODO change code above to something more generic
|