EP RScript 修复因跳跃导致的变量层次混乱

This commit is contained in:
2025-10-11 17:24:40 +08:00
parent f850030d10
commit bacfcd550f
8 changed files with 38 additions and 17 deletions

View File

@@ -1,7 +1,5 @@
--- ---
description: alwaysApply: false
globs:
alwaysApply: true
--- ---
## RIPER-5 + O1 思维 + 代理执行协议 ## RIPER-5 + O1 思维 + 代理执行协议

View File

@@ -6,11 +6,11 @@ namespace Convention.RScript.Matcher
{ {
public bool Match(string expression, ref RScriptSentence sentence) public bool Match(string expression, ref RScriptSentence sentence)
{ {
Regex LabelRegex = new(@"^back\s*\(\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\)$"); Regex LabelRegex = new(@"back\s*\(\s*(.+)\s*\)");
var LabelMatch = LabelRegex.Match(expression); var LabelMatch = LabelRegex.Match(expression);
if (LabelMatch.Success) if (LabelMatch.Success)
{ {
sentence.mode = RScriptSentence.Mode.Label; sentence.mode = RScriptSentence.Mode.Backpoint;
sentence.content = LabelMatch.Groups[1].Value; sentence.content = LabelMatch.Groups[1].Value;
return true; return true;
} }

View File

@@ -6,11 +6,11 @@ namespace Convention.RScript.Matcher
{ {
public bool Match(string expression, ref RScriptSentence sentence) public bool Match(string expression, ref RScriptSentence sentence)
{ {
Regex LabelRegex = new(@"^break\(\s*(.+)\s*\)$"); Regex LabelRegex = new(@"break\s*\(\s*(.+)\s*\)");
var LabelMatch = LabelRegex.Match(expression); var LabelMatch = LabelRegex.Match(expression);
if (LabelMatch.Success) if (LabelMatch.Success)
{ {
sentence.mode = RScriptSentence.Mode.Label; sentence.mode = RScriptSentence.Mode.Breakpoint;
sentence.content = LabelMatch.Groups[1].Value; sentence.content = LabelMatch.Groups[1].Value;
return true; return true;
} }

View File

@@ -6,7 +6,7 @@ namespace Convention.RScript.Matcher
{ {
public bool Match(string expression, ref RScriptSentence sentence) public bool Match(string expression, ref RScriptSentence sentence)
{ {
Regex DefineVariableRegex = new(@"^(string|int|double|float|bool|var)\s+([a-zA-Z_][a-zA-Z0-9_]*)$"); Regex DefineVariableRegex = new(@"(string|int|double|float|bool|var)\s+([a-zA-Z_][a-zA-Z0-9_]*)");
var DefineVariableMatch = DefineVariableRegex.Match(expression); var DefineVariableMatch = DefineVariableRegex.Match(expression);
if (DefineVariableMatch.Success) if (DefineVariableMatch.Success)
{ {

View File

@@ -7,7 +7,7 @@ namespace Convention.RScript.Matcher
public bool Match(string expression, ref RScriptSentence sentence) public bool Match(string expression, ref RScriptSentence sentence)
{ {
Regex GotoRegex = new(@"^goto\s*\(\s*(.+)\s*,\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\)$"); Regex GotoRegex = new(@"goto\s*\(\s*(.+)\s*,\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\)");
var GotoMatch = GotoRegex.Match(expression); var GotoMatch = GotoRegex.Match(expression);
if (GotoMatch.Success) if (GotoMatch.Success)
{ {

View File

@@ -6,7 +6,7 @@ namespace Convention.RScript.Matcher
{ {
public bool Match(string expression, ref RScriptSentence sentence) public bool Match(string expression, ref RScriptSentence sentence)
{ {
Regex LabelRegex = new(@"^label\s*\(\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\)$"); Regex LabelRegex = new(@"label\s*\(\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\)");
var LabelMatch = LabelRegex.Match(expression); var LabelMatch = LabelRegex.Match(expression);
if (LabelMatch.Success) if (LabelMatch.Success)
{ {

View File

@@ -200,7 +200,7 @@ namespace Convention.RScript
private void DoExitNamespace(ExpressionParser parser) private void DoExitNamespace(ExpressionParser parser)
{ {
// 移除在本命名空间中定义的变量 // 移除当前命名空间的变量
foreach (var local in CurrentLocalSpaceVariableNames.Peek()) foreach (var local in CurrentLocalSpaceVariableNames.Peek())
{ {
Variables.Remove(local); Variables.Remove(local);
@@ -216,6 +216,29 @@ namespace Convention.RScript
RuntimePointerStack.Pop(); RuntimePointerStack.Pop();
} }
private void DoJumpRuntimePointer(ExpressionParser parser, int target)
{
bool isForwardMove = target > CurrentRuntimePointer;
int step = isForwardMove ? 1 : -1;
for (; CurrentRuntimePointer != target; CurrentRuntimePointer += step)
{
if (CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace)
{
if (isForwardMove)
DoExitNamespace(parser);
else
DoEnterNamespace(parser);
}
else if (CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
{
if (isForwardMove)
DoEnterNamespace(parser);
else
DoExitNamespace(parser);
}
}
}
private void DoGoto(ExpressionParser parser, RScriptSentence sentence) private void DoGoto(ExpressionParser parser, RScriptSentence sentence)
{ {
// 检查并跳转到指定标签 // 检查并跳转到指定标签
@@ -224,7 +247,7 @@ namespace Convention.RScript
if (Labels.TryGetValue(sentence.content, out var labelPointer)) if (Labels.TryGetValue(sentence.content, out var labelPointer))
{ {
GotoPointerStack.Push(CurrentRuntimePointer); GotoPointerStack.Push(CurrentRuntimePointer);
CurrentRuntimePointer = labelPointer; DoJumpRuntimePointer(parser, labelPointer);
} }
else else
{ {
@@ -256,7 +279,7 @@ namespace Convention.RScript
private void DoBackpoint(ExpressionParser parser, RScriptSentence sentence) private void DoBackpoint(ExpressionParser parser, RScriptSentence sentence)
{ {
// 检查并跳转到上次跳转的位置的后一个位置 // 检查并跳转到上次跳转的位置
if (parser.Evaluate<bool>(sentence.content)) if (parser.Evaluate<bool>(sentence.content))
{ {
if (GotoPointerStack.Count == 0) if (GotoPointerStack.Count == 0)
@@ -265,7 +288,7 @@ namespace Convention.RScript
} }
else else
{ {
CurrentRuntimePointer = GotoPointerStack.Pop() + 1; DoJumpRuntimePointer(parser, GotoPointerStack.Pop());
} }
} }
} }

View File

@@ -14,8 +14,8 @@ public class Program
var result = engine.Run(@" var result = engine.Run(@"
double i; double i;
i = 2.0; i = 2.0;
label(test);
{ {
label(test);
goto(true,func1); goto(true,func1);
goto(100>i,test); goto(100>i,test);
} }
@@ -24,11 +24,11 @@ result = i;
goto(true,end); goto(true,end);
label(func1);
{ {
label(func1);
i = Pow(i,2.0); i = Pow(i,2.0);
back(true);
} }
back(true);
label(end); label(end);
", import); ", import);