Can now do integer addition (with floats! :D).
This commit is contained in:
parent
4514d94be9
commit
8e5b39a6e4
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@ tags
|
||||
.cache
|
||||
build/*
|
||||
compile_commands.json
|
||||
vgcore.*
|
||||
|
24
src/ast.c
24
src/ast.c
@ -23,8 +23,8 @@ void ast_destroy(AST* ast) {
|
||||
if (!ast) return;
|
||||
|
||||
switch (ast->type) {
|
||||
case AST_TYPE_NUM: ast_type_num_destroy(ast->data); break;
|
||||
case AST_TYPE_CALL: ast_type_call_destroy(ast->data); break;
|
||||
case AST_TYPE_NUM: ast_num_data_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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
|
||||
num->val = val;
|
||||
*num = val;
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
void ast_type_num_destroy(ASTNumData* num) {
|
||||
if (!num)
|
||||
return
|
||||
|
||||
free(num);
|
||||
void ast_num_data_destroy(ASTNumData* num) {
|
||||
if (!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);
|
||||
|
||||
call->to = to;
|
||||
@ -60,11 +57,8 @@ ASTCallData* ast_type_call_init(char* to, size_t argc, AST** argv) {
|
||||
return call;
|
||||
}
|
||||
|
||||
void ast_type_call_destroy(ASTCallData* call) {
|
||||
if (!call)
|
||||
return
|
||||
|
||||
free(call->to);
|
||||
void ast_call_data_destroy(ASTCallData* call) {
|
||||
if (!call) return free(call->to);
|
||||
for (size_t i = 0; i < call->argc; i++) free(call->argv[i]);
|
||||
free(call);
|
||||
}
|
||||
|
@ -25,8 +25,8 @@ void exec_call() {
|
||||
ASTNumData* n1 = (ASTNumData*)calldata->argv[0]->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); }
|
||||
|
@ -14,14 +14,14 @@
|
||||
}
|
||||
|
||||
%union {
|
||||
int intval;
|
||||
double fval;
|
||||
char* strval;
|
||||
AST* ast;
|
||||
}
|
||||
|
||||
%define parse.error verbose
|
||||
|
||||
%token<intval> NUM
|
||||
%token<fval> NUM
|
||||
%token<strval> CALL
|
||||
%token PLUS
|
||||
%token NL
|
||||
|
@ -18,12 +18,10 @@ AST* ast_init(ASTType type, void* data);
|
||||
void ast_destroy(AST* ast);
|
||||
void ast_print(AST* ast);
|
||||
|
||||
typedef struct {
|
||||
int val;
|
||||
} ASTNumData;
|
||||
typedef double ASTNumData;
|
||||
|
||||
ASTNumData* ast_type_num_init(int val);
|
||||
void ast_type_num_destroy(ASTNumData* num);
|
||||
ASTNumData* ast_num_data_init(double val);
|
||||
void ast_num_data_destroy(ASTNumData* num);
|
||||
|
||||
typedef struct {
|
||||
char* to; // What the call's to.
|
||||
@ -31,7 +29,7 @@ typedef struct {
|
||||
AST** argv; // Argument vector.
|
||||
} ASTCallData;
|
||||
|
||||
ASTCallData* ast_type_call_init(char* to, size_t argc, AST** argv);
|
||||
void ast_type_call_destroy(ASTCallData* call);
|
||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
|
||||
void ast_call_data_destroy(ASTCallData* call);
|
||||
|
||||
#endif
|
||||
|
@ -3,6 +3,6 @@
|
||||
|
||||
void exec_expr();
|
||||
void exec_call();
|
||||
void exec_return(int n);
|
||||
void exec_return(double n);
|
||||
|
||||
#endif
|
||||
|
@ -20,7 +20,7 @@ int yylex() {
|
||||
value = value * 10 + (*inp - '0'); // Accumulate value.
|
||||
inp++;
|
||||
}
|
||||
yylval.intval = value; // Set the token value.
|
||||
yylval.fval = value; // Set the token value.
|
||||
return NUM;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user