Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #include "parser.h"
  2.  
  3. #define MAXEXP 1000
  4.  
  5. /* registers */
  6. float reg_a;
  7. float reg_b;
  8. float reg_c;
  9. float reg_ans;
  10.  
  11. float eval(Tree *t);
  12.  
  13. int main(void) {
  14.     Tree *t;
  15.     char buf[MAXEXP];
  16.  
  17.     printf("\n");
  18.     printf("--------------------------------------------\n");
  19.     printf("welcome to calc>, where dreams come true.\n");
  20.     printf("type in your expressions lisp-style.\n");
  21.     printf("press ctrl-D or type (quit) to quit\n");
  22.     printf("--------------------------------------------\n");
  23.     printf("\n");
  24.     printf("calc> ");
  25.  
  26.     while (fgets(buf, MAXEXP, stdin)) {
  27.         buf[strlen(buf)-1] = '\0';
  28.         if ((t = parse(buf)) == NULL)
  29.             break;
  30.         if ((reg_ans = eval(t)) < 0)
  31.             break;
  32.         printf("%f\n", reg_ans);
  33.         printf("calc> ");
  34.     }
  35.     printf("program quit\n");
  36.  
  37.     return 0;
  38. }
  39.  
  40. float eval(Tree *t) {
  41.     List *arglist = List_new();
  42.     Tree *tptr = Tree_first_child(t);
  43.     Node *ptr;
  44.     char *proc;
  45.     float n, result = 0;
  46.  
  47.     /* fetch the procedure */
  48.     proc = tptr->datum;
  49.  
  50.     /* replace tree with correct values */
  51.     for (tptr = Tree_next_sibling(tptr); tptr != NULL; tptr = Tree_next_sibling(tptr)) {
  52.         /* find real value if needed */
  53.         if (tptr->datum == NULL)
  54.             n = eval(tptr);
  55.         else {
  56.             if (!(strcmp(tptr->datum, "a")))
  57.                 n = reg_a;
  58.             else if (!(strcmp(tptr->datum, "b")))
  59.                 n = reg_b;
  60.             else if (!(strcmp(tptr->datum, "c")))
  61.                 n = reg_c;
  62.             else if (!(strcmp(tptr->datum, "ans")))
  63.                 n = reg_ans;
  64.             else
  65.                 n = atof(tptr->datum);
  66.         }
  67.         /* replace tree string with real value */
  68.         free(tptr->datum);
  69.         tptr->datum = malloc(sizeof(float));
  70.         *((float *)(tptr->datum)) = n;
  71.         /* put it in list */
  72.         List_append(arglist, tptr->datum);
  73.     }
  74.  
  75.     /* assume at least one argument */
  76.     if (proc == NULL) {
  77.         printf("error: missing procedure\n");
  78.         return -1;
  79.     }
  80.     else if (*proc == '*') {
  81.         result = *((float *)(List_first(arglist)->data));
  82.         for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
  83.             result *= *((float *)ptr->data);
  84.     }
  85.     else if (*proc == '+') {
  86.         result = *((float *)(List_first(arglist)->data));
  87.         for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
  88.             result += *((float *)ptr->data);
  89.     }
  90.     else if (*proc == '-') {
  91.         result = *((float *)(List_first(arglist)->data));
  92.         for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
  93.             result /= *((float *)ptr->data);
  94.     }
  95.     else if (*proc == '/') {
  96.         result = *((float *)(List_first(arglist)->data));
  97.         for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
  98.             result /= *((float *)ptr->data);
  99.     }
  100.     else if (*proc == 'a')
  101.         result = reg_a = *((float *)(List_first(arglist)->data));
  102.     else if (*proc == 'b')
  103.         result = reg_b = *((float *)(List_first(arglist)->data));
  104.     else if (*proc == 'c')
  105.         result = reg_c = *((float *)(List_first(arglist)->data));
  106.     else if (!(strcmp(proc, "quit")))
  107.         return -1;
  108.     else {
  109.         printf("error: unsupported procedure\n");
  110.         return -1;
  111.     }
  112.  
  113.     /* free list */
  114.     List_remove_all(arglist);
  115.     /* free children */
  116.     Tree_remove_children(t);
  117.  
  118.     return result;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement