if(cartype == SYM && strcmp(")", curr_car->value.s)==0) {
heads_stack = cons(curr_head, heads_stack);
curr_head = from_nil();
+ sexpr_free(curr_car);
}
else if(cartype == SYM && strcmp("(", curr_car->value.s)==0) {
Sexpr* prev_head = car(heads_stack);
curr_head = cons(curr_head, prev_head);
+ Sexpr* needtofreethistofixmemorybug = heads_stack;
heads_stack = cdr(heads_stack);
+ free(needtofreethistofixmemorybug->value.c);
+ free(needtofreethistofixmemorybug);
+ sexpr_free(curr_car);
}
else {
curr_head = cons(curr_car, curr_head);
}
sexpr_free(reversedptr);
sexpr_free(heads_stack);
- sexpr_free(curr_car);
return curr_head;
}
Sexpr* tokens = tokenize(s);
//printf("t: %s\n", sprint_sexpr(tokens));
Sexpr* vals = vals_parse(tokens);
- sexpr_free(vals);
- vals = vals_parse(tokens);
sexpr_free(tokens);
//printf("v: %s\n", sprint_sexpr(vals));
if(!balance_checker(vals)) {
Sexpr* from_sym(char* s) {
Sexpr* ret = malloc(sizeof(Sexpr));
ret->type = SYM;
- ret->value.s = malloc(strlen(s)+1);
- strcpy(ret->value.s, s);
+ ret->value.s = strdup(s);
return ret;
}
if(s->type == CONS) {
sexpr_free(car(s));
sexpr_free(cdr(s));
+ free(s->value.c);
}
if(s->type ==QUOTE) {
sexpr_free(s->value.q);
ret = from_uint(s->value.u);
break;
case SYM:
- ret = from_sym(strdup(s->value.s));
+ ret = from_sym(s->value.s);
break;
case BUILTIN:
ret = from_opcode(s->value.b.opcode);
currsize += strlen(carstr) + 1; // trailing space/close paren
out = realloc(out, currsize);
strcat(out, carstr);
+ free(carstr);
strcat(out, " ");
curr_cell = cdr(curr_cell);
}
strcat(out, cdrstr);
strcat(out, ")");
out[currsize-1] = '\0';
+ free(cdrstr);
return out;
}
}
void mem_parser() {
printf("starting parser memory testing\n");
- char* toparse = "(car (cons 1 (cons 2 nil)))";
+ //char* toparse = "(car (cons 1 (cons 2 nil)))";
+ char* toparse = "(cons 1 2)";
char* out;
Sexpr* parsed;
unsigned long l = 0;
- while(l < 10000) {
+ while(l < 1) {
parsed = parse(toparse);
out = sprint_sexpr(parsed);
sexpr_free(parsed);
- //printf("%s\n", out);
+ printf("%s\n", out);
free(out);
//getchar();
l++;
}
}
+
+void mem_testing() {
+ printf("mem testing\n");
+ Sexpr* a = cons(from_uint(5), cons(from_uint(6), from_nil()));
+ //Sexpr* a = cons(from_uint(1), from_sym("hi"));
+ char* out = sprint_sexpr(a);
+ printf("%s\n", out);
+ sexpr_free(a);
+ free(out);
+ a = tokenize("(cons 1 2)");
+ out = sprint_sexpr(a);
+ printf("%s\n", out);
+ free(out);
+ Sexpr* b = vals_parse(a);
+ sexpr_free(a);
+ out = sprint_sexpr(b);
+ printf("%s\n", out);
+ free(out);
+ Sexpr* r = reverse(b);
+ out = sprint_sexpr(r);
+ sexpr_free(r);
+ printf("%s\n", out);
+ free(out);
+
+
+
+ Sexpr* c = cons_parse(b);
+ sexpr_free(b);
+ out = sprint_sexpr(c);
+ sexpr_free(c);
+ printf("%s\n", out);
+ free(out);
+}
+
void run_tests(){
+ mem_testing();
mem_parser();
//mem_hammer();
//test_basics();