-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
96 lines (86 loc) · 1.81 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdbool.h>
#include <math.h>
#include "headers/symbol.h"
#include "headers/lexer.h"
// External math functions mapped to the builtins
extern double degtorad(), radtodeg(), Sqrt(), nlog(), Log10(), Rand();
static struct {
char *name;
double cval;
} consts[] = { // Constants
{"PI", 3.14159265358979323846},
{"E", 2.71828182845904523536},
{"GAMMA", 0.57721566490153286060},
{"PHI", 1.61803398874989484820},
{"true", 1},
{"false", 0},
{0, 0}
};
static struct {
char *name;
char *sval;
} sconsts[] = {
{"NL", "\n"},
{0, 0}
};
static struct {
char *name;
int kval;
} keywords[] = {
{"AND", AND},
{"OR", OR},
{"if", IF},
{"else", ELSE},
{"while", WHILE},
{"break", BREAK},
{"continue", CONTINUE},
{"print", PRINT},
{"println", PRINTLN},
{"func", FUNC},
{"return", RETRN},
{0, 0}
};
static struct {
char *name;
double (*func)();
} builtins[] = {
{"sin", sin},
{"asin", asin},
{"cos", cos},
{"acos", acos},
{"tan", tan},
{"atan", atan},
{"ln", nlog}, // > 0
{"log", Log10}, // > 0
{"sqrt", Sqrt}, // >= 0
{"abs", fabs},
{"ceil", ceil},
{"floor", floor},
{"degtorad", degtorad},
{"radtodeg", radtodeg},
{"round", round},
{0, 0}
};
void init() {
// Installs constants into the symbol table
for (int i=0; consts[i].name; i++) {
install(consts[i].name, VAR, consts[i].cval, true);
}
// Install string constants into table
Symbol *sym;
for (int i=0; sconsts[i].name; i++) {
sym = install(sconsts[i].name, VAR, 0, true);
sym->str = sconsts[i].sval;
sym->varType = STRING;
}
// Install builtins into the symbol table
Symbol *s;
for (int i=0; builtins[i].name; i++) {
s = install(builtins[i].name, BLTIN, 0, true);
s->ptr = builtins[i].func;
}
// Install keywords into symbols
for (int i=0; keywords[i].name; i++) {
install(keywords[i].name, keywords[i].kval, 0, true);
}
}