-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.c
95 lines (92 loc) · 2.14 KB
/
symbol.c
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
#include <stdlib.h>
#include <string.h>
#include "symbol.h"
/* SHead is the head of symbol table
* TODO new structure */
SNode* SHead = NULL;
SNode* FunDeclarHead = NULL;
/* executable code cannot occur outside of a function */
Type TypeInt = {.kind = BASIC, .u.basic = INT};
Type TypeFloat = {.kind = BASIC, .u.basic = FLOAT};
Type* TypeNodeInt = &TypeInt;
Type* TypeNodeFloat = &TypeFloat;
SNode* stInitNode(char* name){
SNode* p = (SNode*)malloc(sizeof(SNode));
if(p == NULL)return NULL;
strcpy(p->name, name);
p->visitedTag = UNCLEAR;
p->lineno = 0;
p->next = NULL;
p->fnext = NULL;
p -> op_var_no = 0;
p -> isParam = 0;
return p;
}
/* consider only shown once */
/* this data structure has to updated. */
SNode* stFind(char* name){
SNode* p = SHead;
while(p != NULL){
if(strcmp(name, p->name) == 0)break;
p = p->next;
}
return p;
}
/* according to stFind's return */
void stInsert(SNode* p){
if(SHead == NULL){
SHead = p;
}
else {
SNode* temp = SHead;
SHead = p;
p->next = temp;
}
}
/* no use of stDelete currently */
void stPrint(){
SNode* p = SHead;
int i = 0;
printf("Symbol table:\n");
while(p != NULL){
printf(" No %d: %s\t%d\n", i, p->name, p->visitedTag);
if(p -> visitedTag == VARIABLE){
Type* a = p -> Message.var;
printf(" kind no: %d\n", a -> kind);
if(a -> kind == ARRAY){
printf(" size = %d\n", a -> u.array.size);
}
}
else if(p -> visitedTag == FUNC){
printf(" returnType kind no: %d\n", p ->Message.func -> returnType -> kind);
}
else if(p -> visitedTag == STRUCTDEF){
printf(" fieldlist:\n");
FieldList* head = p -> Message.var -> u.structure;
while(head != NULL){
printf(" name: %s\tkind no: %d\n", head -> name, head -> type ->kind);
head = head -> tail;
}
}
p = p->next;
++ i;
}
printf("Function declar:\n");
p = FunDeclarHead;
i = 0;
while(p != NULL){
printf(" No %d: %s\tLine: %d\n", i, p -> name, p -> lineno);
p = p -> next;
i ++;
}
}
Type* sfFind(Type* p, char* name){
FieldList* head = p -> u.structure;
while(head != NULL){
if(strcmp(name, head -> name) == 0){
return head -> type;
}
head = head -> tail;
}
return NULL;
}