From 018999ff5eff347b3b1c311211942359d2d78ab7 Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 3 Jul 2026 21:32:00 +0900 Subject: [PATCH] =?UTF-8?q?=E8=A4=87=E5=90=88=E4=BB=A3=E5=85=A5/nil?= =?UTF-8?q?=E5=90=88=E4=BD=93=E4=BB=A3=E5=85=A5/++=E8=A6=8F=E5=89=87?= =?UTF-8?q?=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/LuaAssignmentRules.cs | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 LuaScript.Compat/Scripting/Syntax/Rules/LuaAssignmentRules.cs diff --git a/LuaScript.Compat/Scripting/Syntax/Rules/LuaAssignmentRules.cs b/LuaScript.Compat/Scripting/Syntax/Rules/LuaAssignmentRules.cs new file mode 100644 index 0000000..8299040 --- /dev/null +++ b/LuaScript.Compat/Scripting/Syntax/Rules/LuaAssignmentRules.cs @@ -0,0 +1,73 @@ +namespace LuaScript.Compat.Syntax.Rules +{ + [LuaSyntaxRule(70)] + internal sealed class AugmentedAssignmentRule : ILuaSyntaxRule + { + public bool TryApply(LuaRewriteContext context) + { + if (context.Current.Kind != LuaSyntaxTokenKind.Operator) + return false; + + string? connective = Connective(context.Current.Text); + if (connective is null) + return false; + if (!context.TryReadTrailingLvalue(out _, out string target)) + return false; + + context.Advance(); + string value = context.ReadLineExpression(); + string separator = context.LastOutputIsTrivia() ? string.Empty : " "; + context.EmitRaw($"{separator}= {target} {connective} ({value})"); + return true; + } + + private static string? Connective(string op) => op switch + { + "+=" => "+", + "-=" => "-", + "*=" => "*", + "/=" => "/", + "%=" => "%", + "^=" => "^", + "..=" => "..", + "||=" => "or", + "&&=" => "and", + _ => null, + }; + } + + [LuaSyntaxRule(80)] + internal sealed class NilCoalesceAssignRule : ILuaSyntaxRule + { + public bool TryApply(LuaRewriteContext context) + { + if (!context.Current.IsOperator("??=")) + return false; + if (!context.TryReadTrailingLvalue(out int start, out string target)) + return false; + + context.RemoveOutputFrom(start); + context.Advance(); + string value = context.ReadLineExpression(); + context.EmitRaw($"if {target} == nil then {target} = ({value}) end"); + return true; + } + } + + [LuaSyntaxRule(90)] + internal sealed class IncrementRule : ILuaSyntaxRule + { + public bool TryApply(LuaRewriteContext context) + { + if (!context.Current.IsOperator("++")) + return false; + if (!context.TryReadTrailingLvalue(out _, out string target)) + return false; + + context.Advance(); + string separator = context.LastOutputIsTrivia() ? string.Empty : " "; + context.EmitRaw($"{separator}= {target} + 1"); + return true; + } + } +}