step: spend a bit obsessing over memory leaks.
This commit is contained in:
parent
f88ad89868
commit
29db17c074
117
src/mumble.c
117
src/mumble.c
|
@ -7,7 +7,9 @@
|
|||
#include "mpc/mpc.h"
|
||||
#define VERBOSE 0
|
||||
#define LISPVAL_ASSERT(cond, err) \
|
||||
if (!(cond)) { return lispval_err(err); }
|
||||
if (!(cond)) { \
|
||||
return lispval_err(err); \
|
||||
}
|
||||
|
||||
// Types
|
||||
typedef struct lispval {
|
||||
|
@ -36,6 +38,7 @@ enum {
|
|||
// Constructors
|
||||
lispval* lispval_num(double x)
|
||||
{
|
||||
printf("\nAllocated num");
|
||||
lispval* v = malloc(sizeof(lispval));
|
||||
v->type = LISPVAL_NUM;
|
||||
v->count = 0;
|
||||
|
@ -45,6 +48,7 @@ lispval* lispval_num(double x)
|
|||
|
||||
lispval* lispval_err(char* message)
|
||||
{
|
||||
printf("\nAllocated err");
|
||||
lispval* v = malloc(sizeof(lispval));
|
||||
v->type = LISPVAL_ERR;
|
||||
v->count = 0;
|
||||
|
@ -55,6 +59,7 @@ lispval* lispval_err(char* message)
|
|||
|
||||
lispval* lispval_sym(char* symbol)
|
||||
{
|
||||
printf("\nAllocated sym");
|
||||
lispval* v = malloc(sizeof(lispval));
|
||||
v->type = LISPVAL_SYM;
|
||||
v->count = 0;
|
||||
|
@ -65,6 +70,7 @@ lispval* lispval_sym(char* symbol)
|
|||
|
||||
lispval* lispval_sexpr(void)
|
||||
{
|
||||
printf("\nAllocated sexpr");
|
||||
lispval* v = malloc(sizeof(lispval));
|
||||
v->type = LISPVAL_SEXPR;
|
||||
v->count = 0;
|
||||
|
@ -74,6 +80,7 @@ lispval* lispval_sexpr(void)
|
|||
|
||||
lispval* lispval_qexpr(void)
|
||||
{
|
||||
printf("\nAllocated qexpr");
|
||||
lispval* v = malloc(sizeof(lispval));
|
||||
v->type = LISPVAL_QEXPR;
|
||||
v->count = 0;
|
||||
|
@ -84,24 +91,51 @@ lispval* lispval_qexpr(void)
|
|||
// Destructor
|
||||
void delete_lispval(lispval* v)
|
||||
{
|
||||
// printf("\n1");
|
||||
switch (v->type) {
|
||||
case LISPVAL_NUM:
|
||||
printf("\nFreed num");
|
||||
// free(v);
|
||||
// v = NULL;
|
||||
// printf("\n2");
|
||||
break;
|
||||
case LISPVAL_ERR:
|
||||
printf("\nFreed err");
|
||||
// printf("\n3");
|
||||
if (v->err != NULL)
|
||||
free(v->err);
|
||||
v->err = NULL;
|
||||
break;
|
||||
case LISPVAL_SYM:
|
||||
printf("\nFreed sym");
|
||||
// printf("\n4");
|
||||
if (v->sym != NULL)
|
||||
free(v->sym);
|
||||
v->sym = NULL;
|
||||
break;
|
||||
case LISPVAL_SEXPR:
|
||||
// printf("\n5");
|
||||
case LISPVAL_QEXPR:
|
||||
printf("\nFreed s/qexpr");
|
||||
// printf("\n6");
|
||||
for (int i = 0; i < v->count; i++) {
|
||||
if (v->cell[i] != NULL)
|
||||
delete_lispval(v->cell[i]);
|
||||
v->cell[i] = NULL;
|
||||
}
|
||||
// printf("\n8");
|
||||
if (v->cell != NULL)
|
||||
free(v->cell);
|
||||
v->cell = NULL;
|
||||
|
||||
// printf("\n9");
|
||||
break;
|
||||
}
|
||||
if (v != NULL)
|
||||
free(v);
|
||||
// printf("\n10");
|
||||
v = NULL;
|
||||
// printf("\n11");
|
||||
}
|
||||
|
||||
// Read ast into a lispval object
|
||||
|
@ -134,7 +168,8 @@ lispval* read_lispval(mpc_ast_t* t)
|
|||
c_index = i;
|
||||
}
|
||||
}
|
||||
if(VERBOSE) printf("\nNon ignorable children: %i", c);
|
||||
if (VERBOSE)
|
||||
printf("\nNon ignorable children: %i", c);
|
||||
|
||||
if (strstr(t->tag, "number")) {
|
||||
return read_lispval_num(t);
|
||||
|
@ -159,11 +194,9 @@ lispval* read_lispval(mpc_ast_t* t)
|
|||
continue;
|
||||
} else if (strcmp(t->children[i]->contents, "{") == 0) {
|
||||
continue;
|
||||
}
|
||||
else if (strcmp(t->children[i]->contents, "}") == 0) {
|
||||
} else if (strcmp(t->children[i]->contents, "}") == 0) {
|
||||
continue;
|
||||
}
|
||||
else if (strcmp(t->children[i]->tag, "regex") == 0) {
|
||||
} else if (strcmp(t->children[i]->tag, "regex") == 0) {
|
||||
continue;
|
||||
} else {
|
||||
x = lispval_append_child(x, read_lispval(t->children[i]));
|
||||
|
@ -212,6 +245,7 @@ void print_lispval_tree(lispval* v, int indent_level)
|
|||
printf("%s", v->sym);
|
||||
}
|
||||
free(indent);
|
||||
indent = NULL;
|
||||
}
|
||||
|
||||
void print_lispval_parenthesis(lispval* v)
|
||||
|
@ -264,6 +298,7 @@ void print_ast(mpc_ast_t* ast, int indent_level)
|
|||
print_ast(child_i, indent_level + 2);
|
||||
}
|
||||
free(indent);
|
||||
indent = NULL;
|
||||
}
|
||||
|
||||
// Lispval helpers
|
||||
|
@ -338,7 +373,8 @@ lispval* take_lispval(lispval* v, int i)
|
|||
|
||||
// Operations
|
||||
// Ops for q-expressions
|
||||
lispval* builtin_head(lispval* v){
|
||||
lispval* builtin_head(lispval* v)
|
||||
{
|
||||
// printf("Entering builtin_head with v->count = %d and v->cell[0]->type = %d\n", v->count, v->cell[0]->type);
|
||||
// head { 1 2 3 }
|
||||
// But actually, that gets processd into head ({ 1 2 3 }), hence the v->cell[0]->cell[0];
|
||||
|
@ -389,7 +425,8 @@ lispval* builtin_tail(lispval* v)
|
|||
// Returns something that doesn't share pointers with the input: yes.
|
||||
}
|
||||
|
||||
lispval* builtin_list(lispval* v){
|
||||
lispval* builtin_list(lispval* v)
|
||||
{
|
||||
// list ( 1 2 3 )
|
||||
LISPVAL_ASSERT(v->count == 1, "Error: function list passed too many arguments");
|
||||
lispval* old = v->cell[0];
|
||||
|
@ -402,20 +439,24 @@ lispval* builtin_list(lispval* v){
|
|||
}
|
||||
|
||||
lispval* evaluate_lispval(lispval* l);
|
||||
lispval* builtin_eval(lispval* v){
|
||||
lispval* builtin_eval(lispval* v)
|
||||
{
|
||||
// eval { + 1 2 3 }
|
||||
// not sure how this will end up working, but we'll see
|
||||
LISPVAL_ASSERT(v->count == 1, "Error: function eval passed too many arguments");
|
||||
lispval* old = v->cell[0];
|
||||
LISPVAL_ASSERT(old->type == LISPVAL_QEXPR, "Error: Argument passed to eval is not a q-expr, i.e., a bracketed list.");
|
||||
lispval* new = clone_lispval(old);
|
||||
new->type=LISPVAL_SEXPR;
|
||||
return evaluate_lispval(new);
|
||||
lispval* temp = clone_lispval(old);
|
||||
temp->type = LISPVAL_SEXPR;
|
||||
lispval* answer = evaluate_lispval(temp);
|
||||
delete_lispval(temp);
|
||||
return answer;
|
||||
// Returns something that should be freed later: probably.
|
||||
// Returns something that is independent of the input: depends on the output of evaluate_lispval.
|
||||
}
|
||||
|
||||
lispval* builtin_join(lispval* l){
|
||||
lispval* builtin_join(lispval* l)
|
||||
{
|
||||
// return lispval_err("Error: Join not ready yet.");
|
||||
// join { {1 2} {3 4} }
|
||||
print_lispval_parenthesis(l);
|
||||
|
@ -489,12 +530,18 @@ lispval* builtin_math_ops(char* op, lispval* v)
|
|||
// Aggregate both math and operations over lists
|
||||
lispval* builtin_functions(char* func, lispval* v)
|
||||
{
|
||||
if (strcmp("list", func) == 0) { return builtin_list(v); }
|
||||
else if (strcmp("head", func) == 0) { return builtin_head(v); }
|
||||
else if (strcmp("tail", func) == 0) { return builtin_tail(v); }
|
||||
else if (strcmp("join", func) == 0) { return builtin_join(v); }
|
||||
else if (strcmp("eval", func) == 0) { return builtin_eval(v); }
|
||||
else if (strstr("+-/*", func)) { return builtin_math_ops(func, v);
|
||||
if (strcmp("list", func) == 0) {
|
||||
return builtin_list(v);
|
||||
} else if (strcmp("head", func) == 0) {
|
||||
return builtin_head(v);
|
||||
} else if (strcmp("tail", func) == 0) {
|
||||
return builtin_tail(v);
|
||||
} else if (strcmp("join", func) == 0) {
|
||||
return builtin_join(v);
|
||||
} else if (strcmp("eval", func) == 0) {
|
||||
return builtin_eval(v);
|
||||
} else if (strstr("+-/*", func)) {
|
||||
return builtin_math_ops(func, v);
|
||||
} else {
|
||||
return lispval_err("Unknown function");
|
||||
}
|
||||
|
@ -506,7 +553,8 @@ lispval* builtin_functions(char* func, lispval* v)
|
|||
lispval* evaluate_lispval(lispval* l)
|
||||
{
|
||||
// Check if this is an s-expression
|
||||
if(l->type != LISPVAL_SEXPR) return l;
|
||||
if (l->type != LISPVAL_SEXPR)
|
||||
return l;
|
||||
// Evaluate the children if needed
|
||||
for (int i = 0; i < l->count; i++) {
|
||||
if (l->cell[i]->type == LISPVAL_SEXPR) {
|
||||
|
@ -522,16 +570,20 @@ lispval* evaluate_lispval(lispval* l)
|
|||
|
||||
// Check if the first element is an operation.
|
||||
if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_SYM)) {
|
||||
// lispval* op = pop_lispval(l, 0);
|
||||
lispval* operation = clone_lispval(l->cell[0]);
|
||||
lispval* operands = lispval_sexpr();
|
||||
for(int i=1; i<l->count; i++){
|
||||
lispval_append_child(operands, l->cell[i]);
|
||||
}
|
||||
lispval* result = builtin_functions(operation->sym, operands);
|
||||
lispval* temp = clone_lispval(l->cell[0]);
|
||||
lispval* operation = pop_lispval(l, 0);
|
||||
lispval* operands = temp;
|
||||
// lispval* operation = clone_lispval(l->cell[0]);
|
||||
// lispval* operands = lispval_sexpr();
|
||||
// for (int i = 1; i < l->count; i++) {
|
||||
// lispval_append_child(operands, l->cell[i]);
|
||||
// }
|
||||
lispval* answer = builtin_functions(operation->sym, l);
|
||||
delete_lispval(operation);
|
||||
delete_lispval(operands);
|
||||
return result;
|
||||
// delete_lispval(operation);
|
||||
// delete_lispval(operands); // < wrong! they still remain in the list.
|
||||
return answer;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
@ -540,7 +592,7 @@ int main(int argc, char** argv)
|
|||
{
|
||||
// Info
|
||||
puts("Mumble version 0.0.2\n");
|
||||
puts("Press Ctrl+C/Ctrl+D to exit\n");
|
||||
puts("Press Ctrl+C to exit\n");
|
||||
|
||||
/* Create Some Parsers */
|
||||
mpc_parser_t* Number = mpc_new("number");
|
||||
|
@ -595,14 +647,14 @@ int main(int argc, char** argv)
|
|||
printf("\nParenthesis printing: ");
|
||||
print_lispval_parenthesis(l);
|
||||
}
|
||||
lispval* result = evaluate_lispval(l);
|
||||
lispval* answer = evaluate_lispval(l);
|
||||
{
|
||||
printf("\n\nResult: ");
|
||||
print_lispval_parenthesis(result);
|
||||
print_lispval_parenthesis(answer);
|
||||
printf("\n");
|
||||
}
|
||||
delete_lispval(l);
|
||||
delete_lispval(result);
|
||||
delete_lispval(answer);
|
||||
} else {
|
||||
/* Otherwise Print the Error */
|
||||
mpc_err_print(result.error);
|
||||
|
@ -613,6 +665,7 @@ int main(int argc, char** argv)
|
|||
}
|
||||
puts("");
|
||||
free(input);
|
||||
input = NULL;
|
||||
}
|
||||
|
||||
/* Undefine and Delete our Parsers */
|
||||
|
|
Loading…
Reference in New Issue
Block a user