init.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "hoc.h"
  4. #include "y.tab.h"
  5. static struct { /* Keywords */
  6. char *name;
  7. int kval;
  8. } keywords[] = {
  9. "proc", PROC,
  10. "func", FUNC,
  11. "return", RETURN,
  12. "if", IF,
  13. "else", ELSE,
  14. "while", WHILE,
  15. "for", FOR,
  16. "print", PRINT,
  17. "read", READ,
  18. 0, 0,
  19. };
  20. static struct { /* Constants */
  21. char *name;
  22. double cval;
  23. } consts[] = {
  24. "PI", 3.14159265358979323846,
  25. "E", 2.71828182845904523536,
  26. "GAMMA", 0.57721566490153286060, /* Euler */
  27. "DEG", 57.29577951308232087680, /* deg/radian */
  28. "PHI", 1.61803398874989484820, /* golden ratio */
  29. 0, 0
  30. };
  31. static struct { /* Built-ins */
  32. char *name;
  33. double (*func)(double);
  34. } builtins[] = {
  35. "sin", sin,
  36. "cos", cos,
  37. "tan", tan,
  38. "atan", atan,
  39. "asin", Asin, /* checks range */
  40. "acos", Acos, /* checks range */
  41. "sinh", Sinh, /* checks range */
  42. "cosh", Cosh, /* checks range */
  43. "tanh", tanh,
  44. "log", Log, /* checks range */
  45. "log10", Log10, /* checks range */
  46. "exp", Exp, /* checks range */
  47. "sqrt", Sqrt, /* checks range */
  48. "int", integer,
  49. "abs", fabs,
  50. 0, 0
  51. };
  52. void
  53. init(void) /* install constants and built-ins in table */
  54. {
  55. int i;
  56. Symbol *s;
  57. for (i = 0; keywords[i].name; i++)
  58. install(keywords[i].name, keywords[i].kval, 0.0);
  59. for (i = 0; consts[i].name; i++)
  60. install(consts[i].name, VAR, consts[i].cval);
  61. for (i = 0; builtins[i].name; i++) {
  62. s = install(builtins[i].name, BLTIN, 0.0);
  63. s->u.ptr = builtins[i].func;
  64. }
  65. }