Generalized arithmetic functions

to arbitrary arguments.
This commit is contained in:
Jacob Signorovitch 2025-01-11 10:53:49 -05:00
parent 2ce89fb39a
commit 681e005a68
2 changed files with 39 additions and 24 deletions

View File

@ -1,3 +1,4 @@
#include <stddef.h>
#include <stdio.h>
#include <string.h>
@ -21,32 +22,46 @@ ASTNumData exec_call(AST* ast) {
log_dbg("Started call execution.");
fflush(stdout);
ASTCallData* calldata = (ASTCallData*)ast->data;
if (!strcmp(calldata->to, "sum") && calldata->argc == 2) {
ASTNumData n1 = exec_expr(calldata->argv[0]);
ASTNumData n2 = exec_expr(calldata->argv[1]);
if (!strcmp(calldata->to, "sum")) {
double total = 0;
return n1 + n2;
} else if (!strcmp(calldata->to, "sub") && calldata->argc == 2) {
ASTNumData n1 = exec_expr(calldata->argv[0]);
ASTNumData n2 = exec_expr(calldata->argv[1]);
for (
size_t i = 0;
i < calldata->argc;
total += exec_expr(calldata->argv[i++])
);
return n1 - n2;
} else if (!strcmp(calldata->to, "mul") && calldata->argc == 2) {
ASTNumData n1 = exec_expr(calldata->argv[0]);
ASTNumData n2 = exec_expr(calldata->argv[1]);
return total;
} else if (!strcmp(calldata->to, "sub")) {
double total = 0;
return n1 * n2;
} else if (!strcmp(calldata->to, "div") && calldata->argc == 2) {
ASTNumData n1 = exec_expr(calldata->argv[0]);
ASTNumData n2 = exec_expr(calldata->argv[1]);
for (
size_t i = 0;
i < calldata->argc;
total -= exec_expr(calldata->argv[i++])
);
return n1 / n2;
} else if (!strcmp(calldata->to, "sum") && calldata->argc == 3) {
ASTNumData n1 = exec_expr(calldata->argv[0]);
ASTNumData n2 = exec_expr(calldata->argv[1]);
ASTNumData n3 = exec_expr(calldata->argv[2]);
return total;
} else if (!strcmp(calldata->to, "mul")) {
double total = 1;
return n1 + n2 + n3;
for (
size_t i = 0;
i < calldata->argc;
total *= exec_expr(calldata->argv[i++])
);
return total;
} else if (!strcmp(calldata->to, "div")) {
double total = 1;
for (
size_t i = 0;
i < calldata->argc;
total /= exec_expr(calldata->argv[i++])
);
return total;
}
return -1000;
}

View File

@ -11,9 +11,9 @@
ArgArr* argarr_init() {
ArgArr* argarr = malloc(sizeof(ArgArr));
argarr->sz = ARLN;
argarr->sz = ARLN * sizeof(AST*);
argarr->ln = 0;
argarr->buf = malloc(ARLN);
argarr->buf = malloc(argarr->sz);
return argarr;
}
@ -24,7 +24,7 @@ void argarr_destroy(ArgArr* argarr) {
}
void argarr_add(ArgArr* argarr, AST* arg) {
if (argarr->ln + 1 > argarr->sz) {
if ((argarr->ln + 1) * argarr->sz > argarr->sz) {
argarr->sz *= 2;
argarr->buf = realloc(argarr->buf, argarr->sz);
log_dbgf(