Can now do integer addition (with floats! :D).

This commit is contained in:
Jacob Signorovitch 2024-11-30 10:44:21 -05:00
parent 4514d94be9
commit 8e5b39a6e4
7 changed files with 21 additions and 28 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ tags
.cache .cache
build/* build/*
compile_commands.json compile_commands.json
vgcore.*

View File

@ -23,8 +23,8 @@ void ast_destroy(AST* ast) {
if (!ast) return; if (!ast) return;
switch (ast->type) { switch (ast->type) {
case AST_TYPE_NUM: ast_type_num_destroy(ast->data); break; case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break;
case AST_TYPE_CALL: ast_type_call_destroy(ast->data); break; case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break;
default: log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX); default: log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
} }
} }
@ -35,22 +35,19 @@ void ast_print(AST* ast) {
} }
ASTNumData* ast_type_num_init(int val) { ASTNumData* ast_num_data_init(double val) {
talloc(ASTNumData, num); talloc(ASTNumData, num);
num->val = val; *num = val;
return num; return num;
} }
void ast_type_num_destroy(ASTNumData* num) { void ast_num_data_destroy(ASTNumData* num) {
if (!num) if (!num) return free(num);
return
free(num);
} }
ASTCallData* ast_type_call_init(char* to, size_t argc, AST** argv) { ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
talloc(ASTCallData, call); talloc(ASTCallData, call);
call->to = to; call->to = to;
@ -60,11 +57,8 @@ ASTCallData* ast_type_call_init(char* to, size_t argc, AST** argv) {
return call; return call;
} }
void ast_type_call_destroy(ASTCallData* call) { void ast_call_data_destroy(ASTCallData* call) {
if (!call) if (!call) return free(call->to);
return
free(call->to);
for (size_t i = 0; i < call->argc; i++) free(call->argv[i]); for (size_t i = 0; i < call->argc; i++) free(call->argv[i]);
free(call); free(call);
} }

View File

@ -25,8 +25,8 @@ void exec_call() {
ASTNumData* n1 = (ASTNumData*)calldata->argv[0]->data; ASTNumData* n1 = (ASTNumData*)calldata->argv[0]->data;
ASTNumData* n2 = (ASTNumData*)calldata->argv[1]->data; ASTNumData* n2 = (ASTNumData*)calldata->argv[1]->data;
exec_return(n1->val + n2->val); exec_return(*n1 + *n2);
} }
} }
void exec_return(int n) { printf("= %d\n", n); } void exec_return(double n) { printf("= %lf\n", n); }

View File

@ -14,14 +14,14 @@
} }
%union { %union {
int intval; double fval;
char* strval; char* strval;
AST* ast; AST* ast;
} }
%define parse.error verbose %define parse.error verbose
%token<intval> NUM %token<fval> NUM
%token<strval> CALL %token<strval> CALL
%token PLUS %token PLUS
%token NL %token NL

View File

@ -18,12 +18,10 @@ AST* ast_init(ASTType type, void* data);
void ast_destroy(AST* ast); void ast_destroy(AST* ast);
void ast_print(AST* ast); void ast_print(AST* ast);
typedef struct { typedef double ASTNumData;
int val;
} ASTNumData;
ASTNumData* ast_type_num_init(int val); ASTNumData* ast_num_data_init(double val);
void ast_type_num_destroy(ASTNumData* num); void ast_num_data_destroy(ASTNumData* num);
typedef struct { typedef struct {
char* to; // What the call's to. char* to; // What the call's to.
@ -31,7 +29,7 @@ typedef struct {
AST** argv; // Argument vector. AST** argv; // Argument vector.
} ASTCallData; } ASTCallData;
ASTCallData* ast_type_call_init(char* to, size_t argc, AST** argv); ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
void ast_type_call_destroy(ASTCallData* call); void ast_call_data_destroy(ASTCallData* call);
#endif #endif

View File

@ -3,6 +3,6 @@
void exec_expr(); void exec_expr();
void exec_call(); void exec_call();
void exec_return(int n); void exec_return(double n);
#endif #endif

View File

@ -20,7 +20,7 @@ int yylex() {
value = value * 10 + (*inp - '0'); // Accumulate value. value = value * 10 + (*inp - '0'); // Accumulate value.
inp++; inp++;
} }
yylval.intval = value; // Set the token value. yylval.fval = value; // Set the token value.
return NUM; return NUM;
} }