Working on tree optimizer

This commit is contained in:
Youmu Liu
2026-02-11 18:02:45 +08:00
parent 4735b987f4
commit 39dd32992d
9 changed files with 193 additions and 63 deletions
@@ -33,6 +33,7 @@ using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace StgSharp.RegularAnalysis.TextRegex
{
@@ -52,10 +53,8 @@ namespace StgSharp.RegularAnalysis.TextRegex
public long NodeFlag => (long)Source.Flag;
public static RegexAstNode Empty
{
get => new EmptyRegexAstNode();
}
public static RegexAstNode Empty { get; }
= new EmptyRegexAstNode();
public RegexAstNode Previous { get; set; }
@@ -68,7 +67,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
get;
set
{
field?.Parent = null;
field?.Parent = Empty;
field = value;
value.Parent = this;
}
@@ -79,7 +78,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
get;
set
{
field?.Parent = null;
field?.Parent = Empty;
field = value;
value.Parent = this;
}
@@ -90,13 +89,19 @@ namespace StgSharp.RegularAnalysis.TextRegex
get => Source.Flag;
}
public string Value => Source.Value;
public string CodeConvertTemplate { get; }
public Token<RegexElementLabel> Source { get; }
public void AppendNode(
RegexAstNode nextToken
) { }
)
{
Next = nextToken;
nextToken.Previous = this;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static (bool Valid,int Value) ComparePrecedence(
@@ -113,9 +118,33 @@ namespace StgSharp.RegularAnalysis.TextRegex
return (true,v_left - v_right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrEmpty(
RegexAstNode node
)
{
return node is null || node.Equals(Empty);
}
public void PrependNode(
RegexAstNode previousNode
) { }
)
{
Previous = previousNode;
previousNode.Next = this;
}
public void RemoveChild(
RegexAstNode node
)
{
if (Left == node)
{
Left = Empty;
} else if (Right == node) {
Right = Empty;
}
}
public static bool TryMergeUnit(
RegexAstNode prev,