Maybe fixed some memory leaks.
I don't know cauase lsan isn't working.
This commit is contained in:
parent
408a5a46c0
commit
6cbc28e092
2
Makefile
2
Makefile
@ -13,7 +13,7 @@ TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
|
|||||||
|
|
||||||
CC = clang
|
CC = clang
|
||||||
LINK = clang
|
LINK = clang
|
||||||
CFLAGS = -Wall -DDBG -ggdb
|
CFLAGS = -Wall -DDBG -ggdb -fsanitize=leak
|
||||||
LDFLAGS = -lm
|
LDFLAGS = -lm
|
||||||
BATS = bats
|
BATS = bats
|
||||||
BISON = bison
|
BISON = bison
|
||||||
|
58
src/exec.c
58
src/exec.c
@ -4,15 +4,14 @@
|
|||||||
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
#include "include/exec.h"
|
#include "include/exec.h"
|
||||||
|
#include "include/htab.h"
|
||||||
#include "include/stack.h"
|
#include "include/stack.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
#include "include/htab.h"
|
|
||||||
|
|
||||||
extern AST* root;
|
extern AST* root;
|
||||||
|
|
||||||
ASTNumData exec_start(AST* ast) {
|
ASTNumData exec_start(AST* ast) {
|
||||||
// The `Stack` of `HTab` that makes up the scope of any given `AST`.
|
scope = stack_init();
|
||||||
Stack* scope = stack_init();
|
|
||||||
|
|
||||||
// Global scope. Just dummy values for testing.
|
// Global scope. Just dummy values for testing.
|
||||||
HTab* global = htab_init();
|
HTab* global = htab_init();
|
||||||
@ -20,11 +19,16 @@ ASTNumData exec_start(AST* ast) {
|
|||||||
// n = 42.02
|
// n = 42.02
|
||||||
char* name = malloc(2); // TODO: Write a macro for this pattern.
|
char* name = malloc(2); // TODO: Write a macro for this pattern.
|
||||||
strcpy(name, "n");
|
strcpy(name, "n");
|
||||||
AST* n = ast_init(AST_TYPE_VDEF, ast_vdef_data_init(name,
|
AST* n = ast_init(
|
||||||
ast_init(AST_TYPE_NUM, ast_num_data_init(42.02))
|
AST_TYPE_VDEF,
|
||||||
));
|
ast_vdef_data_init(
|
||||||
|
name, ast_init(AST_TYPE_NUM, ast_num_data_init(42.02))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
htab_ins(global, ((ASTVDefData*)n->data)->name, ((ASTVDefData*)n->data)->val);
|
htab_ins(
|
||||||
|
global, ((ASTVDefData*)n->data)->name, ((ASTVDefData*)n->data)->val
|
||||||
|
);
|
||||||
|
|
||||||
// Push global namespace to `scope`.
|
// Push global namespace to `scope`.
|
||||||
stack_push(scope, global);
|
stack_push(scope, global);
|
||||||
@ -40,8 +44,7 @@ ASTNumData exec_exp(AST* ast, Stack* scope) {
|
|||||||
case AST_TYPE_NUM: return *(ASTNumData*)ast->data;
|
case AST_TYPE_NUM: return *(ASTNumData*)ast->data;
|
||||||
case AST_TYPE_VREF: return exec_vref(ast, scope);
|
case AST_TYPE_VREF: return exec_vref(ast, scope);
|
||||||
case AST_TYPE_VDEF: return exec_vdef(ast, scope);
|
case AST_TYPE_VDEF: return exec_vdef(ast, scope);
|
||||||
default: printf("what\n");
|
default: printf("what\n"); exit(1);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,8 +52,7 @@ ASTNumData exec_block(AST* ast, Stack* scope) {
|
|||||||
ASTBlockData* block = (ASTBlockData*)ast->data;
|
ASTBlockData* block = (ASTBlockData*)ast->data;
|
||||||
|
|
||||||
// Loop through all but last ast.
|
// Loop through all but last ast.
|
||||||
for (int i = 0; i < block->ln - 1; i++)
|
for (int i = 0; i < block->ln - 1; i++) exec_exp(block->inside[i], scope);
|
||||||
exec_exp(block->inside[i], scope);
|
|
||||||
|
|
||||||
return exec_exp(block->inside[block->ln - 1], scope);
|
return exec_exp(block->inside[block->ln - 1], scope);
|
||||||
}
|
}
|
||||||
@ -63,44 +65,33 @@ ASTNumData exec_call(AST* ast, Stack* scope) {
|
|||||||
if (!strcmp(calldata->to, "sum")) {
|
if (!strcmp(calldata->to, "sum")) {
|
||||||
double total = exec_exp(calldata->argv[0], scope);
|
double total = exec_exp(calldata->argv[0], scope);
|
||||||
|
|
||||||
for (
|
for (size_t i = 1; i < calldata->argc;
|
||||||
size_t i = 1;
|
total += exec_exp(calldata->argv[i++], scope));
|
||||||
i < calldata->argc;
|
|
||||||
total += exec_exp(calldata->argv[i++], scope)
|
|
||||||
);
|
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
} else if (!strcmp(calldata->to, "sub")) {
|
} else if (!strcmp(calldata->to, "sub")) {
|
||||||
double total = exec_exp(calldata->argv[0], scope);
|
double total = exec_exp(calldata->argv[0], scope);
|
||||||
|
|
||||||
for (
|
for (size_t i = 1; i < calldata->argc;
|
||||||
size_t i = 1;
|
total -= exec_exp(calldata->argv[i++], scope));
|
||||||
i < calldata->argc;
|
|
||||||
total -= exec_exp(calldata->argv[i++], scope)
|
|
||||||
);
|
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
} else if (!strcmp(calldata->to, "mul")) {
|
} else if (!strcmp(calldata->to, "mul")) {
|
||||||
double total = exec_exp(calldata->argv[0], scope);
|
double total = exec_exp(calldata->argv[0], scope);
|
||||||
|
|
||||||
for (
|
for (size_t i = 1; i < calldata->argc;
|
||||||
size_t i = 1;
|
total *= exec_exp(calldata->argv[i++], scope));
|
||||||
i < calldata->argc;
|
|
||||||
total *= exec_exp(calldata->argv[i++], scope)
|
|
||||||
);
|
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
} else if (!strcmp(calldata->to, "div")) {
|
} else if (!strcmp(calldata->to, "div")) {
|
||||||
double total = exec_exp(calldata->argv[0], scope);
|
double total = exec_exp(calldata->argv[0], scope);
|
||||||
|
|
||||||
for (
|
for (size_t i = 1; i < calldata->argc;
|
||||||
size_t i = 1;
|
total /= exec_exp(calldata->argv[i++], scope));
|
||||||
i < calldata->argc;
|
|
||||||
total /= exec_exp(calldata->argv[i++], scope)
|
|
||||||
);
|
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
return -1000;
|
return -1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +111,8 @@ ASTNumData exec_vref(AST* ast, Stack* scope) {
|
|||||||
AST* val;
|
AST* val;
|
||||||
|
|
||||||
val = htab_get(scope->val[0], key);
|
val = htab_get(scope->val[0], key);
|
||||||
if (val != NULL) return exec_exp(val, scope); else log_dbg("didn't find def");
|
if (val != NULL) return exec_exp(val, scope);
|
||||||
|
else log_dbg("didn't find def");
|
||||||
|
|
||||||
return *ast_num_data_init(101.0);
|
return *ast_num_data_init(101.0);
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ input:
|
|||||||
inputend:
|
inputend:
|
||||||
%empty
|
%empty
|
||||||
| input {
|
| input {
|
||||||
root = ast_init(AST_TYPE_BLOCK, ast_block_data_init($1->buf, $1->ln));
|
root = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $1->buf, $1->ln));
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
|
// The `Stack` of `HTab` that makes up the scope of any given `AST`.
|
||||||
|
extern Stack* scope;
|
||||||
|
|
||||||
// Start executing at the root of the AST. Initialize the `scope`.
|
// Start executing at the root of the AST. Initialize the `scope`.
|
||||||
ASTNumData exec_start(AST* ast);
|
ASTNumData exec_start(AST* ast);
|
||||||
|
13
src/main.c
13
src/main.c
@ -1,11 +1,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "include/global.h"
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
#include "include/dstr.h"
|
#include "include/dstr.h"
|
||||||
#include "include/exec.h"
|
#include "include/exec.h"
|
||||||
|
#include "include/global.h"
|
||||||
|
#include "include/htab.h"
|
||||||
#include "include/lexer.h"
|
#include "include/lexer.h"
|
||||||
|
#include "include/stack.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
|
||||||
#include "../build/grammars/grammar.tab.h"
|
#include "../build/grammars/grammar.tab.h"
|
||||||
@ -15,11 +17,16 @@ extern AST* root;
|
|||||||
extern char* inp;
|
extern char* inp;
|
||||||
extern int yyparse();
|
extern int yyparse();
|
||||||
|
|
||||||
|
Stack* scope;
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
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_start(root));
|
exec_print(exec_start(root));
|
||||||
|
HTab* global = stack_pop(scope);
|
||||||
|
htab_destroy(global);
|
||||||
|
stack_destroy(scope);
|
||||||
ast_destroy(root);
|
ast_destroy(root);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
@ -56,6 +63,10 @@ int main(int argc, char** argv) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
exec_print(exec_start(root));
|
exec_print(exec_start(root));
|
||||||
|
HTab* global = stack_pop(scope);
|
||||||
|
htab_destroy(global);
|
||||||
|
stack_destroy(scope);
|
||||||
|
|
||||||
ast_destroy(root);
|
ast_destroy(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user