代码拉取完成,页面将自动刷新
//
// Created by Gorun on 2022/5/3.
//
#include "LoxFunction.h"
#include "Environment.h"
#include "Interpreter.h"
LoxFunction::LoxFunction(std::shared_ptr<Function> declaration,
std::shared_ptr<Environment> closure,
bool isNative, bool isInitializer)
: declaration{move(declaration)}, closure{move(closure)},
isNative{isNative}, isInitializer{isInitializer}
{}
int LoxFunction::arity() {
return static_cast<int>(declaration->params.size());
}
string LoxFunction::toString() {
if (isNative)
return "<native fn>";
else
return "<fn " + declaration->name.lexeme + " >";
}
any LoxFunction::call(Interpreter &interpreter, vector<any> arguments) {
auto environment = make_shared<Environment>(closure);
for (int i = 0; i < declaration->params.size(); i++) {
environment->define(declaration->params[i].lexeme,
arguments[i]);
}
try {
interpreter.executeBlock(declaration->body, environment);
} catch (LoxReturn& returnValue) {
if (isInitializer) return closure->getAt(0, "this");
return returnValue.value;
}
if (isInitializer) return closure->getAt(0, "this");
return {};
}
shared_ptr<LoxFunction> LoxFunction::bind(shared_ptr<LoxInstance> instance) {
auto environment = make_shared<Environment>(closure);
environment->define("this", instance);
return make_shared<LoxFunction>(declaration, environment, false, isInitializer);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。