xref: /plan9-contrib/sys/src/cmd/hoc/init.c (revision 3e12c5d1bb89fc02707907988834ef147769ddaf)
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 	"gamma", Gamma,	/* checks range */
52 	"int",	integer,
53 	"abs",	fabs,
54 	"erf",	erf,
55 	"erfc",	erfc,
56 	0,	0
57 };
58 
59 void
60 init(void)	/* install constants and built-ins in table */
61 {
62 	int i;
63 	Symbol *s;
64 	for (i = 0; keywords[i].name; i++)
65 		install(keywords[i].name, keywords[i].kval, 0.0);
66 	for (i = 0; consts[i].name; i++)
67 		install(consts[i].name, VAR, consts[i].cval);
68 	for (i = 0; builtins[i].name; i++) {
69 		s = install(builtins[i].name, BLTIN, 0.0);
70 		s->u.ptr = builtins[i].func;
71 	}
72 }
73