-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.h
136 lines (123 loc) · 3 KB
/
object.h
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
//
// Created by 23766 on 2024/10/6.
//
#ifndef CPPMONKEY_OBJECT_H
#define CPPMONKEY_OBJECT_H
#include <string>
#include <memory>
#include <vector>
#include "ast.h"
#define INTEGER_OBJ "INTEGER"
#define BOOLEAN_OBJ "BOOLEAN"
#define NULL_OBJ "NULL"
#define RETURN_VALUE_OBJ "RETURN_VALUE"
#define ERROR_OBJ "ERROR"
#define FUNCTION_OBJ "FUNCTION"
using ObjectType = std::string;
class Object {
public:
virtual ObjectType Type() const = 0;
virtual std::string Inspect() const = 0;
Object() = default;
~Object() = default;
};
class Integer : public Object {
public:
int64_t value;
Integer(int64_t val);
~Integer();
ObjectType Type() const override {
return INTEGER_OBJ;
}
std::string Inspect() const override {
return std::to_string(value);
}
};
class Boolean_obj : public Object{
public:
bool value;
Boolean_obj(bool value);
~Boolean_obj();
ObjectType Type() const override {
return BOOLEAN_OBJ;
}
std::string Inspect() const override {
return std::to_string(value);
}
};
class Null : public Object {
public:
Null() = default ;
~Null() = default ;
ObjectType Type() const override {
return NULL_OBJ;
}
std::string Inspect() const override {
return "null";
}
};
class ReturnValue : public Object {
public:
std::unique_ptr<Object> value;
ReturnValue(std::unique_ptr<Object> parm);
~ReturnValue();
ObjectType Type() const override {
return RETURN_VALUE_OBJ;
}
std::string Inspect() const override {
return value->Inspect();
}
};
class Error : public Object {
public:
explicit Error(std::string msg);
~Error();
std::string message;
ObjectType Type() const override {
return ERROR_OBJ;
}
std::string Inspect() const override {
return "ERROR: " + message;
}
};
class Environment {
public:
std::map<std::string,std::unique_ptr<Object>> store;
std::unique_ptr<Environment> outer;
explicit Environment(std::unique_ptr<Environment> new_env);
~Environment();
// get获取
std::pair<std::unique_ptr<Object>,bool> Get(std::string name);
// set设置
void Set(std::string name, std::unique_ptr<Object> val);
};
class Function : public Object {
public:
std::vector<std::unique_ptr<Identifier>> parameters;
std::unique_ptr<BlockStatement> body;
std::unique_ptr<Environment> env;
Function();
~Function();
ObjectType Type() const override {
return FUNCTION_OBJ;
}
std::string Inspect() const override{
std::ostringstream oss;
std::vector<std::string> params;
for(auto &item:parameters){
params.push_back(item->String());
}
oss << "fn";
oss << "(";
std::string result;
for (const auto& str : params) {
result += str;
}
oss << result;
oss << ") {\n";
oss << body->String();
oss << "\n}";
return oss.str();
}
};
#endif //CPPMONKEY_OBJECT_H