diff --git a/.gitignore b/.gitignore index 57274f2..5da68f3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ tags .cache build/* compile_commands.json +vgcore.* diff --git a/src/ast.c b/src/ast.c index a0c15d3..e8d0b17 100644 --- a/src/ast.c +++ b/src/ast.c @@ -23,8 +23,8 @@ void ast_destroy(AST* ast) { if (!ast) return; switch (ast->type) { - case AST_TYPE_NUM: ast_type_num_destroy(ast->data); break; - case AST_TYPE_CALL: ast_type_call_destroy(ast->data); break; + case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break; + case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break; default: log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX); } } @@ -35,22 +35,19 @@ void ast_print(AST* ast) { } -ASTNumData* ast_type_num_init(int val) { +ASTNumData* ast_num_data_init(double val) { talloc(ASTNumData, num); - num->val = val; + *num = val; return num; } -void ast_type_num_destroy(ASTNumData* num) { - if (!num) - return - - free(num); +void ast_num_data_destroy(ASTNumData* num) { + if (!num) return free(num); } -ASTCallData* ast_type_call_init(char* to, size_t argc, AST** argv) { +ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) { talloc(ASTCallData, call); call->to = to; @@ -60,11 +57,8 @@ ASTCallData* ast_type_call_init(char* to, size_t argc, AST** argv) { return call; } -void ast_type_call_destroy(ASTCallData* call) { - if (!call) - return - - free(call->to); +void ast_call_data_destroy(ASTCallData* call) { + if (!call) return free(call->to); for (size_t i = 0; i < call->argc; i++) free(call->argv[i]); free(call); } diff --git a/src/exec.c b/src/exec.c index 37873ad..0ade84a 100644 --- a/src/exec.c +++ b/src/exec.c @@ -25,8 +25,8 @@ void exec_call() { ASTNumData* n1 = (ASTNumData*)calldata->argv[0]->data; ASTNumData* n2 = (ASTNumData*)calldata->argv[1]->data; - exec_return(n1->val + n2->val); + exec_return(*n1 + *n2); } } -void exec_return(int n) { printf("= %d\n", n); } +void exec_return(double n) { printf("= %lf\n", n); } diff --git a/src/grammar.y b/src/grammar.y index 374c278..cf17279 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -14,14 +14,14 @@ } %union { - int intval; + double fval; char* strval; AST* ast; } %define parse.error verbose -%token NUM +%token NUM %token CALL %token PLUS %token NL diff --git a/src/include/ast.h b/src/include/ast.h index 27e11b4..e6ca0f1 100644 --- a/src/include/ast.h +++ b/src/include/ast.h @@ -18,12 +18,10 @@ AST* ast_init(ASTType type, void* data); void ast_destroy(AST* ast); void ast_print(AST* ast); -typedef struct { - int val; -} ASTNumData; +typedef double ASTNumData; -ASTNumData* ast_type_num_init(int val); -void ast_type_num_destroy(ASTNumData* num); +ASTNumData* ast_num_data_init(double val); +void ast_num_data_destroy(ASTNumData* num); typedef struct { char* to; // What the call's to. @@ -31,7 +29,7 @@ typedef struct { AST** argv; // Argument vector. } ASTCallData; -ASTCallData* ast_type_call_init(char* to, size_t argc, AST** argv); -void ast_type_call_destroy(ASTCallData* call); +ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv); +void ast_call_data_destroy(ASTCallData* call); #endif diff --git a/src/include/exec.h b/src/include/exec.h index 625abdc..c8a4389 100644 --- a/src/include/exec.h +++ b/src/include/exec.h @@ -3,6 +3,6 @@ void exec_expr(); void exec_call(); -void exec_return(int n); +void exec_return(double n); #endif diff --git a/src/lexer.c b/src/lexer.c index 88e8fb6..c06c44b 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -20,7 +20,7 @@ int yylex() { value = value * 10 + (*inp - '0'); // Accumulate value. inp++; } - yylval.intval = value; // Set the token value. + yylval.fval = value; // Set the token value. return NUM; }