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);
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;
}
}
#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) { \