]> git.lizzy.rs Git - nothing.git/commitdiff
Merge pull request #375 from tsoding/361
authorAlexey Kutepov <reximkut@gmail.com>
Sat, 6 Oct 2018 17:12:37 +0000 (00:12 +0700)
committerGitHub <noreply@github.com>
Sat, 6 Oct 2018 17:12:37 +0000 (00:12 +0700)
(#361) Make scripting parser support negative numbers

src/script/parser.c
test/parser_suite.h
test/test.h

index 7971bb8275d51e24beea06c799b30779e242cf4f..6f86a425335ac148f896eb269dafb0a43731abef 100644 (file)
@@ -152,8 +152,11 @@ static struct ParseResult parse_expr(Gc *gc, struct Token current_token)
     default: {}
     }
 
-    if (isdigit(*current_token.begin)) {
-        return parse_number(gc, current_token);
+    if (*current_token.begin == '-' || isdigit(*current_token.begin)) {
+        struct ParseResult result = parse_number(gc, current_token);
+        if (!result.is_error) {
+            return result;
+        }
     }
 
     return parse_symbol(gc, current_token);
index 1d0634f80e7293b15111eb57f021f98a494c0db0..67d71f63bd56c6945e351e16f1a60a2e240c8316 100644 (file)
@@ -49,9 +49,25 @@ TEST(read_expr_from_file_test)
     return 0;
 }
 
+TEST(parse_negative_numbers_test)
+{
+    Gc *gc = create_gc();
+    struct ParseResult result = read_expr_from_string(gc, "-12345");
+
+    ASSERT_FALSE(result.is_error, "Parsing failed");
+    ASSERT_TRUE(result.expr.type == EXPR_ATOM, "Parsed expression is not an atom");
+    ASSERT_TRUE(result.expr.atom->type == ATOM_NUMBER, "Parsed expression is not a number");
+    ASSERT_LONGINTEQ(-12345L, result.expr.atom->num);
+
+    destroy_gc(gc);
+
+    return 0;
+}
+
 TEST_SUITE(parser_suite)
 {
     TEST_RUN(read_expr_from_file_test);
+    TEST_RUN(parse_negative_numbers_test);
 
     return 0;
 }
index 750575038badd2cc04b0d69db25a27e942c8b212..f8a092d8cef274d738410d14d62c5e353a5a817a 100644 (file)
     }
 
 #define ASSERT_TRUE(condition, message)                                 \
-    if (!condition) {                                                   \
+    if (!(condition)) {                                                 \
         fprintf(stderr, "\n%s:%d: ASSERT_TRUE: false\n",                \
                 __FILE__, __LINE__);                                    \
         fprintf(stderr, "%s\n", message);                               \
         return -1;                                                      \
     }
 
+#define ASSERT_FALSE(condition, message)                                \
+    if (condition) {                                                    \
+        fprintf(stderr, "\n%s:%d: ASSERT_FALSE: false\n",               \
+                __FILE__, __LINE__);                                    \
+        fprintf(stderr, "%s\n", message);                               \
+        return -1;                                                      \
+    }
+
 #define TEST_SUITE(name)                        \
     static int name##_body(void);               \
     static int name(void) {                     \