Cleaned up.

This commit is contained in:
Jacob Signorovitch 2025-01-14 15:11:20 -05:00
parent 1098fa252f
commit bc0c4f33ad
6 changed files with 19 additions and 20 deletions

View File

@ -8,7 +8,7 @@
Dstr* dstr_init(void) {
Dstr* dstr = malloc(sizeof(Dstr));
dstr->bufsz = DSTR_INITSZ;
dstr->sz = DSTR_INITSZ;
dstr->buf = malloc(DSTR_INITSZ);
*dstr->buf = '\0';
dstr->ln = 0;
@ -23,13 +23,13 @@ void dstr_destroy(Dstr* dstr) {
// Check whether the buffer is overflowing and resize it if necessary.
void check_resz(Dstr* dstr, size_t ln) {
while (dstr->ln + ln + 1 > dstr->bufsz) {
while (dstr->ln + ln + 1 > dstr->sz) {
// Double the buffer size when overflown.
dstr->bufsz *= 2;
dstr->buf = realloc(dstr->buf, dstr->bufsz);
dstr->sz *= 2;
dstr->buf = realloc(dstr->buf, dstr->sz);
log_dbgf(
"dstr @ %p doubled from %ld to %ld", dstr, dstr->bufsz / 2,
dstr->bufsz
"dstr @ %p doubled from %ld to %ld", dstr, dstr->sz / 2,
dstr->sz
);
}
}

View File

@ -8,7 +8,7 @@
extern AST* root;
ASTNumData exec_expr(AST* ast) {
ASTNumData exec_exp(AST* ast) {
log_dbg("Started execution.");
switch (ast->type) {
case AST_TYPE_CALL: return exec_call(ast);
@ -23,42 +23,42 @@ ASTNumData exec_call(AST* ast) {
ASTCallData* calldata = (ASTCallData*)ast->data;
if (calldata->argc >= 1) {
if (!strcmp(calldata->to, "sum")) {
double total = exec_expr(calldata->argv[0]);
double total = exec_exp(calldata->argv[0]);
for (
size_t i = 1;
i < calldata->argc;
total += exec_expr(calldata->argv[i++])
total += exec_exp(calldata->argv[i++])
);
return total;
} else if (!strcmp(calldata->to, "sub")) {
double total = exec_expr(calldata->argv[0]);
double total = exec_exp(calldata->argv[0]);
for (
size_t i = 1;
i < calldata->argc;
total -= exec_expr(calldata->argv[i++])
total -= exec_exp(calldata->argv[i++])
);
return total;
} else if (!strcmp(calldata->to, "mul")) {
double total = exec_expr(calldata->argv[0]);
double total = exec_exp(calldata->argv[0]);
for (
size_t i = 1;
i < calldata->argc;
total *= exec_expr(calldata->argv[i++])
total *= exec_exp(calldata->argv[i++])
);
return total;
} else if (!strcmp(calldata->to, "div")) {
double total = exec_expr(calldata->argv[0]);
double total = exec_exp(calldata->argv[0]);
for (
size_t i = 1;
i < calldata->argc;
total /= exec_expr(calldata->argv[i++])
total /= exec_exp(calldata->argv[i++])
);
return total;

View File

@ -39,7 +39,6 @@
%left MULT DIV
%precedence NEG
%type<fval> num;
%type<ast> exp;
%%

View File

@ -7,7 +7,7 @@
typedef struct {
char* buf; // The buffer containing the string.
size_t bufsz; // The size of the buffer.
size_t sz; // The size of the buffer.
size_t ln; // The number of characters in the buffer.
} Dstr;

View File

@ -3,7 +3,7 @@
#include "ast.h"
ASTNumData exec_expr(AST* ast);
ASTNumData exec_exp(AST* ast);
ASTNumData exec_call(AST* ast);
void exec_print(double n);

View File

@ -21,7 +21,7 @@ int main(int argc, char** argv) {
if (argc - 1 && strlen(argv[1]) > 0 && (inp = argv[1]) && !yyparse()) {
log_dbg("Parsed successfully!\n");
exec_print(exec_expr(root));
exec_print(exec_exp(root));
exit(0);
}
@ -52,7 +52,7 @@ int main(int argc, char** argv) {
log_dbg("Parsed successfully!\n");
} else printf("Parse error.\n");
exec_print(exec_expr(root));
exec_print(exec_exp(root));
#ifdef DBG
ast_print(root);
#endif