修复NamedSpaceRunner的遗漏

This commit is contained in:
2025-10-16 17:23:37 +08:00
parent 15bdb6f8db
commit 97a6e4d76b
4 changed files with 84 additions and 24 deletions

View File

@@ -0,0 +1,15 @@
using Convention.RScript.Parser;
using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner
{
public class EnterNamedSpaceRunner : IRSentenceRunner
{
[return: MaybeNull]
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
{
context.CurrentRuntimePointer = context.NamespaceLayer[context.NamespaceLabels[sentence.content]];
return null;
}
}
}

View File

@@ -5,29 +5,56 @@ namespace Convention.RScript.Runner
{
public abstract class JumpRuntimePointerRunner : IRSentenceRunner
{
protected void DoJumpRuntimePointer(ExpressionParser parser, int target, RScriptContext context)
protected static void DoJumpRuntimePointer(ExpressionParser parser, int target, RScriptContext context)
{
bool isForwardMove = target > context.CurrentRuntimePointer;
int step = isForwardMove ? 1 : -1;
int insLayer = 0;
for (; context.CurrentRuntimePointer != target; context.CurrentRuntimePointer += step)
{
if (context.CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace)
{
if (isForwardMove)
//DoExitNamespace(parser);
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
{
if (insLayer > 0)
insLayer--;
else
{
for (int disLayer = -insLayer; disLayer > 0; disLayer--)
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
}
}
else
//DoEnterNamespace(parser);
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
insLayer++;
}
else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
{
if (isForwardMove)
//DoEnterNamespace(parser);
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
insLayer++;
else
//DoExitNamespace(parser);
{
if (insLayer > 0)
insLayer--;
else
{
for (int disLayer = -insLayer; disLayer > 0; disLayer--)
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
}
}
}
if (insLayer > 0)
{
for (; insLayer > 0; insLayer--)
{
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
}
}
else if (insLayer < 0)
{
for (; insLayer < 0; insLayer++)
{
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
}
}
}
}

View File

@@ -2,15 +2,24 @@
namespace Convention.RScript
{
[Serializable]
public class RScriptRuntimeException : Exception
public class RScriptException : Exception
{
public RScriptException() { }
public RScriptException(string message) : base(message) { }
public RScriptException(string message, Exception inner) : base(message, inner) { }
}
[Serializable]
public class RScriptRuntimeException : RScriptException
{
public RScriptRuntimeException(string message, int runtimePointer) : base($"when running {runtimePointer}, {message}") { }
public RScriptRuntimeException(string message, int runtimePointer, Exception inner) : base($"when running {runtimePointer}, {message}", inner) { }
}
[Serializable]
public class RScriptCompileException : Exception
public class RScriptCompileException : RScriptException
{
public RScriptCompileException(string message, int line, int chIndex) : base($"when compile on line {line} char {chIndex}, {message}") { }
public RScriptCompileException(string message, int line, int chIndex, Exception inner) : base($"when compile on line {line} char {chIndex}, {message}", inner) { }

View File

@@ -95,6 +95,18 @@ namespace Convention.RScript
new BackMatcher(),
};
public Dictionary<RScriptSentence.Mode, IRSentenceRunner> SentenceRunners = new()
{
{ RScriptSentence.Mode.DefineVariable, new DefineVariableRunner() },
{ RScriptSentence.Mode.EnterNamespace, new EnterNamespaceRunner() },
{ RScriptSentence.Mode.ExitNamespace, new ExitNamespaceRunner() },
{ RScriptSentence.Mode.Goto, new GoToRunner() },
{ RScriptSentence.Mode.Breakpoint, new BreakpointRunner() },
{ RScriptSentence.Mode.Backpoint, new BackpointRunner() },
{ RScriptSentence.Mode.Expression, new ExpressionRunner() },
{ RScriptSentence.Mode.NamedSpace, new EnterNamedSpaceRunner() },
};
private RScriptSentence ParseToSentence(string expression)
{
RScriptSentence result = new()
@@ -157,28 +169,25 @@ namespace Convention.RScript
}
}
public RScriptContext(string[] expressions, RScriptImportClass import = null, RScriptVariables variables = null)
public RScriptContext(string[] expressions,
RScriptImportClass import = null,
RScriptVariables variables = null,
List<IRSentenceMatcher> matcher = null,
Dictionary<RScriptSentence.Mode, IRSentenceRunner> sentenceRunners = null)
{
this.Import = import ?? new();
this.Variables = variables ?? new();
this.Sentences = (from item in expressions select ParseToSentence(item)).ToArray();
if (matcher != null)
this.SentenceParser = matcher;
if (sentenceRunners != null)
this.SentenceRunners = sentenceRunners;
BuildUpLabelsAndNamespace();
}
public RScriptSentence CurrentSentence => Sentences[CurrentRuntimePointer];
public Dictionary<RScriptSentence.Mode, IRSentenceRunner> SentenceRunners = new()
{
{ RScriptSentence.Mode.DefineVariable, new DefineVariableRunner() },
{ RScriptSentence.Mode.EnterNamespace, new EnterNamespaceRunner() },
{ RScriptSentence.Mode.ExitNamespace, new ExitNamespaceRunner() },
{ RScriptSentence.Mode.Goto, new GoToRunner() },
{ RScriptSentence.Mode.Breakpoint, new BreakpointRunner() },
{ RScriptSentence.Mode.Backpoint, new BackpointRunner() },
{ RScriptSentence.Mode.Expression, new ExpressionRunner() },
};
internal object RunNextStep(ExpressionParser parser)
{
@@ -187,7 +196,7 @@ namespace Convention.RScript
{
return SentenceRunners.TryGetValue(sentence.mode, out var runner) ? runner.Run(parser, sentence, this) : null;
}
catch (RScriptRuntimeException)
catch (RScriptException)
{
throw;
}