修复NamedSpaceRunner的遗漏
This commit is contained in:
15
DoRunner/EnterNamedSpaceRunner.cs
Normal file
15
DoRunner/EnterNamedSpaceRunner.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -5,32 +5,59 @@ 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
|
||||
//DoEnterNamespace(parser);
|
||||
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
|
||||
{
|
||||
for (int disLayer = -insLayer; disLayer > 0; disLayer--)
|
||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||
}
|
||||
}
|
||||
else
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[return: MaybeNull]
|
||||
public abstract object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||
|
@@ -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) { }
|
||||
|
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user