Made the infix ops call the normal functions.

This commit is contained in:
Jacob Signorovitch 2024-12-28 18:42:37 -05:00
parent b4cd46a1e7
commit bdca40bae4
2 changed files with 16 additions and 16 deletions

View File

@ -21,23 +21,23 @@ ASTNumData exec_call(AST* ast) {
log_dbg("Started call execution.");
fflush(stdout);
ASTCallData* calldata = (ASTCallData*)ast->data;
if (!strcmp(calldata->to, "+") && calldata->argc == 2) {
if (!strcmp(calldata->to, "sum") && calldata->argc == 2) {
ASTNumData n1 = exec_expr(calldata->argv[0]);
ASTNumData n2 = exec_expr(calldata->argv[1]);
return n1 + n2;
} else if (!strcmp(calldata->to, "-") && calldata->argc == 2) {
} else if (!strcmp(calldata->to, "sub") && calldata->argc == 2) {
ASTNumData n1 = exec_expr(calldata->argv[0]);
ASTNumData n2 = exec_expr(calldata->argv[1]);
return n1 - n2;
} else if (!strcmp(calldata->to, "*") && calldata->argc == 2) {
} else if (!strcmp(calldata->to, "mul") && calldata->argc == 2) {
ASTNumData n1 = exec_expr(calldata->argv[0]);
ASTNumData n2 = exec_expr(calldata->argv[1]);
return n1 * n2;
} else if (!strcmp(calldata->to, "/") && calldata->argc == 2) {
} else if (!strcmp(calldata->to, "div") && calldata->argc == 2) {
ASTNumData n1 = exec_expr(calldata->argv[0]);
ASTNumData n2 = exec_expr(calldata->argv[1]);

View File

@ -46,74 +46,74 @@ exp:
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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("sum", 2, argv));
}
| NEG NUM PLUS 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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("sum", 2, argv));
}
| NUM 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($3));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("-", 2, argv));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("sub", 2, argv));
}
| NEG NUM 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($4));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("-", 2, argv));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("sub", 2, argv));
}
| NUM MULT 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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("mul", 2, argv));
}
| NEG NUM MULT 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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("mul", 2, argv));
}
| NEG NUM MULT 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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("mul", 2, argv));
}
| NUM MULT 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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("mul", 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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("div", 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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("div", 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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("div", 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));
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("div", 2, argv));
}
%%