single ir is briefly ok

This commit is contained in:
Youmu Liu
2026-08-07 18:08:33 +08:00
parent b3e198dbaf
commit 7f73c2fbe3
4 changed files with 53 additions and 2 deletions
@@ -277,6 +277,11 @@ namespace StgSharp.RegularAnalysis.Text
ir = ir_span[i]; ir = ir_span[i];
if ((ir.Prefix.Mask & simple_alt) == simple_alt) if ((ir.Prefix.Mask & simple_alt) == simple_alt)
{ {
if (TryGenerateSingleIR(outer_level, [ir], sc, code, local_func) > 0)
{
// TODO add additional code for the alt chain
}
// simple match // simple match
} else if (ir is RegexTryIR _try && (ir.Prefix.Mask & simple_alt) == simple_alt) } else if (ir is RegexTryIR _try && (ir.Prefix.Mask & simple_alt) == simple_alt)
{ {
@@ -5,7 +5,9 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StgSharp.RegularAnalysis.Abstraction; using StgSharp.RegularAnalysis.Abstraction;
using System.Diagnostics.Metrics;
using System.Globalization; using System.Globalization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@@ -76,6 +78,25 @@ namespace StgSharp.RegularAnalysis.Text
#region code #region code
private static int TryGenerateSingleIR(
int outer_level,
ReadOnlySpan<RegexIR> ir_span,
SourceGenContext sc,
in SequenceEmitter<string> code,
in List<SequenceEmitter<string>> local_func
)
{
return TryGenerateCountSeq(outer_level, ir_span, sc, code,
local_func) > 0 ||
TryGenerateCountSet(outer_level, ir_span, sc, code,
local_func) > 0 ||
TryGenerateMatchSet(outer_level, ir_span, sc, code,
local_func) > 0 ||
TryGenerateMatchSeq(outer_level, ir_span, sc, code, local_func) > 0 ?
1 :
-1;
}
private static int TryGeneratePack( private static int TryGeneratePack(
int outer_level, int outer_level,
ReadOnlySpan<RegexIR> ir_span, ReadOnlySpan<RegexIR> ir_span,
@@ -203,6 +224,7 @@ namespace StgSharp.RegularAnalysis.Text
.AppendLine("}") .AppendLine("}")
.AppendLine("else") .AppendLine("else")
.AppendLine("{")//TODO fail here .AppendLine("{")//TODO fail here
.AppendLine("break;")
.AppendLine("}"); .AppendLine("}");
return 1; return 1;
} }
@@ -233,6 +255,10 @@ namespace StgSharp.RegularAnalysis.Text
* { * {
* success here * success here
* } * }
* else
* {
* fail case
* }
*/ */
for (int i = 1; i < set_arr.Length - 1; i++) for (int i = 1; i < set_arr.Length - 1; i++)
{ {
@@ -244,7 +270,11 @@ namespace StgSharp.RegularAnalysis.Text
_case_code = GenLine(set, sc, ")"); _case_code = GenLine(set, sc, ")");
_ = code.AppendLine(_case_code) _ = code.AppendLine(_case_code)
.AppendLine("{") .AppendLine("{")
.AppendLine(sc.MakeRemainOffset(1))// TODO case success .AppendLine(sc.MakeRemainOffset(1)) // TODO case success
.AppendLine("}")
.AppendLine("else")
.AppendLine("{")
.AppendLine("break;") // TODO case fail
.AppendLine("}"); .AppendLine("}");
return 1; return 1;
} }
@@ -56,6 +56,21 @@ namespace StgSharp.RegularAnalysis.Text
public string StepResult { get; } = _stepResult; public string StepResult { get; } = _stepResult;
public string[] BeginRegion()
{
return [
"do",
"{"
];
}
public string[] CloseRegion()
{
return [
"} while(true);"
];
}
/// <summary> /// <summary>
/// Emits declarations for every slot requested during body generation. /// Emits declarations for every slot requested during body generation.
/// </summary> /// </summary>
@@ -91,7 +91,8 @@ namespace StgSharp.RegularAnalysis.Text
brace_count = 0; brace_count = 0;
foreach (string line in ce) foreach (string line in ce)
{ {
bool is_right_brace = line == "}"; bool is_right_brace = (line == "}") ||
line.StartsWith("} while", StringComparison.InvariantCulture);
brace_count -= is_right_brace ? 1 : 0; brace_count -= is_right_brace ? 1 : 0;
for (int i = 0; i < brace_count; i++) { for (int i = 0; i < brace_count; i++) {
_ = code_source.Append(" "); _ = code_source.Append(" ");