diff --git a/src/ast.c b/src/ast.c index 7a0023a..0bc0918 100644 --- a/src/ast.c +++ b/src/ast.c @@ -1,8 +1,8 @@ #include #include "include/ast.h" -#include "include/util.h" #include "include/dstr.h" +#include "include/util.h" extern AST* root; @@ -30,9 +30,7 @@ void ast_destroy(AST* ast) { } } -void ast_print(AST* ast) { - ast_print_i(ast, 0); -} +void ast_print(AST* ast) { ast_print_i(ast, 0); } void ast_print_i(AST* ast, int i) { INDENT_BEGIN(i); @@ -41,9 +39,11 @@ void ast_print_i(AST* ast, int i) { INDENT_FIELD("type", "%s", asttype_names[ast->type]); INDENT_FIELD_EXT_NONL_START("data"); switch (ast->type) { - case AST_TYPE_NUM: ast_num_print(ast->data, i + 1); break; - case AST_TYPE_CALL: ast_call_print(ast->data, i + 1); break; - default: exit(1); + case AST_TYPE_NUM: + printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data); + break; + case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break; + default: exit(1); } INDENT_FIELD_NONL_END; INDENT_END; diff --git a/src/grammar.y b/src/grammar.y index e194cf9..8695dfb 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -36,13 +36,20 @@ input: exp: NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); } - | NUM PLUS exp { + | NUM PLUS NUM { + AST* argv[2] = { + ast_init(AST_TYPE_NUM, ast_num_data_init($1)), + ast_init(AST_TYPE_NUM, ast_num_data_init($3)) + }; + $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("+", 2, argv)); + } + /*| NUM PLUS exp { AST* argv[2] = { ast_init(AST_TYPE_NUM, ast_num_data_init($1)), $3 }; $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("+", 2, argv)); - }/* + } | exp PLUS exp { AST* argv[2] = { $1, $3 }; $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("+", 2, argv)); diff --git a/src/include/util.h b/src/include/util.h index ed54c58..590a346 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -50,7 +50,7 @@ // Print & indent the title of a section. #define INDENT_TITLE(THING, WHERE) \ - printf("%s" COL_BCYA THING " @ %p\n" COL_RESET, INDENT_spacing->buf, WHERE); + printf("%s" COL_BCYA THING COL_RESET " @" COL_MAG " %p\n" COL_RESET, INDENT_spacing->buf, WHERE); // Print & indent a thing. #define INDENT_FIELD(FIELD, VAL, ...) \