-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
128 lines (99 loc) · 3.05 KB
/
Copy pathobject.cpp
File metadata and controls
128 lines (99 loc) · 3.05 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
#include "object.h"
#include <cstdio>
#include <fmt/format.h>
#include <memory>
namespace object {
ObjectType Error::type() const {
return ERROR_OBJ;
}
ObjectType ReturnValue::type() const {
return RETURN_VALUE_OBJ;
}
ObjectType Boolean::type() const {
return BOOLEAN_OBJ;
}
ObjectType Integer::type() const {
return INTEGER_OBJ;
}
ObjectType Null::type() const {
return NULL_OBJ;
}
ObjectType Function::type() const {
return FUNCTION_OBJ;
}
std::string Error::inspect() const {
return fmt::format("ERROR: {}", message);
}
std::string ReturnValue::inspect() const {
return fmt::format("{}", value->inspect());
}
std::string Boolean::inspect() const {
return fmt::format("{}", value);
}
std::string Integer::inspect() const {
return fmt::format("{}", value);
}
std::string Null::inspect() const {
return "null";
}
std::string Function::inspect() const {
std::string fn_lit_str = "";
fn_lit_str += "fn";
fn_lit_str += "(";
for(int i=0; i<parameters.size(); i++){
fn_lit_str += parameters[i]->string();
if(i < parameters.size()-1){
fn_lit_str += ", ";
}
}
fn_lit_str += "){\n";
fn_lit_str += body->string();
fn_lit_str += "\n}";
return fn_lit_str;
}
std::unique_ptr<Object> Error::clone() const {
return std::make_unique<Error>(*this);
}
std::unique_ptr<Object> ReturnValue::clone() const {
return std::make_unique<ReturnValue>(value->clone());
}
std::unique_ptr<Object> Boolean::clone() const {
return std::make_unique<Boolean>(this->value);
}
std::unique_ptr<Object> Integer::clone() const {
return std::make_unique<Integer>(this->value);
}
std::unique_ptr<Object> Null::clone() const {
return std::make_unique<Null>();
}
// empty clone : have to check
// causes segmentation fault;
std::unique_ptr<Object> Function::clone() const {
std::vector<std::unique_ptr<parser::Identifier>> c_parameters;
for (int i=0; i < parameters.size(); i++) {
auto param = dynamic_cast<parser::Identifier*>(parameters[i]->clone().release());
c_parameters.push_back(std::unique_ptr<parser::Identifier>(param));
}
auto c_body = std::unique_ptr<parser::BlockStatement>(
static_cast<parser::BlockStatement*>(body->clone().release()));
auto c_environment = std::make_unique<Environment>(&environment);
// Create and return a new Function object
return std::make_unique<Function>(std::move(c_parameters),
std::move(c_body), c_environment);
}
std::tuple<std::unique_ptr<object::Object>, bool> Environment::get(std::string name){
if(store.find(name) != store.end()){
return std::make_tuple(store[name]->clone(), true);
}
return std::make_tuple(nullptr, false);
}
std::unique_ptr<object::Object> Environment::set(std::string name,
std::unique_ptr<object::Object> val){
store[name] = val->clone();
return val;
}
std::unique_ptr<Environment> new_enclosed_environment(
std::unique_ptr<Environment> &outer){
return std::make_unique<Environment>(&outer);
}
}