Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 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 (exit) 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.  
  36.     return 0;
  37. }
  38.  
  39. float eval(Tree *t) {
  40.     List *arglist = List_new();
  41.     Tree *tptr = Tree_first_child(t);
  42.     Node *ptr;
  43.     char *proc;
  44.     float n, result = 0;
  45.  
  46.     /* fetch the procedure */
  47.     proc = tptr->datum;
  48.  
  49.     /* replace tree with correct values */
  50.     for (tptr = Tree_next_sibling(tptr); tptr != NULL; tptr = Tree_next_sibling(tptr)) {
  51.         /* find real value if needed */
  52.         if (tptr->datum == NULL)
  53.             n = eval(tptr);
  54.         else {
  55.             if (!(strcmp(tptr->datum, "a")))
  56.                 n = reg_a;
  57.             else if (!(strcmp(tptr->datum, "b")))
  58.                 n = reg_b;
  59.             else if (!(strcmp(tptr->datum, "c")))
  60.                 n = reg_c;
  61.             else if (!(strcmp(tptr->datum, "ans")))
  62.                 n = reg_ans;
  63.             else
  64.                 n = atof(tptr->datum);
  65.         }
  66.         /* replace tree string with real value */
  67.         free(tptr->datum);
  68.         tptr->datum = malloc(sizeof(float));
  69.         *((float *)(tptr->datum)) = n;
  70.         /* put it in list */
  71.         List_append(arglist, tptr->datum);
  72.     }
  73.  
  74.     if (proc == NULL) {
  75.         printf("error: missing procedure\n");
  76.         return -1;
  77.     }
  78.     /* elementary arithmetic */
  79.     else if (*proc == '*') {
  80.         if (List_first(arglist) == NULL) {
  81.             printf("error: wrong number of arguments\n");
  82.             return -1;
  83.         }
  84.         result = *((float *)(List_first(arglist)->data));
  85.         for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
  86.             result *= *((float *)ptr->data);
  87.     }
  88.     else if (*proc == '+') {
  89.         if (List_first(arglist) == NULL) {
  90.             printf("error: wrong number of arguments\n");
  91.             return -1;
  92.         }
  93.         result = *((float *)(List_first(arglist)->data));
  94.         for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
  95.             result += *((float *)ptr->data);
  96.     }
  97.     else if (*proc == '-') {
  98.         if (List_first(arglist) == NULL) {
  99.             printf("error: wrong number of arguments\n");
  100.             return -1;
  101.         }
  102.         result = *((float *)(List_first(arglist)->data));
  103.         for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
  104.             result /= *((float *)ptr->data);
  105.     }
  106.     else if (*proc == '/') {
  107.         if (List_first(arglist) == NULL) {
  108.             printf("error: wrong number of arguments\n");
  109.             return -1;
  110.         }
  111.         result = *((float *)(List_first(arglist)->data));
  112.         for (ptr = List_first(arglist)->next; ptr != NULL; ptr = ptr->next)
  113.             result /= *((float *)ptr->data);
  114.     }
  115.     /* store register */
  116.     else if (*proc == 'a')
  117.         result = reg_a = *((float *)(List_first(arglist)->data));
  118.     else if (*proc == 'b')
  119.         result = reg_b = *((float *)(List_first(arglist)->data));
  120.     else if (*proc == 'c')
  121.         result = reg_c = *((float *)(List_first(arglist)->data));
  122.     /* quit */
  123.     else if (!(strcmp(proc, "exit")))
  124.         return -1;
  125.     else {
  126.         printf("error: unsupported procedure\n");
  127.         return -1;
  128.     }
  129.  
  130.     /* free list */
  131.     List_remove_all(arglist);
  132.     /* free children */
  133.     Tree_remove_children(t);
  134.  
  135.     return result;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement