Compare commits
8 Commits
3866cdd525
...
main
Author | SHA1 | Date | |
---|---|---|---|
58f3d1067c | |||
7b48066aaf | |||
dde9e6b82d | |||
d3e21cad15 | |||
52e8e85542 | |||
97a6e4d76b | |||
15bdb6f8db | |||
e2ab2a1077 |
31
DoRunner/BackpointRunner.cs
Normal file
31
DoRunner/BackpointRunner.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class BackpointRunner : JumpRuntimePointerRunner
|
||||||
|
{
|
||||||
|
public override void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[return: MaybeNull]
|
||||||
|
public override object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
// 检查并跳转到上次跳转的位置
|
||||||
|
if (parser.Evaluate<bool>(sentence.content))
|
||||||
|
{
|
||||||
|
if (context.GotoPointerStack.Count == 0)
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"No position to back.", context.CurrentRuntimePointer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DoJumpRuntimePointer(parser, context.GotoPointerStack.Pop(), context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
DoRunner/BreakpointRunner.cs
Normal file
36
DoRunner/BreakpointRunner.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript
|
||||||
|
{
|
||||||
|
public class BreakpointRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
parser.Compile<bool>(sentence.content);
|
||||||
|
}
|
||||||
|
|
||||||
|
[return: MaybeNull]
|
||||||
|
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
// 检查并跳转到当前命名空间的结束位置
|
||||||
|
if (parser.Evaluate<bool>(sentence.content))
|
||||||
|
{
|
||||||
|
if (context.RuntimePointerStack.Count == 0)
|
||||||
|
{
|
||||||
|
context.CurrentRuntimePointer = context.Sentences.Length;
|
||||||
|
}
|
||||||
|
else if (context.NamespaceLayer.TryGetValue(context.RuntimePointerStack.Peek(), out var exitPointer))
|
||||||
|
{
|
||||||
|
context.CurrentRuntimePointer = exitPointer;
|
||||||
|
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"No namespace to break.", context.CurrentRuntimePointer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
127
DoRunner/DefineVariableRunner.cs
Normal file
127
DoRunner/DefineVariableRunner.cs
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class DefineVariableRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
var varTypeName = sentence.info[0];
|
||||||
|
var varName = sentence.info[1];
|
||||||
|
var varInitExpression = sentence.info[2];
|
||||||
|
Type varType;
|
||||||
|
object varDefaultValue;
|
||||||
|
{
|
||||||
|
if (varTypeName == "string")
|
||||||
|
{
|
||||||
|
varType = typeof(string);
|
||||||
|
varDefaultValue = "";
|
||||||
|
}
|
||||||
|
else if (varTypeName == "int")
|
||||||
|
{
|
||||||
|
varType = typeof(int);
|
||||||
|
if (varInitExpression != null) parser.Compile<int>(varInitExpression);
|
||||||
|
varDefaultValue = 0;
|
||||||
|
}
|
||||||
|
else if (varTypeName == "double")
|
||||||
|
{
|
||||||
|
varType = typeof(double);
|
||||||
|
if (varInitExpression != null) parser.Compile<double>(varInitExpression);
|
||||||
|
varDefaultValue = 0.0;
|
||||||
|
}
|
||||||
|
else if (varTypeName == "float")
|
||||||
|
{
|
||||||
|
varType = typeof(float);
|
||||||
|
if (varInitExpression != null) parser.Compile<float>(varInitExpression);
|
||||||
|
varDefaultValue = 0.0f;
|
||||||
|
}
|
||||||
|
else if (varTypeName == "bool")
|
||||||
|
{
|
||||||
|
varType = typeof(bool);
|
||||||
|
if (varInitExpression != null) parser.Compile<bool>(varInitExpression);
|
||||||
|
varDefaultValue = false;
|
||||||
|
}
|
||||||
|
else if (varTypeName == "var")
|
||||||
|
{
|
||||||
|
varType = typeof(object);
|
||||||
|
if (varInitExpression != null) parser.Compile(varInitExpression);
|
||||||
|
varDefaultValue = new object();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"Unsupported variable type '{varTypeName}'.", context.CurrentRuntimePointer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (context.CurrentLocalSpaceVariableNames.Peek().Contains(varName) == false)
|
||||||
|
{
|
||||||
|
context.Variables.Add(varName, new(varType, default));
|
||||||
|
parser.context.Variables[varName] = varDefaultValue;
|
||||||
|
context.CurrentLocalSpaceVariableNames.Peek().Add(varName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"Variable '{varName}' already defined on this namespace.", context.CurrentRuntimePointer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[return: MaybeNull]
|
||||||
|
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
// 定义变量
|
||||||
|
var varTypeName = sentence.info[0];
|
||||||
|
var varName = sentence.info[1];
|
||||||
|
var varInitExpression = sentence.info[2];
|
||||||
|
Type varType;
|
||||||
|
object varDefaultValue;
|
||||||
|
{
|
||||||
|
if (varTypeName == "string")
|
||||||
|
{
|
||||||
|
varType = typeof(string);
|
||||||
|
varDefaultValue = varInitExpression == null ? string.Empty : varInitExpression;
|
||||||
|
}
|
||||||
|
else if (varTypeName == "int")
|
||||||
|
{
|
||||||
|
varType = typeof(int);
|
||||||
|
varDefaultValue = varInitExpression == null ? 0 : parser.Evaluate<int>(varInitExpression);
|
||||||
|
}
|
||||||
|
else if (varTypeName == "double")
|
||||||
|
{
|
||||||
|
varType = typeof(double);
|
||||||
|
varDefaultValue = varInitExpression == null ? 0.0 : parser.Evaluate<double>(varInitExpression);
|
||||||
|
}
|
||||||
|
else if (varTypeName == "float")
|
||||||
|
{
|
||||||
|
varType = typeof(float);
|
||||||
|
varDefaultValue = varInitExpression == null ? 0.0f : parser.Evaluate<float>(varInitExpression);
|
||||||
|
}
|
||||||
|
else if (varTypeName == "bool")
|
||||||
|
{
|
||||||
|
varType = typeof(bool);
|
||||||
|
varDefaultValue = varInitExpression == null ? false : parser.Evaluate<bool>(varInitExpression);
|
||||||
|
}
|
||||||
|
else if (varTypeName == "var")
|
||||||
|
{
|
||||||
|
varType = typeof(object);
|
||||||
|
varDefaultValue = varInitExpression == null ? new object() : parser.Evaluate(varInitExpression);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"Unsupported variable type '{varTypeName}'.", context.CurrentRuntimePointer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (context.CurrentLocalSpaceVariableNames.Peek().Contains(varName) == false)
|
||||||
|
{
|
||||||
|
context.Variables.Add(varName, new(varType, varDefaultValue));
|
||||||
|
parser.context.Variables[varName] = varDefaultValue;
|
||||||
|
context.CurrentLocalSpaceVariableNames.Peek().Add(varName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"Variable '{varName}' already defined on this namespace.", context.CurrentRuntimePointer);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
DoRunner/EnterNamedSpaceRunner.cs
Normal file
20
DoRunner/EnterNamedSpaceRunner.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class EnterNamedSpaceRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[return: MaybeNull]
|
||||||
|
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
context.CurrentRuntimePointer = context.NamespaceLayer[context.NamespaceLabels[sentence.content]];
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
28
DoRunner/EnterNamespaceRunner.cs
Normal file
28
DoRunner/EnterNamespaceRunner.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class EnterNamespaceRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[return: MaybeNull]
|
||||||
|
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
// 准备记录当前命名空间中定义的变量, 清空上层命名空间的变量
|
||||||
|
context.CurrentLocalSpaceVariableNames.Push(new());
|
||||||
|
// 更新变量值
|
||||||
|
foreach (var (varName, varValue) in parser.context.Variables)
|
||||||
|
{
|
||||||
|
context.Variables.SetValue(varName, varValue);
|
||||||
|
}
|
||||||
|
// 压栈
|
||||||
|
context.RuntimePointerStack.Push(context.CurrentRuntimePointer);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
DoRunner/ExitNamespaceRunner.cs
Normal file
33
DoRunner/ExitNamespaceRunner.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class ExitNamespaceRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[return: MaybeNull]
|
||||||
|
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
// 移除当前命名空间的变量
|
||||||
|
foreach (var local in context.CurrentLocalSpaceVariableNames.Peek())
|
||||||
|
{
|
||||||
|
context.Variables.Remove(local);
|
||||||
|
parser.context.Variables.Remove(local);
|
||||||
|
}
|
||||||
|
// 还原上层命名空间的变量
|
||||||
|
foreach (var local in context.CurrentLocalSpaceVariableNames.Peek())
|
||||||
|
{
|
||||||
|
parser.context.Variables[local] = context.Variables[local].data;
|
||||||
|
}
|
||||||
|
context.CurrentLocalSpaceVariableNames.Pop();
|
||||||
|
// 弹栈
|
||||||
|
context.RuntimePointerStack.Pop();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
DoRunner/ExpressionRunner.cs
Normal file
19
DoRunner/ExpressionRunner.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class ExpressionRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
parser.Compile(sentence.content);
|
||||||
|
}
|
||||||
|
|
||||||
|
[return: MaybeNull]
|
||||||
|
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
return parser.Evaluate(sentence.content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
DoRunner/GoToRunner.cs
Normal file
52
DoRunner/GoToRunner.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class GoToRunner : JumpRuntimePointerRunner
|
||||||
|
{
|
||||||
|
public override void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
parser.Compile<bool>(sentence.info[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[return: MaybeNull]
|
||||||
|
public override object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
// 检查并跳转到指定标签
|
||||||
|
if (parser.Evaluate<bool>(sentence.info[0]))
|
||||||
|
{
|
||||||
|
if (context.Labels.TryGetValue(sentence.content, out var labelPointer))
|
||||||
|
{
|
||||||
|
context.GotoPointerStack.Push(context.CurrentRuntimePointer);
|
||||||
|
DoJumpRuntimePointer(parser, labelPointer, context);
|
||||||
|
}
|
||||||
|
else if (context.NamespaceLabels.TryGetValue(sentence.content, out labelPointer))
|
||||||
|
{
|
||||||
|
int current = context.CurrentRuntimePointer;
|
||||||
|
//DoEnterNamespace(parser);
|
||||||
|
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
|
||||||
|
context.CurrentRuntimePointer = labelPointer;
|
||||||
|
for (int e = context.NamespaceLayer[context.NamespaceLabels[sentence.content]]; ;)
|
||||||
|
{
|
||||||
|
context.RunNextStep(parser);
|
||||||
|
if (context.CurrentRuntimePointer >= context.Sentences.Length)
|
||||||
|
break;
|
||||||
|
else if (context.CurrentRuntimePointer == e)
|
||||||
|
break;
|
||||||
|
else
|
||||||
|
context.CurrentRuntimePointer++;
|
||||||
|
}
|
||||||
|
//context.DoExitNamespace(parser);
|
||||||
|
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||||
|
context.CurrentRuntimePointer = current;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"Label '{sentence.content}' not found.", context.CurrentRuntimePointer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
64
DoRunner/JumpRuntimePointerRunner.cs
Normal file
64
DoRunner/JumpRuntimePointerRunner.cs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public abstract class JumpRuntimePointerRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
protected static void DoJumpRuntimePointer(ExpressionParser parser, int target, RScriptContext context)
|
||||||
|
{
|
||||||
|
int currentPointer = context.CurrentRuntimePointer;
|
||||||
|
bool isForwardMove = target > context.CurrentRuntimePointer;
|
||||||
|
int step = isForwardMove ? 1 : -1;
|
||||||
|
int depth = 0;
|
||||||
|
int lastLayer = 0;
|
||||||
|
for (; context.CurrentRuntimePointer != target; context.CurrentRuntimePointer += step)
|
||||||
|
{
|
||||||
|
if (context.CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace)
|
||||||
|
{
|
||||||
|
if (isForwardMove)
|
||||||
|
lastLayer--;
|
||||||
|
else
|
||||||
|
lastLayer++;
|
||||||
|
}
|
||||||
|
else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
|
||||||
|
{
|
||||||
|
if (isForwardMove)
|
||||||
|
lastLayer++;
|
||||||
|
else
|
||||||
|
lastLayer--;
|
||||||
|
}
|
||||||
|
depth = lastLayer < depth ? lastLayer : depth;
|
||||||
|
}
|
||||||
|
// 对上层的最深影响
|
||||||
|
for (; depth < 0; depth++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"Jump pointer with error", currentPointer, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 恢复正确的层数
|
||||||
|
for (int i = depth, e = lastLayer; i < e; i++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"Jump pointer with error", currentPointer, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||||
|
[return: MaybeNull]
|
||||||
|
public abstract object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||||
|
}
|
||||||
|
}
|
@@ -4,15 +4,28 @@ namespace Convention.RScript.Matcher
|
|||||||
{
|
{
|
||||||
public class DefineVariableMatcher : IRSentenceMatcher
|
public class DefineVariableMatcher : IRSentenceMatcher
|
||||||
{
|
{
|
||||||
|
private readonly Regex DefineVariableRegex = new(@"(string|int|double|float|bool|var)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.+)");
|
||||||
|
private readonly Regex DeclareVariableRegex = new(@"(string|int|double|float|bool|var)\s+([a-zA-Z_][a-zA-Z0-9_]*)");
|
||||||
|
|
||||||
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_]*)");
|
|
||||||
var DefineVariableMatch = DefineVariableRegex.Match(expression);
|
|
||||||
if (DefineVariableMatch.Success)
|
|
||||||
{
|
{
|
||||||
sentence.mode = RScriptSentence.Mode.DefineVariable;
|
var DefineVariableMatch = DefineVariableRegex.Match(expression);
|
||||||
sentence.info = new() { DefineVariableMatch.Groups[1].Value, DefineVariableMatch.Groups[2].Value };
|
if (DefineVariableMatch.Success)
|
||||||
return true;
|
{
|
||||||
|
sentence.mode = RScriptSentence.Mode.DefineVariable;
|
||||||
|
sentence.info = new[] { DefineVariableMatch.Groups[1].Value, DefineVariableMatch.Groups[2].Value, DefineVariableMatch.Groups[3].Value };
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var DeclareVariableMatch = DeclareVariableRegex.Match(expression);
|
||||||
|
if (DeclareVariableMatch.Success)
|
||||||
|
{
|
||||||
|
sentence.mode = RScriptSentence.Mode.DefineVariable;
|
||||||
|
sentence.info = new[] { DeclareVariableMatch.Groups[1].Value, DeclareVariableMatch.Groups[2].Value, null };
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -13,7 +13,7 @@ namespace Convention.RScript.Matcher
|
|||||||
{
|
{
|
||||||
sentence.mode = RScriptSentence.Mode.Goto;
|
sentence.mode = RScriptSentence.Mode.Goto;
|
||||||
sentence.content = GotoMatch.Groups[2].Value;
|
sentence.content = GotoMatch.Groups[2].Value;
|
||||||
sentence.info = new() { GotoMatch.Groups[1].Value, GotoMatch.Groups[2].Value };
|
sentence.info = new[] { GotoMatch.Groups[1].Value, GotoMatch.Groups[2].Value };
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
using Flee.PublicTypes;
|
using Flee.PublicTypes;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Convention.RScript
|
namespace Convention.RScript
|
||||||
{
|
{
|
||||||
@@ -95,6 +97,7 @@ namespace Convention.RScript.Parser
|
|||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readonly Dictionary<string, Type> CompileGenericExpressionTypen = new();
|
||||||
private readonly Dictionary<string, IExpression> CompileGenericExpression = new();
|
private readonly Dictionary<string, IExpression> CompileGenericExpression = new();
|
||||||
private readonly Dictionary<string, IDynamicExpression> CompileDynamicExpression = new();
|
private readonly Dictionary<string, IDynamicExpression> CompileDynamicExpression = new();
|
||||||
|
|
||||||
@@ -110,9 +113,7 @@ namespace Convention.RScript.Parser
|
|||||||
{
|
{
|
||||||
return (result as IGenericExpression<T>).Evaluate();
|
return (result as IGenericExpression<T>).Evaluate();
|
||||||
}
|
}
|
||||||
var compile = context.CompileGeneric<T>(expression);
|
return Compile<T>(expression).Evaluate();
|
||||||
CompileGenericExpression[expression] = compile;
|
|
||||||
return compile.Evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public object Evaluate(string expression)
|
public object Evaluate(string expression)
|
||||||
@@ -121,9 +122,77 @@ namespace Convention.RScript.Parser
|
|||||||
{
|
{
|
||||||
return result.Evaluate();
|
return result.Evaluate();
|
||||||
}
|
}
|
||||||
|
return Compile(expression).Evaluate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IGenericExpression<T> Compile<T>(string expression)
|
||||||
|
{
|
||||||
|
var compile = context.CompileGeneric<T>(expression);
|
||||||
|
CompileGenericExpression[expression] = compile;
|
||||||
|
CompileGenericExpressionTypen[expression] = typeof(T);
|
||||||
|
return compile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IDynamicExpression Compile(string expression)
|
||||||
|
{
|
||||||
var compile = context.CompileDynamic(expression);
|
var compile = context.CompileDynamic(expression);
|
||||||
CompileDynamicExpression[expression] = compile;
|
CompileDynamicExpression[expression] = compile;
|
||||||
return compile.Evaluate();
|
return compile;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public struct SerializableParser
|
||||||
|
{
|
||||||
|
public Tuple<string, string>[] CompileGenericExpression;
|
||||||
|
public string[] CompileDynamicExpression;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SerializableParser Serialize()
|
||||||
|
{
|
||||||
|
return new()
|
||||||
|
{
|
||||||
|
CompileGenericExpression = (from key in CompileGenericExpression.Keys select Tuple.Create(CompileGenericExpressionTypen[key].Name, key)).ToArray(),
|
||||||
|
CompileDynamicExpression = CompileDynamicExpression.Keys.ToArray()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Deserialize(SerializableParser data)
|
||||||
|
{
|
||||||
|
foreach (var (type, expr) in data.CompileGenericExpression)
|
||||||
|
{
|
||||||
|
if (type == nameof(String))
|
||||||
|
{
|
||||||
|
this.Compile<string>(expr);
|
||||||
|
}
|
||||||
|
else if (type == nameof(Single))
|
||||||
|
{
|
||||||
|
this.Compile<float>(expr);
|
||||||
|
}
|
||||||
|
else if (type == nameof(Double))
|
||||||
|
{
|
||||||
|
this.Compile<double>(expr);
|
||||||
|
}
|
||||||
|
else if (type == nameof(Int32))
|
||||||
|
{
|
||||||
|
this.Compile<int>(expr);
|
||||||
|
}
|
||||||
|
else if (type == nameof(Boolean))
|
||||||
|
{
|
||||||
|
this.Compile<bool>(expr);
|
||||||
|
}
|
||||||
|
else if (type == nameof(Object))
|
||||||
|
{
|
||||||
|
this.Compile<object>(expr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new NotSupportedException($"Unsupported expression type: {type}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (var expr in data.CompileDynamicExpression)
|
||||||
|
{
|
||||||
|
this.Compile(expr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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) { }
|
||||||
|
@@ -25,15 +25,21 @@ namespace Convention.RScript
|
|||||||
internalData = value;
|
internalData = value;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
internalData = Convert.ChangeType(value, type);
|
if (value == null)
|
||||||
|
{
|
||||||
|
if (type.IsClass)
|
||||||
|
internalData = null;
|
||||||
|
else
|
||||||
|
internalData = Activator.CreateInstance(type);
|
||||||
|
}
|
||||||
|
else if (type == typeof(object) || type == value.GetType())
|
||||||
|
internalData = value;
|
||||||
|
else
|
||||||
|
internalData = Convert.ChangeType(value, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private object internalData;
|
private object internalData;
|
||||||
|
|
||||||
public RScriptVariableEntry(object data) : this()
|
|
||||||
{
|
|
||||||
this.data = data;
|
|
||||||
}
|
|
||||||
public RScriptVariableEntry(Type type, object data) : this()
|
public RScriptVariableEntry(Type type, object data) : this()
|
||||||
{
|
{
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
@@ -1,14 +1,19 @@
|
|||||||
using Convention.RScript.Matcher;
|
using Convention.RScript.Matcher;
|
||||||
using Convention.RScript.Parser;
|
using Convention.RScript.Parser;
|
||||||
|
using Convention.RScript.Runner;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using static Convention.RScript.RScriptContext;
|
||||||
|
|
||||||
namespace Convention.RScript
|
namespace Convention.RScript
|
||||||
{
|
{
|
||||||
|
[Serializable]
|
||||||
public struct RScriptSentence
|
public struct RScriptSentence
|
||||||
{
|
{
|
||||||
|
[Serializable]
|
||||||
public enum Mode
|
public enum Mode
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -16,7 +21,7 @@ namespace Convention.RScript
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
Expression,
|
Expression,
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义变量, 格式: 类型 变量名
|
/// 定义变量, 格式: 类型 变量名 [=Expression]
|
||||||
/// <para>类型支持: string, int, double, float, bool, var</para>
|
/// <para>类型支持: string, int, double, float, bool, var</para>
|
||||||
/// <para>每层命名空间中不可重复定义变量, 不可使用未定义的变量, 不存在时会自动向上查找上级空间的变量</para>
|
/// <para>每层命名空间中不可重复定义变量, 不可使用未定义的变量, 不存在时会自动向上查找上级空间的变量</para>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -55,12 +60,12 @@ namespace Convention.RScript
|
|||||||
}
|
}
|
||||||
|
|
||||||
public string content;
|
public string content;
|
||||||
public List<string> info;
|
public string[] info;
|
||||||
public Mode mode;
|
public Mode mode;
|
||||||
|
|
||||||
public override string ToString()
|
public override readonly string ToString()
|
||||||
{
|
{
|
||||||
return $"{mode.ToString()}/: {content}";
|
return $"{mode}: {content}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,14 +74,44 @@ namespace Convention.RScript
|
|||||||
bool Match(string expression, ref RScriptSentence sentence);
|
bool Match(string expression, ref RScriptSentence sentence);
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class RScriptContext
|
public interface IRSentenceRunner
|
||||||
{
|
{
|
||||||
public readonly RScriptImportClass Import;
|
[return: MaybeNull] object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||||
public readonly RScriptVariables Variables;
|
void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||||
private readonly RScriptSentence[] Sentences;
|
}
|
||||||
private readonly Dictionary<string, int> Labels = new();
|
|
||||||
private readonly Dictionary<int, int> NamespaceLayer = new();
|
public interface IBasicRScriptContext
|
||||||
private readonly Dictionary<string, int> NamespaceLabels = new();
|
{
|
||||||
|
RScriptImportClass Import { get; }
|
||||||
|
RScriptVariables Variables { get; }
|
||||||
|
RScriptSentence[] Sentences { get; }
|
||||||
|
int CurrentRuntimePointer { get; }
|
||||||
|
RScriptSentence CurrentSentence { get; }
|
||||||
|
|
||||||
|
Dictionary<string, RScriptVariableEntry> GetCurrentVariables();
|
||||||
|
void Run(ExpressionParser parser);
|
||||||
|
IEnumerator RunAsync(ExpressionParser parser);
|
||||||
|
SerializableClass Compile(ExpressionParser parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class RScriptContext : IBasicRScriptContext
|
||||||
|
{
|
||||||
|
public RScriptImportClass Import { get;private set; }
|
||||||
|
public RScriptVariables Variables { get;private set; }
|
||||||
|
public RScriptSentence[] Sentences { get; private set; }
|
||||||
|
internal readonly Dictionary<string, int> Labels = new();
|
||||||
|
internal readonly Dictionary<int, int> NamespaceLayer = new();
|
||||||
|
internal readonly Dictionary<string, int> NamespaceLabels = new();
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public struct SerializableClass
|
||||||
|
{
|
||||||
|
public RScriptSentence[] Sentences;
|
||||||
|
public Tuple<string, int>[] Labels;
|
||||||
|
public Tuple<int, int>[] NamespaceLayer;
|
||||||
|
public Tuple<string, int>[] NamespaceLabels;
|
||||||
|
public ExpressionParser.SerializableParser CompileParser;
|
||||||
|
}
|
||||||
|
|
||||||
public List<IRSentenceMatcher> SentenceParser = new()
|
public List<IRSentenceMatcher> SentenceParser = new()
|
||||||
{
|
{
|
||||||
@@ -88,6 +123,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()
|
||||||
@@ -150,253 +197,77 @@ namespace Convention.RScript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public RScriptContext(string[] expressions, RScriptImportClass import = null, RScriptVariables variables = null)
|
public class BuildInContext
|
||||||
|
{
|
||||||
|
private RScriptContext context;
|
||||||
|
public BuildInContext(RScriptContext context)
|
||||||
|
{
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ExistVar(string name)
|
||||||
|
{
|
||||||
|
return context.Variables.ContainsKey(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ExistNamespace(string name)
|
||||||
|
{
|
||||||
|
return context.NamespaceLabels.ContainsKey(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ExistLabel(string name)
|
||||||
|
{
|
||||||
|
return context.Labels.ContainsKey(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.Variables.Add("context", new(typeof(object), new BuildInContext(this)));
|
||||||
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 RScriptContext(SerializableClass data,
|
||||||
|
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.Variables.Add("context", new(typeof(object), new BuildInContext(this)));
|
||||||
|
|
||||||
|
this.Sentences = data.Sentences;
|
||||||
|
this.Labels = (from item in data.Labels select item).ToDictionary(t => t.Item1, t => t.Item2);
|
||||||
|
this.NamespaceLayer = (from item in data.NamespaceLayer select item).ToDictionary(t => t.Item1, t => t.Item2);
|
||||||
|
this.NamespaceLabels = (from item in data.NamespaceLabels select item).ToDictionary(t => t.Item1, t => t.Item2);
|
||||||
|
}
|
||||||
|
|
||||||
public RScriptSentence CurrentSentence => Sentences[CurrentRuntimePointer];
|
public RScriptSentence CurrentSentence => Sentences[CurrentRuntimePointer];
|
||||||
|
|
||||||
private void DoDefineVariable(ExpressionParser parser, RScriptSentence sentence)
|
public int StepCount { get; private set; }
|
||||||
{
|
|
||||||
// 定义变量
|
|
||||||
var varTypeName = sentence.info[0];
|
|
||||||
var varName = sentence.info[1];
|
|
||||||
Type varType;
|
|
||||||
object varDefaultValue;
|
|
||||||
{
|
|
||||||
if (varTypeName == "string")
|
|
||||||
{
|
|
||||||
varType = typeof(string);
|
|
||||||
varDefaultValue = string.Empty;
|
|
||||||
}
|
|
||||||
else if (varTypeName == "int")
|
|
||||||
{
|
|
||||||
varType = typeof(int);
|
|
||||||
varDefaultValue = 0;
|
|
||||||
}
|
|
||||||
else if (varTypeName == "double")
|
|
||||||
{
|
|
||||||
varType = typeof(double);
|
|
||||||
varDefaultValue = 0.0;
|
|
||||||
}
|
|
||||||
else if (varTypeName == "float")
|
|
||||||
{
|
|
||||||
varType = typeof(float);
|
|
||||||
varDefaultValue = 0.0f;
|
|
||||||
}
|
|
||||||
else if (varTypeName == "bool")
|
|
||||||
{
|
|
||||||
varType = typeof(bool);
|
|
||||||
varDefaultValue = false;
|
|
||||||
}
|
|
||||||
else if (varTypeName == "var")
|
|
||||||
{
|
|
||||||
varType = typeof(object);
|
|
||||||
varDefaultValue = new object();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new RScriptRuntimeException($"Unsupported variable type '{varTypeName}'.", CurrentRuntimePointer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (CurrentLocalSpaceVariableNames.Peek().Contains(varName) == false)
|
|
||||||
{
|
|
||||||
Variables.Add(varName, new(varType, varDefaultValue));
|
|
||||||
parser.context.Variables[varName] = varDefaultValue;
|
|
||||||
CurrentLocalSpaceVariableNames.Peek().Add(varName);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new RScriptRuntimeException($"Variable '{varName}' already defined on this namespace.", CurrentRuntimePointer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DoEnterNamespace(ExpressionParser parser)
|
internal object RunNextStep(ExpressionParser parser)
|
||||||
{
|
|
||||||
// 准备记录当前命名空间中定义的变量, 清空上层命名空间的变量
|
|
||||||
CurrentLocalSpaceVariableNames.Push(new());
|
|
||||||
// 更新变量值
|
|
||||||
foreach (var (varName, varValue) in parser.context.Variables)
|
|
||||||
{
|
|
||||||
Variables.SetValue(varName, varValue);
|
|
||||||
}
|
|
||||||
// 压栈
|
|
||||||
RuntimePointerStack.Push(CurrentRuntimePointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DoExitNamespace(ExpressionParser parser)
|
|
||||||
{
|
|
||||||
// 移除当前命名空间的变量
|
|
||||||
foreach (var local in CurrentLocalSpaceVariableNames.Peek())
|
|
||||||
{
|
|
||||||
Variables.Remove(local);
|
|
||||||
parser.context.Variables.Remove(local);
|
|
||||||
}
|
|
||||||
// 还原上层命名空间的变量
|
|
||||||
foreach (var local in CurrentLocalSpaceVariableNames.Peek())
|
|
||||||
{
|
|
||||||
parser.context.Variables[local] = Variables[local].data;
|
|
||||||
}
|
|
||||||
CurrentLocalSpaceVariableNames.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)
|
|
||||||
{
|
|
||||||
// 检查并跳转到指定标签
|
|
||||||
if (parser.Evaluate<bool>(sentence.info[0]))
|
|
||||||
{
|
|
||||||
if (Labels.TryGetValue(sentence.content, out var labelPointer))
|
|
||||||
{
|
|
||||||
GotoPointerStack.Push(CurrentRuntimePointer);
|
|
||||||
DoJumpRuntimePointer(parser, labelPointer);
|
|
||||||
}
|
|
||||||
else if (NamespaceLabels.TryGetValue(sentence.content, out labelPointer))
|
|
||||||
{
|
|
||||||
int current = CurrentRuntimePointer;
|
|
||||||
DoEnterNamespace(parser);
|
|
||||||
CurrentRuntimePointer = labelPointer;
|
|
||||||
for (int e = NamespaceLayer[NamespaceLabels[sentence.content]]; ;)
|
|
||||||
{
|
|
||||||
RunNextStep(parser);
|
|
||||||
if (CurrentRuntimePointer >= Sentences.Length)
|
|
||||||
break ;
|
|
||||||
else if (CurrentRuntimePointer == e)
|
|
||||||
break;
|
|
||||||
else
|
|
||||||
CurrentRuntimePointer++;
|
|
||||||
}
|
|
||||||
DoExitNamespace(parser);
|
|
||||||
CurrentRuntimePointer = current;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new RScriptRuntimeException($"Label '{sentence.content}' not found.", CurrentRuntimePointer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DoBreakpoint(ExpressionParser parser, RScriptSentence sentence)
|
|
||||||
{
|
|
||||||
// 检查并跳转到当前命名空间的结束位置
|
|
||||||
if (parser.Evaluate<bool>(sentence.content))
|
|
||||||
{
|
|
||||||
if (RuntimePointerStack.Count == 0)
|
|
||||||
{
|
|
||||||
CurrentRuntimePointer = Sentences.Length;
|
|
||||||
}
|
|
||||||
else if (NamespaceLayer.TryGetValue(RuntimePointerStack.Peek(), out var exitPointer))
|
|
||||||
{
|
|
||||||
CurrentRuntimePointer = exitPointer;
|
|
||||||
DoExitNamespace(parser);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new RScriptRuntimeException($"No namespace to break.", CurrentRuntimePointer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DoBackpoint(ExpressionParser parser, RScriptSentence sentence)
|
|
||||||
{
|
|
||||||
// 检查并跳转到上次跳转的位置
|
|
||||||
if (parser.Evaluate<bool>(sentence.content))
|
|
||||||
{
|
|
||||||
if (GotoPointerStack.Count == 0)
|
|
||||||
{
|
|
||||||
throw new RScriptRuntimeException($"No position to back.", CurrentRuntimePointer);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
DoJumpRuntimePointer(parser, GotoPointerStack.Pop());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DoEnterNamedSpace(RScriptSentence sentence)
|
|
||||||
{
|
|
||||||
CurrentRuntimePointer = NamespaceLayer[NamespaceLabels[sentence.content]];
|
|
||||||
}
|
|
||||||
|
|
||||||
private object RunNextStep(ExpressionParser parser)
|
|
||||||
{
|
{
|
||||||
|
StepCount++;
|
||||||
var sentence = CurrentSentence;
|
var sentence = CurrentSentence;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
switch (sentence.mode)
|
return SentenceRunners.TryGetValue(sentence.mode, out var runner) ? runner.Run(parser, sentence, this) : null;
|
||||||
{
|
|
||||||
case RScriptSentence.Mode.Expression:
|
|
||||||
return parser.Evaluate(sentence.content);
|
|
||||||
case RScriptSentence.Mode.DefineVariable:
|
|
||||||
{
|
|
||||||
DoDefineVariable(parser, sentence);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case RScriptSentence.Mode.EnterNamespace:
|
|
||||||
{
|
|
||||||
DoEnterNamespace(parser);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case RScriptSentence.Mode.ExitNamespace:
|
|
||||||
{
|
|
||||||
DoExitNamespace(parser);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case RScriptSentence.Mode.Goto:
|
|
||||||
{
|
|
||||||
DoGoto(parser, sentence);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case RScriptSentence.Mode.Breakpoint:
|
|
||||||
{
|
|
||||||
DoBreakpoint(parser, sentence);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case RScriptSentence.Mode.Backpoint:
|
|
||||||
{
|
|
||||||
DoBackpoint(parser, sentence);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case RScriptSentence.Mode.NamedSpace:
|
|
||||||
{
|
|
||||||
DoEnterNamedSpace(sentence);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// Do nothing
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (RScriptRuntimeException)
|
catch (RScriptException)
|
||||||
{
|
{
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
@@ -404,13 +275,12 @@ namespace Convention.RScript
|
|||||||
{
|
{
|
||||||
throw new RScriptRuntimeException($"Runtime error: {ex.Message}", CurrentRuntimePointer, ex);
|
throw new RScriptRuntimeException($"Runtime error: {ex.Message}", CurrentRuntimePointer, ex);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly Stack<int> RuntimePointerStack = new();
|
internal readonly Stack<int> RuntimePointerStack = new();
|
||||||
private readonly Stack<int> GotoPointerStack = new();
|
internal readonly Stack<int> GotoPointerStack = new();
|
||||||
private int CurrentRuntimePointer = 0;
|
public int CurrentRuntimePointer { get; internal set; } = 0;
|
||||||
private readonly Stack<HashSet<string>> CurrentLocalSpaceVariableNames = new();
|
internal readonly Stack<HashSet<string>> CurrentLocalSpaceVariableNames = new();
|
||||||
|
|
||||||
public Dictionary<string, RScriptVariableEntry> GetCurrentVariables()
|
public Dictionary<string, RScriptVariableEntry> GetCurrentVariables()
|
||||||
{
|
{
|
||||||
@@ -422,18 +292,26 @@ namespace Convention.RScript
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Run(ExpressionParser parser)
|
private void BeforeRun(ExpressionParser parser)
|
||||||
{
|
{
|
||||||
|
StepCount = 0;
|
||||||
CurrentLocalSpaceVariableNames.Clear();
|
CurrentLocalSpaceVariableNames.Clear();
|
||||||
RuntimePointerStack.Clear();
|
RuntimePointerStack.Clear();
|
||||||
GotoPointerStack.Clear();
|
GotoPointerStack.Clear();
|
||||||
CurrentLocalSpaceVariableNames.Clear();
|
CurrentLocalSpaceVariableNames.Clear();
|
||||||
CurrentLocalSpaceVariableNames.Push(new());
|
CurrentLocalSpaceVariableNames.Push(new());
|
||||||
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
|
foreach (var staticType in Import)
|
||||||
{
|
{
|
||||||
RunNextStep(parser);
|
parser.context.Imports.AddType(staticType);
|
||||||
}
|
}
|
||||||
// 更新上下文变量
|
foreach (var (name, varObject) in Variables)
|
||||||
|
{
|
||||||
|
parser.context.Variables[name] = varObject.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AfterRun(ExpressionParser parser)
|
||||||
|
{
|
||||||
foreach (var (varName, varValue) in parser.context.Variables)
|
foreach (var (varName, varValue) in parser.context.Variables)
|
||||||
{
|
{
|
||||||
if (Variables.ContainsKey(varName))
|
if (Variables.ContainsKey(varName))
|
||||||
@@ -441,13 +319,19 @@ namespace Convention.RScript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Run(ExpressionParser parser)
|
||||||
|
{
|
||||||
|
BeforeRun(parser);
|
||||||
|
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
|
||||||
|
{
|
||||||
|
RunNextStep(parser);
|
||||||
|
}
|
||||||
|
AfterRun(parser);
|
||||||
|
}
|
||||||
|
|
||||||
public IEnumerator RunAsync(ExpressionParser parser)
|
public IEnumerator RunAsync(ExpressionParser parser)
|
||||||
{
|
{
|
||||||
CurrentLocalSpaceVariableNames.Clear();
|
BeforeRun(parser);
|
||||||
RuntimePointerStack.Clear();
|
|
||||||
GotoPointerStack.Clear();
|
|
||||||
CurrentLocalSpaceVariableNames.Clear();
|
|
||||||
CurrentLocalSpaceVariableNames.Push(new());
|
|
||||||
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
|
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
|
||||||
{
|
{
|
||||||
var ret = RunNextStep(parser);
|
var ret = RunNextStep(parser);
|
||||||
@@ -457,12 +341,25 @@ namespace Convention.RScript
|
|||||||
}
|
}
|
||||||
yield return null;
|
yield return null;
|
||||||
}
|
}
|
||||||
// 更新上下文变量
|
AfterRun(parser);
|
||||||
foreach (var (varName, varValue) in parser.context.Variables)
|
}
|
||||||
|
|
||||||
|
public SerializableClass Compile(ExpressionParser parser)
|
||||||
|
{
|
||||||
|
BeforeRun(parser);
|
||||||
|
foreach (var item in Sentences)
|
||||||
{
|
{
|
||||||
if (Variables.ContainsKey(varName))
|
if (SentenceRunners.TryGetValue(item.mode, out var runner))
|
||||||
Variables.SetValue(varName, varValue);
|
runner.Compile(parser, item, this);
|
||||||
}
|
}
|
||||||
|
return new SerializableClass()
|
||||||
|
{
|
||||||
|
CompileParser = parser.Serialize(),
|
||||||
|
Labels = (from item in Labels select Tuple.Create(item.Key, item.Value)).ToArray(),
|
||||||
|
NamespaceLayer = (from item in NamespaceLayer select Tuple.Create(item.Key, item.Value)).ToArray(),
|
||||||
|
NamespaceLabels = (from item in NamespaceLabels select Tuple.Create(item.Key, item.Value)).ToArray(),
|
||||||
|
Sentences = Sentences,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,13 +6,25 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using static Convention.RScript.RScriptContext;
|
||||||
|
|
||||||
namespace Convention.RScript
|
namespace Convention.RScript
|
||||||
{
|
{
|
||||||
public class RScriptEngine
|
public interface IRScriptEngine
|
||||||
|
{
|
||||||
|
IBasicRScriptContext context { get; }
|
||||||
|
|
||||||
|
Dictionary<string, RScriptVariableEntry> Run(string script, RScriptImportClass import = null, RScriptVariables variables = null);
|
||||||
|
IEnumerator RunAsync(string script, RScriptImportClass import = null, RScriptVariables variables = null);
|
||||||
|
SerializableClass Compile(string script, RScriptImportClass import = null, RScriptVariables variables = null);
|
||||||
|
Dictionary<string, RScriptVariableEntry> Run(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null);
|
||||||
|
IEnumerator RunAsync(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class RScriptEngine : IRScriptEngine
|
||||||
{
|
{
|
||||||
private ExpressionParser parser;
|
private ExpressionParser parser;
|
||||||
private RScriptContext context;
|
public IBasicRScriptContext context { get; private set; }
|
||||||
|
|
||||||
private IEnumerable<string> SplitScript(string script)
|
private IEnumerable<string> SplitScript(string script)
|
||||||
{
|
{
|
||||||
@@ -42,18 +54,10 @@ namespace Convention.RScript
|
|||||||
else if (c == '/' && i + 1 < e)
|
else if (c == '/' && i + 1 < e)
|
||||||
{
|
{
|
||||||
// Skip single-line comment
|
// Skip single-line comment
|
||||||
if (script[i + 1] == '/')
|
if (line[i + 1] == '/')
|
||||||
{
|
{
|
||||||
while (i < script.Length && script[i] != '\n')
|
PushBuilder();
|
||||||
i++;
|
break;
|
||||||
}
|
|
||||||
// Skip multi-line comment
|
|
||||||
else if (script[i + 1] == '*')
|
|
||||||
{
|
|
||||||
i += 2;
|
|
||||||
while (i + 1 < script.Length && !(script[i] == '*' && script[i + 1] == '/'))
|
|
||||||
i++;
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -62,24 +66,24 @@ namespace Convention.RScript
|
|||||||
}
|
}
|
||||||
else if (c == '#')
|
else if (c == '#')
|
||||||
{
|
{
|
||||||
// Skip single-line comment
|
PushBuilder();
|
||||||
while (i < script.Length && script[i] != '\n')
|
break;
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
else if (c == '\"')
|
else if (c == '\"')
|
||||||
{
|
{
|
||||||
|
builder.Append(c);
|
||||||
for (i++; i < e; i++)
|
for (i++; i < e; i++)
|
||||||
{
|
{
|
||||||
builder.Append(script[i]);
|
builder.Append(line[i]);
|
||||||
if (script[i] == '\"')
|
if (line[i] == '\"')
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (script[i] == '\\')
|
else if (line[i] == '\\')
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
if (i < e)
|
if (i < e)
|
||||||
builder.Append(script[i]);
|
builder.Append(line[i]);
|
||||||
else
|
else
|
||||||
throw new RScriptCompileException("Invalid escape sequence in string literal", lineIndex, i);
|
throw new RScriptCompileException("Invalid escape sequence in string literal", lineIndex, i);
|
||||||
}
|
}
|
||||||
@@ -90,14 +94,10 @@ namespace Convention.RScript
|
|||||||
PushBuilder();
|
PushBuilder();
|
||||||
statements.Add(c.ToString());
|
statements.Add(c.ToString());
|
||||||
}
|
}
|
||||||
else if (string.Compare("namespace", 0, script, i, "namespace".Length) == 0)
|
else if (string.Compare("namespace", 0, line, i, "namespace".Length) == 0)
|
||||||
{
|
{
|
||||||
builder.Append("namespace");
|
Regex regex = new(@"^\s*namespace\s*\([a-zA-Z_][a-zA-Z0-9_]*\)");
|
||||||
i += "namespace".Length;
|
var match = regex.Match(line);
|
||||||
if (i >= e)
|
|
||||||
throw new RScriptCompileException("Invalid namespace declaration", lineIndex, i);
|
|
||||||
Regex regex = new(@"^\s*\([a-zA-Z_][a-zA-Z0-9_]*\)");
|
|
||||||
var match = regex.Match(script, i);
|
|
||||||
if (match.Success)
|
if (match.Success)
|
||||||
{
|
{
|
||||||
builder.Append(match.Value);
|
builder.Append(match.Value);
|
||||||
@@ -117,20 +117,23 @@ namespace Convention.RScript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (builder.Length > 0)
|
PushBuilder();
|
||||||
{
|
|
||||||
PushBuilder();
|
|
||||||
}
|
|
||||||
|
|
||||||
return statements.Where(s => !string.IsNullOrWhiteSpace(s));
|
return statements.Where(s => !string.IsNullOrWhiteSpace(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IBasicRScriptContext CreateContext(string[] statements, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||||
|
{
|
||||||
|
return new RScriptContext(statements, import, variables);
|
||||||
|
}
|
||||||
|
private IBasicRScriptContext CreateContext(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||||
|
{
|
||||||
|
return new RScriptContext(data, import, variables);
|
||||||
|
}
|
||||||
public Dictionary<string, RScriptVariableEntry> Run(string script, RScriptImportClass import = null, RScriptVariables variables = null)
|
public Dictionary<string, RScriptVariableEntry> Run(string script, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||||
{
|
{
|
||||||
parser = new(new());
|
parser = new(new());
|
||||||
context = new(SplitScript(script).ToArray(), import, variables);
|
context = CreateContext(SplitScript(script).ToArray(), import, variables);
|
||||||
foreach (var type in context.Import)
|
|
||||||
parser.context.Imports.AddType(type);
|
|
||||||
context.Run(parser);
|
context.Run(parser);
|
||||||
return context.GetCurrentVariables();
|
return context.GetCurrentVariables();
|
||||||
}
|
}
|
||||||
@@ -138,9 +141,31 @@ namespace Convention.RScript
|
|||||||
public IEnumerator RunAsync(string script, RScriptImportClass import = null, RScriptVariables variables = null)
|
public IEnumerator RunAsync(string script, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||||
{
|
{
|
||||||
parser = new(new());
|
parser = new(new());
|
||||||
context = new(SplitScript(script).ToArray(), import, variables);
|
context = CreateContext(SplitScript(script).ToArray(), import, variables);
|
||||||
foreach (var type in context.Import)
|
return context.RunAsync(parser);
|
||||||
parser.context.Imports.AddType(type);
|
}
|
||||||
|
|
||||||
|
public SerializableClass Compile(string script, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||||
|
{
|
||||||
|
parser = new(new());
|
||||||
|
context = CreateContext(SplitScript(script).ToArray(), import, variables);
|
||||||
|
return context.Compile(parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dictionary<string, RScriptVariableEntry> Run(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||||
|
{
|
||||||
|
parser = new(new());
|
||||||
|
//parser.Deserialize(data.CompileParser);
|
||||||
|
context = CreateContext(data, import, variables);
|
||||||
|
context.Run(parser);
|
||||||
|
return context.GetCurrentVariables();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator RunAsync(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||||
|
{
|
||||||
|
parser = new(new());
|
||||||
|
//parser.Deserialize(data.CompileParser);
|
||||||
|
context = CreateContext(data, import, variables);
|
||||||
return context.RunAsync(parser);
|
return context.RunAsync(parser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
151
RScriptSerializer.cs
Normal file
151
RScriptSerializer.cs
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using static Convention.RScript.RScriptContext;
|
||||||
|
|
||||||
|
namespace Convention.RScript
|
||||||
|
{
|
||||||
|
public static class RScriptSerializer
|
||||||
|
{
|
||||||
|
public static byte[] SerializeClass(SerializableClass data)
|
||||||
|
{
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
using (var writer = new BinaryWriter(stream))
|
||||||
|
{
|
||||||
|
// 序列化 Sentences 数组
|
||||||
|
writer.Write(data.Sentences?.Length ?? 0);
|
||||||
|
if (data.Sentences != null)
|
||||||
|
{
|
||||||
|
foreach (var sentence in data.Sentences)
|
||||||
|
{
|
||||||
|
writer.Write(sentence.content ?? "");
|
||||||
|
writer.Write(sentence.info?.Length ?? 0);
|
||||||
|
if (sentence.info != null)
|
||||||
|
{
|
||||||
|
foreach (var info in sentence.info)
|
||||||
|
{
|
||||||
|
writer.Write(info ?? "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writer.Write((int)sentence.mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 序列化 Labels 数组
|
||||||
|
writer.Write(data.Labels?.Length ?? 0);
|
||||||
|
if (data.Labels != null)
|
||||||
|
{
|
||||||
|
foreach (var label in data.Labels)
|
||||||
|
{
|
||||||
|
writer.Write(label.Item1 ?? "");
|
||||||
|
writer.Write(label.Item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 序列化 NamespaceLayer 数组
|
||||||
|
writer.Write(data.NamespaceLayer?.Length ?? 0);
|
||||||
|
if (data.NamespaceLayer != null)
|
||||||
|
{
|
||||||
|
foreach (var layer in data.NamespaceLayer)
|
||||||
|
{
|
||||||
|
writer.Write(layer.Item1);
|
||||||
|
writer.Write(layer.Item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 序列化 NamespaceLabels 数组
|
||||||
|
writer.Write(data.NamespaceLabels?.Length ?? 0);
|
||||||
|
if (data.NamespaceLabels != null)
|
||||||
|
{
|
||||||
|
foreach (var nsLabel in data.NamespaceLabels)
|
||||||
|
{
|
||||||
|
writer.Write(nsLabel.Item1 ?? "");
|
||||||
|
writer.Write(nsLabel.Item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 这里需要根据 ExpressionParser.SerializableParser 的结构来序列化
|
||||||
|
// writer.Write(...); // CompileParser 的序列化
|
||||||
|
|
||||||
|
return stream.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SerializableClass DeserializeClass(byte[] data)
|
||||||
|
{
|
||||||
|
using (var stream = new MemoryStream(data))
|
||||||
|
using (var reader = new BinaryReader(stream))
|
||||||
|
{
|
||||||
|
var result = new SerializableClass();
|
||||||
|
|
||||||
|
// 反序列化 Sentences 数组
|
||||||
|
int sentencesLength = reader.ReadInt32();
|
||||||
|
if (sentencesLength > 0)
|
||||||
|
{
|
||||||
|
result.Sentences = new RScriptSentence[sentencesLength];
|
||||||
|
for (int i = 0; i < sentencesLength; i++)
|
||||||
|
{
|
||||||
|
var sentence = new RScriptSentence();
|
||||||
|
sentence.content = reader.ReadString();
|
||||||
|
|
||||||
|
int infoLength = reader.ReadInt32();
|
||||||
|
if (infoLength > 0)
|
||||||
|
{
|
||||||
|
sentence.info = new string[infoLength];
|
||||||
|
for (int j = 0; j < infoLength; j++)
|
||||||
|
{
|
||||||
|
sentence.info[j] = reader.ReadString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sentence.mode = (RScriptSentence.Mode)reader.ReadInt32();
|
||||||
|
result.Sentences[i] = sentence;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 反序列化 Labels 数组
|
||||||
|
int labelsLength = reader.ReadInt32();
|
||||||
|
if (labelsLength > 0)
|
||||||
|
{
|
||||||
|
result.Labels = new Tuple<string, int>[labelsLength];
|
||||||
|
for (int i = 0; i < labelsLength; i++)
|
||||||
|
{
|
||||||
|
string item1 = reader.ReadString();
|
||||||
|
int item2 = reader.ReadInt32();
|
||||||
|
result.Labels[i] = Tuple.Create(item1, item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 反序列化 NamespaceLayer 数组
|
||||||
|
int namespaceLayerLength = reader.ReadInt32();
|
||||||
|
if (namespaceLayerLength > 0)
|
||||||
|
{
|
||||||
|
result.NamespaceLayer = new Tuple<int, int>[namespaceLayerLength];
|
||||||
|
for (int i = 0; i < namespaceLayerLength; i++)
|
||||||
|
{
|
||||||
|
int item1 = reader.ReadInt32();
|
||||||
|
int item2 = reader.ReadInt32();
|
||||||
|
result.NamespaceLayer[i] = Tuple.Create(item1, item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 反序列化 NamespaceLabels 数组
|
||||||
|
int namespaceLabelsLength = reader.ReadInt32();
|
||||||
|
if (namespaceLabelsLength > 0)
|
||||||
|
{
|
||||||
|
result.NamespaceLabels = new Tuple<string, int>[namespaceLabelsLength];
|
||||||
|
for (int i = 0; i < namespaceLabelsLength; i++)
|
||||||
|
{
|
||||||
|
string item1 = reader.ReadString();
|
||||||
|
int item2 = reader.ReadInt32();
|
||||||
|
result.NamespaceLabels[i] = Tuple.Create(item1, item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 反序列化 CompileParser
|
||||||
|
// result.CompileParser = ...; // 根据具体结构实现
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user