math.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* tripl/arch/interp/math.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * tripl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <string.h>
  22. #include <klist.h>
  23. #include <tripl/tripl.h>
  24. #include <tripl/limits.h>
  25. #include <tripl/arch.h>
  26. extern char *psave;
  27. extern struct klist_node *find_name(char *);
  28. __arch_templ static long getnum(char **p, int prio);
  29. #define PRIO_MAX 9
  30. #define PRIO_MIN 0
  31. __arch_templ long interp_math_expr(char *p, char *var) {
  32. struct klist_node *n;
  33. p++;
  34. _skip_spaces(p);
  35. n = find_name(var);
  36. if (!n) {
  37. error(var);
  38. exit(generic_error("name not found", psave, lno));
  39. }
  40. if (n->xmask & XMASK_WEAK)
  41. exit(generic_error("trying to modify weak variable", psave, lno));
  42. if (n->xmask & 4)
  43. exit(generic_error("variable sets to be read-only", psave, lno));
  44. n->value = getnum(&p, PRIO_MAX);
  45. return n->value;
  46. }
  47. /* algorithm copied from os in a month (write an os in 30 days), kawai */
  48. __arch_templ static long getnum(char **pp, int prio) {
  49. struct klist_node *n;
  50. int type, out;
  51. char *p = *pp, *var;
  52. long a = 0, b = 0;
  53. /* a + b, first we read a by ourselves.
  54. * and, okay, we cannot use getword here because... */
  55. switch (*p) {
  56. case '+':
  57. case '-':
  58. /* +123 */
  59. getword(p, &type, (int *) &a);
  60. _skip_word(p);
  61. break;
  62. case '(':
  63. p++;
  64. _skip_spaces(p);
  65. a = getnum(&p, PRIO_MAX);
  66. if (*p != ')')
  67. exit(generic_error("no ')' found", psave, lno));
  68. break;
  69. default:
  70. if (isnr(*p))
  71. getword(p, &type, (int *) &a);
  72. else if (ischr(*p)) {
  73. getword(p, &type, &out);
  74. var = strndup(p, out);
  75. n = find_name(var);
  76. if (!n) {
  77. error(var);
  78. exit(generic_error("name not found", psave, lno));
  79. }
  80. if (n->xmask & XMASK_WEAK)
  81. exit(generic_error("trying to modify weak variable", psave, lno));
  82. a = n->value;
  83. free(var);
  84. }
  85. else
  86. exit(generic_error("invalid unary operator before number", psave, lno));
  87. _skip_word(p);
  88. break;
  89. }
  90. for (;;) {
  91. switch (*p) {
  92. case '+':
  93. if (prio <= 2)
  94. goto fin;
  95. p++;
  96. _skip_spaces(p);
  97. b = getnum(&p, 2);
  98. a += b;
  99. break;
  100. case '-':
  101. if (prio <= 2)
  102. goto fin;
  103. p++;
  104. _skip_spaces(p);
  105. b = getnum(&p, 2);
  106. a -= b;
  107. break;
  108. case '*':
  109. if (prio <= 1)
  110. goto fin;
  111. p++;
  112. _skip_spaces(p);
  113. b = getnum(&p, 1);
  114. a *= b;
  115. break;
  116. case '/':
  117. if (prio <= 1)
  118. goto fin;
  119. p++;
  120. _skip_spaces(p);
  121. b = getnum(&p, 1);
  122. a /= b;
  123. break;
  124. default:
  125. goto fin;
  126. }
  127. }
  128. fin: _skip_spaces(p);
  129. *pp = p;
  130. return a;
  131. }