Add support for addition in SOC/Lua arithmetic parser.

Don't abort reading the whole script if you can't parse a single value.
This commit is contained in:
MascaraSnake 2017-09-04 14:09:38 +02:00
parent 36293654ea
commit 6abdb27e36
2 changed files with 36 additions and 31 deletions

View File

@ -135,7 +135,7 @@ namespace CodeImp.DoomBuilder.SRB2
break;
}
LogWarning("The sprite \"" + token + "\" assigned by the \"$sprite\" property does not exist");
return false;
break;
case "$Category":
SkipWhitespace(true);
token = ReadLine();
@ -144,27 +144,18 @@ namespace CodeImp.DoomBuilder.SRB2
case "doomednum":
if (!ReadParameter(out token, out finished)) return false;
if (!int.TryParse(token, NumberStyles.Integer, CultureInfo.InvariantCulture, out mapThingNum))
{
ReportError("Invalid map thing number");
return false;
}
LogWarning("Could not parse map thing number");
break;
case "radius":
if (!ReadParameter(out token, out finished)) return false;
if (!ParseWithArithmetic(token, out radius))
{
ReportError("Invalid radius");
return false;
}
LogWarning("Could not parse radius");
radius /= 65536;
break;
case "height":
if (!ReadParameter(out token, out finished)) return false;
if (!ParseWithArithmetic(token, out height))
{
ReportError("Invalid height");
return false;
}
LogWarning("Could not parse height");
height /= 65536;
break;
case "spawnstate":
@ -610,6 +601,20 @@ namespace CodeImp.DoomBuilder.SRB2
}
private bool ParseWithArithmetic(string input, out int output)
{
output = 0;
string[] tokens = input.Split(new char[] { '+' });
foreach (string t in tokens)
{
int val = 0;
if (!ParseMultiplication(t, out val))
return false;
output += val;
}
return true;
}
private bool ParseMultiplication(string input, out int output)
{
output = 1;
string[] tokens = input.Split(new char[] { '*' });
@ -619,10 +624,7 @@ namespace CodeImp.DoomBuilder.SRB2
int val = 1;
if (trimmed == "FRACUNIT") val = 65536;
else if (!int.TryParse(trimmed, NumberStyles.Integer, CultureInfo.InvariantCulture, out val))
{
ReportError("Invalid radius");
return false;
}
output *= val;
}
return true;

View File

@ -161,6 +161,7 @@ namespace CodeImp.DoomBuilder.SRB2
continue;
}
LogWarning("The sprite \"" + spritename + "\" assigned by the \"$sprite\" property does not exist");
continue;
}
if (line.StartsWith("#$Name "))
{
@ -186,25 +187,16 @@ namespace CodeImp.DoomBuilder.SRB2
{
case "MAPTHINGNUM":
if (!int.TryParse(tokens[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out mapThingNum))
{
ReportError("Invalid map thing number");
return false;
}
LogWarning("Could not parse map thing number");
break;
case "RADIUS":
if (!ParseWithArithmetic(tokens[1], out radius))
{
ReportError("Invalid radius");
return false;
}
LogWarning("Could not parse radius");
radius /= 65536;
break;
case "HEIGHT":
if (!ParseWithArithmetic(tokens[1], out height))
{
ReportError("Invalid height");
return false;
}
LogWarning("Could not parse height");
height /= 65536;
break;
@ -293,6 +285,20 @@ namespace CodeImp.DoomBuilder.SRB2
#region ================== Methods
private bool ParseWithArithmetic(string input, out int output)
{
output = 0;
string[] tokens = input.Split(new char[] { '+' });
foreach (string t in tokens)
{
int val = 0;
if (!ParseMultiplication(t, out val))
return false;
output += val;
}
return true;
}
private bool ParseMultiplication(string input, out int output)
{
output = 1;
string[] tokens = input.Split(new char[] { '*' });
@ -302,10 +308,7 @@ namespace CodeImp.DoomBuilder.SRB2
int val = 1;
if (trimmed == "FRACUNIT") val = 65536;
else if (!int.TryParse(trimmed, NumberStyles.Integer, CultureInfo.InvariantCulture, out val))
{
ReportError("Invalid radius");
return false;
}
output *= val;
}
return true;