-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.h
More file actions
172 lines (155 loc) · 5.08 KB
/
object.h
File metadata and controls
172 lines (155 loc) · 5.08 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
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
172
#ifndef OBJECT_H
#define OBJECT_H
#include <cstddef>
#include <map>
#include <string>
#include <string_view>
#include <vector>
#include "ast.h"
#include "parser.h"
#include "token.h"
#include "utils.h"
#include <functional>
using ast::Identifier;
using ast::Block;
namespace obj
{
enum class ObjectType
{
BOOLEAN,
INTEGER,
_NULL,
RETURN,
ERROR,
FUNCTION,
STRING,
BUILTIN
};
static constexpr std::array<const NameValuePair<ObjectType>, 8> objects_enums_string {{
{ObjectType::BOOLEAN, "BOOLEAN"},
{ObjectType::INTEGER, "INTEGER"},
{ObjectType::_NULL, "NULL"},
{ObjectType::RETURN, "RETURN"},
{ObjectType::ERROR, "ERROR"},
{ObjectType::FUNCTION, "FUNCTION"},
{ObjectType::STRING, "STRING"},
{ObjectType::BUILTIN, "BUILTIN"}
}};
class Object
{
public:
virtual ObjectType type() const = 0;
virtual std::string inspect() const = 0;
virtual std::string_view type_string() const = 0;
virtual ~Object(){}
Object() = default;
Object (const Object&) = delete;
Object& operator=(const Object&) = delete;
Object (Object&&) = delete;
Object& operator=(Object&&) = delete;
};
class Integer : public Object
{
public:
const std::size_t value;
explicit Integer(const std::size_t v) : value(v) {}
ObjectType type() const override { return ObjectType::INTEGER; }
std::string inspect() const override { return std::to_string(value); }
std::string_view type_string() const override { return getNameForValue(objects_enums_string, ObjectType::INTEGER); }
};
class Boolean : public Object
{
public:
const bool value;
explicit Boolean(bool v) : value(v) {}
ObjectType type() const override { return ObjectType::BOOLEAN; }
std::string inspect() const override { return value ? "verdadero" : "falso"; }
std::string_view type_string() const override { return getNameForValue(objects_enums_string, ObjectType::BOOLEAN); }
};
class Null : public Object
{
public:
ObjectType type() const override { return ObjectType::_NULL;}
std::string inspect() const override { return "nulo"; }
std::string_view type_string() const override { return getNameForValue(objects_enums_string, ObjectType::_NULL); }
};
class Return : public Object
{
public:
Object* value;
explicit Return(Object* v) : value(v) {}
ObjectType type() const override { return ObjectType::RETURN; }
std::string inspect() const override { return value->inspect(); }
std::string_view type_string() const override { return getNameForValue(objects_enums_string, ObjectType::RETURN); }
};
class Error : public Object
{
public:
const std::string message;
explicit Error(const std::string& msg) : message(msg) {}
ObjectType type() const override { return ObjectType::ERROR; }
std::string inspect() const override { return message; }
std::string_view type_string() const override { return getNameForValue(objects_enums_string, ObjectType::ERROR); }
};
class Environment
{
std::map<std::string, Object*> store;
Environment* outer = nullptr;
public:
Environment() = default;
explicit Environment(Environment* outer) : outer(outer) { }
void set_item(const std::string& key, Object* value) { store[key] = value; }
void del_item(const std::string& key){ store.erase(key); }
Object* get_item(const std::string& key)
{
if(store[key])
return store[key];
else
return outer->get_item(key);
}
bool item_exist(const std::string& key)
{
if(store.find(key) != store.end())
return true;
else if(outer && outer->item_exist(key))
return true;
return false;
}
};
class Function : public Object
{
public:
std::vector<Identifier*> parameters;
Block* body;
Environment* env;
Function(const std::vector<Identifier*>& params, Block* b, Environment* env )
: parameters(params), body(b), env(env) {}
ObjectType type() const override { return ObjectType::FUNCTION; }
std::string_view type_string() const override { return getNameForValue(objects_enums_string, ObjectType::FUNCTION); }
std::string inspect() const override
{
return "Función";
}
};
class String : public Object
{
public:
const std::string value;
explicit String(const std::string& v) : value(v) {}
ObjectType type() const override { return ObjectType::STRING; }
std::string inspect() const override { return value; }
std::string_view type_string() const override { return getNameForValue(objects_enums_string, ObjectType::STRING); }
};
using BuiltinFunction = std::function<Object*(const std::vector<Object*>&, const int)>;
class Builtin : public Object
{
public:
const BuiltinFunction& fn;
explicit Builtin(const BuiltinFunction& builtin_fn) : fn(builtin_fn) {}
ObjectType type() const override { return ObjectType::BUILTIN; }
std::string inspect() const override { return "builtin function"; }
std::string_view type_string() const override { return getNameForValue(objects_enums_string, ObjectType::BUILTIN); }
Builtin(const Builtin& cp) : fn(cp.fn) {}
};
} // namespace obj
#endif // OBJECT_H