-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySymbolTable.h
83 lines (75 loc) · 2.07 KB
/
mySymbolTable.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
#ifndef __MY_SYMBOL_TABLE_H
#define __MY_SYMBOL_TABLE_H
#include "compiler_hw_common.h"
#include "myType.h"
struct symbol_node{
char* name;
unsigned char type;
int lineno;
int addr;
};
struct func_node{
char* name;
unsigned char type;
int lineno;
char *func_sig;
};
struct symbol_node symbol_table[1000];
int scope_level=-1;//next is 0
int scope_level_stack_init[1000]={0};
int stack_back=0;
int next_addr=0;
struct func_node func_table[1000];
int func_back=0;
int symbol_find(char* id){
for(int i=stack_back-1;i>=0;--i){
if(strcmp(id,symbol_table[i].name)==0)
return i;
}
return -1;
}
unsigned char ident_type(char* id){
int i=symbol_find(id);
if(i==-1)return type_error;
return symbol_table[i].type;
}
int func_find(char* id){
for(int i=func_back-1;i>=0;--i){
if(strcmp(id,func_table[i].name)==0)
return i;
}
return -1;
}
int symbol_find_in_block(char* id){
for(int i=stack_back-1;i>=scope_level_stack_init[scope_level];--i){
if(strcmp(id,symbol_table[i].name)==0)
return i;
}
return -1;
}
void create_symbol() {
scope_level_stack_init[++scope_level]=stack_back;
}
void dump_symbol() {
stack_back=scope_level_stack_init[scope_level--];
}
void insert_symbol(char * ident, unsigned char data_type) {
int prev=symbol_find_in_block(ident);
if(prev!=-1){
printf("error:%d: %s redeclared in this block. previous declaration at line %d\n",yylineno,ident,symbol_table[prev].lineno);
g_has_error=true;
}
symbol_table[stack_back].lineno=yylineno;
symbol_table[stack_back].type=data_type;
symbol_table[stack_back].name=ident;
symbol_table[stack_back].addr=next_addr++;
stack_back++;
}
void insert_func(char * ident, char* sig, unsigned char ret_type) {
func_table[func_back].type=ret_type;
func_table[func_back].name=ident;
func_table[func_back].func_sig=sig;
func_table[func_back].lineno=yylineno;
func_back++;
}
#endif