Skip to content

Instantly share code, notes, and snippets.

@jgouly
Created October 11, 2011 16:45
Show Gist options
  • Save jgouly/1278639 to your computer and use it in GitHub Desktop.
Save jgouly/1278639 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define h ('('==c||c==')')
FILE *r;typedef struct n a;struct n{int t;char *v;a *f;a *s;};
a*n(int t,char*v,a*f,a*s){a*x=(a*)malloc(sizeof(a));if((x->t = t)){x->f
=f;x->s=s;}else{char*c=malloc(strlen(v)+1);strcpy(c,v);x->v=c;}return
x;}void p(a*a){if(!a){printf("()");}else{if(a->t){printf("(cons ")
;p(a->f);printf(" ");p(a->s);printf(")");}else{printf("%s",a->v);}}}a*
x(){char c=' ',b[256];int i=0;while(isspace(c))c=getc(r);if h b[i++]=c;
else{while(!h&&!isspace(c)){b[i++]=c;c=getc(r);}!h?:ungetc(c,r);}b[i]=
'\0';return n(0,b,NULL,NULL);}a*c(int i){a*a=x();return i?a->v[0]=='('
?c(0):a:a->v[0]==')'?NULL:a->v[0]=='('?n(1,"",c(0),c(0)):n(1,"",a,c(0)
);}int main(){a*e;r=stdin;while(1){e=c(1);p(e);printf("\n");}return 0;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment