TPTScriptInterface: Ability to read hexadecimal constants

This commit is contained in:
Simon Robertshaw 2013-03-26 09:24:15 +00:00
parent 010f7790eb
commit 0791f5b0f8
2 changed files with 58 additions and 1 deletions

View File

@ -85,10 +85,22 @@ ValueType TPTScriptInterface::testType(std::string word)
{ {
if(rawWord[i] == ',' && rawWord[i+1] >= '0' && rawWord[i+1] <= '9') if(rawWord[i] == ',' && rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
goto parsePoint; goto parsePoint;
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')))
goto parseNumberHex;
else else
goto parseString; goto parseString;
} }
} }
parseNumberHex:
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; return TypeNumber;
parsePoint: parsePoint:
i++; i++;
@ -102,6 +114,50 @@ ValueType TPTScriptInterface::testType(std::string word)
return TypeString; return TypeString;
} }
int TPTScriptInterface::parseNumber(char * stringData)
{
char cc;
int base = 10;
int currentNumber = 0;
if(stringData[0] == '#')
{
stringData++;
base = 16;
}
else if(stringData[0] == '0' && stringData[1] == 'x')
{
stringData+=2;
base = 16;
}
if(base == 16)
{
while(cc = *(stringData++))
{
currentNumber *= base;
if(cc >= '0' && cc <= '9')
currentNumber += cc - '0';
else if(cc >= 'a' && cc <= 'f')
currentNumber += (cc - 'A') + 10;
else if(cc >= 'A' && cc <= 'F')
currentNumber += (cc - 'A') + 10;
else
break;
}
}
else
{
while(cc = *(stringData++))
{
currentNumber *= base;
if(cc >= '0' && cc <= '9')
currentNumber += cc - '0';
else
break;
}
}
return currentNumber;
}
AnyType TPTScriptInterface::eval(std::deque<std::string> * words) AnyType TPTScriptInterface::eval(std::deque<std::string> * words)
{ {
if(words->size() < 1) if(words->size() < 1)
@ -128,7 +184,7 @@ AnyType TPTScriptInterface::eval(std::deque<std::string> * words)
return tptS_quit(words); return tptS_quit(words);
break; break;
case TypeNumber: case TypeNumber:
return NumberType(atoi(rawWord)); return NumberType(parseNumber(rawWord));
case TypePoint: case TypePoint:
{ {
int pointX, pointY; int pointX, pointY;

View File

@ -7,6 +7,7 @@
class TPTScriptInterface: public CommandInterface { class TPTScriptInterface: public CommandInterface {
protected: protected:
AnyType eval(std::deque<std::string> * words); AnyType eval(std::deque<std::string> * words);
int parseNumber(char * stringData);
AnyType tptS_set(std::deque<std::string> * words); AnyType tptS_set(std::deque<std::string> * words);
AnyType tptS_create(std::deque<std::string> * words); AnyType tptS_create(std::deque<std::string> * words);
AnyType tptS_delete(std::deque<std::string> * words); AnyType tptS_delete(std::deque<std::string> * words);