From b4cd46a1e77862e98e8747b9e1400e4039b32029 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 28 Dec 2024 18:39:33 -0500 Subject: [PATCH] Added division. --- src/exec.c | 5 +++++ src/grammar.y | 25 +++++++++++++++++++++++++ src/include/lexer.h | 9 --------- src/lexer.c | 11 +---------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/exec.c b/src/exec.c index a578659..bb62d6f 100644 --- a/src/exec.c +++ b/src/exec.c @@ -37,6 +37,11 @@ ASTNumData exec_call(AST* ast) { ASTNumData n2 = exec_expr(calldata->argv[1]); return n1 * n2; + } else if (!strcmp(calldata->to, "/") && calldata->argc == 2) { + ASTNumData n1 = exec_expr(calldata->argv[0]); + ASTNumData n2 = exec_expr(calldata->argv[1]); + + return n1 / n2; } return -1000; } diff --git a/src/grammar.y b/src/grammar.y index 8b3d0de..870af2f 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -27,6 +27,7 @@ %token NEG %token PLUS %token MULT +%token DIV %token NL %type exp @@ -91,4 +92,28 @@ exp: $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("*", 2, argv)); } + | NUM DIV NUM { + AST** argv = calloc(2, sizeof(AST*)); + argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); + argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init($3)); + $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("/", 2, argv)); + } + | NEG NUM DIV NUM { + AST** argv = calloc(2, sizeof(AST*)); + argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$2)); + argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init($4)); + $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("/", 2, argv)); + } + | NEG NUM DIV NEG NUM { + AST** argv = calloc(2, sizeof(AST*)); + argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$2)); + argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$5)); + $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("/", 2, argv)); + } + | NUM DIV NEG NUM { + AST** argv = calloc(2, sizeof(AST*)); + argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); + argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$4)); + $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("/", 2, argv)); + } %% diff --git a/src/include/lexer.h b/src/include/lexer.h index d3adfb9..e5d7e1d 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -3,16 +3,7 @@ #include -#ifdef __has_include -#if __has_include("../../build/grammars/grammar.tab.h") #include "../../build/grammars/grammar.tab.h" -#else -#warn "Build resources not present!" -#endif -#else -#warn "Not sure whether build-time resources are present." -#include "../../build/grammars/grammar.tab.h" -#endif extern YYSTYPE yylval; extern char* inp; diff --git a/src/lexer.c b/src/lexer.c index 899f05d..9943181 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -28,22 +28,12 @@ double acc_float(int c) { inp++; while (isdigit(*inp)) { - // TODO: - // Accumulate as int, divide once at end. - // value = value + (((double)(*inp - '0'))/pow(10.0l, - // (double)(inp-oinp))); // Accumulate value. value = value * 10 + (*inp - '0'); // Accumulate value. dplaces++; inp++; } value = value / pow(10, dplaces); } - - // > 1.20000 - // = 1.0 + 2/10 - - // > 1.23 - // = 1.2 + 3/100 return value; } @@ -67,6 +57,7 @@ int yylex() { case '\n': return NL; case '-': return NEG; case '*': return MULT; + case '/': return DIV; default: return CALL; }