diff --git a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs index 8b2924be..71f859ce 100644 --- a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs +++ b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs @@ -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) diff --git a/Cesium.Preprocessor/CPreprocessorTokenType.cs b/Cesium.Preprocessor/CPreprocessorTokenType.cs index b202ba4d..7a03802a 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/BinaryExpression.cs b/Cesium.Preprocessor/ConditionExpressions/BinaryExpression.cs index 8695049e..393dde47 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]+$")) diff --git a/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs b/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs index f512c935..26e66ccf 100644 --- a/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs +++ b/Cesium.Preprocessor/ConditionExpressions/UnaryExpression.cs @@ -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") }; }