fix errors in interpreter. add a enumerator for tree. working on tree optimizer

This commit is contained in:
Youmu Liu
2026-02-10 18:31:30 +08:00
parent 807658111c
commit 4735b987f4
4 changed files with 221 additions and 136 deletions
@@ -37,7 +37,7 @@ using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
{
public class RegexInterpreter
public partial class RegexInterpreter
{
private AbstractSyntaxTree<RegexAstNode, RegexElementLabel> _tree = new();
@@ -113,8 +113,11 @@ namespace StgSharp.RegularAnalysis.TextRegex
if (op.Source.Flag != RegexElementLabel.GROUP_BEGIN) {
throw new InvalidOperationException("Unmatched group begin and end symbol");
}
op.Right = _stack.PopOperand();
_stack.PushOperand(op);
if (op.Source.Value.Length > 1)
{
op.Right = _stack.PopOperand();
_stack.PushOperand(op);
}
} else if ((token.Flag & RegexElementLabel.OPERATOR) != 0)
{
Console.Write($"Operator {token.Value} with flag {token.Flag}, ");
@@ -179,27 +182,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
RegexAstNode node = enumerator.Current;
if ((RegexElementLabel)node.NodeFlag == RegexElementLabel.ALT)
{
RegexAstNode right, left = node.Left;
while (node.Right.Source.Flag == RegexElementLabel.ALT)
{
// flatten a alternation to left linked list of root ALT
right = node.Right;
RegexAstNode nextAlt = right.Left;
left.Next = nextAlt;
nextAlt.Previous = left;
nextAlt.Parent = node;
right.Left = RegexAstNode.Empty;
left = nextAlt;
// remove the right ALT node and connect its right child to current node
node.Right = right.Right;
right.Right.Parent = node;
}
// connect the last ALT node to the right child of current node
right = node.Right;
left.Next = right;
right.Previous = left;
RegexAstNode list = FlattenAlt(node);
node.Left = list;
node.Right = RegexAstNode.Empty;
}
}