-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCall.cpp
More file actions
33 lines (31 loc) · 1.14 KB
/
Call.cpp
File metadata and controls
33 lines (31 loc) · 1.14 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
#include "Call.hpp"
#include "Expr.hpp"
#include "Environment.hpp"
#include "visitor/Interpreter.hpp"
#include "Class.hpp"
#include <utility>
Value LoxFunction::call(const Interpreter& parent, const std::vector<std::shared_ptr<Expr>>& arguments) {
Interpreter interpreter{ parent };
interpreter.environment = Environment::makeShared(closure);
for (size_t i = 0; i != arguments.size(); ++i) {
interpreter.environment->define(params.at(i), arguments.at(i)->accept(parent));
}
try {
body->accept(interpreter);
return isInitializer ? closure->getAt(0, "this") : std::monostate{};
}
catch (const Return& r) {
return isInitializer ? closure->getAt(0, "this") : r.get();
}
}
std::shared_ptr<LoxFunction> LoxFunction::bind(std::shared_ptr<LoxInstance> instance) {
auto environment = Environment::makeShared(closure);
environment->define("this", instance);
auto function = std::make_shared<LoxFunction>();
function->name = name;
function->body = body;
function->params = params;
function->closure = environment;
function->isInitializer = isInitializer;
return function;
}