Sexpr* append_fragment(Sexpr* tokens, char* tok_start, size_t currlen) {
// helper so that i dont repeat code
- char* newsym = malloc(sizeof(char)*currlen);
- strncpy(newsym, tok_start, currlen);
- Sexpr* newtok = from_sym(newsym);
- free(newsym);
- return cons(newtok, tokens);
+ char* newsym = malloc(sizeof(char)*currlen);
+ strncpy(newsym, tok_start, currlen);
+ Sexpr* newtok = from_sym(newsym);
+ free(newsym);
+ return cons(newtok, tokens);
}
+
Sexpr* tokenize(char* s) {
// note: also reverses
- Sexpr* openparen = from_sym("(");
- Sexpr* closeparen = from_sym(")");
Sexpr* tokens = from_nil();
char* tok_start = NULL;
// returns a list of every token
tokens = append_fragment(tokens, tok_start, currlen);
}
if(*s=='(') {
- tokens = cons(openparen, tokens);
+ tokens = cons(from_sym("("), tokens);
}
else {
- tokens = cons(closeparen, tokens);
+ tokens = cons(from_sym(")"), tokens);
}
already_in_sym = false;
currlen = 0;
}
Sexpr* cons_parse(Sexpr* tokens) {
- Sexpr* reversed = reverse(tokens);
+ Sexpr* reversedptr = reverse(tokens);
+ Sexpr* reversed = reversedptr;
// takes results from previous parsing ops, aka the forward-facing?
Sexpr* heads_stack = from_nil();
Sexpr* curr_head = from_nil();
Sexpr* curr_car;
while(reversed->type != NIL) {
- curr_car = car(reversed);
+ curr_car = clone(car(reversed));
Sexpr_Type cartype = curr_car->type;
if(cartype == SYM && strcmp(")", curr_car->value.s)==0) {
heads_stack = cons(curr_head, heads_stack);
}
reversed = cdr(reversed);
}
+ sexpr_free(reversedptr);
+ sexpr_free(heads_stack);
+ sexpr_free(curr_car);
return curr_head;
}
Sexpr* parse(char* s) {
//printf("s: %s\n", s);
Sexpr* tokens = tokenize(s);
- //printf("t: %s\n", sprint_sexpr(*tokens));
+ //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)) {
printf("unbalanced parenthesis\n");
+ sexpr_free(vals);
return NULL;
}
+ //printf("v1: %s\n", sprint_sexpr(vals));
Sexpr* done = cons_parse(vals);
+ //printf("v2: %s\n", sprint_sexpr(vals));
+ sexpr_free(vals);
//printf("c: %s\n", sprint_sexpr(*done));
return done;
}