Skip to content

Commit

Permalink
parsin like mofo
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Dipert committed Jul 2, 2009
1 parent 09c04bf commit 0d4c23e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
Binary file removed .Makefile.swp
Binary file not shown.
63 changes: 55 additions & 8 deletions parse.c
Expand Up @@ -20,26 +20,50 @@ static Token* t_append(Token *t1, Token *t2) {
} }
} }


//recursively free child tokens
static void t_free(Token *t) {
}

static Token* t_rewind(Token *t) { static Token* t_rewind(Token *t) {
while(t->prev) { while(t->prev) {
t = t->prev; t = t->prev;
} }
return t; return t;
} }


static void t_pp(Token *t) { static void t_tab(int level) {
while(level--) {
printf("\t");
}
}

static char* op_pp(enum operator_type op) {
switch(op) {
case ADD: return "add";
case SUBTRACT: return "subtract";
case MULTIPLY: return "multiply";
case DIVIDE: return "divide";
default: return "unknown";
}
}

static void t_pp(Token *t, int level) {
if(t != NULL) { if(t != NULL) {
t = t_rewind(t); t = t_rewind(t);
do { do {
switch(t->type) { switch(t->type) {
case ATOM: case ATOM:
printf("val: %f\n", t->n_value); t_tab(level);
printf("atom: %f\n", t->n_value);
break; break;
case OPERATOR: case OPERATOR:
printf("operator\n"); t_tab(level);
printf("%s\n", op_pp(t->o_type));
break; break;
case LIST: case LIST:
t_tab(level);
printf("list\n"); printf("list\n");
t_pp(t->head, level+1);
break; break;
} }
} while ((t = t->next)); } while ((t = t->next));
Expand All @@ -53,8 +77,32 @@ static Token* tokenize(char *input) {
Token *last_token = NULL; Token *last_token = NULL;
char buf[100]; char buf[100];
for(i = 0; i < strlen(input); i++) { for(i = 0; i < strlen(input); i++) {
//numbers if(input[i] == '(') {
if(isdigit(input[i])) { int p_count = 1;
char tmp;
m_start = &input[i+1];
m_count = 0;
while((tmp = input[i+(++m_count)]) != '\0' && p_count != 0) {
if(tmp == '(') {
p_count++;
} else if(tmp == ')') {
p_count--;
}
}
if(p_count) {
fprintf(stderr, "unmatched parens\n");
exit(1);
} else {
/*printf("all parens match\n");*/
strncpy(buf, m_start, m_count-1);
buf[m_count-2] = '\0';
Token *t2 = t_mk();
t2->type = LIST;
t2->head = tokenize(buf);
last_token = t_append(last_token, t2);
}
i += m_count;
} else if(isdigit(input[i])) {
m_start = &input[i]; m_start = &input[i];
m_count = 1; m_count = 1;
while(isdigit(m_start[m_count]) || m_start[m_count] == '.'){ while(isdigit(m_start[m_count]) || m_start[m_count] == '.'){
Expand All @@ -64,7 +112,7 @@ static Token* tokenize(char *input) {
t2->type = ATOM; t2->type = ATOM;
t2->a_type = NUMBER; t2->a_type = NUMBER;
strncpy(buf, m_start, m_count); strncpy(buf, m_start, m_count);
buf[m_count+1] = '\0'; buf[m_count] = '\0';
t2->n_value = atof(buf); t2->n_value = atof(buf);
last_token = t_append(last_token, t2); last_token = t_append(last_token, t2);
i += m_count; i += m_count;
Expand All @@ -77,10 +125,9 @@ static Token* tokenize(char *input) {
} }
} }


t_pp(last_token);
return last_token; return last_token;
} }


void eval(char *input) { void eval(char *input) {
tokenize(input); t_pp(tokenize(input), 0);
} }

0 comments on commit 0d4c23e

Please sign in to comment.