-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoxClass.cpp
More file actions
35 lines (30 loc) · 846 Bytes
/
LoxClass.cpp
File metadata and controls
35 lines (30 loc) · 846 Bytes
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
#include "headers/LoxClass.h"
#include "headers/LoxInstance.h"
std::any LoxClass::call(Interpreter& interpeter, std::vector<std::any> arguments)
{
auto instance = std::make_shared<LoxInstance>(shared_from_this());
std::shared_ptr<LoxFunction> initializer = find_method("init");
if (initializer != nullptr) {
initializer->bind(instance)->call(interpeter, arguments);
}
return instance;
}
int LoxClass::arity()
{
std::shared_ptr<LoxFunction> initializer = find_method("init");
if (initializer == nullptr) {
return 0;
}
return initializer->arity();
}
std::shared_ptr<LoxFunction> LoxClass::find_method(std::string name)
{
if (methods.find(name) != methods.end())
{
return methods[name];
}
if (superclass != nullptr) {
return superclass->find_method(name);
}
return nullptr;
}