Skip to content

Commit

Permalink
update Yuescript.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Jul 31, 2023
1 parent 27d953c commit dd42eb8
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 57 deletions.
18 changes: 16 additions & 2 deletions Assets/Script/Lib/Dora/en/yue.d.tl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local record yue
dirsep: string
yue_compiled: {string:string}
yue_compiled: {string: string}
version: string

record Config
Expand All @@ -10,6 +10,19 @@ local record yue
space_over_tab: boolean
same_module: boolean
line_offset: integer
enum LuaTarget
"5.1"
"5.2"
"5.3"
"5.4"
end
record Options
target: LuaTarget
path: string
metamethod __newindex: function(self: Options, key: string, value: string)
metamethod __index: function(self: Options, key: string): string
end
options: Options
end

to_lua: function(code: string, config?: Config): --[[codes]] string | nil, --[[error]] string | nil, --[[globals]] {{string, integer, integer}} | nil
Expand All @@ -28,12 +41,12 @@ local record yue
loadstring: function(input: string, chunkname?: string, mode?: LoadMode, env?: table, config?: Config): function(...: any): (any...), string
loadfile: function(filename: string, chunkname?: string, mode?: LoadMode, env?: table, config?: Config): function(...: any): (any...), string
dofile: function(filename: string, chunkname?: string, mode?: LoadMode, env?: table, config?: Config): any...
create_yuepath: function(package_path: string): string
find_modulepath: function(name: string): string
pcall: function(f: function, ...: any): boolean, any...
require: function(name: string): any...
p: function(...: any)
options: {string: string}
traceback: function(message: string): string

type CompileCodeHandler = function(
codes: string,
Expand All @@ -55,4 +68,5 @@ local record yue

metamethod __call: function(self: yue, module: string): any...
end

return yue
18 changes: 16 additions & 2 deletions Assets/Script/Lib/Dora/zh-Hans/yue.d.tl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local record yue
dirsep: string
yue_compiled: {string:string}
yue_compiled: {string: string}
version: string

record Config
Expand All @@ -10,6 +10,19 @@ local record yue
space_over_tab: boolean
same_module: boolean
line_offset: integer
enum LuaTarget
"5.1"
"5.2"
"5.3"
"5.4"
end
record Options
target: LuaTarget
path: string
metamethod __newindex: function(self: Options, key: string, value: string)
metamethod __index: function(self: Options, key: string): string
end
options: Options
end

to_lua: function(code: string, config?: Config): --[[codes]] string | nil, --[[error]] string | nil, --[[globals]] {{string, integer, integer}} | nil
Expand All @@ -28,12 +41,12 @@ local record yue
loadstring: function(input: string, chunkname?: string, mode?: LoadMode, env?: table, config?: Config): function(...: any): (any...), string
loadfile: function(filename: string, chunkname?: string, mode?: LoadMode, env?: table, config?: Config): function(...: any): (any...), string
dofile: function(filename: string, chunkname?: string, mode?: LoadMode, env?: table, config?: Config): any...
create_yuepath: function(package_path: string): string
find_modulepath: function(name: string): string
pcall: function(f: function, ...: any): boolean, any...
require: function(name: string): any...
p: function(...: any)
options: {string: string}
traceback: function(message: string): string

type CompileCodeHandler = function(
codes: string,
Expand All @@ -55,4 +68,5 @@ local record yue

metamethod __call: function(self: yue, module: string): any...
end

return yue
56 changes: 33 additions & 23 deletions Assets/Script/Lib/luaminify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,12 @@ local function Format_Mini(ast)
return a.."\n"..b
end]]
sep = sep or ' '
if sep == ';' then
local token = a:match("([%w_]+)%s*$")
if token == "then" or token == "do" then
sep = ' '
end
end
local aa, bb = a:sub(-1,-1), b:sub(1,1)
if UpperChars[aa] or LowerChars[aa] or aa == '_' then
if not (UpperChars[bb] or LowerChars[bb] or bb == '_' or Digits[bb]) then
Expand Down Expand Up @@ -2094,9 +2100,15 @@ local function FormatYue(ast, lineMap)
break
end
end
aa = aa or ''
sep = sep or ' '
if spaceSep then
sep = ''
elseif sep == ';' then
local token = aa:match("([%w_]+)%s*$")
if token == "then" or token == "do" then
sep = ' '
end
end
local bb = b:match("^%s*([^%s])")
if UpperChars[aa] or LowerChars[aa] or aa == '_' then
Expand Down Expand Up @@ -2136,21 +2148,6 @@ local function FormatYue(ast, lineMap)

formatExpr = function(expr)
local out = {string.rep('(', expr.ParenCount or 0)}
if expr.Tokens then
local line = expr.Tokens[1].Line
local targetLine = lineMap[line]
if targetLine and currentLine < targetLine then
out[#out + 1] = string.rep('\n', targetLine - currentLine)
currentLine = targetLine
end
elseif expr.Value then
local line = expr.Value.Line
local targetLine = lineMap[line]
if targetLine and currentLine < targetLine then
out[#out + 1] = string.rep('\n', targetLine - currentLine)
currentLine = targetLine
end
end
if expr.AstType == 'VarExpr' then
if expr.Variable then
out[#out + 1] = expr.Variable.Name
Expand Down Expand Up @@ -2214,6 +2211,11 @@ local function FormatYue(ast, lineMap)

elseif expr.AstType == 'MemberExpr' then
out[#out + 1] = formatExpr(expr.Base)
local targetLine = lineMap[expr.Ident.Line]
if targetLine and currentLine < targetLine then
out[#out + 1] = string.rep('\n', targetLine - currentLine)
currentLine = targetLine
end
out[#out + 1] = expr.Indexer
out[#out + 1] = expr.Ident.Data

Expand Down Expand Up @@ -2264,19 +2266,19 @@ local function FormatYue(ast, lineMap)

end
out[#out + 1] = string.rep(')', expr.ParenCount or 0)
return table.concat(out)
end

local formatStatement = function(statement)
local out = {""}
if statement.Tokens and statement.Tokens[1] then
local line = statement.Tokens[1].Line
if expr.Tokens and expr.Tokens[1] then
local line = expr.Tokens[1].Line
local targetLine = lineMap[line]
if targetLine and currentLine < targetLine then
out[#out + 1] = string.rep('\n', targetLine - currentLine)
table.insert(out, 1, string.rep('\n', targetLine - currentLine))
currentLine = targetLine
end
end
return table.concat(out)
end

local formatStatement = function(statement)
local out = {""}
if statement.AstType == 'AssignmentStatement' then
for i = 1, #statement.Lhs do
out[#out + 1] = formatExpr(statement.Lhs[i])
Expand Down Expand Up @@ -2431,6 +2433,14 @@ local function FormatYue(ast, lineMap)
else
print("Unknown AST Type: ", statement.AstType)
end
if statement.Tokens and statement.Tokens[1] then
local line = statement.Tokens[1].Line
local targetLine = lineMap[line]
if targetLine and currentLine < targetLine then
table.insert(out, 1, string.rep('\n', targetLine - currentLine))
currentLine = targetLine
end
end
return table.concat(out)
end

Expand Down
79 changes: 57 additions & 22 deletions Source/3rdParty/yuescript/yue_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static std::unordered_set<std::string> Metamethods = {
"close"s // Lua 5.4
};

const std::string_view version = "0.17.10"sv;
const std::string_view version = "0.17.14"sv;
const std::string_view extension = "yue"sv;

class CompileError : public std::logic_error {
Expand Down Expand Up @@ -5366,9 +5366,10 @@ class YueCompilerImpl {

void transformUnaryValue(UnaryValue_t* unary_value, str_list& out) {
str_list temp;
int target = getLuaTarget(unary_value);
for (auto _op : unary_value->ops.objects()) {
std::string op = _parser.toString(_op);
if (op == "~"sv && getLuaTarget(_op) < 503) {
if (op == "~"sv && target < 503) {
throw CompileError("bitwise operator is not available when not targeting Lua version 5.3 or higher"sv, _op);
}
temp.push_back(op == "not"sv ? op + ' ' : op);
Expand Down Expand Up @@ -5630,9 +5631,10 @@ class YueCompilerImpl {
return;
}
std::string unary_op;
int target = getLuaTarget(unary_exp);
for (auto _op : unary_exp->ops.objects()) {
std::string op = _parser.toString(_op);
if (op == "~"sv && getLuaTarget(_op) < 503) {
if (op == "~"sv && target < 503) {
throw CompileError("bitwise operator is not available when not targeting Lua version 5.3 or higher"sv, _op);
}
unary_op.append(op == "not"sv ? op + ' ' : op);
Expand Down Expand Up @@ -7951,6 +7953,20 @@ class YueCompilerImpl {
body->content.set(tryNode->catchBlock->body);
funLit->body.set(body);
}
if (auto tryBlock = tryNode->func.as<Block_t>()) {
BLOCK_START
BREAK_IF(tryBlock->statements.size() != 1);
auto stmt = static_cast<Statement_t*>(tryBlock->statements.front());
auto expListAssign = stmt->content.as<ExpListAssign_t>();
BREAK_IF(expListAssign->action);
auto value = singleValueFrom(expListAssign->expList);
BREAK_IF(!value);
auto chainValue = value->item.as<ChainValue_t>();
BREAK_IF(!chainValue);
BREAK_IF(!isChainValueCall(chainValue));
tryNode->func.set(expListAssign->expList->exprs.front());
BLOCK_END
}
if (auto tryBlock = tryNode->func.as<Block_t>()) {
auto tryExp = toAst<Exp_t>("->"sv, x);
auto funLit = simpleSingleValueFrom(tryExp)->value.to<FunLit_t>();
Expand Down Expand Up @@ -7978,31 +7994,50 @@ class YueCompilerImpl {
auto chainValue = value->item.as<ChainValue_t>();
BREAK_IF(!chainValue);
BREAK_IF(!isChainValueCall(chainValue));
ast_ptr<true, ast_node> last = chainValue->items.back();
chainValue->items.pop_back();
_ast_list* args = nullptr;
if (auto invoke = ast_cast<InvokeArgs_t>(last)) {
args = &invoke->args;
} else {
args = &(ast_to<Invoke_t>(last)->args);
}
if (errHandler) {
if (errHandler && getLuaTarget(x) < 502) {
auto tryExp = toAst<Exp_t>("->"sv, x);
auto funLit = simpleSingleValueFrom(tryExp)->value.to<FunLit_t>();
auto expList = x->new_ptr<ExpList_t>();
expList->exprs.push_back(tryNode->func);
auto expListAssign = x->new_ptr<ExpListAssign_t>();
expListAssign->expList.set(expList);
auto stmt = x->new_ptr<Statement_t>();
stmt->content.set(expListAssign);
auto body = x->new_ptr<Body_t>();
body->content.set(stmt);
funLit->body.set(body);
auto xpcall = toAst<ChainValue_t>("xpcall()", x);
auto invoke = ast_to<Invoke_t>(xpcall->items.back());
invoke->args.push_back(tryNode->func);
invoke->args.push_back(tryExp);
invoke->args.push_back(errHandler);
for (auto arg : args->objects()) {
invoke->args.push_back(arg);
}
transformChainValue(xpcall, out, ExpUsage::Closure);
} else {
auto pcall = toAst<ChainValue_t>("pcall()", x);
auto invoke = ast_to<Invoke_t>(pcall->items.back());
invoke->args.push_back(tryNode->func);
for (auto arg : args->objects()) {
invoke->args.push_back(arg);
ast_ptr<true, ast_node> last = chainValue->items.back();
chainValue->items.pop_back();
_ast_list* args = nullptr;
if (auto invoke = ast_cast<InvokeArgs_t>(last)) {
args = &invoke->args;
} else {
args = &(ast_to<Invoke_t>(last)->args);
}
if (errHandler) {
auto xpcall = toAst<ChainValue_t>("xpcall()", x);
auto invoke = ast_to<Invoke_t>(xpcall->items.back());
invoke->args.push_back(tryNode->func);
invoke->args.push_back(errHandler);
for (auto arg : args->objects()) {
invoke->args.push_back(arg);
}
transformChainValue(xpcall, out, ExpUsage::Closure);
} else {
auto pcall = toAst<ChainValue_t>("pcall()", x);
auto invoke = ast_to<Invoke_t>(pcall->items.back());
invoke->args.push_back(tryNode->func);
for (auto arg : args->objects()) {
invoke->args.push_back(arg);
}
transformChainValue(pcall, out, ExpUsage::Closure);
}
transformChainValue(pcall, out, ExpUsage::Closure);
}
if (usage == ExpUsage::Common) {
out.back().append(nlr(x));
Expand Down
16 changes: 13 additions & 3 deletions Source/3rdParty/yuescript/yuescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,20 @@ static void get_config(lua_State* L, yue::YueConfig& config) {
config.useSpaceOverTab = lua_toboolean(L, -1) != 0;
}
lua_pop(L, 1);
lua_pushliteral(L, "target");
lua_pushliteral(L, "options");
lua_gettable(L, -2);
if (lua_isstring(L, -1) != 0) {
config.options["target"] = lua_tostring(L, -1);
if (lua_istable(L, -1) != 0) {
lua_pushnil(L); // options startKey
while (lua_next(L, -2) != 0) { // options key value
size_t len = 0;
auto pstr = lua_tolstring(L, -2, &len);
std::string key{pstr, len};
pstr = lua_tolstring(L, -1, &len);
std::string value{pstr, len};
config.options[key] = value;
lua_pop(L, 1); // options key
}
lua_pop(L, 1); // options
}
lua_pop(L, 1);
}
Expand Down
15 changes: 10 additions & 5 deletions Source/3rdParty/yuescript/yuescript.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ local function yue_loader(name)
return concat(tried, "\n\t")
end
local function yue_call(f, ...)
return xpcall(f, function(err)
return yue.traceback(err, 1)
end, ...)
local args = {...}
return xpcall(function()
return f(unpack(args))
end, function(err)
return yue.traceback(err, 2)
end)
end
yue_loadstring = function(...)
local options, str, chunk_name, mode, env = get_options(...)
Expand Down Expand Up @@ -166,8 +169,10 @@ local function yue_traceback(err, level)
end
local function yue_require(name)
insert_loader()
local success, res = xpcall(require, function(err)
return yue_traceback(err, 2)
local success, res = xpcall(function()
return require(name)
end, function(err)
return yue_traceback(err, 3)
end)
if success then
return res
Expand Down

0 comments on commit dd42eb8

Please sign in to comment.