diff --git a/LuaScript.Compat/Scripting/Syntax/Rules/LuaComparisonRules.cs b/LuaScript.Compat/Scripting/Syntax/Rules/LuaComparisonRules.cs new file mode 100644 index 0000000..c6fca2c --- /dev/null +++ b/LuaScript.Compat/Scripting/Syntax/Rules/LuaComparisonRules.cs @@ -0,0 +1,60 @@ +namespace LuaScript.Compat.Syntax.Rules +{ + [LuaSyntaxRule(10)] + internal sealed class IsNotRule : ILuaSyntaxRule + { + public bool TryApply(LuaRewriteContext context) + { + if (!context.Current.IsName("is")) + return false; + if (!context.TryPreviousOutputSignificantSameLine(out var previous) || !LuaSyntaxFacts.IsValueEnd(previous)) + return false; + + int notIndex = context.NextSignificantSameLine(context.Index + 1); + if (notIndex < 0 || !context.Input[notIndex].IsName("not")) + return false; + + int after = context.NextSignificant(notIndex + 1); + if (after < 0 || !LuaSyntaxFacts.IsValueStart(context.Input[after])) + return false; + + context.EmitOperator("~="); + context.Index = notIndex + 1; + return true; + } + } + + [LuaSyntaxRule(20)] + internal sealed class IsRule : ILuaSyntaxRule + { + public bool TryApply(LuaRewriteContext context) + { + if (!context.Current.IsName("is")) + return false; + if (!context.TryPreviousOutputSignificantSameLine(out var previous) || !LuaSyntaxFacts.IsValueEnd(previous)) + return false; + + int next = context.NextSignificantSameLine(context.Index + 1); + if (next < 0 || !LuaSyntaxFacts.IsValueStart(context.Input[next])) + return false; + + context.EmitOperator("=="); + context.Advance(); + return true; + } + } + + [LuaSyntaxRule(30)] + internal sealed class NotEqualRule : ILuaSyntaxRule + { + public bool TryApply(LuaRewriteContext context) + { + if (!context.Current.IsOperator("!=")) + return false; + + context.EmitOperator("~="); + context.Advance(); + return true; + } + } +}