Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "parser.h"
- #define MAXEXP 1000
- /* registers */
- float reg_a;
- float reg_b;
- float reg_c;
- float reg_ans;
- float eval(Tree *t);
- int main(void) {
- Tree *t;
- char buf[MAXEXP];
- printf("\n");
- printf("--------------------------------------------\n");
- printf("welcome to calc>, where dreams come true.\n");
- printf("type in your expressions lisp-style.\n");
- printf("press ctrl-D or type (quit) to quit\n");
- printf("--------------------------------------------\n");
- printf("\n");
- printf("calc> ");
- while (fgets(buf, MAXEXP, stdin)) {
- buf[strlen(buf)-1] = '\0';
- if ((t = parse(buf)) == NULL)
- break;
- if ((reg_ans = eval(t)) < 0)
- break;
- printf("%f\n", reg_ans);
- printf("calc> ");
- }
- printf("program quit\n");
- return 0;
- }
- float eval(Tree *t) {
- List *arglist = List_new();
- Tree *tptr = Tree_first_child(t);
- Node *ptr;
- char *proc;
- float n, result = 0;
- /* fetch the procedure */
- proc = tptr->datum;
- /* replace tree with correct values */
- for (tptr = Tree_next_sibling(tptr); tptr != NULL; tptr = Tree_next_sibling(tptr)) {
- /* find real value if needed */
- if (tptr->datum == NULL)
- n = eval(tptr);
- else {
- if (!(strcmp(tptr->datum, "a")))
- n = reg_a;
- else if (!(strcmp(tptr->datum, "b")))
- n = reg_b;
- else if (!(strcmp(tptr->datum, "c")))
- n = reg_c;
- else if (!(strcmp(tptr->datum, "ans")))
- n = reg_ans;
- else
- n = atof(tptr->datum);
- }
- /* replace tree string with real value */
- free(tptr->datum);
- tptr->datum = malloc(sizeof(float));
- *((float *)(tptr->datum)) = n;
- /* put it in list */
- List_append(arglist, tptr->datum);
- }
- /* assume at least one argument */
- if (proc == NULL) {
- printf("error: missing procedure\n");
- return -1;
- }
- else if (*proc == '*') {
- result = *((float *)(List_first(arglist)->data));
- for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
- result *= *((float *)ptr->data);
- }
- else if (*proc == '+') {
- result = *((float *)(List_first(arglist)->data));
- for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
- result += *((float *)ptr->data);
- }
- else if (*proc == '-') {
- result = *((float *)(List_first(arglist)->data));
- for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
- result /= *((float *)ptr->data);
- }
- else if (*proc == '/') {
- result = *((float *)(List_first(arglist)->data));
- for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
- result /= *((float *)ptr->data);
- }
- else if (*proc == 'a')
- result = reg_a = *((float *)(List_first(arglist)->data));
- else if (*proc == 'b')
- result = reg_b = *((float *)(List_first(arglist)->data));
- else if (*proc == 'c')
- result = reg_c = *((float *)(List_first(arglist)->data));
- else if (!(strcmp(proc, "quit")))
- return -1;
- else {
- printf("error: unsupported procedure\n");
- return -1;
- }
- /* free list */
- List_remove_all(arglist);
- /* free children */
- Tree_remove_children(t);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement