-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarescript-types.hpp
More file actions
126 lines (98 loc) · 4.1 KB
/
carescript-types.hpp
File metadata and controls
126 lines (98 loc) · 4.1 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
#ifndef CARESCRIPT_TYPES_HPP
#define CARESCRIPT_TYPES_HPP
#include <string>
#include <vector>
namespace carescript {
// abstract class to provide an interface for all types
struct ScriptValue {
using type = void;
virtual const std::string get_type() const noexcept= 0;
virtual bool operator==(const ScriptValue*) const noexcept = 0;
virtual bool operator==(const ScriptValue&v) const noexcept { return operator==(&v); }
virtual std::string to_printable() const noexcept = 0;
virtual std::string to_string() const noexcept = 0;
virtual ScriptValue* copy() const noexcept = 0;
void get_value() const noexcept {}
void get_value() noexcept {}
virtual ~ScriptValue() {};
};
// default number type implementation
struct ScriptNumberValue : public ScriptValue {
const std::string get_type() const noexcept override { return "Number"; }
long double number = 0.0;
bool operator==(const ScriptValue* val) const noexcept override {
return val->get_type() == get_type() && ((ScriptNumberValue*)val)->number == number;
}
std::string to_printable() const noexcept override {
std::string str = std::to_string(number);
str.erase(str.find_last_not_of('0') + 1, std::string::npos);
str.erase(str.find_last_not_of('.') + 1, std::string::npos);
return str;
}
std::string to_string() const noexcept override {
return to_printable();
}
long double get_value() const noexcept { return number; }
long double& get_value() noexcept { return number; }
ScriptValue* copy() const noexcept override { return new ScriptNumberValue(number); }
ScriptNumberValue() {}
ScriptNumberValue(long double num): number(num) {}
operator long double() const noexcept { return get_value(); }
};
// default string type implementation
struct ScriptStringValue : public ScriptValue {
const std::string get_type() const noexcept override { return "String"; }
std::string string = "";
bool operator==(const ScriptValue* val) const noexcept override {
return val->get_type() == get_type() && ((ScriptStringValue*)val)->string == string;
}
std::string to_printable() const noexcept override {
return string;
}
std::string to_string() const noexcept override {
return "\"" + string + "\"";
}
std::string get_value() const noexcept { return string; }
std::string& get_value() noexcept { return string; }
ScriptValue* copy() const noexcept override { return new ScriptStringValue(string); }
ScriptStringValue() {}
ScriptStringValue(std::string str): string(str) {}
operator std::string() const noexcept { return get_value(); }
};
// default name type implementation
struct ScriptNameValue : public ScriptValue {
const std::string get_type() const noexcept override { return "Name"; }
std::string name = "";
bool operator==(const ScriptValue* val) const noexcept override {
return val->get_type() == get_type() && ((ScriptNameValue*)val)->name == name;
}
std::string to_printable() const noexcept override {
return name;
}
std::string to_string() const noexcept override {
return to_printable();
}
std::string get_value() const noexcept { return name; }
std::string& get_value() noexcept { return name; }
ScriptValue* copy() const noexcept override { return new ScriptNameValue(name); }
ScriptNameValue() {}
ScriptNameValue(std::string name): name(name) {}
};
// default null type implementation
struct ScriptNullValue : public ScriptValue {
const std::string get_type() const noexcept override { return "Null"; }
bool operator==(const ScriptValue* val) const noexcept override {
return val->get_type() == get_type();
}
std::string to_printable() const noexcept override {
return "null";
}
std::string to_string() const noexcept override {
return to_printable();
}
void get_value() const noexcept { return; }
ScriptValue* copy() const noexcept override { return new ScriptNullValue(); }
ScriptNullValue() {}
};
} /* namespace carescript */
#endif