Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,22 @@ public Task FileDefine() => DoTest(
[InlineData("1 || 0", true)]
[InlineData("1 || 1", true)]

// TODO[#532]: Need to add support for parsing negative numbers, now "-" is recognized as a separator
// [InlineData("-10 < 9", true)]
// [InlineData("-10 > 9", false)]

[InlineData("0b11 == 3", true)]
[InlineData("021 == 17", true)]
[InlineData("0xF == 15", true)]


[InlineData("-10 > 9", false)]
[InlineData("-10 < 9", true)]
[InlineData("-10 < -9", true)]
[InlineData("-9 == -9", true)]
[InlineData("-9 <= -9", true)]
[InlineData("-9 >= 10", false)]
[InlineData(" -10 < 0x10", true)]
[InlineData(" -0 == 0", true)]
[InlineData(" -0 == +0", true)]
[InlineData(" -10 != +10", true)]
public async Task EvaluateExpressionAllVariants(
string expression,
bool expectedResult)
Expand Down
2 changes: 1 addition & 1 deletion Cesium.Preprocessor/CPreprocessorTokenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum CPreprocessorTokenType
[Regex("[^ \t\v\f\r\n#;+\\-*/()=!<>\",.|\\[\\]&\\\\]+")]
PreprocessingToken,

[Regex(@"([.]|[;+\\-*/=!,|&]+|<=|>=|>|<|[|])")]
[Regex(@"([.]|[;+\-*/=!,|&]+|<=|>=|>|<|[|])")]
Separator,

[Token("(")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public string EvaluateExpression(IMacroContext context)

int Parse(Location location, string macrosValue)
{
if (Regex.IsMatch(macrosValue, $"^(0|[1-9][0-9]*)$"))
if (Regex.IsMatch(macrosValue, $"^(-?|\\+?)(0|[1-9][0-9]*)$"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Minor) I believe we can simplify this to (-|\\+)?

In plain English, the current code says "either an optional minus sign or an optional plus sign", while the new one would say "a minus or plus sign, optional", which I think better describes the intention.

return int.Parse(macrosValue);

if (Regex.IsMatch(macrosValue, "^0b[01]+$"))
Expand Down
2 changes: 2 additions & 0 deletions Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public string EvaluateExpression(IMacroContext context)
return Operator switch
{
CPreprocessorOperator.Negation => !expressionValue.AsBoolean(Location) ? "1" : "0",
CPreprocessorOperator.Sub => $"-{expressionValue}",
CPreprocessorOperator.Add => $"{expressionValue}",
_ => throw new CompilationException($"Operator {Operator} cannot be used in the preprocessor directives")
};
}
Expand Down
Loading