init.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include "hoc.h"
  12. #include "y.tab.h"
  13. static struct { /* Keywords */
  14. char *name;
  15. int kval;
  16. } keywords[] = {
  17. "proc", PROC,
  18. "func", FUNC,
  19. "return", RETURN,
  20. "if", IF,
  21. "else", ELSE,
  22. "while", WHILE,
  23. "for", FOR,
  24. "print", PRINT,
  25. "read", READ,
  26. 0, 0,
  27. };
  28. static struct { /* Constants */
  29. char *name;
  30. double cval;
  31. } consts[] = {
  32. "PI", 3.14159265358979323846,
  33. "E", 2.71828182845904523536,
  34. "GAMMA", 0.57721566490153286060, /* Euler */
  35. "DEG", 57.29577951308232087680, /* deg/radian */
  36. "PHI", 1.61803398874989484820, /* golden ratio */
  37. 0, 0
  38. };
  39. static struct { /* Built-ins */
  40. char *name;
  41. double (*func)(double);
  42. } builtins[] = {
  43. "sin", sin,
  44. "cos", cos,
  45. "tan", tan,
  46. "atan", atan,
  47. "asin", Asin, /* checks range */
  48. "acos", Acos, /* checks range */
  49. "sinh", Sinh, /* checks range */
  50. "cosh", Cosh, /* checks range */
  51. "tanh", tanh,
  52. "log", Log, /* checks range */
  53. "log10", Log10, /* checks range */
  54. "exp", Exp, /* checks range */
  55. "sqrt", Sqrt, /* checks range */
  56. "int", integer,
  57. "abs", fabs,
  58. 0, 0
  59. };
  60. void
  61. init(void) /* install constants and built-ins in table */
  62. {
  63. int i;
  64. Symbol *s;
  65. for (i = 0; keywords[i].name; i++)
  66. install(keywords[i].name, keywords[i].kval, 0.0);
  67. for (i = 0; consts[i].name; i++)
  68. install(consts[i].name, VAR, consts[i].cval);
  69. for (i = 0; builtins[i].name; i++) {
  70. s = install(builtins[i].name, BLTIN, 0.0);
  71. s->u.ptr = builtins[i].func;
  72. }
  73. }