-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScope.cpp
More file actions
140 lines (130 loc) · 5.33 KB
/
Scope.cpp
File metadata and controls
140 lines (130 loc) · 5.33 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "./Lexer.hpp"
#include "./Scope.hpp"
Symbols::Symbols() : current(new Scope(std::map<std::string, token>(), nullptr))
{
global = current;
token newTokenGetBool(IDENTIFIER, tokenMk("getbool"), "getbool", BOOLEAN_RW);
global->symbol_table["getbool"] = newTokenGetBool;
token newTokenGetInteger(IDENTIFIER, tokenMk("getinteger"), "getinteger", INTEGER_RW);
global->symbol_table["getinteger"] = newTokenGetInteger;
token newTokenGetFloat(IDENTIFIER, tokenMk("getfloat"), "getfloat", FLOAT_RW);
global->symbol_table["getfloat"] = newTokenGetFloat;
token newTokenGetString(IDENTIFIER, tokenMk("getstring"), "getstring", STRING_RW);
global->symbol_table["getstring"] = newTokenGetString;
token newTokenPutBool(IDENTIFIER, tokenMk("putbool"), "putbool", BOOLEAN_RW, -1, {token(IDENTIFIER, tokenMk("internal_bool"), "1050", BOOLEAN_RW)});
global->symbol_table["putbool"] = newTokenPutBool;
token newTokenPutInteger(IDENTIFIER, tokenMk("putinteger"), "putinteger", BOOLEAN_RW, -1, {token(IDENTIFIER, tokenMk("internal_int"), "1051", INTEGER_RW)});
global->symbol_table["putinteger"] = newTokenPutInteger;
token newTokenPutFloat(IDENTIFIER, tokenMk("putfloat"), "putfloat", BOOLEAN_RW, -1, {token(IDENTIFIER, tokenMk("internal_float"), "1052", FLOAT_RW)});
global->symbol_table["putfloat"] = newTokenPutFloat;
token newTokenPutString(IDENTIFIER, tokenMk("putstring"), "putstring", BOOLEAN_RW, -1, {token(IDENTIFIER, tokenMk("internal_double"), "1053", STRING_RW)});
global->symbol_table["putstring"] = newTokenPutString;
token newTokenSqrt(IDENTIFIER, tokenMk("sqrt"), "sqrt", FLOAT_RW, -1, {token(IDENTIFIER, tokenMk("internal_sqrt"), "1054", INTEGER_RW)});
global->symbol_table["sqrt"] = newTokenSqrt;
}
void Symbols::enterScope()
{
level++;
current = new Scope(std::map<std::string, token>(), current);
}
void Symbols::exitScope()
{
if (current->previous)
{
level--;
current = current->previous;
}
else
throw std::runtime_error("Hit the exitScope call at outermost scope");
}
token Symbols::HashLookup(token &search_for, bool &global_def, bool definintion)
{
if (definintion)
{
auto foundToken = current->symbol_table.find(search_for.tokenMark.stringValue);
auto glbfoundToken = global->symbol_table.find(search_for.tokenMark.stringValue);
if (foundToken != current->symbol_table.end() && foundToken->second.tokenHash==std::to_string(level) + search_for.tokenMark.stringValue)
{
throw std::runtime_error("Token already defined at current scope: " + std::string(search_for.tokenMark.stringValue));
return search_for;
}
else if ((global_def || current == global) && glbfoundToken != global->symbol_table.end())
{
global_def = false;
throw std::runtime_error("Token already defined at global scope: " + std::string(search_for.tokenMark.stringValue));
return search_for;
}
else if ((global_def || current == global))
{
global_def = false;
search_for.global_var = true;
search_for.tokenHash = std::to_string(level) + search_for.tokenMark.stringValue;
global->symbol_table[search_for.tokenMark.stringValue] = search_for;
return global->symbol_table.find(search_for.tokenMark.stringValue)->second;
}
else
{
search_for.tokenHash = std::to_string(level) + search_for.tokenMark.stringValue;
current->symbol_table[search_for.tokenMark.stringValue] = search_for;
return current->symbol_table.find(search_for.tokenMark.stringValue)->second;
}
}
else
{
auto temp = current;
while (temp)
{
auto search_token = temp->symbol_table.find(search_for.tokenMark.stringValue);
if (search_token != temp->symbol_table.end())
return search_token->second;
else if (search_token == temp->symbol_table.end())
temp = temp->previous;
}
throw std::runtime_error("Token not found: " + std::string(search_for.tokenMark.stringValue));
}
}
void Symbols::Completetoken(token &search_for)
{
if (search_for.global_var)
{
auto glbfoundToken = global->symbol_table.find(search_for.tokenMark.stringValue);
if (glbfoundToken != global->symbol_table.end())
glbfoundToken->second = search_for;
return;
}
else
{
auto temp = current;
while (temp)
{
auto search_token = temp->symbol_table.find(search_for.tokenMark.stringValue);
if (search_token != temp->symbol_table.end())
{
search_token->second = search_for;
return;
}
else if (search_token == temp->symbol_table.end())
temp = temp->previous;
}
}
}
void Symbols::Addtoken(token &search_for)
{
current->symbol_table[search_for.tokenMark.stringValue]=search_for;
return;
}
std::vector<token> Symbols::get_internal_args()
{
std::vector<token> internal_args;
if (level == 0)
return internal_args;
else
{
for (auto i : current->symbol_table)
{
if (i.second.llvm_value)
internal_args.push_back(i.second);
}
return internal_args;
}
}