Files
RScript/DoRunner/JumpRuntimePointerRunner.cs
2025-10-21 10:26:08 +08:00

76 lines
3.0 KiB
C#

using Convention.RScript.Parser;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner
{
public abstract class JumpRuntimePointerRunner : IRSentenceRunner
{
protected static void DoJumpRuntimePointer(ExpressionParser parser, int target, RScriptContext context)
{
Tuple<int, int> enterKey = Tuple.Create(context.CurrentRuntimePointer, target);
int currentPointer = context.CurrentRuntimePointer;
int depth = 0;
int lastLayer = 0;
if (context.JumpPointerCache.TryGetValue(enterKey, out var jumpResult))
{
depth = jumpResult.Item1;
lastLayer = jumpResult.Item2;
}
else
{
bool isForwardMove = target > context.CurrentRuntimePointer;
int step = isForwardMove ? 1 : -1;
for (; context.CurrentRuntimePointer != target; context.CurrentRuntimePointer += step)
{
if (context.CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace)
{
if (isForwardMove)
lastLayer--;
else
lastLayer++;
}
else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
{
if (isForwardMove)
lastLayer++;
else
lastLayer--;
}
depth = lastLayer < depth ? lastLayer : depth;
}
context.JumpPointerCache.Add(enterKey, Tuple.Create(depth, lastLayer));
}
// 对上层的最深影响
for (; depth < 0; depth++)
{
try
{
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
}
catch (Exception ex)
{
throw new RScriptRuntimeException($"Jump pointer with error", currentPointer, ex);
}
}
// 恢复正确的层数
for (int i = depth, e = lastLayer; i < e; i++)
{
try
{
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
}
catch (Exception ex)
{
throw new RScriptRuntimeException($"Jump pointer with error", currentPointer, ex);
}
}
}
public abstract void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
[return: MaybeNull]
public abstract object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
}
}