-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.c
124 lines (109 loc) · 3.55 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <math.h>
#include "hoc.h"
#include "y.tab.h"
extern double Log(double), Log10(double), Exp(double), Sqrt(double),
Integer(double);
static struct {
const char* name;
double cval;
} consts[]
= {{"PI", 3.14159265358979323846}, {"E", 2.71828182845904523536},
{"GAMMA", 0.57721566490153286060}, {"DEG", 57.29577951308232087680},
{"PHI", 1.61803398874989484820}, {NULL, 0}};
static struct {
const char* name;
double (*func)(double);
} builtins[] = {{"sin", sin}, {"cos", cos},
{"atan", atan}, {"log", Log},
{"log10", Log10}, {"exp", Exp},
{"sqrt", Sqrt}, {"int", Integer},
{"abs", fabs}, {"atan2", (double (*)(double))atan2},
{NULL, 0}};
static struct {
const char* name;
int kval;
} keywords[] = {{"proc", PROC}, {"func", FUNC}, {"return", RETURN},
{"if", IF}, {"else", ELSE}, {"while", WHILE},
{"print", PRINT}, {"read", READ}, {NULL, 0}};
typedef struct DebugSymbol {
const char* name;
void* addr;
struct DebugSymbol* next;
} DebugSymbol;
static DebugSymbol* sDebugSymList = NULL;
static void debugInstallSymbol(const char* name, void* addr)
{
DebugSymbol* p = (DebugSymbol*)emalloc(sizeof(DebugSymbol));
p->name = name;
p->addr = addr;
p->next = sDebugSymList;
sDebugSymList = p;
}
void debugInitSymbolTable()
{
debugInstallSymbol("eval", eval);
debugInstallSymbol("add", add);
debugInstallSymbol("sub", sub);
debugInstallSymbol("mul", mul);
debugInstallSymbol("div", hocDiv);
debugInstallSymbol("power", power);
debugInstallSymbol("negate", negate);
debugInstallSymbol("assign", assign);
debugInstallSymbol("execBltin", bltin);
debugInstallSymbol("varpush", varpush);
debugInstallSymbol("constpush", constpush);
debugInstallSymbol("varread", varread);
debugInstallSymbol("pop", pop);
debugInstallSymbol("popPrint", print);
debugInstallSymbol("printexpr", printexpr);
debugInstallSymbol("printstr", printstr);
debugInstallSymbol("gt", gt);
debugInstallSymbol("lt", lt);
debugInstallSymbol("eq", eq);
debugInstallSymbol("ge", ge);
debugInstallSymbol("le", le);
debugInstallSymbol("ne", ne);
debugInstallSymbol("and", hocAnd);
debugInstallSymbol("or", hocOr);
debugInstallSymbol("not", hocNot);
debugInstallSymbol("ifcode", ifcode);
debugInstallSymbol("whilecode", whilecode);
debugInstallSymbol("call", call);
debugInstallSymbol("argpush", arg);
debugInstallSymbol("argassign", argassign);
debugInstallSymbol("funcret", funcret);
debugInstallSymbol("procret", procret);
int i;
for (i = 0; builtins[i].name != NULL; i++) {
debugInstallSymbol(builtins[i].name, builtins[i].func);
}
}
const char* debugLookupBuiltinFuncName(void* addr)
{
DebugSymbol* p = sDebugSymList;
while (p != NULL) {
if (p->addr == addr) {
return p->name;
}
p = p->next;
}
return NULL;
}
/**
* install constants and built-ins in table
*/
void init()
{
int i;
for (i = 0; consts[i].name != NULL; i++) {
install(consts[i].name, CONST, consts[i].cval);
}
for (i = 0; builtins[i].name != NULL; i++) {
Symbol* s = install(builtins[i].name, BLTIN, 0.0);
s->u.ptr = builtins[i].func;
}
for (i = 0; keywords[i].name != NULL; i++) {
install(keywords[i].name, keywords[i].kval, 0.0);
}
debugInitSymbolTable();
}