Permalink
Browse files

fix bugs with previous commit using FloatType

  • Loading branch information...
jacob1 committed Aug 7, 2014
1 parent ccb3de7 commit 298e0b63c3ccf025bcbe3cc243f4ef2d70689699
Showing with 72 additions and 24 deletions.
  1. +28 −4 src/lua/TPTSTypes.cpp
  2. +16 −3 src/lua/TPTSTypes.h
  3. +28 −17 src/lua/TPTScriptInterface.cpp
@@ -26,10 +26,22 @@ AnyType::AnyType(const AnyType & v):
AnyType::operator NumberType()
{
if(type != TypeNumber)
if (type == TypeNumber)
return NumberType(value.num);
else if (type == TypeFloat)
return NumberType(value.numf);
else
throw InvalidConversionException(type, TypeNumber);
}
AnyType::operator FloatType()
{
if (type == TypeNumber)
return FloatType(value.num);
else if (type == TypeFloat)
return FloatType(value.numf);
else
return NumberType(value.num);
throw InvalidConversionException(type, TypeFloat);
}
AnyType::operator StringType()
@@ -86,16 +98,28 @@ AnyType::~AnyType()
//Number Type
NumberType::NumberType(float number): AnyType(TypeNumber, ValueValue())
NumberType::NumberType(int number): AnyType(TypeNumber, ValueValue())
{
value.num = number;
}
float NumberType::Value()
int NumberType::Value()
{
return value.num;
}
//Float Type
FloatType::FloatType(float number): AnyType(TypeFloat, ValueValue())
{
value.numf = number;
}
float FloatType::Value()
{
return value.numf;
}
//String type
StringType::StringType(std::string string): AnyType(TypeString, ValueValue())
@@ -5,8 +5,8 @@
#include <typeinfo>
#include "gui/interface/Point.h"
enum ValueType { TypeNumber, TypePoint, TypeString, TypeNull, TypeFunction };
typedef union { float num; std::string* str; ui::Point* pt; } ValueValue;
enum ValueType { TypeNumber, TypeFloat, TypePoint, TypeString, TypeNull, TypeFunction };
typedef union { int num; float numf; std::string* str; ui::Point* pt; } ValueValue;
class GeneralException
{
@@ -23,6 +23,7 @@ class GeneralException
class NumberType;
class FloatType;
class StringType;
class PointType;
@@ -35,6 +36,7 @@ class AnyType
AnyType(ValueType type_, ValueValue value_);
AnyType(const AnyType & v);
operator NumberType();
operator FloatType();
operator StringType();
operator PointType();
ValueType GetType();
@@ -44,6 +46,8 @@ class AnyType
{
case TypeNumber:
return "Number";
case TypeFloat:
return "Float";
case TypePoint:
return "Point";
case TypeString:
@@ -62,6 +66,8 @@ class AnyType
{
case TypeNumber:
return "Number";
case TypeFloat:
return "Float";
case TypePoint:
return "Point";
case TypeString:
@@ -91,7 +97,14 @@ class InvalidConversionException: public GeneralException
class NumberType: public AnyType
{
public:
NumberType(float number);
NumberType(int number);
int Value();
};
class FloatType: public AnyType
{
public:
FloatType(float number);
float Value();
};
@@ -84,11 +84,13 @@ ValueType TPTScriptInterface::testType(std::string word)
parseNumber:
for(i = 0; i < word.length(); i++)
{
if(!(rawWord[i] >= '0' && rawWord[i] <= '9') && rawWord[i] != '.' && !(rawWord[i] == '-' && !i))
if (!(rawWord[i] >= '0' && rawWord[i] <= '9') && !(rawWord[i] == '-' && !i))
{
if(rawWord[i] == ',' && rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
if (rawWord[i] == '.' && i)
return TypeFloat;
else if (rawWord[i] == ',' && rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
goto parsePoint;
else if((rawWord[i] == '#' || rawWord[i] == 'x') &&
else if ((rawWord[i] == '#' || rawWord[i] == 'x') &&
((rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
|| (rawWord[i+1] >= 'a' && rawWord[i+1] <= 'f')
|| (rawWord[i+1] >= 'A' && rawWord[i+1] <= 'F')))
@@ -99,15 +101,15 @@ ValueType TPTScriptInterface::testType(std::string word)
}
parseNumberHex:
i++;
for(; i < word.length(); i++)
for (; i < word.length(); i++)
if(!((rawWord[i] >= '0' && rawWord[i] <= '9') || (rawWord[i] >= 'a' && rawWord[i] <= 'f') || (rawWord[i] >= 'A' && rawWord[i] <= 'F')))
{
goto parseString;
}
return TypeNumber;
parsePoint:
i++;
for(; i < word.length(); i++)
for (; i < word.length(); i++)
if(!(rawWord[i] >= '0' && rawWord[i] <= '9'))
{
goto parseString;
@@ -122,17 +124,17 @@ float TPTScriptInterface::parseNumber(char * stringData)
char cc;
int base = 10;
int currentNumber = 0;
if(stringData[0] == '#')
if (stringData[0] == '#')
{
stringData++;
base = 16;
}
else if(stringData[0] == '0' && stringData[1] == 'x')
else if (stringData[0] == '0' && stringData[1] == 'x')
{
stringData+=2;
base = 16;
}
if(base == 16)
if (base == 16)
{
while(cc = *(stringData++))
{
@@ -149,7 +151,7 @@ float TPTScriptInterface::parseNumber(char * stringData)
}
else
{
return atof(stringData);
return atoi(stringData);
}
return currentNumber;
}
@@ -181,6 +183,8 @@ AnyType TPTScriptInterface::eval(std::deque<std::string> * words)
break;
case TypeNumber:
return NumberType(parseNumber(rawWord));
case TypeFloat:
return FloatType(atof(rawWord));
case TypePoint:
{
int pointX, pointY;
@@ -257,20 +261,27 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
throw GeneralException("Invalid property");
//Selector
float newValue;
if(value.GetType() == TypeNumber)
int newValue;
float newValuef;
if (value.GetType() == TypeNumber)
{
newValue = ((NumberType)value).Value();
newValue = newValuef = ((NumberType)value).Value();
}
else if (value.GetType() == TypeFloat)
{
newValue = newValuef = ((FloatType)value).Value();
}
else if(value.GetType() == TypeString)
{
if (property.Value() == "temp")
{
std::string newString = ((StringType)value).Value();
if (newString.at(newString.length()-1) == 'C')
newValue = atof(newString.substr(0, newString.length()-1).c_str())+273.15;
newValuef = atof(newString.substr(0, newString.length()-1).c_str())+273.15;
else if (newString.at(newString.length()-1) == 'F')
newValue = (atof(newString.substr(0, newString.length()-1).c_str())-32.0f)*5/9+273.15f;
newValuef = (atof(newString.substr(0, newString.length()-1).c_str())-32.0f)*5/9+273.15f;
else
throw GeneralException("Invalid value for assignment");
}
else
{
@@ -310,7 +321,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
*((int*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValue;
break;
case FormatFloat:
*((float*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValue;
*((float*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValuef;
break;
}
returnValue = 1;
@@ -335,7 +346,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
if(sim->parts[j].type)
{
returnValue++;
*((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue;
*((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValuef;
}
}
break;
@@ -372,7 +383,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
if(sim->parts[j].type == type)
{
returnValue++;
*((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue;
*((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValuef;
}
}
break;

0 comments on commit 298e0b6

Please sign in to comment.