正在新增编译缓存功能,用于加速与加密
This commit is contained in:
@@ -6,11 +6,14 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using static Convention.RScript.RScriptContext;
|
||||
|
||||
namespace Convention.RScript
|
||||
{
|
||||
[Serializable]
|
||||
public struct RScriptSentence
|
||||
{
|
||||
[Serializable]
|
||||
public enum Mode
|
||||
{
|
||||
/// <summary>
|
||||
@@ -57,12 +60,12 @@ namespace Convention.RScript
|
||||
}
|
||||
|
||||
public string content;
|
||||
public List<string> info;
|
||||
public string[] info;
|
||||
public Mode mode;
|
||||
|
||||
public override string ToString()
|
||||
public override readonly string ToString()
|
||||
{
|
||||
return $"{mode.ToString()}/: {content}";
|
||||
return $"{mode}: {content}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,17 +77,42 @@ namespace Convention.RScript
|
||||
public interface IRSentenceRunner
|
||||
{
|
||||
[return: MaybeNull] object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||
void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||
}
|
||||
|
||||
public partial class RScriptContext
|
||||
public interface IBasicRScriptContext
|
||||
{
|
||||
public readonly RScriptImportClass Import;
|
||||
public readonly RScriptVariables Variables;
|
||||
internal readonly RScriptSentence[] Sentences;
|
||||
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()
|
||||
{
|
||||
new NamespaceMater(),
|
||||
@@ -211,11 +239,29 @@ namespace Convention.RScript
|
||||
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 int StepCount { get; private set; }
|
||||
|
||||
internal object RunNextStep(ExpressionParser parser)
|
||||
{
|
||||
StepCount++;
|
||||
var sentence = CurrentSentence;
|
||||
try
|
||||
{
|
||||
@@ -248,6 +294,7 @@ namespace Convention.RScript
|
||||
|
||||
private void BeforeRun(ExpressionParser parser)
|
||||
{
|
||||
StepCount = 0;
|
||||
CurrentLocalSpaceVariableNames.Clear();
|
||||
RuntimePointerStack.Clear();
|
||||
GotoPointerStack.Clear();
|
||||
@@ -257,12 +304,21 @@ namespace Convention.RScript
|
||||
{
|
||||
parser.context.Imports.AddType(staticType);
|
||||
}
|
||||
foreach (var (name,varObject) in Variables)
|
||||
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)
|
||||
{
|
||||
if (Variables.ContainsKey(varName))
|
||||
Variables.SetValue(varName, varValue);
|
||||
}
|
||||
}
|
||||
|
||||
public void Run(ExpressionParser parser)
|
||||
{
|
||||
BeforeRun(parser);
|
||||
@@ -270,12 +326,7 @@ namespace Convention.RScript
|
||||
{
|
||||
RunNextStep(parser);
|
||||
}
|
||||
// 更新上下文变量
|
||||
foreach (var (varName, varValue) in parser.context.Variables)
|
||||
{
|
||||
if (Variables.ContainsKey(varName))
|
||||
Variables.SetValue(varName, varValue);
|
||||
}
|
||||
AfterRun(parser);
|
||||
}
|
||||
|
||||
public IEnumerator RunAsync(ExpressionParser parser)
|
||||
@@ -290,12 +341,25 @@ namespace Convention.RScript
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
// 更新上下文变量
|
||||
foreach (var (varName, varValue) in parser.context.Variables)
|
||||
AfterRun(parser);
|
||||
}
|
||||
|
||||
public SerializableClass Compile(ExpressionParser parser)
|
||||
{
|
||||
BeforeRun(parser);
|
||||
foreach (var item in Sentences)
|
||||
{
|
||||
if (Variables.ContainsKey(varName))
|
||||
Variables.SetValue(varName, varValue);
|
||||
if (SentenceRunners.TryGetValue(item.mode, out var runner))
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user