-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptObject.cpp
More file actions
67 lines (53 loc) · 1.57 KB
/
ScriptObject.cpp
File metadata and controls
67 lines (53 loc) · 1.57 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
#include "ScriptObject.h"
#include <iostream>
#include <sstream>
ScriptObject::ScriptObject(const char* c) : root(new Json::Value()) {
std::istringstream iss(c);
try {
iss >> *root;
} catch (Json::RuntimeError e) {
throw std::invalid_argument(string("unable to convert to JSon Object: ") + e.what());
}
current = &(*root);
}
ScriptObject::ScriptObject(const string &c) : root(new Json::Value()) {
std::istringstream iss(c);
try {
iss >> *root;
} catch (Json::RuntimeError e) {
throw std::invalid_argument(string("unable to convert to JSon Object: ") + e.what());
}
current = &(*root);
}
ScriptObject::ScriptObject(const ScriptObject & obj) : root(obj.root), current(obj.current) {
}
std::string ScriptObject::toString() {
std::ostringstream oss;
oss << *current;
return oss.str();
}
unique_ptr<Expression> ScriptObject::eval() {
return unique_ptr<Expression>(new ScriptObject(*this));
}
void ScriptObject::assign(unique_ptr<Expression> rval) {
std::istringstream iss(rval->toString());
iss >> *current;
}
unique_ptr<Expression> ScriptObject::add(Expression& e) {
int lval = 0;
if (current->isConvertibleTo(Json::ValueType::intValue)) {
lval = current->asInt();
}
ScriptInteger lObj = ScriptInteger(lval);
return lObj.add(e);
}
unique_ptr<Expression> ScriptObject::index(Expression& e) {
int idx = atoi(e.toString().c_str());
unique_ptr<ScriptObject> result(new ScriptObject(*this));
try {
result->current = &((*current)[idx]);
} catch(Json::LogicError e) {
throw std::invalid_argument(std::string("unable to index: ") + e.what());
}
return result;
}