From 556d8a5f258ed6cde427d1c115eb698e9b2210ae Mon Sep 17 00:00:00 2001 From: devilboi95 Date: Mon, 13 Oct 2025 13:00:02 +0500 Subject: [PATCH 1/6] add recognize negative number --- Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs | 7 ++++--- Cesium.Preprocessor/CPreprocessorTokenType.cs | 1 + .../ConditionExpressions/BinaryExpression.cs | 3 +++ .../ConditionExpressions/IdentifierExpression.cs | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs index 8b2924be1..82411371a 100644 --- a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs +++ b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs @@ -909,13 +909,14 @@ 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)] public async Task EvaluateExpressionAllVariants( string expression, bool expectedResult) diff --git a/Cesium.Preprocessor/CPreprocessorTokenType.cs b/Cesium.Preprocessor/CPreprocessorTokenType.cs index b202ba4de..bb744411b 100644 --- a/Cesium.Preprocessor/CPreprocessorTokenType.cs +++ b/Cesium.Preprocessor/CPreprocessorTokenType.cs @@ -29,6 +29,7 @@ public enum CPreprocessorTokenType [Token("...")] Ellipsis, + [Regex("-[0-9]+")] [Regex("[^ \t\v\f\r\n#;+\\-*/()=!<>\",.|\\[\\]&\\\\]+")] PreprocessingToken, diff --git a/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs b/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs index 8695049e5..748e54409 100644 --- a/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs +++ b/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs @@ -51,6 +51,9 @@ int Parse(Location location, string macrosValue) if (Regex.IsMatch(macrosValue, $"^(0|[1-9][0-9]*)$")) return int.Parse(macrosValue); + if (Regex.IsMatch(macrosValue, $"^(-[1-9][0-9]*)$")) + return int.Parse(macrosValue); + if (Regex.IsMatch(macrosValue, "^0b[01]+$")) return Convert.ToInt32(macrosValue[2..], 2); diff --git a/Cesium.Preprocessor/ConditionExpressions/IdentifierExpression.cs b/Cesium.Preprocessor/ConditionExpressions/IdentifierExpression.cs index f7de1c87f..6f17f11f6 100644 --- a/Cesium.Preprocessor/ConditionExpressions/IdentifierExpression.cs +++ b/Cesium.Preprocessor/ConditionExpressions/IdentifierExpression.cs @@ -18,7 +18,8 @@ internal sealed record IdentifierExpression(Location Location, string Identifier if (Regex.IsMatch(searchValue, $"^(0|[1-9][0-9]*)$") || Regex.IsMatch(searchValue, $"^{Regexes.HexLiteral}$") || Regex.IsMatch(searchValue, "^0[0-7]+$") - || Regex.IsMatch(searchValue, "^0b[01]+$")) + || Regex.IsMatch(searchValue, "^0b[01]+$") + || Regex.IsMatch(searchValue, "^(-[1-9][0-9]*)$")) { return searchValue; } From b435fe2344ac6e10cdb4a7754a7c9730b45c0489 Mon Sep 17 00:00:00 2001 From: devilboi95 Date: Mon, 13 Oct 2025 14:36:01 +0500 Subject: [PATCH 2/6] fix --- Cesium.Preprocessor/CPreprocessorTokenType.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cesium.Preprocessor/CPreprocessorTokenType.cs b/Cesium.Preprocessor/CPreprocessorTokenType.cs index bb744411b..21345a5f6 100644 --- a/Cesium.Preprocessor/CPreprocessorTokenType.cs +++ b/Cesium.Preprocessor/CPreprocessorTokenType.cs @@ -29,7 +29,7 @@ public enum CPreprocessorTokenType [Token("...")] Ellipsis, - [Regex("-[0-9]+")] + [Regex("-[1-9][0-9]*")] [Regex("[^ \t\v\f\r\n#;+\\-*/()=!<>\",.|\\[\\]&\\\\]+")] PreprocessingToken, From 1ba0dcf4b4dbf914631edf91463a41e7e318ca20 Mon Sep 17 00:00:00 2001 From: devilboi95 Date: Tue, 14 Oct 2025 17:52:32 +0500 Subject: [PATCH 3/6] fix --- Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs | 6 ++++++ Cesium.Preprocessor/CPreprocessorTokenType.cs | 1 - .../ConditionExpressions/BinaryExpression.cs | 5 +---- .../ConditionExpressions/IdentifierExpression.cs | 3 +-- Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs | 1 + 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs index 82411371a..ca4fe8c7e 100644 --- a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs +++ b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs @@ -913,10 +913,16 @@ public Task FileDefine() => DoTest( [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(" -10 != 0x10", true)] public async Task EvaluateExpressionAllVariants( string expression, bool expectedResult) diff --git a/Cesium.Preprocessor/CPreprocessorTokenType.cs b/Cesium.Preprocessor/CPreprocessorTokenType.cs index 21345a5f6..b202ba4de 100644 --- a/Cesium.Preprocessor/CPreprocessorTokenType.cs +++ b/Cesium.Preprocessor/CPreprocessorTokenType.cs @@ -29,7 +29,6 @@ public enum CPreprocessorTokenType [Token("...")] Ellipsis, - [Regex("-[1-9][0-9]*")] [Regex("[^ \t\v\f\r\n#;+\\-*/()=!<>\",.|\\[\\]&\\\\]+")] PreprocessingToken, diff --git a/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs b/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs index 748e54409..3be8db42a 100644 --- a/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs +++ b/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs @@ -48,10 +48,7 @@ public string EvaluateExpression(IMacroContext context) int Parse(Location location, string macrosValue) { - if (Regex.IsMatch(macrosValue, $"^(0|[1-9][0-9]*)$")) - return int.Parse(macrosValue); - - if (Regex.IsMatch(macrosValue, $"^(-[1-9][0-9]*)$")) + if (Regex.IsMatch(macrosValue, $"^(0|-*[1-9][0-9]*)$")) return int.Parse(macrosValue); if (Regex.IsMatch(macrosValue, "^0b[01]+$")) diff --git a/Cesium.Preprocessor/ConditionExpressions/IdentifierExpression.cs b/Cesium.Preprocessor/ConditionExpressions/IdentifierExpression.cs index 6f17f11f6..f7de1c87f 100644 --- a/Cesium.Preprocessor/ConditionExpressions/IdentifierExpression.cs +++ b/Cesium.Preprocessor/ConditionExpressions/IdentifierExpression.cs @@ -18,8 +18,7 @@ internal sealed record IdentifierExpression(Location Location, string Identifier if (Regex.IsMatch(searchValue, $"^(0|[1-9][0-9]*)$") || Regex.IsMatch(searchValue, $"^{Regexes.HexLiteral}$") || Regex.IsMatch(searchValue, "^0[0-7]+$") - || Regex.IsMatch(searchValue, "^0b[01]+$") - || Regex.IsMatch(searchValue, "^(-[1-9][0-9]*)$")) + || Regex.IsMatch(searchValue, "^0b[01]+$")) { return searchValue; } diff --git a/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs b/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs index f512c935b..d72d9d9d5 100644 --- a/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs +++ b/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs @@ -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") }; } From 957728c760c0f7b052c94c85017d5cc87d54df6e Mon Sep 17 00:00:00 2001 From: devilboi95 Date: Thu, 16 Oct 2025 14:41:49 +0500 Subject: [PATCH 4/6] I changed regexp --- Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs b/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs index 3be8db42a..60d750799 100644 --- a/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs +++ b/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs @@ -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]*)$")) return int.Parse(macrosValue); if (Regex.IsMatch(macrosValue, "^0b[01]+$")) From f3ededb817d9963f7236bd2c11b47e6422386b54 Mon Sep 17 00:00:00 2001 From: devilboi95 Date: Thu, 16 Oct 2025 15:12:35 +0500 Subject: [PATCH 5/6] add plus --- Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs b/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs index 60d750799..393dde478 100644 --- a/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs +++ b/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs @@ -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]*)$")) return int.Parse(macrosValue); if (Regex.IsMatch(macrosValue, "^0b[01]+$")) From 911bda4e623b63ade509db3190b811091941be3b Mon Sep 17 00:00:00 2001 From: devilboi95 Date: Sun, 19 Oct 2025 11:25:21 +0500 Subject: [PATCH 6/6] add tests and plus in UnaryExpression.cs --- Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs | 4 +++- Cesium.Preprocessor/CPreprocessorTokenType.cs | 2 +- Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs index ca4fe8c7e..71f859cea 100644 --- a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs +++ b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs @@ -922,7 +922,9 @@ public Task FileDefine() => DoTest( [InlineData("-9 <= -9", true)] [InlineData("-9 >= 10", false)] [InlineData(" -10 < 0x10", true)] - [InlineData(" -10 != 0x10", true)] + [InlineData(" -0 == 0", true)] + [InlineData(" -0 == +0", true)] + [InlineData(" -10 != +10", true)] public async Task EvaluateExpressionAllVariants( string expression, bool expectedResult) diff --git a/Cesium.Preprocessor/CPreprocessorTokenType.cs b/Cesium.Preprocessor/CPreprocessorTokenType.cs index b202ba4de..7a03802a0 100644 --- a/Cesium.Preprocessor/CPreprocessorTokenType.cs +++ b/Cesium.Preprocessor/CPreprocessorTokenType.cs @@ -32,7 +32,7 @@ public enum CPreprocessorTokenType [Regex("[^ \t\v\f\r\n#;+\\-*/()=!<>\",.|\\[\\]&\\\\]+")] PreprocessingToken, - [Regex(@"([.]|[;+\\-*/=!,|&]+|<=|>=|>|<|[|])")] + [Regex(@"([.]|[;+\-*/=!,|&]+|<=|>=|>|<|[|])")] Separator, [Token("(")] diff --git a/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs b/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs index d72d9d9d5..26e66ccfa 100644 --- a/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs +++ b/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs @@ -19,7 +19,8 @@ public string EvaluateExpression(IMacroContext context) return Operator switch { CPreprocessorOperator.Negation => !expressionValue.AsBoolean(Location) ? "1" : "0", - CPreprocessorOperator.Sub => $"-{expressionValue}", + CPreprocessorOperator.Sub => $"-{expressionValue}", + CPreprocessorOperator.Add => $"{expressionValue}", _ => throw new CompilationException($"Operator {Operator} cannot be used in the preprocessor directives") }; }