-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateTool.h
85 lines (72 loc) · 1.64 KB
/
calculateTool.h
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
extern int yylineno;
void yyerror(char* s, ...);
struct symbol {
char* name;
double value;
struct ast* func;
struct symlist* syms;
};
#define NHASH 9997
struct symbol symtab[NHASH];
struct symbol* lookup(char*);
struct symlist {
struct symbol* sym;
struct symlist* next;
};
struct symlist* newsymlist(struct symbol* sym, struct symlist* next);
void symlistfree(struct symlist* sl);
// build-in functions
enum bifs {
B_sqrt = 1,
B_exp,
B_log,
B_print
};
struct ast {
int nodetype;
struct ast* l;
struct ast* r;
};
// Q: why they only have left child node
// build-in function
struct fncall {
int nodetype;
struct ast* l;
enum bifs functype;
};
struct ufncall {
int nodetype;
struct ast* l;
struct symbol* s;
};
struct flow {
int nodetype;
struct ast* cond;
struct ast* tl;
struct ast* el;
};
struct numval {
int nodetype;
double number;
};
struct symref {
int nodetype;
struct symbol* s;
};
struct symasgn {
int nodetype;
struct symbol* s;
struct ast* v;
};
struct ast *newast(int nodetype, struct ast* l, struct ast* r);
struct ast* newcmp(int cmptype, struct ast* l, struct ast* r);
struct ast* newfunc(int functype, struct ast* l);
struct ast* newcall(struct symbol* s, struct ast* l);
struct ast* newref(struct symbol* s);
struct ast* newasgn(struct symbol* s, struct ast* v);
struct ast* newnum(double d);
struct ast* newflow(int nodetype, struct ast* cond, struct ast* tl, struct ast* tr);
void dodef(struct symbol* name, struct symlist* syms, struct ast* stmts);
// calculate the ast
double eval(struct ast*);
void treefree(struct ast*);