Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Convert the list of tokens to a cons cell */
- /* tokens have a "type" (one of NUMBER, NUMBER_FLT, STRING, SYMBOL, LPAREN, RPAREN) */
- struct cons *tok_to_cons(struct tokens *tok)
- {
- struct tokens *node = tok;
- struct cons *cell = malloc(sizeof(struct cons *));
- struct cons *head = cell;
- node = node->next;
- int *integer = NULL;
- float *fl = NULL;
- while (node != NULL) {
- object_t *obj = (node->type != RPAREN) ? malloc(sizeof(object_t *)) : NULL;
- /* Similarly, objects also have a type, except they cannot be (L|R)PAREN */
- switch(node->type) {
- case NUMBER:
- integer = malloc(sizeof(int *));
- *integer = atoi(node->str);
- obj->val = (void *)integer;
- break;
- case NUMBER_FLT:
- fl = malloc(sizeof(float *));
- *fl = atof(node->str);
- obj->val = (void *)fl;
- break;
- case SYMBOL:
- obj->val = (char *)strdup(node->str);
- break;
- case LPAREN:
- /* Entering a nested cell */
- /* For reference, (a . (b . ((c . (d . nil)) . nil))) = (a b (c d)) */
- obj->val = tok_to_cons(node);
- break;
- case RPAREN:
- /* Reached end of cons cell */
- head->cdr = NULL;
- return cell;
- case STRING:
- obj->val = strdup(node->str);
- break;
- }
- obj->type = node->type;
- head->car = obj;
- head->cdr = malloc(sizeof(struct cons *));
- node = node->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement