修复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 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; bool isForwardMove = target > context.CurrentRuntimePointer;
int step = isForwardMove ? 1 : -1; int step = isForwardMove ? 1 : -1;
int insLayer = 0;
for (; context.CurrentRuntimePointer != target; context.CurrentRuntimePointer += step) for (; context.CurrentRuntimePointer != target; context.CurrentRuntimePointer += step)
{ {
if (context.CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace) if (context.CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace)
{ {
if (isForwardMove) 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 else
//DoEnterNamespace(parser); insLayer++;
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
} }
else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace) else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
{ {
if (isForwardMove) if (isForwardMove)
//DoEnterNamespace(parser); insLayer++;
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
else 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); context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
}
} }
} }
} }

View File

@@ -2,15 +2,24 @@
namespace Convention.RScript namespace Convention.RScript
{ {
[Serializable] [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) : base($"when running {runtimePointer}, {message}") { }
public RScriptRuntimeException(string message, int runtimePointer, Exception inner) : base($"when running {runtimePointer}, {message}", inner) { } public RScriptRuntimeException(string message, int runtimePointer, Exception inner) : base($"when running {runtimePointer}, {message}", inner) { }
} }
[Serializable] [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) : 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) { } 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(), 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) private RScriptSentence ParseToSentence(string expression)
{ {
RScriptSentence result = new() 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.Import = import ?? new();
this.Variables = variables ?? new(); this.Variables = variables ?? new();
this.Sentences = (from item in expressions select ParseToSentence(item)).ToArray(); this.Sentences = (from item in expressions select ParseToSentence(item)).ToArray();
if (matcher != null)
this.SentenceParser = matcher;
if (sentenceRunners != null)
this.SentenceRunners = sentenceRunners;
BuildUpLabelsAndNamespace(); BuildUpLabelsAndNamespace();
} }
public RScriptSentence CurrentSentence => Sentences[CurrentRuntimePointer]; 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) 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; return SentenceRunners.TryGetValue(sentence.mode, out var runner) ? runner.Run(parser, sentence, this) : null;
} }
catch (RScriptRuntimeException) catch (RScriptException)
{ {
throw; throw;
} }