From 138a7ac4344686a657c28e6f24058a464c975402 Mon Sep 17 00:00:00 2001 From: Capostrophic Date: Thu, 26 Sep 2019 02:44:49 +0300 Subject: [PATCH] Expression parser: Try to parse strings as number literals (bug #5097) --- CHANGELOG.md | 1 + components/compiler/exprparser.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 811a276f13..9dfb026c10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -127,6 +127,7 @@ Bug #5089: Swimming/Underwater creatures only swim around ground level Bug #5092: NPCs with enchanted weapons play sound when out of charges Bug #5093: Hand to hand sound plays on knocked out enemies + Bug #5097: String arguments can't be parsed as number literals in scripts Bug #5099: Non-swimming enemies will enter water if player is water walking Bug #5103: Sneaking state behavior is still inconsistent Bug #5104: Black Dart's enchantment doesn't trigger at low Enchant levels diff --git a/components/compiler/exprparser.cpp b/components/compiler/exprparser.cpp index e3a306b8f3..871800fa7f 100644 --- a/components/compiler/exprparser.cpp +++ b/components/compiler/exprparser.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -324,6 +325,21 @@ namespace Compiler mExplicit = name2; return true; } + + // This is terrible, but of course we must have this for legacy content. + // Convert the string to a number even if it's impossible and use it as a number literal. + // Can't use stof/atof or to_string out of locale concerns. + float number; + std::stringstream stream(name2); + stream >> number; + stream.str(std::string()); + stream.clear(); + stream << number; + + pushFloatLiteral(number); + mTokenLoc = loc; + getErrorHandler().warning ("Parsing a non-variable string as a number: " + stream.str(), loc); + return true; } else {