Permalink
Browse files

Add support for exponential notation float literals

- Implement exponential notation support in tokenizer
- Increase precision of toString/message float formatting
  • Loading branch information...
sp1187 committed Dec 26, 2017
1 parent 832ab57 commit ff6ceb6ccb583a110a39c5d466c33b4fb9fb671c
@@ -8,7 +8,7 @@ inline std::wstring to_wstring(int64_t value)

inline std::wstring to_wstring(double value)
{
return formatString(L"%f", value);
return formatString(L"%#.17g", value);
}

enum class OperatorType
@@ -144,7 +144,7 @@ ExpressionValue expFuncToString(const std::wstring& funcName, const std::vector<
result.strValue = formatString(L"%d",parameters[0].intValue);
break;
case ExpressionValueType::Float:
result.strValue = formatString(L"%g",parameters[0].floatValue);
result.strValue = formatString(L"%#.17g",parameters[0].floatValue);
break;
default:
return result;
@@ -485,19 +485,11 @@ bool FileTokenizer::convertInteger(size_t start, size_t end, int64_t& result)

bool FileTokenizer::convertFloat(size_t start, size_t end, double& result)
{
std::string str;
std::wstring str = currentLine.substr(start, end - start);
wchar_t* end_ptr;

for (size_t i = start; i < end; i++)
{
wchar_t c = currentLine[i];
if (c != '.' && (c < '0' || c > '9'))
return false;

str += (char) c;
}

result = atof(str.c_str());
return true;
result = wcstod(str.c_str(), &end_ptr);
return str.c_str() + str.size() == end_ptr;
}

Token FileTokenizer::loadToken()
@@ -580,25 +572,42 @@ Token FileTokenizer::loadToken()
}

// numbers
if ((first >= '0' && first <= '9') || first == '$')
if (first >= '0' && first <= '9')
{
// find end of number
size_t start = pos;
size_t end = pos;
bool isFloat = false;
bool isValid = true;
bool foundPoint = false;
bool foundExp = false;
while (end < currentLine.size() && (iswalnum(currentLine[end]) || currentLine[end] == '.'))
{
if (currentLine[end] == '.')
{
if (isFloat == true)
if (foundExp || foundPoint)
isValid = false;
isFloat = true;
foundPoint = true;
}
else if (towlower(currentLine[end]) == 'e')
{
if (foundExp)
{
isValid = false;
} else if (end+1 < currentLine.size() && (currentLine[end+1] == '+' || currentLine[end+1] == '-')){
end++;
if (end+1 >= currentLine.size() || !iswalnum(currentLine[end+1]))
isValid = false;
}
foundExp = true;
}

end++;
}

bool isFloat = foundPoint || (foundExp &&
towlower(currentLine[end-1]) != 'h' &&
!(towlower(currentLine[start]) == '0' && towlower(currentLine[start+1]) == 'x'));

if (!isFloat)
{
int64_t value;
@@ -13,15 +13,15 @@ ExpressionFunctions.asm(51) notice: reads32("file.bin"): FFFFFFFFDDCCBBAA
ExpressionFunctions.asm(53) notice: int(3): 3
ExpressionFunctions.asm(54) notice: int(3.7): 3
ExpressionFunctions.asm(55) notice: int(3): 3
ExpressionFunctions.asm(57) notice: float(3.7): 3.700000
ExpressionFunctions.asm(58) notice: float(3): 3.000000
ExpressionFunctions.asm(60) notice: frac(3): 0.000000
ExpressionFunctions.asm(61) notice: frac(3.7): 0.700000
ExpressionFunctions.asm(62) notice: frac((-3.7)): -0.700000
ExpressionFunctions.asm(57) notice: float(3.7): 3.7000000000000002
ExpressionFunctions.asm(58) notice: float(3): 3.0000000000000000
ExpressionFunctions.asm(60) notice: frac(3): 0.0000000000000000
ExpressionFunctions.asm(61) notice: frac(3.7): 0.70000000000000018
ExpressionFunctions.asm(62) notice: frac((-3.7)): -0.70000000000000018
ExpressionFunctions.asm(64) notice: abs(3): 3
ExpressionFunctions.asm(65) notice: abs((-3)): 3
ExpressionFunctions.asm(66) notice: abs(3.7): 3.700000
ExpressionFunctions.asm(67) notice: abs((-3.7)): 3.700000
ExpressionFunctions.asm(66) notice: abs(3.7): 3.7000000000000002
ExpressionFunctions.asm(67) notice: abs((-3.7)): 3.7000000000000002
ExpressionFunctions.asm(72) notice: strlen("teststest"): 9
ExpressionFunctions.asm(73) notice: substr("teststest",0,4): test
ExpressionFunctions.asm(74) notice: find("teststest","test",0): 0
@@ -88,11 +88,9 @@ check 3 * 2 * 1.5, 9
check 5 / 2,2
check 5 / 2.0, 2.5
check 5.0 / 2, 2.5
check 1 / 0, "undef"

; modulo
check 3 % 2, 1
check 1 % 0, "undef"

; combined addition and multiplication
check 3*2+2,8

0 comments on commit ff6ceb6

Please sign in to comment.