新增跳转缓存

This commit is contained in:
2025-10-21 10:26:08 +08:00
parent 58f3d1067c
commit 7eb53fc3c5
4 changed files with 83 additions and 23 deletions

View File

@@ -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(),
};
}
}