-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabsym.h
86 lines (54 loc) · 1.77 KB
/
tabsym.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
//
// Created by Adam Zvada on 17.06.17.
//
#ifndef TESTLLVM_TABSYM_H
#define TESTLLVM_TABSYM_H
#include <stdio.h>
#include <iostream>
#include <map>
#include <vector>
#include "llvm/IR/Value.h"
using namespace std;
/**
* Symbole table for declared identifiers (variable or constants).
* We accept just integer values.
* Store in linked list(O(n)) because gonna be used for small programes and init of maps etc would be costly.
*
*/
enum SymboleType {
CONST, VAR, ARRAY, FUNC, UNDEF
};
struct Symbole {
string ident;
SymboleType type;
int start;
int end;
int value;
Symbole(string ident, SymboleType type, int value) : ident(ident), type(type), value(value), start(0), end(0) {}
Symbole(string ident, SymboleType type, int start, int end) : ident(ident), type(type), value(0), start(start), end(end) {}
};
class SymboleTable {
public:
SymboleTable() : _root(nullptr), _isLocalScope(false) {};
~SymboleTable();
vector<Symbole *> GetAllGlobalVar() const;
vector<Symbole *> GetAllLocalVar() const;
void DeclareConst(string ident, int val);
void DeclareVar(string ident);
void DeclareVarArr(string ident, int start, int end);
void DeclareFunc(const string & name);
int GetValue(string & ident) const;
Symbole * FindIdent(string & ident) const;
SymboleType GetConstOrVar(string & ident, int * value);
void Error(string & ident, string errorWhat) const { cout << "Identifier " << ident << " " << errorWhat << endl; }
void EnterLocalScope();
void LeaveLocalScope();
private:
Symbole * _root;
bool _isLocalScope;
map<string, Symbole *> _globalTable;
map<string, Symbole *> _localTable;
//TODO FUNC
map<string, llvm::Value *> _funcTable;
};
#endif //TESTLLVM_TABSYM_H