2 Star 6 Fork 0

秩启/Lox++

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LoxFunction.cpp 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
秩启 提交于 2022-05-07 13:32 . Support class further
//
// 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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/rankrev/loxpp.git
git@gitee.com:rankrev/loxpp.git
rankrev
loxpp
Lox++
master

搜索帮助