-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuf_header.h
133 lines (119 loc) · 2.98 KB
/
buf_header.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Declarations for a calculator fb3-1
*/
/* interface to the lexer */
extern int yylineno; /* from lexer */
void yyerror(char *s, ...);
/* 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 */
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, bit coded 04 equal, 02 less, 01 greater
* M unary minus
* L expression or 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 */
B_sqrt = 1,
B_exp,
B_log,
B_print,
B_sin,
B_cos,
B_tan,
B_asin,
B_acos,
B_atan,
B_rand
};
enum bifs2 {
B_pow = 1,
B_atan2,
};
struct fncall2 {
int nodetype;
struct ast* l;
struct ast *r;
enum bifs2 functype;
};
/* 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; //exp or explist
//struct ast *r;
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 branch or do list */
struct ast *el; /* optional else branch */
};
struct numval {
int nodetype; /* type K */
double number;
};
struct ast_string {
int nodetype;
char *value;
};
struct symref { /*references to sumbols*/
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 *newfunc2(int functype, struct ast *l, struct ast *r);
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);
struct ast *newstring(const char *value);
/* define a function */
void dodef(struct symbol *name, struct symlist *syms, struct ast *stmts);
/* evaluate an AST */
double eval(struct ast *);
/* delete and free an AST */
void treefree(struct ast *);
/* interface to the lexer */
extern int yylineno; /* from lexer */
void yyerror(char *s, ...);