add ir emitter

This commit is contained in:
Youmu Liu
2026-06-16 21:04:51 +08:00
parent 3cfd29f793
commit a7bb3209dd
9 changed files with 711 additions and 148 deletions
@@ -28,23 +28,72 @@
using StgSharp.RegularAnalysis.Abstraction;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace StgSharp.RegularAnalysis.TextRegex
{
public partial class RegexInterpreter
{
private List<RegexIR> EmptyIR { get; } = [];
internal List<RegexIR> GenerateIR(
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree
)
{
using TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(tree);
while (enumerator.MoveNext())
return GenerateIRRecursion(tree.Root, 0);
}
private List<RegexIR> GenerateIRRecursion(
RegexAstNode node,
int depth
)
{
if (depth > 1024) {
throw new OverflowException("Regex parse exceeds maximum recursion depth");
}
int curDepth = depth;
RegexAstNode left = node.Left;
RegexAstNode right = node.Right;
List<RegexIR> leftIR = EmptyIR;
List<RegexIR> rightIR = EmptyIR;
bool isLeftOperator = !RegexAstNode.IsNullOrEmpty(left) &&
(left.Source.Flag & RegexElementLabel.OPERATOR) != 0;
bool isRightOperator = !RegexAstNode.IsNullOrEmpty(right) &&
(right.Source.Flag & RegexElementLabel.OPERATOR) != 0;
Token<RegexElementLabel> token = node.Source;
switch (node.Source.Flag)
{
if (true) { }
case RegexElementLabel.GROUP_BEGIN:
string group_source = token.Value;
bool is_group = group_source.Length == 1;
string group_name = group_source.StartsWith(@"(?<", StringComparison.Ordinal) ?
group_source[3..^2] :
string.Empty;
List<RegexIR> groupIR = [];
rightIR = GenerateIRRecursion(right, curDepth + 1);
groupIR.Add(RegexIR.Try(rightIR.Count));
groupIR.AddRange(rightIR);
groupIR.Add(RegexIR.Condition(2, 1));
groupIR.Add(RegexIR.PopLevel(false));
groupIR.Add(RegexIR.PopLevel(true));
break;
case RegexElementLabel.COUNT:
break;
case RegexElementLabel.CONCAT:
break;
case RegexElementLabel.ALT:
break;
default:
break;
}
return [];