-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsymtab.h
125 lines (97 loc) · 3.24 KB
/
symtab.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
/* maximum size of hash table */
#define SIZE 211
/* maximum size of tokens-identifiers */
#define MAXTOKENLEN 40
/* how parameter is passed */
#define BY_VALUE 1
#define BY_REFER 2
/* Types of values that we can have */
typedef union Value{
int ival;
double fval;
char cval;
char *sval;
}Value;
/* parameter struct */
typedef struct Param{
// parameter type and name
int par_type;
char param_name[MAXTOKENLEN];
// to store the value
Value val;
int passing; // value or reference
}Param;
/* a linked list of references (lineno's) for each variable */
typedef struct RefList{
int lineno;
struct RefList *next;
}RefList;
// struct that represents a list node
typedef struct list_t{
// name, size of name, scope and occurrences (lines)
char st_name[MAXTOKENLEN];
int st_size;
int scope;
RefList *lines;
// to store value
Value val;
// type
int st_type;
// for arrays (info type), for pointers (pointing type)
// and for functions (return type)
int inf_type;
// array stuff
Value *vals;
int array_size;
// function parameters
Param *parameters;
int num_of_pars;
// pointer to next item in the list
struct list_t *next;
}list_t;
/* Queue of identifiers to revisit */
typedef struct revisit_queue{
// symbol table entry
list_t *entry;
// name of identifier
char *st_name;
// type of revisit
int revisit_type;
// parameters of function calls
int **par_types;
int *num_of_pars;
int num_of_calls;
// assignment expression nodes
void **nodes;
int num_of_assigns;
// maybe additional information to simplify the process ...
struct revisit_queue *next;
}revisit_queue;
/* revisit types */
#define PARAM_CHECK 1 /* Check parameters of function call when functions gets declared */
#define ASSIGN_CHECK 2 /* Check assignment when function call part of the expression */
/* static structures */
static list_t **hash_table;
static revisit_queue *queue;
// Symbol Table Functions
void init_hash_table(); // initialize hash table
unsigned int hash(char *key); // hash function
void insert(char *name, int len, int type, int lineno); // insert entry
list_t *lookup(char *name); // search for entry
void symtab_dump(FILE *of); // dump file
// Type Functions
void set_type(char *name, int st_type, int inf_type); // set the type of an entry (declaration)
int get_type(char *name); // get the type of an entry
// Scope Management Functions
void hide_scope(); // hide the current scope
void incr_scope(); // go to next scope
// Function Declaration and Parameters
Param def_param(int par_type, char *param_name, int passing); // define parameter
int func_declare(char *name, int ret_type, int num_of_pars, Param *parameters); // declare function
int func_param_check(char *name, int num_of_calls, int** par_types, int *num_of_pars); // check parameters
// Revisit Queue Functions
void add_to_queue(list_t *entry, char *name, int type); // add to queue
revisit_queue *search_queue(char *name); // search queue
revisit_queue *search_prev_queue(char *name); // search previous of element
int revisit(char *name); // revisit entry by also removing it from queue
void revisit_dump(FILE *of); // dump file