Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
13 changes: 10 additions & 3 deletions Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,20 @@ 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)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add tests for <= >= too. so we can test that these cases coverted too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also can you check that -10 < 0x10 for example.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, i added

[InlineData("-9 <= -9", true)]
[InlineData("-9 >= 10", false)]
[InlineData(" -10 < 0x10", true)]
[InlineData(" -10 != 0x10", true)]
public async Task EvaluateExpressionAllVariants(
string expression,
bool expectedResult)
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
Collaborator

Choose a reason for hiding this comment

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

Are --0 allowed in C ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also we probably should support -0

Copy link
Author

Choose a reason for hiding this comment

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

Yeap, -0 is supported by c.
fix

Copy link
Collaborator

Choose a reason for hiding this comment

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

What about +0 ?

Copy link
Author

@nt-devilboi nt-devilboi Oct 16, 2025

Choose a reason for hiding this comment

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

too.
+-...+-+0 and -+...-+-0 are supported. in these cases the result will be 0.
In other words, the result will always be without a sign.
+-+4 => 4

Copy link
Author

Choose a reason for hiding this comment

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

sorry,
If count of - is even then number otherwise -number

return int.Parse(macrosValue);

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