Compare commits
5 Commits
d3e21cad15
...
main
Author | SHA1 | Date | |
---|---|---|---|
4860aa251e | |||
7eb53fc3c5 | |||
58f3d1067c | |||
7b48066aaf | |||
dde9e6b82d |
@@ -1,4 +1,6 @@
|
||||
using Convention.RScript.Parser;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Convention.RScript.Runner
|
||||
@@ -7,54 +9,61 @@ namespace Convention.RScript.Runner
|
||||
{
|
||||
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)
|
||||
Tuple<int, int> enterKey = Tuple.Create(context.CurrentRuntimePointer, target);
|
||||
int currentPointer = context.CurrentRuntimePointer;
|
||||
int depth = 0;
|
||||
int lastLayer = 0;
|
||||
if (context.JumpPointerCache.TryGetValue(enterKey, out var jumpResult))
|
||||
{
|
||||
if (context.CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace)
|
||||
depth = jumpResult.Item1;
|
||||
lastLayer = jumpResult.Item2;
|
||||
}
|
||||
else
|
||||
{
|
||||
bool isForwardMove = target > context.CurrentRuntimePointer;
|
||||
int step = isForwardMove ? 1 : -1;
|
||||
for (; context.CurrentRuntimePointer != target; context.CurrentRuntimePointer += step)
|
||||
{
|
||||
if (isForwardMove)
|
||||
if (context.CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace)
|
||||
{
|
||||
if (insLayer > 0)
|
||||
insLayer--;
|
||||
if (isForwardMove)
|
||||
lastLayer--;
|
||||
else
|
||||
{
|
||||
for (int disLayer = -insLayer; disLayer > 0; disLayer--)
|
||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||
}
|
||||
lastLayer++;
|
||||
}
|
||||
else
|
||||
insLayer++;
|
||||
}
|
||||
else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
|
||||
{
|
||||
if (isForwardMove)
|
||||
insLayer++;
|
||||
else
|
||||
else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
|
||||
{
|
||||
if (insLayer > 0)
|
||||
insLayer--;
|
||||
if (isForwardMove)
|
||||
lastLayer++;
|
||||
else
|
||||
{
|
||||
for (int disLayer = -insLayer; disLayer > 0; disLayer--)
|
||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||
}
|
||||
lastLayer--;
|
||||
}
|
||||
depth = lastLayer < depth ? lastLayer : depth;
|
||||
}
|
||||
if (insLayer > 0)
|
||||
context.JumpPointerCache.Add(enterKey, Tuple.Create(depth, lastLayer));
|
||||
}
|
||||
// 对上层的最深影响
|
||||
for (; depth < 0; depth++)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (; insLayer > 0; insLayer--)
|
||||
{
|
||||
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
|
||||
}
|
||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||
}
|
||||
else if (insLayer < 0)
|
||||
catch (Exception ex)
|
||||
{
|
||||
for (; insLayer < 0; insLayer++)
|
||||
{
|
||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
72
PublicTypes/RScriptInjectVariable.cs
Normal file
72
PublicTypes/RScriptInjectVariable.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.RScript.Variable
|
||||
{
|
||||
[System.AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
|
||||
public sealed class RScriptMethodAttribute : Attribute
|
||||
{
|
||||
public string Description { get; set; }
|
||||
}
|
||||
|
||||
public abstract class RScriptInjectVariable
|
||||
{
|
||||
public string RScriptString { get; private set; }
|
||||
|
||||
protected abstract string WriteClassHead(Type currentType);
|
||||
protected abstract string WriteClassBodyEnter(Type currentType);
|
||||
protected abstract string WriteClassMethod(Type returnType, string methodName, string[] parameterNames, Type[] parameterTypes, string description);
|
||||
protected abstract string WriteClassBodyExit(Type currentType);
|
||||
protected abstract string WriteClassTail(Type currentType);
|
||||
|
||||
public delegate object Generater();
|
||||
|
||||
private Generater MyGenerater;
|
||||
public string name { get; private set; }
|
||||
public object Generate()
|
||||
{
|
||||
return MyGenerater();
|
||||
}
|
||||
|
||||
public RScriptInjectVariable([MaybeNull]Generater generater, string name)
|
||||
{
|
||||
var currentType = this.GetType();
|
||||
StringBuilder builder = new();
|
||||
builder.AppendLine(this.WriteClassHead(currentType));
|
||||
builder.AppendLine(this.WriteClassBodyEnter(currentType));
|
||||
var scriptMethods = from method in currentType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
|
||||
where method.GetCustomAttribute<RScriptMethodAttribute>() != null
|
||||
select method;
|
||||
foreach (var method in scriptMethods)
|
||||
{
|
||||
var attr = method.GetCustomAttribute<RScriptMethodAttribute>();
|
||||
var returnType = method.ReturnType;
|
||||
var methodName = method.Name;
|
||||
var parameters = method.GetParameters();
|
||||
var parameterNames = from item in parameters select item.Name;
|
||||
var parameterTypes = from item in parameters select item.ParameterType;
|
||||
builder.AppendLine(this.WriteClassMethod(returnType, methodName, parameterNames.ToArray(), parameterTypes.ToArray(), attr.Description));
|
||||
}
|
||||
builder.AppendLine(this.WriteClassBodyExit(currentType));
|
||||
builder.AppendLine(this.WriteClassTail(currentType));
|
||||
|
||||
this.MyGenerater = generater;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static object GenerateRScriptVariable(string name, Dictionary<string, RScriptInjectVariable> classes)
|
||||
{
|
||||
if (classes.TryGetValue(name, out var variable))
|
||||
{
|
||||
if (variable.MyGenerater != null)
|
||||
return variable.Generate();
|
||||
}
|
||||
throw new InvalidOperationException($"{name} target is not exist or abstract");
|
||||
}
|
||||
}
|
||||
}
|
@@ -92,6 +92,7 @@ namespace Convention.RScript
|
||||
void Run(ExpressionParser parser);
|
||||
IEnumerator RunAsync(ExpressionParser parser);
|
||||
SerializableClass Compile(ExpressionParser parser);
|
||||
SerializableClass CompileFromCurrent(ExpressionParser parser);
|
||||
}
|
||||
|
||||
public partial class RScriptContext : IBasicRScriptContext
|
||||
@@ -111,6 +112,7 @@ namespace Convention.RScript
|
||||
public Tuple<int, int>[] NamespaceLayer;
|
||||
public Tuple<string, int>[] NamespaceLabels;
|
||||
public ExpressionParser.SerializableParser CompileParser;
|
||||
public Tuple<Tuple<int, int>, Tuple<int, int>>[] JumpPointerCache;
|
||||
}
|
||||
|
||||
public List<IRSentenceMatcher> SentenceParser = new()
|
||||
@@ -250,9 +252,14 @@ namespace Convention.RScript
|
||||
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);
|
||||
if (data.Labels != null)
|
||||
this.Labels = (from item in data.Labels select item).ToDictionary(t => t.Item1, t => t.Item2);
|
||||
if (data.NamespaceLayer != null)
|
||||
this.NamespaceLayer = (from item in data.NamespaceLayer select item).ToDictionary(t => t.Item1, t => t.Item2);
|
||||
if (data.NamespaceLabels != null)
|
||||
this.NamespaceLabels = (from item in data.NamespaceLabels select item).ToDictionary(t => t.Item1, t => t.Item2);
|
||||
if (data.JumpPointerCache != null)
|
||||
this.JumpPointerCache = (from item in data.JumpPointerCache select item).ToDictionary(t => t.Item1, t => t.Item2);
|
||||
}
|
||||
|
||||
public RScriptSentence CurrentSentence => Sentences[CurrentRuntimePointer];
|
||||
@@ -279,6 +286,7 @@ namespace Convention.RScript
|
||||
|
||||
internal readonly Stack<int> RuntimePointerStack = new();
|
||||
internal readonly Stack<int> GotoPointerStack = new();
|
||||
internal readonly Dictionary<Tuple<int, int>, Tuple<int, int>> JumpPointerCache = new();
|
||||
public int CurrentRuntimePointer { get; internal set; } = 0;
|
||||
internal readonly Stack<HashSet<string>> CurrentLocalSpaceVariableNames = new();
|
||||
|
||||
@@ -300,6 +308,7 @@ namespace Convention.RScript
|
||||
GotoPointerStack.Clear();
|
||||
CurrentLocalSpaceVariableNames.Clear();
|
||||
CurrentLocalSpaceVariableNames.Push(new());
|
||||
JumpPointerCache.Clear();
|
||||
foreach (var staticType in Import)
|
||||
{
|
||||
parser.context.Imports.AddType(staticType);
|
||||
@@ -359,6 +368,19 @@ namespace Convention.RScript
|
||||
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,
|
||||
JumpPointerCache = (from item in JumpPointerCache select Tuple.Create(item.Key, item.Value)).ToArray(),
|
||||
};
|
||||
}
|
||||
public SerializableClass CompileFromCurrent(ExpressionParser parser)
|
||||
{
|
||||
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,
|
||||
JumpPointerCache = (from item in JumpPointerCache select Tuple.Create(item.Key, item.Value)).ToArray(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,7 @@ namespace Convention.RScript
|
||||
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);
|
||||
SerializableClass GetCompileResultFromCurrent();
|
||||
Dictionary<string, RScriptVariableEntry> Run(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null);
|
||||
IEnumerator RunAsync(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null);
|
||||
}
|
||||
@@ -56,16 +57,8 @@ namespace Convention.RScript
|
||||
// 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++;
|
||||
PushBuilder();
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -74,9 +67,8 @@ namespace Convention.RScript
|
||||
}
|
||||
else if (c == '#')
|
||||
{
|
||||
// Skip single-line comment
|
||||
while (i < line.Length && line[i] != '\n')
|
||||
i++;
|
||||
PushBuilder();
|
||||
break;
|
||||
}
|
||||
else if (c == '\"')
|
||||
{
|
||||
@@ -126,10 +118,7 @@ namespace Convention.RScript
|
||||
}
|
||||
}
|
||||
}
|
||||
if (builder.Length > 0)
|
||||
{
|
||||
PushBuilder();
|
||||
}
|
||||
PushBuilder();
|
||||
|
||||
return statements.Where(s => !string.IsNullOrWhiteSpace(s));
|
||||
}
|
||||
@@ -163,6 +152,10 @@ namespace Convention.RScript
|
||||
context = CreateContext(SplitScript(script).ToArray(), import, variables);
|
||||
return context.Compile(parser);
|
||||
}
|
||||
public SerializableClass GetCompileResultFromCurrent()
|
||||
{
|
||||
return context.CompileFromCurrent(parser);
|
||||
}
|
||||
|
||||
public Dictionary<string, RScriptVariableEntry> Run(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||
{
|
||||
|
173
RScriptSerializer.cs
Normal file
173
RScriptSerializer.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 序列化 JumpPointerCache 数组
|
||||
writer.Write(data.JumpPointerCache?.Length ?? 0);
|
||||
if (data.JumpPointerCache != null)
|
||||
{
|
||||
foreach (var jpItem in data.JumpPointerCache)
|
||||
{
|
||||
writer.Write(jpItem.Item1.Item1);
|
||||
writer.Write(jpItem.Item1.Item2);
|
||||
writer.Write(jpItem.Item2.Item1);
|
||||
writer.Write(jpItem.Item2.Item2);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 反序列化 JumpPointerCache 数组
|
||||
int jumpPointerCacheLength = reader.ReadInt32();
|
||||
if (jumpPointerCacheLength > 0)
|
||||
{
|
||||
result.JumpPointerCache = new Tuple<Tuple<int, int>, Tuple<int, int>>[jumpPointerCacheLength];
|
||||
for(int i=0;i<jumpPointerCacheLength;i++)
|
||||
{
|
||||
int x= reader.ReadInt32();
|
||||
int y= reader.ReadInt32();
|
||||
int z= reader.ReadInt32();
|
||||
int w= reader.ReadInt32();
|
||||
result.JumpPointerCache[i] = Tuple.Create(Tuple.Create(x, y), Tuple.Create(z, w));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user