-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.h
115 lines (96 loc) · 2.51 KB
/
funcs.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
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
/* symbol table */
struct symbol { /* a variable name */
char *name;
double value;
struct ast *func; /* stmt for the function */
struct symlist *syms; /* list of dummy args */
};
/* simple symtab of fixed size */
#define NHASH 9997
struct symbol symtab[NHASH];
struct symbol *lookup(char*);
/* list of symbols, for an argument list */
struct symlist {
struct symbol *sym;
struct symlist *next;
};
struct symlist *newsymlist(struct symbol *sym, struct symlist *next);
void symlistfree(struct symlist *sl);
/* node types
* + - * / |
* 0-7 comparison ops
* M unary minus (negative)
* L statement list
* I IF statement
* W WHILE statement
* N symbol ref
* = assignment
* S list of symbols
* F built in function call
* C user function call
*/
enum bifs { /* built-in functions type */
B_sqrt = 1,
B_exp,
B_log,
B_sin,
B_cos,
B_print
};
/* nodes in the Abstract Syntax Tree */
/* all have common initial nodetype */
struct ast {
int nodetype;
struct ast *l;
struct ast *r;
};
struct fncall { /* built-in function */
int nodetype; /* type F */
struct ast *l;
enum bifs functype;
};
struct ufncall { /* user function */
int nodetype; /* type C */
struct ast *l; /* list of arguments */
struct symbol *s;
};
struct flow {
int nodetype; /* type I or W */
struct ast *cond; /* condition */
struct ast *tl; /* then or do list */
struct ast *el; /* optional else list */
};
struct numval {
int nodetype; /* type K */
double number;
};
struct symref {
int nodetype; /* type N */
struct symbol *s;
};
struct symasgn {
int nodetype; /* type = */
struct symbol *s;
struct ast *v; /* value */
};
// build an AST
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);
// define a function
void dodef(struct symbol *name, struct symlist *syms, struct ast *stmts);
// call to built-in or user-defined function
static double callbuiltin(struct fncall*);
static double calluser(struct ufncall*);
// evaluate an AST, return the result of calculation
double eval(struct ast *);
// delete and free an AST recursively
void treefree(struct ast *);
// interface to the lexer
void yyerror(char *s, ...);
int yywrap();