-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.cpp
71 lines (53 loc) · 1.61 KB
/
object.cpp
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
//
// Created by tate on 04-03-19.
//
#include "value.hpp"
#include "object.hpp"
#include "frame.hpp"
std::shared_ptr<Value>& Object::getMember(const std::string& name) {
auto m = members.find(name);
if (m == members.end()) {
members.emplace(name, std::make_shared<Value>());
m = members.find(name);
}
return m->second;
}
bool Object::callMember(Frame& f, const std::string& name, Exit& ev) {
auto m = members.find(name);
if (m == members.end())
return false;
if (m->second->type != Value::LAM)
return false;
Value lam = m->second;
ev = m->second->lam->call(f);
return true;
}
bool Object::callMember(Frame& f, const std::string& name, Exit& ev, const std::shared_ptr<Value>& obj, const size_t args) {
auto m = members.find(name);
if (m == members.end())
return false;
const Value* v = m->second->defer();
if (v->type == Value::DEF)
f.runDef(*v->def);
if (v->type != Value::LAM)
return false;
Value lam_args{std::vector<std::shared_ptr<Value>>()};
lam_args.arr->reserve(args);
// is there enough args?
if (args > f.stack.size()) {
ev = Frame::Exit(Frame::Exit::ERROR, "OverloadedOperatorStackError?!", "requested more elements from stack than it contains, probably isnt ur fault", f.feed.lineNumber());
return true;
}
// put args into an args list
for (int i = 0; i < args; i++) {
lam_args.arr->emplace_back(std::make_shared<Value>(f.stack.back()));
f.stack.pop_back();
}
// pop back what should be a reference to self
f.stack.pop_back();
// push args list onto stack
f.stack.emplace_back(std::move(lam_args));
Value lam = m->second;
ev = v->lam->call(f, obj);
return true;
}