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