-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticAnalyse.cpp
51 lines (42 loc) · 1.07 KB
/
SemanticAnalyse.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
//
// Created by IceCapriccio on 2018/11/30.
//
#include <iostream>
#include <cstdio>
#include "SemanticAnalyse.h"
#include "IntermediateCodeGenerate.h"
AbstractSyntaxTreeNode::AbstractSyntaxTreeNode() {
info.type = None;
childNum = 0;
for (auto &i : child)
i = nullptr;
}
Symbol::Symbol(string name, int depth, int offset):depth(depth), offset(offset) {
this->name = name;
}
void Symbol::show() {
printf("%5s %5d\n", name.c_str(), offset);
}
void SymbolTable::del(int depth) {
for (int i = table.size() - 1; i >= 0; i--) {
if (table[i].depth == depth)
table.erase(table.begin() + i);
}
}
int SymbolTable::query(string name) {
for (auto it = table.rbegin(); it != table.rend(); it++) {
if ((*it).name == name)
return (*it).offset;
}
return -1;
}
void SymbolTable::insert(string name,int depth) {
Symbol symbol(name,depth, table.size() * 4+10000);
table.push_back(symbol);
}
void SymbolTable::show() {
printf("%5s %s\n", "add", "val");
for (auto &i : table) {
i.show();
}
}