Skip to content

Instantly share code, notes, and snippets.

@vtomole
Last active November 21, 2017 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vtomole/5ec6bb35080c3c544682eaa58c19604e to your computer and use it in GitHub Desktop.
Save vtomole/5ec6bb35080c3c544682eaa58c19604e to your computer and use it in GitHub Desktop.
object* atom(struct token_object token_obj){
if(scmp(token_obj.type, "num")){
return create_number(token_obj.value);
}
else if(scmp(token_obj.type, "string")){
return create_string(token_obj.value);
}
else{
return create_primitiveop(token_obj.value);
}
}
object* parse_pair (token_list* token_list){
struct token_object token = token_list->val;
object *car_obj;
object *cdr_obj;
token_list= token_list->next;
if(scmp(token_list->val.value, ")")){
printf("Empty list\n");
return empty_list;
}
car_obj = parse(token_list);
if(scmp(token_list->val.value, ".")){
printf("Improper list\n");
exit(0);
}
else{
cdr_obj = parse_pair(token_list);
return cons(car_obj, cdr_obj);
}
}
//Takes the token list. for example (+ 7 (* 2 3)) gives a token list (-> + -> 7-> (->- * -> 2-> 3->)->)-> NULL
object* parse(token_list* token_list){
struct token_object token = token_list->val;
if(scmp(token.value, "(")){
return parse_pair(token_list);
}
else{
//printf("IN THE ELSE %s\n",token.value);
//Creates the atom
return atom(token);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment