Repository: microsoft/regorus (language: python)

Issue:
Improve error message for unary -
Unary `-` can only be applied before number literals.
Currently the error message displayed is `not implemented: unar expr is umplemented`.

This ought to be improved.


Discussion / hints:

Fix patch:
diff --git a/src/interpreter.rs b/src/interpreter.rs
index b3e68d93..db3311c4 100644
--- a/src/interpreter.rs
+++ b/src/interpreter.rs
@@ -2699,7 +2699,22 @@ impl Interpreter {
                 key, value, query, ..
             } => self.eval_object_compr(key, value, query),
             Expr::SetCompr { term, query, .. } => self.eval_set_compr(term, query),
-            Expr::UnaryExpr { .. } => unimplemented!("unar expr is umplemented"),
+            Expr::UnaryExpr { span, expr: uexpr } => match uexpr.as_ref() {
+                Expr::Number(_) if !uexpr.span().text().starts_with('-') => {
+                    builtins::numbers::arithmetic_operation(
+                        span,
+                        &ArithOp::Sub,
+                        expr,
+                        uexpr,
+                        Value::from(0),
+                        self.eval_expr(uexpr)?,
+                        self.strict_builtin_errors,
+                    )
+                }
+                _ => bail!(expr
+                    .span()
+                    .error("unary - can only be used with numeric literals")),
+            },
             Expr::Call { span, fcn, params } => {
                 self.eval_call(span, expr, fcn, params, None, false)
             }


Test patch:
diff --git a/tests/interpreter/cases/unary/tests.yaml b/tests/interpreter/cases/unary/tests.yaml
new file mode 100644
index 00000000..10630127
--- /dev/null
+++ b/tests/interpreter/cases/unary/tests.yaml
@@ -0,0 +1,46 @@
+# Copyright (c) Microsoft Corporation.
+# Licensed under the MIT License.
+cases:
+  - note: unary expr on non literals produces an error
+    modules:
+      - |
+        package test
+
+        x = - y
+        y = 1
+    query: data.test
+    error: "unary - can only be used with numeric literals"
+
+  - note: unary expr on literals work
+    modules:
+      - |
+        package test
+
+        x = - 1 # With space
+        y = -1
+        z = -   # With newlines and comment
+        
+          1  
+    query: data.test
+    want_result:
+      x: -1
+      y: -1
+      z: -1
+      
+  - note: double unary expr
+    modules:
+      - |
+        package test
+
+        x = - -1
+    query: data.test
+    error: "unary - can only be used with numeric literals"
+
+  - note: double unary expr double space
+    modules:
+      - |
+        package test
+
+        x = - - 1
+    query: data.test
+    error: "unary - can only be used with numeric literals"