-
Notifications
You must be signed in to change notification settings - Fork 0
/
lox_callable.cpp
46 lines (37 loc) · 1.27 KB
/
lox_callable.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
#include "lox_callable.h"
#include <utility>
#include "interpreter.h"
#include "environment.h"
shared_ptr<lox_object> lox_function::call(interpreter& i, vector<shared_ptr<lox_object>> &arguments)
{
auto new_env = make_shared<environment>(closure);
for(int j = 0; j < declaration->params->size(); j++)
new_env->define((*declaration->params)[j]->lexeme, arguments[j]);
try {
i._execute_Block(declaration->body, new_env);
} catch (return_result_exception& r)
{
if(is_initializer)
return closure->get_at(0, "this");
return r.value;
}
return make_shared<lox_object>();
}
int lox_function::arity()
{
return declaration->params->size();
}
string lox_function::to_string()
{
return "<fn " + declaration->name->lexeme + ">";
}
lox_function::lox_function(shared_ptr<Function_Stmt> declaration, shared_ptr<environment> closure, bool is_initializer)
: declaration(std::move(declaration)), closure(std::move(closure)), is_initializer(is_initializer)
{
}
shared_ptr<lox_function> lox_function::bind(const shared_ptr<lox_instance>& instance)
{
auto new_env = make_shared<environment>(closure);
new_env->define("this", instance);
return make_shared<lox_function>(this->declaration, new_env, is_initializer);
}