From 97a6e4d76b5399c781e3913df5d1529ac606373f Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Thu, 16 Oct 2025 17:23:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DNamedSpaceRunner=E7=9A=84?= =?UTF-8?q?=E9=81=97=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DoRunner/EnterNamedSpaceRunner.cs | 15 ++++++++++ DoRunner/JumpRuntimePointerRunner.cs | 43 ++++++++++++++++++++++------ PublicTypes/RScriptException.cs | 13 +++++++-- RScriptContext.cs | 37 +++++++++++++++--------- 4 files changed, 84 insertions(+), 24 deletions(-) create mode 100644 DoRunner/EnterNamedSpaceRunner.cs diff --git a/DoRunner/EnterNamedSpaceRunner.cs b/DoRunner/EnterNamedSpaceRunner.cs new file mode 100644 index 0000000..7b33ddf --- /dev/null +++ b/DoRunner/EnterNamedSpaceRunner.cs @@ -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; + } + } +} diff --git a/DoRunner/JumpRuntimePointerRunner.cs b/DoRunner/JumpRuntimePointerRunner.cs index 8dd55a3..a05e846 100644 --- a/DoRunner/JumpRuntimePointerRunner.cs +++ b/DoRunner/JumpRuntimePointerRunner.cs @@ -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); + } } } } diff --git a/PublicTypes/RScriptException.cs b/PublicTypes/RScriptException.cs index b925d9a..facb458 100644 --- a/PublicTypes/RScriptException.cs +++ b/PublicTypes/RScriptException.cs @@ -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) { } diff --git a/RScriptContext.cs b/RScriptContext.cs index 5c520e4..22b41d2 100644 --- a/RScriptContext.cs +++ b/RScriptContext.cs @@ -95,6 +95,18 @@ namespace Convention.RScript new BackMatcher(), }; + public Dictionary 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 matcher = null, + Dictionary 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 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; }