-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.cpp
More file actions
executable file
·111 lines (93 loc) · 2.46 KB
/
Copy pathstorage.cpp
File metadata and controls
executable file
·111 lines (93 loc) · 2.46 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
#include "storage.h"
/*
* Symbol table
*/
void SymbolTable::set(std::string key, plsVariable * v){
printf("STORING VAR: %s : %s\n", key.c_str(), v->c_str());
symbolList[key] = v;
}
plsVariable * SymbolTable::get(std::string key){
printf("accessing variable: %s\n", key.c_str());
//plsVariable *tmp = symbolList[key];
std::map<std::string, plsVariable*>::iterator it;
it = symbolList.find(key.c_str());
if(it == symbolList.end()){
printf("OMG THAT VAR DOESN'T EXIST\n");
throw std::logic_error("Unexpected identifier");
}
plsVariable *tmp = symbolList[key];
printf("Variable exists\n");
printf("VARIABLE: %s\n", tmp->c_str());
return tmp;
}
/*
* plastic variable type definitions
*/
/*
* OPERATORS
*/
plsVariable * plsVariable::operator+(plsVariable* RHS){
if(this->type() == var_str){
std::string LHS = this->c_str();
plsString * ret = new plsString(LHS + RHS->c_str());
return ret;
}else if(this->type() == var_num){
plsNum * ret = new plsNum(this->num_val() + RHS->num_val());
return ret;
}
}
plsVariable * plsVariable::operator-(plsVariable* RHS){
if(this->type() == var_num){
printf("Doing subtraction: %f - %f\n", this->num_val(), RHS->num_val());
plsNum * ret = new plsNum(this->num_val() - RHS->num_val());
return ret;
}else{
throw std::logic_error("Invalid operation");
}
}
plsVariable * plsVariable::operator*(plsVariable* RHS){
if(this->type() == var_num){
plsNum * ret = new plsNum(this->num_val() * RHS->num_val());
return ret;
}else{
throw std::logic_error("Invalid operation");
}
}
plsVariable * plsVariable::operator/(plsVariable* RHS){
if(this->type() == var_num){
plsNum * ret = new plsNum(this->num_val() / RHS->num_val());
return ret;
}else{
throw std::logic_error("Invalid operation");
}
}
/*
* STRING
*/
const char * plsString::c_str() const{
return value.c_str();
}
double plsString::num_val() const{
return atof(value.c_str());
}
int plsString::type() const{
return var_str;
}
/*
* NUMBER
*/
std::string plsNum::dtos(double x) const{
std::ostringstream strs;
strs << x;
std::string str = strs.str();
return str;
}
const char * plsNum::c_str() const{
return dtos(value).c_str();
}
double plsNum::num_val() const{
return value;
}
int plsNum::type() const{
return var_num;
}