Cleaned up.
This commit is contained in:
parent
1098fa252f
commit
bc0c4f33ad
12
src/dstr.c
12
src/dstr.c
@ -8,7 +8,7 @@
|
|||||||
Dstr* dstr_init(void) {
|
Dstr* dstr_init(void) {
|
||||||
Dstr* dstr = malloc(sizeof(Dstr));
|
Dstr* dstr = malloc(sizeof(Dstr));
|
||||||
|
|
||||||
dstr->bufsz = DSTR_INITSZ;
|
dstr->sz = DSTR_INITSZ;
|
||||||
dstr->buf = malloc(DSTR_INITSZ);
|
dstr->buf = malloc(DSTR_INITSZ);
|
||||||
*dstr->buf = '\0';
|
*dstr->buf = '\0';
|
||||||
dstr->ln = 0;
|
dstr->ln = 0;
|
||||||
@ -23,13 +23,13 @@ void dstr_destroy(Dstr* dstr) {
|
|||||||
|
|
||||||
// Check whether the buffer is overflowing and resize it if necessary.
|
// Check whether the buffer is overflowing and resize it if necessary.
|
||||||
void check_resz(Dstr* dstr, size_t ln) {
|
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.
|
// Double the buffer size when overflown.
|
||||||
dstr->bufsz *= 2;
|
dstr->sz *= 2;
|
||||||
dstr->buf = realloc(dstr->buf, dstr->bufsz);
|
dstr->buf = realloc(dstr->buf, dstr->sz);
|
||||||
log_dbgf(
|
log_dbgf(
|
||||||
"dstr @ %p doubled from %ld to %ld", dstr, dstr->bufsz / 2,
|
"dstr @ %p doubled from %ld to %ld", dstr, dstr->sz / 2,
|
||||||
dstr->bufsz
|
dstr->sz
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
18
src/exec.c
18
src/exec.c
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
extern AST* root;
|
extern AST* root;
|
||||||
|
|
||||||
ASTNumData exec_expr(AST* ast) {
|
ASTNumData exec_exp(AST* ast) {
|
||||||
log_dbg("Started execution.");
|
log_dbg("Started execution.");
|
||||||
switch (ast->type) {
|
switch (ast->type) {
|
||||||
case AST_TYPE_CALL: return exec_call(ast);
|
case AST_TYPE_CALL: return exec_call(ast);
|
||||||
@ -23,42 +23,42 @@ ASTNumData exec_call(AST* ast) {
|
|||||||
ASTCallData* calldata = (ASTCallData*)ast->data;
|
ASTCallData* calldata = (ASTCallData*)ast->data;
|
||||||
if (calldata->argc >= 1) {
|
if (calldata->argc >= 1) {
|
||||||
if (!strcmp(calldata->to, "sum")) {
|
if (!strcmp(calldata->to, "sum")) {
|
||||||
double total = exec_expr(calldata->argv[0]);
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
for (
|
for (
|
||||||
size_t i = 1;
|
size_t i = 1;
|
||||||
i < calldata->argc;
|
i < calldata->argc;
|
||||||
total += exec_expr(calldata->argv[i++])
|
total += exec_exp(calldata->argv[i++])
|
||||||
);
|
);
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
} else if (!strcmp(calldata->to, "sub")) {
|
} else if (!strcmp(calldata->to, "sub")) {
|
||||||
double total = exec_expr(calldata->argv[0]);
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
for (
|
for (
|
||||||
size_t i = 1;
|
size_t i = 1;
|
||||||
i < calldata->argc;
|
i < calldata->argc;
|
||||||
total -= exec_expr(calldata->argv[i++])
|
total -= exec_exp(calldata->argv[i++])
|
||||||
);
|
);
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
} else if (!strcmp(calldata->to, "mul")) {
|
} else if (!strcmp(calldata->to, "mul")) {
|
||||||
double total = exec_expr(calldata->argv[0]);
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
for (
|
for (
|
||||||
size_t i = 1;
|
size_t i = 1;
|
||||||
i < calldata->argc;
|
i < calldata->argc;
|
||||||
total *= exec_expr(calldata->argv[i++])
|
total *= exec_exp(calldata->argv[i++])
|
||||||
);
|
);
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
} else if (!strcmp(calldata->to, "div")) {
|
} else if (!strcmp(calldata->to, "div")) {
|
||||||
double total = exec_expr(calldata->argv[0]);
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
for (
|
for (
|
||||||
size_t i = 1;
|
size_t i = 1;
|
||||||
i < calldata->argc;
|
i < calldata->argc;
|
||||||
total /= exec_expr(calldata->argv[i++])
|
total /= exec_exp(calldata->argv[i++])
|
||||||
);
|
);
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
|
@ -39,7 +39,6 @@
|
|||||||
%left MULT DIV
|
%left MULT DIV
|
||||||
%precedence NEG
|
%precedence NEG
|
||||||
|
|
||||||
%type<fval> num;
|
|
||||||
%type<ast> exp;
|
%type<ast> exp;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* buf; // The buffer containing the string.
|
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.
|
size_t ln; // The number of characters in the buffer.
|
||||||
} Dstr;
|
} Dstr;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
|
|
||||||
ASTNumData exec_expr(AST* ast);
|
ASTNumData exec_exp(AST* ast);
|
||||||
ASTNumData exec_call(AST* ast);
|
ASTNumData exec_call(AST* ast);
|
||||||
void exec_print(double n);
|
void exec_print(double n);
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
if (argc - 1 && strlen(argv[1]) > 0 && (inp = argv[1]) && !yyparse()) {
|
if (argc - 1 && strlen(argv[1]) > 0 && (inp = argv[1]) && !yyparse()) {
|
||||||
log_dbg("Parsed successfully!\n");
|
log_dbg("Parsed successfully!\n");
|
||||||
exec_print(exec_expr(root));
|
exec_print(exec_exp(root));
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ int main(int argc, char** argv) {
|
|||||||
log_dbg("Parsed successfully!\n");
|
log_dbg("Parsed successfully!\n");
|
||||||
} else printf("Parse error.\n");
|
} else printf("Parse error.\n");
|
||||||
|
|
||||||
exec_print(exec_expr(root));
|
exec_print(exec_exp(root));
|
||||||
#ifdef DBG
|
#ifdef DBG
|
||||||
ast_print(root);
|
ast_print(root);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user