Compare commits
8 Commits
4f358c9664
...
52e8e85542
| Author | SHA1 | Date | |
|---|---|---|---|
| 52e8e85542 | |||
| 97a6e4d76b | |||
| 15bdb6f8db | |||
| e2ab2a1077 | |||
| 3866cdd525 | |||
| 70051b46a5 | |||
| 35800776b9 | |||
| b8a87bae4c |
26
DoRunner/BackpointRunner.cs
Normal file
26
DoRunner/BackpointRunner.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class BackpointRunner : JumpRuntimePointerRunner
|
||||||
|
{
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
32
DoRunner/BreakpointRunner.cs
Normal file
32
DoRunner/BreakpointRunner.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript
|
||||||
|
{
|
||||||
|
public class BreakpointRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
[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;
|
||||||
|
//DoExitNamespace(parser);
|
||||||
|
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"No namespace to break.", context.CurrentRuntimePointer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
67
DoRunner/DefineVariableRunner.cs
Normal file
67
DoRunner/DefineVariableRunner.cs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class DefineVariableRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
DoRunner/EnterNamespaceRunner.cs
Normal file
23
DoRunner/EnterNamespaceRunner.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class EnterNamespaceRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
28
DoRunner/ExitNamespaceRunner.cs
Normal file
28
DoRunner/ExitNamespaceRunner.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class ExitNamespaceRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
DoRunner/ExpressionRunner.cs
Normal file
14
DoRunner/ExpressionRunner.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class ExpressionRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
[return: MaybeNull]
|
||||||
|
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||||
|
{
|
||||||
|
return parser.Evaluate(sentence.content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
47
DoRunner/GoToRunner.cs
Normal file
47
DoRunner/GoToRunner.cs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public class GoToRunner : JumpRuntimePointerRunner
|
||||||
|
{
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
65
DoRunner/JumpRuntimePointerRunner.cs
Normal file
65
DoRunner/JumpRuntimePointerRunner.cs
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
using Convention.RScript.Parser;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Runner
|
||||||
|
{
|
||||||
|
public abstract class JumpRuntimePointerRunner : IRSentenceRunner
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (insLayer > 0)
|
||||||
|
insLayer--;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
insLayer++;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
namespace Convention.RScript.Matcher
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace Convention.RScript.Matcher
|
||||||
{
|
{
|
||||||
public class NamespaceMater : IRSentenceMatcher
|
public class NamespaceMater : IRSentenceMatcher
|
||||||
{
|
{
|
||||||
@@ -14,6 +16,21 @@
|
|||||||
sentence.mode = RScriptSentence.Mode.ExitNamespace;
|
sentence.mode = RScriptSentence.Mode.ExitNamespace;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
else if (expression.StartsWith("namespace"))
|
||||||
|
{
|
||||||
|
sentence.mode = RScriptSentence.Mode.NamedSpace;
|
||||||
|
Regex regex = new(@"namespace\s*\(([a-zA-Z_][a-zA-Z0-9_]*)\)");
|
||||||
|
var match = regex.Match(expression);
|
||||||
|
if (match.Success)
|
||||||
|
{
|
||||||
|
sentence.content = match.Groups[1].Value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException("Invalid namespace declaration", -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,62 @@
|
|||||||
using Flee.PublicTypes;
|
using Flee.PublicTypes;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Convention.RScript.Parser
|
namespace Convention.RScript
|
||||||
{
|
{
|
||||||
|
public static class ExpressionMath
|
||||||
|
{
|
||||||
|
public static int Sign(double value, double accuracy = ExpressionExtension.DefaultDoubleAccuracy)
|
||||||
|
{
|
||||||
|
if (value.IsCloseToZero(accuracy))
|
||||||
|
return 0;
|
||||||
|
return value > 0 ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int Compare(double value1, double value2, double accuracy = ExpressionExtension.DefaultDoubleAccuracy)
|
||||||
|
{
|
||||||
|
if (value1.IsClose(value2, accuracy))
|
||||||
|
return 0;
|
||||||
|
return value1 > value2 ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsBetween(double value, double minValue, double maxValue, double accuracy = ExpressionExtension.DefaultDoubleAccuracy)
|
||||||
|
{
|
||||||
|
return Compare(value, minValue, accuracy) >= 0 && Compare(value, maxValue, accuracy) <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsBetweenExclusive(double value, double minValue, double maxValue, double accuracy = ExpressionExtension.DefaultDoubleAccuracy)
|
||||||
|
{
|
||||||
|
return Compare(value, minValue, accuracy) > 0 && Compare(value, maxValue, accuracy) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int ToInt(double value)
|
||||||
|
{
|
||||||
|
return (int)value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int ToInt(float value)
|
||||||
|
{
|
||||||
|
return (int)value;
|
||||||
|
}
|
||||||
|
public static int ToInt(int value)
|
||||||
|
{
|
||||||
|
return (int)value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double ToDouble(float value)
|
||||||
|
{
|
||||||
|
return (double)value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double ToDouble(int value)
|
||||||
|
{
|
||||||
|
return (double)value;
|
||||||
|
}
|
||||||
|
public static double ToDouble(double value)
|
||||||
|
{
|
||||||
|
return (double)value;
|
||||||
|
}
|
||||||
|
}
|
||||||
public static class ExpressionExtension
|
public static class ExpressionExtension
|
||||||
{
|
{
|
||||||
public const double DefaultDoubleAccuracy = 1e-7;
|
public const double DefaultDoubleAccuracy = 1e-7;
|
||||||
@@ -32,7 +82,10 @@ namespace Convention.RScript.Parser
|
|||||||
return !(double.IsInfinity(value) || double.IsNaN(value) || value > maximumAbsoluteError || value < -maximumAbsoluteError);
|
return !(double.IsInfinity(value) || double.IsNaN(value) || value > maximumAbsoluteError || value < -maximumAbsoluteError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Convention.RScript.Parser
|
||||||
|
{
|
||||||
public class ExpressionParser
|
public class ExpressionParser
|
||||||
{
|
{
|
||||||
public readonly ExpressionContext context;
|
public readonly ExpressionContext context;
|
||||||
|
|||||||
@@ -2,10 +2,26 @@
|
|||||||
|
|
||||||
namespace Convention.RScript
|
namespace Convention.RScript
|
||||||
{
|
{
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class RScriptException : Exception
|
public class RScriptException : Exception
|
||||||
{
|
{
|
||||||
public RScriptException(string message, int runtimePointer) : base($"when running {runtimePointer}, {message}") { }
|
public RScriptException() { }
|
||||||
public RScriptException(string message, int runtimePointer, Exception inner) : base($"when running {runtimePointer}, {message}", inner) { }
|
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 : 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) { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,33 @@ namespace Convention.RScript
|
|||||||
public struct RScriptVariableEntry
|
public struct RScriptVariableEntry
|
||||||
{
|
{
|
||||||
public Type type;
|
public Type type;
|
||||||
public object data;
|
public object data
|
||||||
|
{
|
||||||
|
readonly get
|
||||||
|
{
|
||||||
|
return internalData;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (type == null)
|
||||||
|
{
|
||||||
|
type = value.GetType();
|
||||||
|
internalData = value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (type == typeof(object) || type == value.GetType())
|
||||||
|
internalData = value;
|
||||||
|
else
|
||||||
|
internalData = Convert.ChangeType(value, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private object internalData;
|
||||||
|
|
||||||
|
public RScriptVariableEntry(Type type, object data) : this()
|
||||||
|
{
|
||||||
|
this.type = type;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public class RScriptVariables : IDictionary<string, RScriptVariableEntry>
|
public class RScriptVariables : IDictionary<string, RScriptVariableEntry>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
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;
|
||||||
|
|
||||||
namespace Convention.RScript
|
namespace Convention.RScript
|
||||||
@@ -16,7 +18,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>
|
||||||
@@ -32,11 +34,11 @@ namespace Convention.RScript
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
ExitNamespace,
|
ExitNamespace,
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 标签, 格式: label(labelname)
|
/// 标签, 格式: label(labelname);
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Label,
|
Label,
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 跳转到指定标签, 格式: goto(boolean,labelname)
|
/// 跳转到指定标签, 格式: goto(boolean,labelname);
|
||||||
/// <para>判断为真时跳转到labelname</para>
|
/// <para>判断为真时跳转到labelname</para>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Goto,
|
Goto,
|
||||||
@@ -48,11 +50,20 @@ namespace Convention.RScript
|
|||||||
/// 跳转到上次跳转的位置的后一个位置, 格式: back(boolean);
|
/// 跳转到上次跳转的位置的后一个位置, 格式: back(boolean);
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Backpoint,
|
Backpoint,
|
||||||
|
/// <summary>
|
||||||
|
/// 命名空间命名, 格式: namespace(labelname){}
|
||||||
|
/// </summary>
|
||||||
|
NamedSpace,
|
||||||
}
|
}
|
||||||
|
|
||||||
public string content;
|
public string content;
|
||||||
public List<string> info;
|
public List<string> info;
|
||||||
public Mode mode;
|
public Mode mode;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{mode.ToString()}/: {content}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IRSentenceMatcher
|
public interface IRSentenceMatcher
|
||||||
@@ -60,13 +71,19 @@ namespace Convention.RScript
|
|||||||
bool Match(string expression, ref RScriptSentence sentence);
|
bool Match(string expression, ref RScriptSentence sentence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface IRSentenceRunner
|
||||||
|
{
|
||||||
|
[return: MaybeNull] object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||||
|
}
|
||||||
|
|
||||||
public partial class RScriptContext
|
public partial class RScriptContext
|
||||||
{
|
{
|
||||||
public readonly RScriptImportClass Import;
|
public readonly RScriptImportClass Import;
|
||||||
public readonly RScriptVariables Variables;
|
public readonly RScriptVariables Variables;
|
||||||
private readonly RScriptSentence[] Sentences;
|
internal readonly RScriptSentence[] Sentences;
|
||||||
private readonly Dictionary<string, int> Labels;
|
internal readonly Dictionary<string, int> Labels = new();
|
||||||
private readonly Dictionary<int, int> Namespace;
|
internal readonly Dictionary<int, int> NamespaceLayer = new();
|
||||||
|
internal readonly Dictionary<string, int> NamespaceLabels = new();
|
||||||
|
|
||||||
public List<IRSentenceMatcher> SentenceParser = new()
|
public List<IRSentenceMatcher> SentenceParser = new()
|
||||||
{
|
{
|
||||||
@@ -78,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()
|
||||||
@@ -86,262 +115,126 @@ namespace Convention.RScript
|
|||||||
mode = RScriptSentence.Mode.Expression
|
mode = RScriptSentence.Mode.Expression
|
||||||
};
|
};
|
||||||
expression = expression.Trim();
|
expression = expression.Trim();
|
||||||
expression.TrimEnd(';');
|
expression = expression.TrimEnd(';');
|
||||||
SentenceParser.Any(matcher => matcher.Match(expression, ref result));
|
var _ = SentenceParser.Any(matcher => matcher.Match(expression, ref result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void BuildUpLabelsAndNamespace(ref Dictionary<string, int> labelIndicator, ref Dictionary<int, int> namespaceIndicator)
|
private void BuildUpLabelsAndNamespace()
|
||||||
{
|
{
|
||||||
Stack<int> namespaceLayers = new();
|
Stack<int> namespaceLayers = new();
|
||||||
|
string namespaceName = "";
|
||||||
for (int i = 0, e = Sentences.Length; i != e; i++)
|
for (int i = 0, e = Sentences.Length; i != e; i++)
|
||||||
{
|
{
|
||||||
if (Sentences[i].mode == RScriptSentence.Mode.Label)
|
var sentence = Sentences[i];
|
||||||
|
if (string.IsNullOrEmpty(namespaceName))
|
||||||
{
|
{
|
||||||
labelIndicator[Sentences[i].content] = i;
|
if (sentence.mode == RScriptSentence.Mode.Label)
|
||||||
|
{
|
||||||
|
this.Labels[Sentences[i].content] = i;
|
||||||
|
}
|
||||||
|
else if (sentence.mode == RScriptSentence.Mode.EnterNamespace)
|
||||||
|
{
|
||||||
|
namespaceLayers.Push(i);
|
||||||
|
}
|
||||||
|
else if (sentence.mode == RScriptSentence.Mode.ExitNamespace)
|
||||||
|
{
|
||||||
|
if (namespaceLayers.Count == 0)
|
||||||
|
throw new RScriptRuntimeException("Namespace exit without enter.", i);
|
||||||
|
var enterPointer = namespaceLayers.Pop();
|
||||||
|
this.NamespaceLayer[enterPointer] = i;
|
||||||
|
}
|
||||||
|
else if (sentence.mode == RScriptSentence.Mode.NamedSpace)
|
||||||
|
{
|
||||||
|
namespaceName = sentence.content;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (Sentences[i].mode == RScriptSentence.Mode.EnterNamespace)
|
else
|
||||||
{
|
{
|
||||||
namespaceLayers.Push(i);
|
if (sentence.mode == RScriptSentence.Mode.EnterNamespace)
|
||||||
}
|
{
|
||||||
else if (Sentences[i].mode == RScriptSentence.Mode.ExitNamespace)
|
namespaceLayers.Push(i);
|
||||||
{
|
this.NamespaceLabels[namespaceName] = i;
|
||||||
if (namespaceLayers.Count == 0)
|
namespaceName = "";
|
||||||
throw new RScriptException("Namespace exit without enter.", i);
|
}
|
||||||
var enterPointer = namespaceLayers.Pop();
|
else
|
||||||
namespaceIndicator[enterPointer] = i;
|
{
|
||||||
|
throw new RScriptRuntimeException($"Namespace is invalid", i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (namespaceLayers.Count > 0)
|
if (namespaceLayers.Count > 0)
|
||||||
{
|
{
|
||||||
throw new RScriptException("Namespace enter without exit.", namespaceLayers.Peek());
|
throw new RScriptRuntimeException("Namespace enter without exit.", namespaceLayers.Peek());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
this.Labels = new();
|
if (matcher != null)
|
||||||
this.Namespace = new();
|
this.SentenceParser = matcher;
|
||||||
BuildUpLabelsAndNamespace(ref this.Labels, ref this.Namespace);
|
if (sentenceRunners != null)
|
||||||
}
|
this.SentenceRunners = sentenceRunners;
|
||||||
|
|
||||||
|
BuildUpLabelsAndNamespace();
|
||||||
|
}
|
||||||
|
|
||||||
public RScriptSentence CurrentSentence => Sentences[CurrentRuntimePointer];
|
public RScriptSentence CurrentSentence => Sentences[CurrentRuntimePointer];
|
||||||
|
|
||||||
private void DoDefineVariable(ExpressionParser parser, RScriptSentence sentence)
|
|
||||||
{
|
|
||||||
// 定义变量
|
|
||||||
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 RScriptException($"Unsupported variable type '{varTypeName}'.", CurrentRuntimePointer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (CurrentLocalSpaceVariableNames.Peek().Contains(varName) == false)
|
|
||||||
{
|
|
||||||
Variables.Add(varName, new() { type = varType, data = varDefaultValue });
|
|
||||||
parser.context.Variables[varName] = varDefaultValue;
|
|
||||||
CurrentLocalSpaceVariableNames.Peek().Add(varName);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new RScriptException($"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
|
|
||||||
{
|
|
||||||
throw new RScriptException($"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 (Namespace.TryGetValue(RuntimePointerStack.Peek(), out var exitPointer))
|
|
||||||
{
|
|
||||||
CurrentRuntimePointer = exitPointer;
|
|
||||||
DoExitNamespace(parser);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new NotImplementedException($"No namespace to break.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DoBackpoint(ExpressionParser parser, RScriptSentence sentence)
|
|
||||||
{
|
|
||||||
// 检查并跳转到上次跳转的位置
|
|
||||||
if (parser.Evaluate<bool>(sentence.content))
|
|
||||||
{
|
|
||||||
if (GotoPointerStack.Count == 0)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException($"No position to back.");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
DoJumpRuntimePointer(parser, GotoPointerStack.Pop());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private object RunNextStep(ExpressionParser parser)
|
|
||||||
{
|
{
|
||||||
var sentence = CurrentSentence;
|
var sentence = CurrentSentence;
|
||||||
switch (sentence.mode)
|
try
|
||||||
{
|
{
|
||||||
case RScriptSentence.Mode.Expression:
|
return SentenceRunners.TryGetValue(sentence.mode, out var runner) ? runner.Run(parser, sentence, this) : null;
|
||||||
return parser.Evaluate(sentence.content);
|
}
|
||||||
case RScriptSentence.Mode.DefineVariable:
|
catch (RScriptException)
|
||||||
{
|
{
|
||||||
DoDefineVariable(parser, sentence);
|
throw;
|
||||||
}
|
}
|
||||||
break;
|
catch (Exception ex)
|
||||||
case RScriptSentence.Mode.EnterNamespace:
|
{
|
||||||
{
|
throw new RScriptRuntimeException($"Runtime error: {ex.Message}", CurrentRuntimePointer, ex);
|
||||||
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;
|
|
||||||
default:
|
|
||||||
// Do nothing
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
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()
|
||||||
{
|
{
|
||||||
@@ -353,13 +246,26 @@ namespace Convention.RScript
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Run(ExpressionParser parser)
|
private void BeforeRun(ExpressionParser parser)
|
||||||
{
|
{
|
||||||
CurrentLocalSpaceVariableNames.Clear();
|
CurrentLocalSpaceVariableNames.Clear();
|
||||||
RuntimePointerStack.Clear();
|
RuntimePointerStack.Clear();
|
||||||
GotoPointerStack.Clear();
|
GotoPointerStack.Clear();
|
||||||
CurrentLocalSpaceVariableNames.Clear();
|
CurrentLocalSpaceVariableNames.Clear();
|
||||||
CurrentLocalSpaceVariableNames.Push(new());
|
CurrentLocalSpaceVariableNames.Push(new());
|
||||||
|
foreach (var staticType in Import)
|
||||||
|
{
|
||||||
|
parser.context.Imports.AddType(staticType);
|
||||||
|
}
|
||||||
|
foreach (var (name,varObject) in Variables)
|
||||||
|
{
|
||||||
|
parser.context.Variables[name] = varObject.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run(ExpressionParser parser)
|
||||||
|
{
|
||||||
|
BeforeRun(parser);
|
||||||
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
|
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
|
||||||
{
|
{
|
||||||
RunNextStep(parser);
|
RunNextStep(parser);
|
||||||
@@ -374,11 +280,7 @@ namespace Convention.RScript
|
|||||||
|
|
||||||
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);
|
||||||
|
|||||||
144
RScriptEngine.cs
144
RScriptEngine.cs
@@ -19,82 +19,104 @@ namespace Convention.RScript
|
|||||||
StringBuilder builder = new();
|
StringBuilder builder = new();
|
||||||
List<string> statements = new();
|
List<string> statements = new();
|
||||||
|
|
||||||
for (int i = 0, e = script.Length; i < e; i++)
|
void PushBuilder()
|
||||||
{
|
{
|
||||||
char c = script[i];
|
if (builder.Length > 0)
|
||||||
if (c == ';')
|
|
||||||
{
|
{
|
||||||
if (builder.Length > 0)
|
statements.Add(builder.ToString().Trim());
|
||||||
{
|
builder.Clear();
|
||||||
statements.Add(builder.ToString().Trim());
|
|
||||||
builder.Clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (c == '/' && i + 1 < e)
|
}
|
||||||
|
|
||||||
|
var lines = script.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
|
||||||
|
for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
|
||||||
|
{
|
||||||
|
string line = lines[lineIndex];
|
||||||
|
for (int i = 0, e = line.Length; i < e; i++)
|
||||||
{
|
{
|
||||||
// Skip single-line comment
|
char c = line[i];
|
||||||
if (script[i + 1] == '/')
|
if (c == ';')
|
||||||
{
|
{
|
||||||
while (i < script.Length && script[i] != '\n')
|
PushBuilder();
|
||||||
|
}
|
||||||
|
else if (c == '/' && i + 1 < e)
|
||||||
|
{
|
||||||
|
// Skip single-line comment
|
||||||
|
if (line[i + 1] == '/')
|
||||||
|
{
|
||||||
|
while (i < line.Length && line[i] != '\n')
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
// Skip multi-line comment
|
||||||
|
else if (line[i + 1] == '*')
|
||||||
|
{
|
||||||
|
i += 2;
|
||||||
|
while (i + 1 < line.Length && !(line[i] == '*' && line[i + 1] == '/'))
|
||||||
|
i++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.Append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == '#')
|
||||||
|
{
|
||||||
|
// Skip single-line comment
|
||||||
|
while (i < line.Length && line[i] != '\n')
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
// Skip multi-line comment
|
else if (c == '\"')
|
||||||
else if (script[i + 1] == '*')
|
|
||||||
{
|
{
|
||||||
i += 2;
|
builder.Append(c);
|
||||||
while (i + 1 < script.Length && !(script[i] == '*' && script[i + 1] == '/'))
|
for (i++; i < e; i++)
|
||||||
i++;
|
{
|
||||||
i++;
|
builder.Append(line[i]);
|
||||||
|
if (line[i] == '\"')
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (line[i] == '\\')
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
if (i < e)
|
||||||
|
builder.Append(line[i]);
|
||||||
|
else
|
||||||
|
throw new RScriptCompileException("Invalid escape sequence in string literal", lineIndex, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == '{' || c == '}')
|
||||||
|
{
|
||||||
|
PushBuilder();
|
||||||
|
statements.Add(c.ToString());
|
||||||
|
}
|
||||||
|
else if (string.Compare("namespace", 0, line, i, "namespace".Length) == 0)
|
||||||
|
{
|
||||||
|
Regex regex = new(@"^\s*namespace\s*\([a-zA-Z_][a-zA-Z0-9_]*\)");
|
||||||
|
var match = regex.Match(line);
|
||||||
|
if (match.Success)
|
||||||
|
{
|
||||||
|
builder.Append(match.Value);
|
||||||
|
PushBuilder();
|
||||||
|
i += match.Length;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RScriptCompileException("Invalid namespace declaration", lineIndex, i);
|
||||||
|
}
|
||||||
|
// 前面的操作中已经完成位移, 此处是抵消循环中的i++
|
||||||
|
i -= 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
builder.Append(c);
|
builder.Append(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (c == '#')
|
|
||||||
{
|
|
||||||
// Skip single-line comment
|
|
||||||
while (i < script.Length && script[i] != '\n')
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
else if (c == '\"')
|
|
||||||
{
|
|
||||||
for (i++; i < e; i++)
|
|
||||||
{
|
|
||||||
builder.Append(script[i]);
|
|
||||||
if (script[i] == '\"')
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (script[i] == '\\')
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
if (i < e)
|
|
||||||
builder.Append(script[i]);
|
|
||||||
else
|
|
||||||
throw new RScriptException("Invalid escape sequence in string literal", -1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (c == '{' || c == '}')
|
|
||||||
{
|
|
||||||
// Treat braces as statement separators
|
|
||||||
if (builder.Length > 0)
|
|
||||||
{
|
|
||||||
statements.Add(builder.ToString().Trim());
|
|
||||||
builder.Clear();
|
|
||||||
}
|
|
||||||
statements.Add(c.ToString());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
builder.Append(c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (builder.Length > 0)
|
if (builder.Length > 0)
|
||||||
{
|
{
|
||||||
statements.Add(builder.ToString().Trim());
|
PushBuilder();
|
||||||
builder.Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return statements.Where(s => !string.IsNullOrWhiteSpace(s));
|
return statements.Where(s => !string.IsNullOrWhiteSpace(s));
|
||||||
@@ -104,8 +126,6 @@ namespace Convention.RScript
|
|||||||
{
|
{
|
||||||
parser = new(new());
|
parser = new(new());
|
||||||
context = new(SplitScript(script).ToArray(), import, variables);
|
context = new(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();
|
||||||
}
|
}
|
||||||
@@ -114,8 +134,6 @@ namespace Convention.RScript
|
|||||||
{
|
{
|
||||||
parser = new(new());
|
parser = new(new());
|
||||||
context = new(SplitScript(script).ToArray(), import, variables);
|
context = new(SplitScript(script).ToArray(), import, variables);
|
||||||
foreach (var type in context.Import)
|
|
||||||
parser.context.Imports.AddType(type);
|
|
||||||
return context.RunAsync(parser);
|
return context.RunAsync(parser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user