From 298e0b63c3ccf025bcbe3cc243f4ef2d70689699 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Wed, 6 Aug 2014 23:22:37 -0400 Subject: [PATCH] fix bugs with previous commit using FloatType --- src/lua/TPTSTypes.cpp | 34 +++++++++++++++++++++---- src/lua/TPTSTypes.h | 19 +++++++++++--- src/lua/TPTScriptInterface.cpp | 45 +++++++++++++++++++++------------- 3 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/lua/TPTSTypes.cpp b/src/lua/TPTSTypes.cpp index 6df68515a..0779e02f5 100644 --- a/src/lua/TPTSTypes.cpp +++ b/src/lua/TPTSTypes.cpp @@ -26,10 +26,22 @@ AnyType::AnyType(const AnyType & v): AnyType::operator NumberType() { - if(type != TypeNumber) - throw InvalidConversionException(type, TypeNumber); - else + 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 + 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()) diff --git a/src/lua/TPTSTypes.h b/src/lua/TPTSTypes.h index 688e0dc59..1f58e56ad 100644 --- a/src/lua/TPTSTypes.h +++ b/src/lua/TPTSTypes.h @@ -5,8 +5,8 @@ #include #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 @@ public: class NumberType; +class FloatType; class StringType; class PointType; @@ -35,6 +36,7 @@ public: AnyType(ValueType type_, ValueValue value_); AnyType(const AnyType & v); operator NumberType(); + operator FloatType(); operator StringType(); operator PointType(); ValueType GetType(); @@ -44,6 +46,8 @@ public: { case TypeNumber: return "Number"; + case TypeFloat: + return "Float"; case TypePoint: return "Point"; case TypeString: @@ -62,6 +66,8 @@ public: { case TypeNumber: return "Number"; + case TypeFloat: + return "Float"; case TypePoint: return "Point"; case TypeString: @@ -91,7 +97,14 @@ public: class NumberType: public AnyType { public: - NumberType(float number); + NumberType(int number); + int Value(); +}; + +class FloatType: public AnyType +{ +public: + FloatType(float number); float Value(); }; diff --git a/src/lua/TPTScriptInterface.cpp b/src/lua/TPTScriptInterface.cpp index e9d5c4ec2..0da0617bc 100644 --- a/src/lua/TPTScriptInterface.cpp +++ b/src/lua/TPTScriptInterface.cpp @@ -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,7 +101,7 @@ 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; @@ -107,7 +109,7 @@ ValueType TPTScriptInterface::testType(std::string word) 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 * words) break; case TypeNumber: return NumberType(parseNumber(rawWord)); + case TypeFloat: + return FloatType(atof(rawWord)); case TypePoint: { int pointX, pointY; @@ -257,10 +261,15 @@ AnyType TPTScriptInterface::tptS_set(std::deque * 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) { @@ -268,9 +277,11 @@ AnyType TPTScriptInterface::tptS_set(std::deque * words) { 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 * 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 * 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 * words) if(sim->parts[j].type == type) { returnValue++; - *((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue; + *((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValuef; } } break;