Compare commits
7 Commits
97a6e4d76b
...
main
Author | SHA1 | Date | |
---|---|---|---|
4860aa251e | |||
7eb53fc3c5 | |||
58f3d1067c | |||
7b48066aaf | |||
dde9e6b82d | |||
d3e21cad15 | |||
52e8e85542 |
@@ -5,6 +5,11 @@ namespace Convention.RScript.Runner
|
||||
{
|
||||
public class BackpointRunner : JumpRuntimePointerRunner
|
||||
{
|
||||
public override void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[return: MaybeNull]
|
||||
public override object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
@@ -5,6 +5,11 @@ namespace Convention.RScript
|
||||
{
|
||||
public class BreakpointRunner : IRSentenceRunner
|
||||
{
|
||||
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
parser.Compile<bool>(sentence.content);
|
||||
}
|
||||
|
||||
[return: MaybeNull]
|
||||
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
@@ -18,7 +23,6 @@ namespace Convention.RScript
|
||||
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
|
||||
|
@@ -6,6 +6,66 @@ namespace Convention.RScript.Runner
|
||||
{
|
||||
public class DefineVariableRunner : IRSentenceRunner
|
||||
{
|
||||
public void Compile(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 = "";
|
||||
}
|
||||
else if (varTypeName == "int")
|
||||
{
|
||||
varType = typeof(int);
|
||||
if (varInitExpression != null) parser.Compile<int>(varInitExpression);
|
||||
varDefaultValue = 0;
|
||||
}
|
||||
else if (varTypeName == "double")
|
||||
{
|
||||
varType = typeof(double);
|
||||
if (varInitExpression != null) parser.Compile<double>(varInitExpression);
|
||||
varDefaultValue = 0.0;
|
||||
}
|
||||
else if (varTypeName == "float")
|
||||
{
|
||||
varType = typeof(float);
|
||||
if (varInitExpression != null) parser.Compile<float>(varInitExpression);
|
||||
varDefaultValue = 0.0f;
|
||||
}
|
||||
else if (varTypeName == "bool")
|
||||
{
|
||||
varType = typeof(bool);
|
||||
if (varInitExpression != null) parser.Compile<bool>(varInitExpression);
|
||||
varDefaultValue = false;
|
||||
}
|
||||
else if (varTypeName == "var")
|
||||
{
|
||||
varType = typeof(object);
|
||||
if (varInitExpression != null) parser.Compile(varInitExpression);
|
||||
varDefaultValue = new object();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new RScriptRuntimeException($"Unsupported variable type '{varTypeName}'.", context.CurrentRuntimePointer);
|
||||
}
|
||||
}
|
||||
if (context.CurrentLocalSpaceVariableNames.Peek().Contains(varName) == false)
|
||||
{
|
||||
context.Variables.Add(varName, new(varType, default));
|
||||
parser.context.Variables[varName] = varDefaultValue;
|
||||
context.CurrentLocalSpaceVariableNames.Peek().Add(varName);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new RScriptRuntimeException($"Variable '{varName}' already defined on this namespace.", context.CurrentRuntimePointer);
|
||||
}
|
||||
}
|
||||
|
||||
[return: MaybeNull]
|
||||
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
@@ -5,6 +5,11 @@ namespace Convention.RScript.Runner
|
||||
{
|
||||
public class EnterNamedSpaceRunner : IRSentenceRunner
|
||||
{
|
||||
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[return: MaybeNull]
|
||||
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
@@ -5,6 +5,11 @@ namespace Convention.RScript.Runner
|
||||
{
|
||||
public class EnterNamespaceRunner : IRSentenceRunner
|
||||
{
|
||||
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[return: MaybeNull]
|
||||
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
@@ -5,6 +5,11 @@ namespace Convention.RScript.Runner
|
||||
{
|
||||
public class ExitNamespaceRunner : IRSentenceRunner
|
||||
{
|
||||
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[return: MaybeNull]
|
||||
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
@@ -5,6 +5,11 @@ namespace Convention.RScript.Runner
|
||||
{
|
||||
public class ExpressionRunner : IRSentenceRunner
|
||||
{
|
||||
public void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
parser.Compile(sentence.content);
|
||||
}
|
||||
|
||||
[return: MaybeNull]
|
||||
public object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
@@ -5,6 +5,11 @@ namespace Convention.RScript.Runner
|
||||
{
|
||||
public class GoToRunner : JumpRuntimePointerRunner
|
||||
{
|
||||
public override void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
parser.Compile<bool>(sentence.info[0]);
|
||||
}
|
||||
|
||||
[return: MaybeNull]
|
||||
public override object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
|
||||
{
|
||||
|
@@ -1,4 +1,6 @@
|
||||
using Convention.RScript.Parser;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Convention.RScript.Runner
|
||||
@@ -6,59 +8,67 @@ namespace Convention.RScript.Runner
|
||||
public abstract class JumpRuntimePointerRunner : IRSentenceRunner
|
||||
{
|
||||
protected static void DoJumpRuntimePointer(ExpressionParser parser, int target, RScriptContext context)
|
||||
{
|
||||
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))
|
||||
{
|
||||
depth = jumpResult.Item1;
|
||||
lastLayer = jumpResult.Item2;
|
||||
}
|
||||
else
|
||||
{
|
||||
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--;
|
||||
lastLayer--;
|
||||
else
|
||||
{
|
||||
for (int disLayer = -insLayer; disLayer > 0; disLayer--)
|
||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||
}
|
||||
}
|
||||
else
|
||||
insLayer++;
|
||||
lastLayer++;
|
||||
}
|
||||
else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
|
||||
{
|
||||
if (isForwardMove)
|
||||
insLayer++;
|
||||
lastLayer++;
|
||||
else
|
||||
lastLayer--;
|
||||
}
|
||||
depth = lastLayer < depth ? lastLayer : depth;
|
||||
}
|
||||
context.JumpPointerCache.Add(enterKey, Tuple.Create(depth, lastLayer));
|
||||
}
|
||||
// 对上层的最深影响
|
||||
for (; depth < 0; depth++)
|
||||
{
|
||||
if (insLayer > 0)
|
||||
insLayer--;
|
||||
else
|
||||
try
|
||||
{
|
||||
for (int disLayer = -insLayer; disLayer > 0; disLayer--)
|
||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (insLayer > 0)
|
||||
catch (Exception ex)
|
||||
{
|
||||
for (; insLayer > 0; insLayer--)
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||
[return: MaybeNull]
|
||||
public abstract object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ namespace Convention.RScript.Matcher
|
||||
if (DefineVariableMatch.Success)
|
||||
{
|
||||
sentence.mode = RScriptSentence.Mode.DefineVariable;
|
||||
sentence.info = new() { DefineVariableMatch.Groups[1].Value, DefineVariableMatch.Groups[2].Value, DefineVariableMatch.Groups[3].Value };
|
||||
sentence.info = new[] { DefineVariableMatch.Groups[1].Value, DefineVariableMatch.Groups[2].Value, DefineVariableMatch.Groups[3].Value };
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ namespace Convention.RScript.Matcher
|
||||
if (DeclareVariableMatch.Success)
|
||||
{
|
||||
sentence.mode = RScriptSentence.Mode.DefineVariable;
|
||||
sentence.info = new() { DeclareVariableMatch.Groups[1].Value, DeclareVariableMatch.Groups[2].Value, null };
|
||||
sentence.info = new[] { DeclareVariableMatch.Groups[1].Value, DeclareVariableMatch.Groups[2].Value, null };
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -13,7 +13,7 @@ namespace Convention.RScript.Matcher
|
||||
{
|
||||
sentence.mode = RScriptSentence.Mode.Goto;
|
||||
sentence.content = GotoMatch.Groups[2].Value;
|
||||
sentence.info = new() { GotoMatch.Groups[1].Value, GotoMatch.Groups[2].Value };
|
||||
sentence.info = new[] { GotoMatch.Groups[1].Value, GotoMatch.Groups[2].Value };
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
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");
|
||||
}
|
||||
}
|
||||
}
|
@@ -25,15 +25,21 @@ namespace Convention.RScript
|
||||
internalData = value;
|
||||
return;
|
||||
}
|
||||
if (value == null)
|
||||
{
|
||||
if (type.IsClass)
|
||||
internalData = null;
|
||||
else
|
||||
internalData = Activator.CreateInstance(type);
|
||||
}
|
||||
else if (type == typeof(object) || type == value.GetType())
|
||||
internalData = value;
|
||||
else
|
||||
internalData = Convert.ChangeType(value, type);
|
||||
}
|
||||
}
|
||||
private object internalData;
|
||||
|
||||
public RScriptVariableEntry(object data) : this()
|
||||
{
|
||||
this.data = data;
|
||||
}
|
||||
public RScriptVariableEntry(Type type, object data) : this()
|
||||
{
|
||||
this.type = type;
|
||||
|
@@ -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,44 @@ 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);
|
||||
SerializableClass CompileFromCurrent(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 Tuple<Tuple<int, int>, Tuple<int, int>>[] JumpPointerCache;
|
||||
}
|
||||
|
||||
public List<IRSentenceMatcher> SentenceParser = new()
|
||||
{
|
||||
new NamespaceMater(),
|
||||
@@ -169,6 +199,30 @@ namespace Convention.RScript
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -177,6 +231,7 @@ namespace Convention.RScript
|
||||
{
|
||||
this.Import = import ?? 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();
|
||||
if (matcher != null)
|
||||
this.SentenceParser = matcher;
|
||||
@@ -186,11 +241,34 @@ 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;
|
||||
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];
|
||||
|
||||
public int StepCount { get; private set; }
|
||||
|
||||
internal object RunNextStep(ExpressionParser parser)
|
||||
{
|
||||
StepCount++;
|
||||
var sentence = CurrentSentence;
|
||||
try
|
||||
{
|
||||
@@ -208,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();
|
||||
|
||||
@@ -221,18 +300,27 @@ namespace Convention.RScript
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Run(ExpressionParser parser)
|
||||
private void BeforeRun(ExpressionParser parser)
|
||||
{
|
||||
StepCount = 0;
|
||||
CurrentLocalSpaceVariableNames.Clear();
|
||||
RuntimePointerStack.Clear();
|
||||
GotoPointerStack.Clear();
|
||||
CurrentLocalSpaceVariableNames.Clear();
|
||||
CurrentLocalSpaceVariableNames.Push(new());
|
||||
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
|
||||
JumpPointerCache.Clear();
|
||||
foreach (var staticType in Import)
|
||||
{
|
||||
RunNextStep(parser);
|
||||
parser.context.Imports.AddType(staticType);
|
||||
}
|
||||
// 更新上下文变量
|
||||
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))
|
||||
@@ -240,13 +328,19 @@ namespace Convention.RScript
|
||||
}
|
||||
}
|
||||
|
||||
public void Run(ExpressionParser parser)
|
||||
{
|
||||
BeforeRun(parser);
|
||||
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
|
||||
{
|
||||
RunNextStep(parser);
|
||||
}
|
||||
AfterRun(parser);
|
||||
}
|
||||
|
||||
public IEnumerator RunAsync(ExpressionParser parser)
|
||||
{
|
||||
CurrentLocalSpaceVariableNames.Clear();
|
||||
RuntimePointerStack.Clear();
|
||||
GotoPointerStack.Clear();
|
||||
CurrentLocalSpaceVariableNames.Clear();
|
||||
CurrentLocalSpaceVariableNames.Push(new());
|
||||
BeforeRun(parser);
|
||||
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
|
||||
{
|
||||
var ret = RunNextStep(parser);
|
||||
@@ -256,12 +350,38 @@ namespace Convention.RScript
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
// 更新上下文变量
|
||||
foreach (var (varName, varValue) in parser.context.Variables)
|
||||
{
|
||||
if (Variables.ContainsKey(varName))
|
||||
Variables.SetValue(varName, varValue);
|
||||
AfterRun(parser);
|
||||
}
|
||||
|
||||
public SerializableClass Compile(ExpressionParser parser)
|
||||
{
|
||||
BeforeRun(parser);
|
||||
foreach (var item in Sentences)
|
||||
{
|
||||
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,
|
||||
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(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
102
RScriptEngine.cs
102
RScriptEngine.cs
@@ -6,13 +6,26 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using static Convention.RScript.RScriptContext;
|
||||
|
||||
namespace Convention.RScript
|
||||
{
|
||||
public class RScriptEngine
|
||||
public interface IRScriptEngine
|
||||
{
|
||||
IBasicRScriptContext context { get; }
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public sealed class RScriptEngine : IRScriptEngine
|
||||
{
|
||||
private ExpressionParser parser;
|
||||
private RScriptContext context;
|
||||
public IBasicRScriptContext context { get; private set; }
|
||||
|
||||
private IEnumerable<string> SplitScript(string script)
|
||||
{
|
||||
@@ -42,18 +55,10 @@ namespace Convention.RScript
|
||||
else if (c == '/' && i + 1 < e)
|
||||
{
|
||||
// Skip single-line comment
|
||||
if (script[i + 1] == '/')
|
||||
if (line[i + 1] == '/')
|
||||
{
|
||||
while (i < script.Length && script[i] != '\n')
|
||||
i++;
|
||||
}
|
||||
// Skip multi-line comment
|
||||
else if (script[i + 1] == '*')
|
||||
{
|
||||
i += 2;
|
||||
while (i + 1 < script.Length && !(script[i] == '*' && script[i + 1] == '/'))
|
||||
i++;
|
||||
i++;
|
||||
PushBuilder();
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -62,24 +67,24 @@ namespace Convention.RScript
|
||||
}
|
||||
else if (c == '#')
|
||||
{
|
||||
// Skip single-line comment
|
||||
while (i < script.Length && script[i] != '\n')
|
||||
i++;
|
||||
PushBuilder();
|
||||
break;
|
||||
}
|
||||
else if (c == '\"')
|
||||
{
|
||||
builder.Append(c);
|
||||
for (i++; i < e; i++)
|
||||
{
|
||||
builder.Append(script[i]);
|
||||
if (script[i] == '\"')
|
||||
builder.Append(line[i]);
|
||||
if (line[i] == '\"')
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (script[i] == '\\')
|
||||
else if (line[i] == '\\')
|
||||
{
|
||||
i++;
|
||||
if (i < e)
|
||||
builder.Append(script[i]);
|
||||
builder.Append(line[i]);
|
||||
else
|
||||
throw new RScriptCompileException("Invalid escape sequence in string literal", lineIndex, i);
|
||||
}
|
||||
@@ -90,14 +95,10 @@ namespace Convention.RScript
|
||||
PushBuilder();
|
||||
statements.Add(c.ToString());
|
||||
}
|
||||
else if (string.Compare("namespace", 0, script, i, "namespace".Length) == 0)
|
||||
else if (string.Compare("namespace", 0, line, i, "namespace".Length) == 0)
|
||||
{
|
||||
builder.Append("namespace");
|
||||
i += "namespace".Length;
|
||||
if (i >= e)
|
||||
throw new RScriptCompileException("Invalid namespace declaration", lineIndex, i);
|
||||
Regex regex = new(@"^\s*\([a-zA-Z_][a-zA-Z0-9_]*\)");
|
||||
var match = regex.Match(script, i);
|
||||
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);
|
||||
@@ -117,20 +118,23 @@ namespace Convention.RScript
|
||||
}
|
||||
}
|
||||
}
|
||||
if (builder.Length > 0)
|
||||
{
|
||||
PushBuilder();
|
||||
}
|
||||
|
||||
return statements.Where(s => !string.IsNullOrWhiteSpace(s));
|
||||
}
|
||||
|
||||
private IBasicRScriptContext CreateContext(string[] statements, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||
{
|
||||
return new RScriptContext(statements, import, variables);
|
||||
}
|
||||
private IBasicRScriptContext CreateContext(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||
{
|
||||
return new RScriptContext(data, import, variables);
|
||||
}
|
||||
public Dictionary<string, RScriptVariableEntry> Run(string script, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||
{
|
||||
parser = new(new());
|
||||
context = new(SplitScript(script).ToArray(), import, variables);
|
||||
foreach (var type in context.Import)
|
||||
parser.context.Imports.AddType(type);
|
||||
context = CreateContext(SplitScript(script).ToArray(), import, variables);
|
||||
context.Run(parser);
|
||||
return context.GetCurrentVariables();
|
||||
}
|
||||
@@ -138,9 +142,35 @@ namespace Convention.RScript
|
||||
public IEnumerator RunAsync(string script, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||
{
|
||||
parser = new(new());
|
||||
context = new(SplitScript(script).ToArray(), import, variables);
|
||||
foreach (var type in context.Import)
|
||||
parser.context.Imports.AddType(type);
|
||||
context = CreateContext(SplitScript(script).ToArray(), import, variables);
|
||||
return context.RunAsync(parser);
|
||||
}
|
||||
|
||||
public SerializableClass Compile(string script, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||
{
|
||||
parser = new(new());
|
||||
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)
|
||||
{
|
||||
parser = new(new());
|
||||
//parser.Deserialize(data.CompileParser);
|
||||
context = CreateContext(data, import, variables);
|
||||
context.Run(parser);
|
||||
return context.GetCurrentVariables();
|
||||
}
|
||||
|
||||
public IEnumerator RunAsync(SerializableClass data, RScriptImportClass import = null, RScriptVariables variables = null)
|
||||
{
|
||||
parser = new(new());
|
||||
//parser.Deserialize(data.CompileParser);
|
||||
context = CreateContext(data, import, variables);
|
||||
return context.RunAsync(parser);
|
||||
}
|
||||
}
|
||||
|
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