From 85596f76f749821361f72b66877e37f5d322249e Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 3 Jul 2026 21:32:10 +0900 Subject: [PATCH] =?UTF-8?q?nil=E5=90=88=E4=BD=93=E6=BC=94=E7=AE=97?= =?UTF-8?q?=E5=AD=90=3F=3F=E8=A6=8F=E5=89=87=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Syntax/Rules/LuaNilCoalesceRule.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 LuaScript.Compat/Scripting/Syntax/Rules/LuaNilCoalesceRule.cs diff --git a/LuaScript.Compat/Scripting/Syntax/Rules/LuaNilCoalesceRule.cs b/LuaScript.Compat/Scripting/Syntax/Rules/LuaNilCoalesceRule.cs new file mode 100644 index 0000000..3ac9b80 --- /dev/null +++ b/LuaScript.Compat/Scripting/Syntax/Rules/LuaNilCoalesceRule.cs @@ -0,0 +1,20 @@ +namespace LuaScript.Compat.Syntax.Rules +{ + [LuaSyntaxRule(100)] + internal sealed class NilCoalesceRule : ILuaSyntaxRule + { + public bool TryApply(LuaRewriteContext context) + { + if (!context.Current.IsOperator("??")) + return false; + if (!context.TryPopOrOperand(out string left)) + return false; + + context.Advance(); + string right = context.ReadForwardOrOperand(); + string temporary = context.NextTemporary(); + context.EmitRaw($"(function() local {temporary} = ({left}) if {temporary} ~= nil then return {temporary} else return ({right}) end end)()"); + return true; + } + } +}