forked from freeduke33/rerap2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarmanager.cpp
171 lines (154 loc) · 5.61 KB
/
varmanager.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// ReRap Version 0.9
// Copyright 2011 Matthew Mikolay.
//
// This file is part of ReRap.
//
// ReRap is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ReRap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ReRap. If not, see <http://www.gnu.org/licenses/>.
#include "varmanager.h"
/*** The entry of special values ***/
std::map<std::string, Object*> VariableManager::special;
/*** The stack of entries ***/
std::stack< std::map<std::string, Object*> > VariableManager::stack;
/*** The current entry ***/
std::map<std::string, Object*> VariableManager::entry;
/*** Constructor ***/
VariableManager::VariableManager()
{
// Set up special values
if(special.empty())
{
special["pi"] = new Real(3.14159265358979323846);
special["пи"] = new Real(3.14159265358979323846);
special["lf"] = new Text("\n");
special["пс"] = new Text("\n");
special["abs"] = new SpecialFunction(SPF_ABS);
special["sign"] = new SpecialFunction(SPF_SIGN);
special["sqrt"] = new SpecialFunction(SPF_SQRT);
special["корень"] = new SpecialFunction(SPF_SQRT);
special["entier"] = new SpecialFunction(SPF_ENTIER);
special["целч"] = new SpecialFunction(SPF_ENTIER);
special["round"] = new SpecialFunction(SPF_ROUND);
special["окрч"] = new SpecialFunction(SPF_ROUND);
special["rand"] = new SpecialFunction(SPF_RAND);
special["дсч"] = new SpecialFunction(SPF_RAND);
special["int_rand"] = new SpecialFunction(SPF_INTRAND);
special["цсч"] = new SpecialFunction(SPF_INTRAND);
special["index"] = new SpecialFunction(SPF_INDEX);
special["индекс"] = new SpecialFunction(SPF_INDEX);
special["is_empty"] = new SpecialFunction(SPF_ISEMPTY);
special["тип_пуст"] = new SpecialFunction(SPF_ISEMPTY);
special["is_log"] = new SpecialFunction(SPF_ISLOG);
special["тип_лог"] = new SpecialFunction(SPF_ISLOG);
special["is_int"] = new SpecialFunction(SPF_ISINT);
special["тип_цел"] = new SpecialFunction(SPF_ISINT);
special["is_real"] = new SpecialFunction(SPF_ISREAL);
special["тип_вещ"] = new SpecialFunction(SPF_ISREAL);
special["is_text"] = new SpecialFunction(SPF_ISTEXT);
special["тип_текст"] = new SpecialFunction(SPF_ISTEXT);
special["is_seq"] = new SpecialFunction(SPF_ISSEQ);
special["тип_корт"] = new SpecialFunction(SPF_ISSEQ);
special["is_proc"] = new SpecialFunction(SPF_ISPROC);
special["тип_проц"] = new SpecialFunction(SPF_ISPROC);
special["is_fun"] = new SpecialFunction(SPF_ISFUN);
special["тип_функ"] = new SpecialFunction(SPF_ISFUN);
special["sin"] = new SpecialFunction(SPF_SIN);
special["cos"] = new SpecialFunction(SPF_COS);
special["tg"] = new SpecialFunction(SPF_TG);
special["arcsin"] = new SpecialFunction(SPF_ARCSIN);
special["arctg"] = new SpecialFunction(SPF_ARCTG);
special["exp"] = new SpecialFunction(SPF_EXP);
special["ln"] = new SpecialFunction(SPF_LN);
special["lg"] = new SpecialFunction(SPF_LG);
}
}
/*** If an object exists in the current entry ***/
bool VariableManager::hasObject(std::string id)
{
return (special.count(id) != 0 || entry.count(id) != 0);
}
/*** Set the object to which a variable refers ***/
void VariableManager::setObject(std::string id, Object* obj)
{
if(special.count(id) != 0)
{
delete obj;
throw InvalidAssignmentException(id, "Cannot assign value to a special variable.");
}
if(entry.count(id) != 0)
delete entry[id];
entry[id] = obj;
}
/*** Get the object to which a variable refers ***/
Object* VariableManager::getObject(std::string id)
{
if(special.count(id) != 0)
return special[id];
return entry[id];
}
/*** Push the current entry onto the manager ***/
void VariableManager::pushEntry()
{
stack.push(entry);
entry.clear();
}
/*** Pop an entry off the manager ***/
void VariableManager::popEntry()
{
// Clear all items from the current entry
for(std::map<std::string, Object*>::iterator it = entry.begin(); it != entry.end(); it++)
delete it->second;
entry.clear();
if(stack.empty())
return;
entry = stack.top();
stack.pop();
}
/*** Get an object on the top entry ***/
Object* VariableManager::getTopLevelObject(std::string id)
{
if(stack.top().count(id) == 0)
return 0;
return stack.top()[id];
}
/*** Set an object on the top entry ***/
void VariableManager::setTopLevelObject(std::string id, Object* obj)
{
if(special.count(id) != 0)
throw InvalidAssignmentException(id, "Cannot assign value to a special variable.");
if(stack.top().count(id) != 0)
delete stack.top()[id];
stack.top()[id] = obj;
}
/*** Empty the manager ***/
void VariableManager::empty()
{
// Clear all items from the special entry
for(std::map<std::string, Object*>::iterator it = special.begin(); it != special.end(); it++)
delete it->second;
special.clear();
// Clear all entries from the stack
while(!stack.empty())
popEntry();
// Clear the last entry, not stored on the stack
popEntry();
}
/*** Return the size of the stack ***/
unsigned int VariableManager::getStackSize()
{
return stack.size();
}
/*** Destructor ***/
VariableManager::~VariableManager()
{
}