-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableManager.cpp
91 lines (81 loc) · 3.26 KB
/
TableManager.cpp
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
//
// Created by hzt on 2023/10/12.
//
/**
*
* 遵循单一职责原则:
* 符号表管理类 毕竟叫管理了 所以也要有对符号表的操作
* 提供一整套Entry树形结构
* 由Semantic进行调用其中的函数操作 :
* 1.向下一层扩充指定子符号表 ident
* 2.向上一层回到父符号表
* 3.检查当前符号表是否已经存在ident名字
* 4.检查是否存在已经定义的对应符号 包括ident 和type ??
* 5.将若干表项插入当前父符号表 vector<Entry*> arguments
*/
#include <iostream>
#include "TableManager.h"
#include "Semantic.h"//怎么让符号表管理器和语义分析共同包含各自 只能后续赋值?
TableManager::TableManager(ErrorHandler & errorHandler1) :errorHandler(errorHandler1){//?
info = nullptr;//fill会初始化
cur = new Entry;
//initialize cur info can be initialized in semantic's fill_
cur->entries = new map<string , Entry*>;
cur->ident = "CompUnit";
cur->loop_count = 0;
cur->fParams = nullptr;
cur->kind = CompUnit;
cur->Father_Entry = nullptr;
cur->return_error = false;
}
bool TableManager::isRedefine(const string &ident) const{//can be made const????
//如果是函数 局部变量区entries和参数区cur->arguments
//
if(cur->entries->find(ident) == cur->entries->end()){
return false;
}
return true;
}
void TableManager::upTable() { //cur 应该作为一个pointer 可以省去重载= 同时方便使用
cur = cur->Father_Entry;
}
void TableManager::insertIntoTable(vector<Entry *> & entries) const { //插入当前表 已经find过 未定义的
if (cur->entries == nullptr){
cur->entries = new map<string,Entry*>;
}
for (auto & entry : entries) {
if(isRedefine(entry->ident)){
errorHandler.Insert_Error(REDEFINE);
}else{
cur->entries->insert(pair<string,Entry*>(entry->ident, entry));
}
}
}
//其实到这里不用参数也可以
//为什么一定需要有一个downTable函数呢 也就是为什么需要记录一个块里面的符号 是因为这个块我门不确定是否还会有内嵌块可能会用到这个块的变量 所以需要记录
void TableManager::downTable(string& ident) { //进入以标识符唯一标识的符号表 似乎只有函数可能会用到 预先定义的
if(cur->entries->find(ident) != cur->entries->end()){
cur = cur->entries->at(ident);
}
}
bool TableManager::isEverDefine(const string& ident,Kind kind) {
Entry * temp = cur;
Entry * e;
while(temp != nullptr){//设置顶层entry的父节点为空
if(temp->entries->find(ident) != temp->entries->end() ){//对于函数 形参放进了entries中 设置完参数后会将设置cur的函数的参数类型设置
e = temp->entries->at(ident);
if (e->kind == kind
|| kind == FUNC_INT && e->kind == FUNC_VOID){ //函数类型返回值的错误 先认为都是函数即可
return true;
}
}
temp = temp->Father_Entry;
}
return false;
}
void TableManager::insertIntoTable(Entry *entry) const {
if (cur->entries == nullptr){
cur->entries = new map<string,Entry*>;
}
cur->entries->insert(pair<string,Entry*>(entry->ident,entry));
}