正在新增编译缓存功能,用于加速与加密

This commit is contained in:
2025-10-17 15:46:44 +08:00
parent 52e8e85542
commit d3e21cad15
15 changed files with 310 additions and 31 deletions

View File

@@ -1,5 +1,7 @@
using Flee.PublicTypes;
using System.Collections.Generic;
using System;
using System.Linq;
namespace Convention.RScript
{
@@ -95,6 +97,7 @@ namespace Convention.RScript.Parser
this.context = context;
}
private readonly Dictionary<string, Type> CompileGenericExpressionTypen = new();
private readonly Dictionary<string, IExpression> CompileGenericExpression = new();
private readonly Dictionary<string, IDynamicExpression> CompileDynamicExpression = new();
@@ -110,9 +113,7 @@ namespace Convention.RScript.Parser
{
return (result as IGenericExpression<T>).Evaluate();
}
var compile = context.CompileGeneric<T>(expression);
CompileGenericExpression[expression] = compile;
return compile.Evaluate();
return Compile<T>(expression).Evaluate();
}
public object Evaluate(string expression)
@@ -121,9 +122,77 @@ namespace Convention.RScript.Parser
{
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);
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);
}
}
}
}