forked from mbbill/flexbison
-
Notifications
You must be signed in to change notification settings - Fork 10
/
fb3-2.h
124 lines (102 loc) · 2.83 KB
/
fb3-2.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
/* Companion source code for "flex & bison", published by O'Reilly
* Media, ISBN 978-0-596-15597-1
* Copyright (c) 2009, Taughannock Networks. All rights reserved.
* See the README file for license conditions and contact info.
* $Header: /home/johnl/flnb/code/RCS/fb3-2.h,v 2.1 2009/11/08 02:53:18 johnl Exp $
*/
/*
* Declarations for a calculator fb3-1
*/
/* 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, bit coded 04 equal, 02 less, 01 greater
* M unary minus
* 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 */
B_sqrt = 1,
B_exp,
B_log,
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);
/* 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, ...);
extern int debug;
void dumpast(struct ast *a, int level);