-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpc_symtable.h
110 lines (88 loc) · 2.81 KB
/
pc_symtable.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
/*
* KEDJI Komlan Akpédjé <eric.kedji@gmail.com>
* http://erickedji.wordpress.com/
*
* Définitions de la table de symbole du compilateur PseudoC
*/
#ifndef _SYMBOL_TABLE_H_
#define _SYMBOL_TABLE_H_
#include "shared.h"
/* must be defined before including pc_parser.tab.h because
* the union used by bison contains a symbol*
*/
typedef struct _symbol symbol;
#include "pcc_defs.h"
#include "pc_parser.tab.h"
typedef enum { FUNCTION, VARIABLE, PARAMETER } symbol_type_t;
typedef enum { VOID_TYPE, INT_TYPE, ARRAY_TYPE } data_type_t;
typedef enum { FUNCTION_LEVEL, FILE_LEVEL, GLOBAL_LEVEL} visibility_level_t;
#ifdef DEFINE_DATA_AND_SYMBOL_TYPES_NAMES
/* must be in sync with data_type_t */
char *data_type_names[3] = {
"VOID","INT","ARRAY"
};
/* must be in sync with symbol_type_t */
char *symbol_type_names[3] = {
"FUNCTION","VARIABLE","PARAMETER"
};
#endif
typedef struct {
int arity;
data_type_t return_type;
int defined;
symbol *plist;
} function_sym_t;
typedef struct {
data_type_t data_type;
int size;
} variable_sym_t;
typedef struct {
data_type_t data_type;
int size; /* number of elements for arrays */
symbol *next; /* pointer to next param in function*/
} parameter_sym_t;
struct _symbol{
char name[MAX_STR];
symbol_type_t type;
YYLTYPE loc;
union {
variable_sym_t var;
function_sym_t func;
parameter_sym_t param;
} u;
UT_hash_handle hh; /* makes this structure hashable, see uthash.h */
};
typedef symbol *symbol_table_t;
symbol* declare_symbol(
symbol_type_t stype,
char *name,
YYLTYPE loc,
visibility_level_t vl,
data_type_t data_type,
int size);
void declare_function(
char *name,
YYLTYPE loc,
data_type_t return_type,
symbol* param_list);
symbol * define_function(
char *name,
YYLTYPE loc,
data_type_t return_type,
symbol* param_list);
symbol * add_param(
char *name,
YYLTYPE loc,
data_type_t dtype,
int size,
symbol *param_list);
void add_symbol(visibility_level_t vl, symbol *sym);
int check_duplicate(visibility_level_t vl, symbol *sym);
symbol* find_symbol(visibility_level_t vl, char *name);
void delete_symbol(visibility_level_t vl, symbol *sym);
void print_symbol_table(visibility_level_t vl);
void sort_symbol_table(visibility_level_t vl);
int level(char *name, data_type_t type, YYLTYPE loc);
//void free_symbol_table(visibility_level_t vl);
extern symbol_table_t symbol_tables[3];
#endif /* _SYMBOL_TABLE_H_ */