Skip to content

Commit

Permalink
update Yuescript. [skip CI]
Browse files Browse the repository at this point in the history
remove one more redundant 'do' block from try-catch. [skip CI]
  • Loading branch information
pigpigyyy committed Mar 21, 2024
1 parent 97e31eb commit 9d3ee94
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 44 deletions.
24 changes: 11 additions & 13 deletions Assets/Script/Dev/Entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -820,19 +820,17 @@ stop = function() -- 476
end -- 476
_module_0["stop"] = stop -- 481
local _anon_func_0 = function(Content, Path, file, require, type) -- 502
do -- 495
local scriptPath = Path:getPath(file) -- 495
Content:insertSearchPath(1, scriptPath) -- 496
scriptPath = Path(scriptPath, "Script") -- 497
if Content:exist(scriptPath) then -- 498
Content:insertSearchPath(1, scriptPath) -- 499
end -- 498
local result = require(file) -- 500
if "function" == type(result) then -- 501
result() -- 501
end -- 501
return nil -- 502
end -- 502
local scriptPath = Path:getPath(file) -- 495
Content:insertSearchPath(1, scriptPath) -- 496
scriptPath = Path(scriptPath, "Script") -- 497
if Content:exist(scriptPath) then -- 498
Content:insertSearchPath(1, scriptPath) -- 499
end -- 498
local result = require(file) -- 500
if "function" == type(result) then -- 501
result() -- 501
end -- 501
return nil -- 502
end -- 495
local _anon_func_1 = function(Label, err, fontSize, scroll, width) -- 532
local label = Label("sarasa-mono-sc-regular", fontSize) -- 529
Expand Down
85 changes: 54 additions & 31 deletions Source/3rdParty/yuescript/yue_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = {
"close"s // Lua 5.4
};

const std::string_view version = "0.23.1"sv;
const std::string_view version = "0.23.2"sv;
const std::string_view extension = "yue"sv;

class CompileError : public std::logic_error {
Expand Down Expand Up @@ -3674,9 +3674,20 @@ class YueCompilerImpl {
}
}

std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Exp_t* exp, str_list* ensureArgListInTheEnd = nullptr) {
bool checkUpValueFuncAvailable(ast_node* node) {
if (_funcLevel <= 1) return false;
return node->traverse([&](ast_node* n) {
switch (n->get_id()) {
case id<MacroName_t>():
return traversal::Stop;
}
return traversal::Continue;
}) != traversal::Stop;
}

std::optional<std::pair<std::string, str_list>> getUpValueFuncFromBlock(Block_t* block, str_list* ensureArgListInTheEnd) {
if (_funcLevel <= 1) return std::nullopt;
auto result = exp->traverse([&](ast_node* node) {
auto result = block->traverse([&](ast_node* node) {
switch (node->get_id()) {
case id<MacroName_t>():
return traversal::Stop;
Expand All @@ -3687,7 +3698,8 @@ class YueCompilerImpl {
str_list args;
bool upVarsAssignedOrCaptured = false;
bool usedVar = false;
ast_ptr<false, Statement_t> stmt;
auto x = block;
ast_ptr<false, Block_t> newBlock(block);
{
str_list globals;
for (const auto& scope : _scopes) {
Expand All @@ -3697,31 +3709,26 @@ class YueCompilerImpl {
}
}
}
auto returnNode = exp->new_ptr<Return_t>();
auto returnList = exp->new_ptr<ExpListLow_t>();
returnList->exprs.push_back(exp);
returnNode->valueList.set(returnList);
std::string codes;
if (_withVars.empty()) {
codes = YueFormat{}.toString(returnNode);
stmt = exp->new_ptr<Statement_t>();
stmt->content.set(returnNode);
codes = YueFormat{}.toString(block);
} else {
auto withNode = exp->new_ptr<With_t>();
withNode->valueList.set(toAst<ExpList_t>(_withVars.top(), exp));
auto returnStmt = exp->new_ptr<Statement_t>();
returnStmt->content.set(returnNode);
withNode->body.set(returnStmt);
auto withNode = block->new_ptr<With_t>();
withNode->valueList.set(toAst<ExpList_t>(_withVars.top(), x));
withNode->body.set(block);
codes = YueFormat{}.toString(withNode);
stmt = exp->new_ptr<Statement_t>();
auto simpleValue = exp->new_ptr<SimpleValue_t>();
auto simpleValue = x->new_ptr<SimpleValue_t>();
simpleValue->value.set(withNode);
auto newExpr = newExp(simpleValue, exp);
auto explist = exp->new_ptr<ExpList_t>();
auto newExpr = newExp(simpleValue, x);
auto explist = x->new_ptr<ExpList_t>();
explist->exprs.push_back(newExpr);
auto expListAssign = exp->new_ptr<ExpListAssign_t>();
auto expListAssign = x->new_ptr<ExpListAssign_t>();
expListAssign->expList.set(explist);
auto stmt = x->new_ptr<Statement_t>();
stmt->content.set(expListAssign);
auto blk = x->new_ptr<Block_t>();
blk->statements.push_back(stmt);
newBlock.set(blk);
}
if (!globals.empty()) {
codes.insert(0, "global "s + join(globals, ","sv) + '\n');
Expand All @@ -3730,7 +3737,7 @@ class YueCompilerImpl {
config.lintGlobalVariable = true;
auto result = YueCompiler{L, _luaOpen, true}.compile(codes, config);
if (result.error) {
YUEE("failed to compile dues to Yue formatter", exp);
YUEE("failed to compile dues to Yue formatter", x);
}
usedVar = result.usedVar;
if (result.globals) {
Expand All @@ -3745,7 +3752,6 @@ class YueCompilerImpl {
}
}
if (!upVarsAssignedOrCaptured) {
auto x = exp;
if (ensureArgListInTheEnd) {
std::unordered_set<std::string> vars;
for (const auto& arg : args) {
Expand Down Expand Up @@ -3775,7 +3781,7 @@ class YueCompilerImpl {
}
}
auto funLit = toAst<FunLit_t>("("s + join(args, ","sv) + ")-> nil"s, x);
funLit->body->content.set(stmt.get());
funLit->body->content.set(newBlock);
funLit->noRecursion = true;
auto simpleValue = x->new_ptr<SimpleValue_t>();
simpleValue->value.set(funLit);
Expand All @@ -3796,6 +3802,29 @@ class YueCompilerImpl {
return std::nullopt;
}


std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Block_t* block, str_list* ensureArgListInTheEnd = nullptr) {
if (checkUpValueFuncAvailable(block)) {
return getUpValueFuncFromBlock(block, ensureArgListInTheEnd);
}
return std::nullopt;
}

std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Exp_t* exp, str_list* ensureArgListInTheEnd = nullptr) {
if (checkUpValueFuncAvailable(exp)) {
auto returnNode = exp->new_ptr<Return_t>();
auto returnList = exp->new_ptr<ExpListLow_t>();
returnList->exprs.push_back(exp);
returnNode->valueList.set(returnList);
auto block = exp->new_ptr<Block_t>();
auto stmt = exp->new_ptr<Statement_t>();
stmt->content.set(returnNode);
block->statements.push_back(stmt);
return getUpValueFuncFromBlock(block, ensureArgListInTheEnd);
}
return std::nullopt;
}

bool transformAsUpValueFunc(Exp_t* exp, str_list& out) {
auto result = upValueFuncFrom(exp);
if (result) {
Expand Down Expand Up @@ -9091,13 +9120,7 @@ class YueCompilerImpl {
}
if (auto tryBlock = tryNode->func.as<Block_t>()) {
{
auto body = tryBlock->new_ptr<Body_t>();
body->content.set(tryBlock);
auto doNode = tryBlock->new_ptr<Do_t>();
doNode->body.set(body);
auto simpleValue = tryBlock->new_ptr<SimpleValue_t>();
simpleValue->value.set(doNode);
if (auto result = upValueFuncFrom(newExp(simpleValue, tryBlock))) {
if (auto result = upValueFuncFrom(tryBlock)) {
auto [funcName, args] = std::move(*result);
if (errHandler) {
auto xpcall = toAst<ChainValue_t>("xpcall()", x);
Expand Down

0 comments on commit 9d3ee94

Please sign in to comment.