Compare commits
30 Commits
6783429ab6
...
master
Author | SHA1 | Date | |
---|---|---|---|
b1c36c08fb | |||
d2bd58568a | |||
15dd1c9aa4 | |||
21f3c580eb | |||
09b334ca58 | |||
4dc6691650 | |||
22b8c1838a | |||
b9012674d6 | |||
8a4edfcb79 | |||
8d6f96b99a | |||
a91c9741e4 | |||
9d7fbf9786 | |||
8e8edd8724 | |||
83ecbfbfd3 | |||
b5e2f4cc16 | |||
bacfcd550f | |||
f850030d10 | |||
d58efb13e2 | |||
c07c64be1e | |||
f8d81d9198 | |||
a8cfb012fc | |||
5041bb5600 | |||
a72c6b285b | |||
632e58cfce | |||
b8836df198 | |||
e8286f711f | |||
71fc384095 | |||
![]() |
522010e7cd | ||
![]() |
8123ed4b15 | ||
![]() |
acada40141 |
@@ -1,7 +1,5 @@
|
||||
---
|
||||
description:
|
||||
globs:
|
||||
alwaysApply: true
|
||||
alwaysApply: false
|
||||
---
|
||||
## RIPER-5 + O1 思维 + 代理执行协议
|
||||
|
||||
|
6
.gitmodules
vendored
Normal file
6
.gitmodules
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
[submodule "Convention/[FLEE]"]
|
||||
path = Convention/[FLEE]
|
||||
url = http://www.liubai.site:3000/ninemine/Flee.git
|
||||
[submodule "Convention/[RScript]"]
|
||||
path = Convention/[RScript]
|
||||
url = http://www.liubai.site:3000/ninemine/RScript.git
|
@@ -5,6 +5,7 @@
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Convention</RootNamespace>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
1
Convention/[FLEE]
Submodule
1
Convention/[FLEE]
Submodule
Submodule Convention/[FLEE] added at 47b12f4bc0
1
Convention/[RScript]
Submodule
1
Convention/[RScript]
Submodule
Submodule Convention/[RScript] added at 4860aa251e
@@ -120,26 +120,6 @@ namespace Convention
|
||||
|
||||
public static class Architecture
|
||||
{
|
||||
public static string FormatType(Type type)
|
||||
{
|
||||
return type.Assembly + "::" + type.FullName;
|
||||
}
|
||||
|
||||
public static Type LoadFromFormat(string data)
|
||||
{
|
||||
var keys = data.Split("::");
|
||||
Assembly asm = null;
|
||||
try
|
||||
{
|
||||
asm = Assembly.LoadFrom(keys[0]);
|
||||
return asm.GetType(keys[1]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Type LoadFromFormat(string data, out Exception exception)
|
||||
{
|
||||
exception = null;
|
||||
@@ -227,7 +207,7 @@ namespace Convention
|
||||
|
||||
public string Save()
|
||||
{
|
||||
return $"{FormatType(registerSlot)}[{ConvertTo()}]";
|
||||
throw new InvalidOperationException($"Cannt use {nameof(Registering)} to save type");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -388,11 +368,15 @@ namespace Convention
|
||||
|
||||
public static void UpdateTimeline()
|
||||
{
|
||||
for (bool stats = true; stats;)
|
||||
{
|
||||
stats = false;
|
||||
foreach (var pair in TimelineQuenes)
|
||||
{
|
||||
var timeline = pair.Value;
|
||||
if (timeline.Quene[timeline.Context].predicate())
|
||||
{
|
||||
stats = true;
|
||||
foreach (var action in timeline.Quene[timeline.Context].actions)
|
||||
{
|
||||
action();
|
||||
@@ -401,6 +385,7 @@ namespace Convention
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void ResetTimelineContext(int timelineId)
|
||||
{
|
||||
|
@@ -1,12 +1,13 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
|
||||
namespace Convention
|
||||
{
|
||||
@@ -66,7 +67,7 @@ namespace Convention
|
||||
public static T ConvertValue<T>(string str)
|
||||
{
|
||||
Type type = typeof(T);
|
||||
var parse_method = type.GetMethod("Parse");
|
||||
var parse_method = type.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(string) }, null);
|
||||
if (parse_method != null &&
|
||||
(parse_method.ReturnType.IsSubclassOf(type) || parse_method.ReturnType == type) &&
|
||||
parse_method.GetParameters().Length == 1 &&
|
||||
@@ -79,6 +80,39 @@ namespace Convention
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] Serialize<T>(T obj)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(obj);
|
||||
|
||||
using var memoryStream = new MemoryStream();
|
||||
DataContractSerializer ser = new DataContractSerializer(typeof(T));
|
||||
ser.WriteObject(memoryStream, obj);
|
||||
var data = memoryStream.ToArray();
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static T Deserialize<T>(byte[] data)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
|
||||
using var memoryStream = new MemoryStream(data);
|
||||
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, new XmlDictionaryReaderQuotas());
|
||||
DataContractSerializer ser = new DataContractSerializer(typeof(T));
|
||||
var result = (T)ser.ReadObject(reader, true);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static object SeekValue(object obj, string name, BindingFlags flags, out bool isSucceed)
|
||||
{
|
||||
Type type = obj.GetType();
|
||||
@@ -409,5 +443,25 @@ namespace Convention
|
||||
{
|
||||
return DateTime.Now.ToString(format);
|
||||
}
|
||||
|
||||
public class EnumerableClass : IEnumerable
|
||||
{
|
||||
private readonly IEnumerator ir;
|
||||
|
||||
public EnumerableClass(IEnumerator ir)
|
||||
{
|
||||
this.ir = ir;
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return ir;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable AsEnumerable(this IEnumerator ir)
|
||||
{
|
||||
return new EnumerableClass(ir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,6 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Convention
|
||||
{
|
||||
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Convention.Symbolization
|
||||
{
|
||||
|
||||
[Serializable]
|
||||
public class InvalidGrammarException : Exception
|
||||
{
|
||||
public InvalidGrammarException() { }
|
||||
public InvalidGrammarException(string message) : base(message) { }
|
||||
public InvalidGrammarException(string message, Exception inner) : base(message, inner) { }
|
||||
protected InvalidGrammarException(
|
||||
System.Runtime.Serialization.SerializationInfo info,
|
||||
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
@@ -1,96 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public abstract class Function : CloneableVariable<Function>
|
||||
{
|
||||
public readonly FunctionSymbol FunctionInfo;
|
||||
|
||||
protected Function(string symbolName,string functionName, Type returnType, Type[] parameterTypes) : base(symbolName)
|
||||
{
|
||||
FunctionInfo = new(functionName, returnType, parameterTypes);
|
||||
}
|
||||
public abstract Variable Invoke(SymbolizationContext context, Variable[] parameters);
|
||||
}
|
||||
|
||||
public sealed class DelegationalFunction : Function
|
||||
{
|
||||
public readonly MethodInfo methodInfo;
|
||||
|
||||
public DelegationalFunction(string symbolName, MethodInfo methodInfo)
|
||||
: base(symbolName, methodInfo.Name, methodInfo.ReturnType, methodInfo.GetParameters().ToList().ConvertAll(x => x.ParameterType).ToArray())
|
||||
{
|
||||
this.methodInfo = methodInfo!;
|
||||
}
|
||||
|
||||
public override DelegationalFunction CloneVariable(string targetSymbolName)
|
||||
{
|
||||
return new DelegationalFunction(targetSymbolName, methodInfo);
|
||||
}
|
||||
|
||||
public override bool Equals(Function other)
|
||||
{
|
||||
return other is DelegationalFunction df && df.methodInfo == this.methodInfo;
|
||||
}
|
||||
|
||||
public override Variable Invoke(SymbolizationContext context, Variable[] parameters)
|
||||
{
|
||||
Variable invoker = parameters.Length > 0 ? parameters[0] : null;
|
||||
Variable[] invokerParameters = parameters.Length > 0 ? parameters.Skip(1).ToArray() : Array.Empty<Variable>();
|
||||
object result = methodInfo.Invoke(invoker, invokerParameters);
|
||||
return result as Variable;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ScriptFunction : Function
|
||||
{
|
||||
public ScriptFunction(string symbolName,
|
||||
string functionName, Type returnType, Type[] parameterTypes)
|
||||
: base(symbolName, functionName, returnType, parameterTypes)
|
||||
{
|
||||
}
|
||||
|
||||
public override Function CloneVariable(string targetSymbolName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool Equals(Function other)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override Variable Invoke(SymbolizationContext context, Variable[] parameters)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct FunctionSymbol
|
||||
{
|
||||
public readonly string FunctionName;
|
||||
public readonly Type ReturnType;
|
||||
public readonly Type[] ParameterTypes;
|
||||
public readonly string FullName;
|
||||
|
||||
public FunctionSymbol(string symbolName, Type returnType, Type[] parameterTypes)
|
||||
{
|
||||
this.FunctionName = symbolName;
|
||||
this.ReturnType = returnType;
|
||||
this.ParameterTypes = parameterTypes;
|
||||
this.FullName = $"{returnType.FullName} {symbolName}({string.Join(',', parameterTypes.ToList().ConvertAll(x => x.FullName))})";
|
||||
}
|
||||
|
||||
public bool Equals(FunctionSymbol other)
|
||||
{
|
||||
return other.FullName.Equals(FullName);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return FullName.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,171 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public abstract class Keyword : CloneableVariable<Keyword>
|
||||
{
|
||||
protected Keyword(string keyword) : base(keyword)
|
||||
{
|
||||
Keywords.TryAdd(keyword, this);
|
||||
}
|
||||
|
||||
public static readonly Dictionary<string, Keyword> Keywords = new();
|
||||
|
||||
public override bool Equals(Keyword other)
|
||||
{
|
||||
return this.GetType() == other.GetType();
|
||||
}
|
||||
|
||||
public abstract Keyword ControlContext(SymbolizationContext context, ScriptWordVariable next);
|
||||
}
|
||||
|
||||
public abstract class Keyword<T> : Keyword where T : Keyword<T>, new()
|
||||
{
|
||||
private static T MyInstance = new();
|
||||
|
||||
protected Keyword(string keyword) : base(keyword)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool Equals(Variable other)
|
||||
{
|
||||
return MyInstance == other;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace Convention.Symbolization.Keyword
|
||||
{
|
||||
/// <summary>
|
||||
/// <b><see langword="import"/> file</b>
|
||||
/// </summary>
|
||||
public sealed class Import : Internal.Keyword<Import>
|
||||
{
|
||||
public Import() : base("import")
|
||||
{
|
||||
}
|
||||
|
||||
public override Internal.Keyword CloneVariable(string targetSymbolName)
|
||||
{
|
||||
return new Import();
|
||||
}
|
||||
|
||||
private ToolFile ImportFile = new("./");
|
||||
private string buffer = "";
|
||||
|
||||
public override Internal.Keyword ControlContext(SymbolizationContext context, Internal.ScriptWordVariable next)
|
||||
{
|
||||
if (next.word == ";")
|
||||
{
|
||||
var importContext = new SymbolizationContext(context);
|
||||
importContext.Compile(ImportFile);
|
||||
return null;
|
||||
}
|
||||
else if(next.word==".")
|
||||
{
|
||||
ImportFile = ImportFile | buffer;
|
||||
buffer = "";
|
||||
if (ImportFile.Exists() == false)
|
||||
throw new FileNotFoundException($"File path not found: {ImportFile}", ImportFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer += next.word;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="namespace"/> name { ... }</b>
|
||||
/// </summary>
|
||||
public sealed class Namespace : Internal.Keyword<Namespace>
|
||||
{
|
||||
public Namespace() : base("namespace")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="def"/> FunctionName(parameter-list) return-type { ... return return-type-instance; }</b>
|
||||
/// </summary>
|
||||
public sealed class FunctionDef : Internal.Keyword<FunctionDef>
|
||||
{
|
||||
public FunctionDef() : base("def")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="return"/> symbol;</b>
|
||||
/// </summary>
|
||||
public sealed class Return : Internal.Keyword<Return>
|
||||
{
|
||||
public Return() : base("return")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="if"/>(bool-expression) expression</b>
|
||||
/// </summary>
|
||||
public sealed class If : Internal.Keyword<If>
|
||||
{
|
||||
public If() : base("if")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="if"/> expression <see langword="else"/> expression</b>
|
||||
/// </summary>
|
||||
public sealed class Else : Internal.Keyword<Else>
|
||||
{
|
||||
public Else() : base("else")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="while"/>(bool-expression) expression</b>
|
||||
/// </summary>
|
||||
public sealed class While : Internal.Keyword<While>
|
||||
{
|
||||
public While() : base("while")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="break"/>;</b>
|
||||
/// </summary>
|
||||
public sealed class Break : Internal.Keyword<Break>
|
||||
{
|
||||
public Break() : base("break")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="continue"/>;</b>
|
||||
/// </summary>
|
||||
public sealed class Continue : Internal.Keyword<Continue>
|
||||
{
|
||||
public Continue() : base("continue")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="struct"/> structureName { ... }</b>
|
||||
/// </summary>
|
||||
public sealed class Structure : Internal.Keyword<Structure>
|
||||
{
|
||||
public Structure() : base("struct")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,185 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public sealed class Namespace : Variable<Namespace>, ICanFindVariable
|
||||
{
|
||||
private int Updateable = 0;
|
||||
|
||||
public void BeginUpdate()
|
||||
{
|
||||
Updateable++;
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, Namespace> SubNamespaces = new();
|
||||
private readonly Dictionary<FunctionSymbol, Function> Functions = new();
|
||||
private readonly Dictionary<string, Structure> Structures = new();
|
||||
|
||||
public Namespace CreateOrGetSubNamespace(string subNamespace)
|
||||
{
|
||||
if (Updateable == 0)
|
||||
throw new InvalidOperationException("Cannot create sub-namespace outside of an update block.");
|
||||
if(!SubNamespaces.TryGetValue(subNamespace,out var result))
|
||||
{
|
||||
result = new Namespace(subNamespace);
|
||||
SubNamespaces[subNamespace] = result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void AddFunction(Function function)
|
||||
{
|
||||
if (Updateable == 0)
|
||||
throw new InvalidOperationException("Cannot add function outside of an update block.");
|
||||
ArgumentNullException.ThrowIfNull(function);
|
||||
Functions.Add(function.FunctionInfo, function);
|
||||
}
|
||||
|
||||
public void AddStructure(Structure structure)
|
||||
{
|
||||
if (Updateable == 0)
|
||||
throw new InvalidOperationException("Cannot add structure outside of an update block.");
|
||||
ArgumentNullException.ThrowIfNull(structure);
|
||||
Structures.Add(structure.SymbolInfo.SymbolName, structure);
|
||||
}
|
||||
|
||||
public bool EndAndTApplyUpdate()
|
||||
{
|
||||
Updateable--;
|
||||
if (Updateable == 0)
|
||||
Refresh();
|
||||
return Updateable == 0;
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, int> SymbolCounter = new();
|
||||
private readonly Dictionary<string, List<Variable>> Symbol2Variable = new();
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
// Refresh Symbols
|
||||
SymbolCounter.Clear();
|
||||
foreach (var ns in SubNamespaces)
|
||||
{
|
||||
ns.Value.Refresh();
|
||||
foreach (var symbol in ns.Value.SymbolCounter)
|
||||
{
|
||||
if (SymbolCounter.ContainsKey(symbol.Key))
|
||||
SymbolCounter[symbol.Key] += symbol.Value;
|
||||
else
|
||||
SymbolCounter[symbol.Key] = symbol.Value;
|
||||
}
|
||||
foreach (var symbol in ns.Value.Symbol2Variable)
|
||||
{
|
||||
if (Symbol2Variable.TryGetValue(symbol.Key, out List<Variable> value))
|
||||
value.AddRange(symbol.Value);
|
||||
else
|
||||
Symbol2Variable[symbol.Key] = new(symbol.Value);
|
||||
}
|
||||
}
|
||||
foreach (var func in Functions)
|
||||
{
|
||||
{
|
||||
if (SymbolCounter.TryGetValue(func.Key.FunctionName, out var value))
|
||||
SymbolCounter[func.Key.FunctionName] = ++value;
|
||||
else
|
||||
SymbolCounter[func.Key.FunctionName] = 1;
|
||||
}
|
||||
{
|
||||
if (Symbol2Variable.TryGetValue(func.Key.FunctionName, out var value))
|
||||
value.Add(func.Value);
|
||||
else
|
||||
Symbol2Variable[func.Key.FunctionName] = new() { func.Value };
|
||||
}
|
||||
{
|
||||
if (Symbol2Variable.TryGetValue(func.Key.FunctionName, out var value))
|
||||
value.Add(func.Value);
|
||||
else
|
||||
Symbol2Variable[func.Key.FunctionName] = new() { func.Value };
|
||||
}
|
||||
}
|
||||
foreach (var @struct in Structures)
|
||||
{
|
||||
{
|
||||
if (SymbolCounter.TryGetValue(@struct.Key, out int value))
|
||||
SymbolCounter[@struct.Key] = ++value;
|
||||
else
|
||||
SymbolCounter[@struct.Key] = 1;
|
||||
}
|
||||
{
|
||||
if (Symbol2Variable.TryGetValue(@struct.Key, out List<Variable> value))
|
||||
value.Add(@struct.Value);
|
||||
else
|
||||
Symbol2Variable[@struct.Key] = new() { @struct.Value };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Namespace CreateRootNamespace()
|
||||
{
|
||||
return new("global");
|
||||
}
|
||||
|
||||
private Namespace(string symbolName) : base(symbolName) { }
|
||||
|
||||
public override bool Equals(Namespace other)
|
||||
{
|
||||
return this == other;
|
||||
}
|
||||
|
||||
public string GetNamespaceName() => this.SymbolInfo.SymbolName;
|
||||
|
||||
public bool ContainsSymbol(string symbolName)
|
||||
{
|
||||
return SymbolCounter.ContainsKey(symbolName);
|
||||
}
|
||||
|
||||
public int CountSymbol(string symbolName)
|
||||
{
|
||||
return SymbolCounter.TryGetValue(symbolName, out var count) ? count : 0;
|
||||
}
|
||||
|
||||
public Variable[] Find(string symbolName)
|
||||
{
|
||||
if (!Symbol2Variable.TryGetValue(symbolName,out var result))
|
||||
return Array.Empty<Variable>();
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
public Namespace FindSubNamespace(string subNamespaceName)
|
||||
{
|
||||
if (!SubNamespaces.TryGetValue(subNamespaceName, out var result))
|
||||
return null;
|
||||
return result;
|
||||
}
|
||||
|
||||
public Function[] FindFunction(string symbolName)
|
||||
{
|
||||
if (!Symbol2Variable.TryGetValue(symbolName, out var result))
|
||||
return Array.Empty<Function>();
|
||||
return result.OfType<Function>().ToArray();
|
||||
}
|
||||
|
||||
public Function FindFunctionInNamespace(FunctionSymbol symbol)
|
||||
{
|
||||
if (!Functions.TryGetValue(symbol, out var result))
|
||||
return null;
|
||||
return result;
|
||||
}
|
||||
|
||||
public Structure[] FindStructure(string symbolName)
|
||||
{
|
||||
if (!Symbol2Variable.TryGetValue(symbolName, out var result))
|
||||
return Array.Empty<Structure>();
|
||||
return result.OfType<Structure>().ToArray();
|
||||
}
|
||||
|
||||
public Structure FindStructureInNamespace(string symbolName)
|
||||
{
|
||||
if (!Structures.TryGetValue(symbolName, out var result))
|
||||
return null;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,57 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Convention.Symbolization.Primitive
|
||||
{
|
||||
public class PrimitiveType<T> : Internal.Variable
|
||||
{
|
||||
public PrimitiveType() : base(new(typeof(T).Name, typeof(T)))
|
||||
{
|
||||
}
|
||||
|
||||
public override bool Equals(Internal.Variable other)
|
||||
{
|
||||
return other is PrimitiveType<T>;
|
||||
}
|
||||
|
||||
public virtual T CloneValue(T value)
|
||||
{
|
||||
if (Utility.IsNumber(typeof(T)))
|
||||
return value;
|
||||
else if (Utility.IsString(typeof(T)))
|
||||
return (T)(object)new string((string)(object)value);
|
||||
else if (value is ICloneable cloneable)
|
||||
return (T)cloneable.Clone();
|
||||
else if (value is Internal.Variable)
|
||||
return value;
|
||||
return value;
|
||||
}
|
||||
|
||||
public virtual T DefaultValue() => default;
|
||||
}
|
||||
|
||||
public class PrimitiveInstance<T> : Internal.CloneableVariable<PrimitiveInstance<T>>
|
||||
{
|
||||
private readonly PrimitiveType<T> MyPrimitiveType = new();
|
||||
public T Value;
|
||||
|
||||
public PrimitiveInstance(string symbolName, T value, PrimitiveType<T> primitiveType) : base(symbolName)
|
||||
{
|
||||
this.Value = value;
|
||||
this.MyPrimitiveType = primitiveType;
|
||||
}
|
||||
|
||||
public override PrimitiveInstance<T> CloneVariable(string targetSymbolName)
|
||||
{
|
||||
return new(targetSymbolName, MyPrimitiveType.CloneValue(this.Value), this.MyPrimitiveType);
|
||||
}
|
||||
|
||||
public override bool Equals(PrimitiveInstance<T> other)
|
||||
{
|
||||
return this.Value.Equals(other.Value);
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return Value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,27 +0,0 @@
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public class ScriptWordVariable : CloneableVariable<ScriptWordVariable>
|
||||
{
|
||||
public readonly string word;
|
||||
|
||||
public ScriptWordVariable(string word) : base("")
|
||||
{
|
||||
this.word = (string)word.Clone();
|
||||
}
|
||||
|
||||
public override ScriptWordVariable CloneVariable(string targetSymbolName)
|
||||
{
|
||||
return new ScriptWordVariable(word);
|
||||
}
|
||||
|
||||
public override bool Equals(ScriptWordVariable other)
|
||||
{
|
||||
return other is not null && word.Equals(other.word);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return word;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,87 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public sealed class Structure : Variable<Structure>, ICanFindVariable
|
||||
{
|
||||
private Dictionary<string, Variable> MemberFields;
|
||||
public readonly Namespace ParentNamespace;
|
||||
|
||||
public Dictionary<string, Variable> CloneMemberFields()
|
||||
{
|
||||
return new(from pair in MemberFields
|
||||
select new KeyValuePair<string, Variable>(
|
||||
pair.Key,
|
||||
pair.Value is ICloneable cloneable ? (cloneable.Clone() as Variable) : pair.Value)
|
||||
);
|
||||
}
|
||||
|
||||
private Structure(string symbolName, Namespace parentNamespace) : base(symbolName)
|
||||
{
|
||||
this.ParentNamespace = parentNamespace;
|
||||
}
|
||||
|
||||
public static Structure Create(string structureName, Namespace parent)
|
||||
{
|
||||
Structure result = new(structureName, parent);
|
||||
parent.AddStructure(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public override bool Equals(Structure other)
|
||||
{
|
||||
return this == other;
|
||||
}
|
||||
|
||||
public Variable[] Find(string symbolName)
|
||||
{
|
||||
if (MemberFields.TryGetValue(symbolName, out var variable))
|
||||
{
|
||||
return new[] { variable };
|
||||
}
|
||||
return Array.Empty<Variable>();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class StructureInstance : CloneableVariable<StructureInstance>,ICanFindVariable
|
||||
{
|
||||
public readonly Structure StructureType;
|
||||
private readonly Dictionary<string, Variable> MemberFields;
|
||||
public StructureInstance(string symbolName, Structure structureType)
|
||||
: base(symbolName)
|
||||
{
|
||||
this.StructureType = structureType;
|
||||
this.MemberFields = structureType.CloneMemberFields();
|
||||
}
|
||||
public override bool Equals(StructureInstance other)
|
||||
{
|
||||
return this == other;
|
||||
}
|
||||
public override StructureInstance CloneVariable(string targetSymbolName)
|
||||
{
|
||||
return new StructureInstance(targetSymbolName, this.StructureType);
|
||||
}
|
||||
|
||||
public Variable GetField(string memberName)
|
||||
{
|
||||
if (MemberFields.TryGetValue(memberName, out var result))
|
||||
return result;
|
||||
else
|
||||
throw new KeyNotFoundException($"Member '{memberName}' not found in structure '{StructureType.SymbolInfo.SymbolName}'.");
|
||||
}
|
||||
|
||||
public Variable[] Find(string symbolName)
|
||||
{
|
||||
if (MemberFields.TryGetValue(symbolName, out var variable))
|
||||
{
|
||||
return new[] { variable };
|
||||
}
|
||||
return Array.Empty<Variable>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,112 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public abstract class Variable : IEquatable<Variable>
|
||||
{
|
||||
public readonly VariableSymbol SymbolInfo;
|
||||
|
||||
protected Variable(VariableSymbol symbolInfo)
|
||||
{
|
||||
this.SymbolInfo = symbolInfo;
|
||||
}
|
||||
|
||||
public abstract bool Equals(Variable other);
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as Variable);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return SymbolInfo.SymbolName;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public abstract class Variable<T> : Variable, IEquatable<T>
|
||||
{
|
||||
protected Variable(string symbolName) : base(new VariableSymbol(symbolName, typeof(T))) { }
|
||||
public abstract bool Equals(T other);
|
||||
|
||||
public override bool Equals(Variable other)
|
||||
{
|
||||
return other is T variable && Equals(variable);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is T variable && Equals(variable);
|
||||
}
|
||||
|
||||
public virtual int GetVariableHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
|
||||
public override sealed int GetHashCode()
|
||||
{
|
||||
return GetVariableHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
public interface ICanFindVariable
|
||||
{
|
||||
public Variable[] Find(string symbolName);
|
||||
}
|
||||
|
||||
public abstract class CloneableVariable<T> : Variable<T>, ICloneable
|
||||
{
|
||||
protected CloneableVariable(string symbolName) : base(symbolName)
|
||||
{
|
||||
}
|
||||
|
||||
public object Clone() => CloneVariable(SymbolInfo.SymbolName);
|
||||
|
||||
public abstract T CloneVariable(string targetSymbolName);
|
||||
}
|
||||
|
||||
public sealed class NullVariable : CloneableVariable<NullVariable>
|
||||
{
|
||||
public NullVariable(string symbolName) : base(symbolName)
|
||||
{
|
||||
}
|
||||
|
||||
public override NullVariable CloneVariable(string targetSymbolName)
|
||||
{
|
||||
return new(targetSymbolName);
|
||||
}
|
||||
|
||||
public override bool Equals(NullVariable other)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct VariableSymbol
|
||||
{
|
||||
public readonly string SymbolName;
|
||||
public readonly Type VariableType;
|
||||
|
||||
public VariableSymbol(string symbolName, Type variableType)
|
||||
{
|
||||
this.SymbolName = symbolName;
|
||||
this.VariableType = variableType;
|
||||
}
|
||||
|
||||
public bool Equals(VariableSymbol other)
|
||||
{
|
||||
return other.SymbolName.Equals(SymbolName) && other.VariableType.Equals(VariableType);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return new Tuple<string, Type>(SymbolName, VariableType).GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Convention.Symbolization
|
||||
{
|
||||
public class SymbolizationContext
|
||||
{
|
||||
public SymbolizationContext() : this(null, Internal.Namespace.CreateRootNamespace()) { }
|
||||
public SymbolizationContext(SymbolizationContext parent) : this(parent, parent.CurrentNamespace) { }
|
||||
public SymbolizationContext(SymbolizationContext parent, Internal.Namespace newNamespace)
|
||||
{
|
||||
this.ParentContext = parent;
|
||||
this.CurrentNamespace = newNamespace;
|
||||
}
|
||||
|
||||
private readonly SymbolizationContext ParentContext;
|
||||
public readonly Internal.Namespace CurrentNamespace;
|
||||
|
||||
public void Compile(string allText)
|
||||
{
|
||||
// Create a new reader to parse the script words
|
||||
Internal.SymbolizationReader reader = new();
|
||||
foreach (char ch in allText)
|
||||
reader.ReadChar(ch);
|
||||
var ScriptWords = reader.ScriptWords;
|
||||
// Turn the script words into scope words
|
||||
reader.CompleteScopeWord(this);
|
||||
}
|
||||
|
||||
public void Compile(ToolFile file)
|
||||
{
|
||||
if (file.Exists() == false)
|
||||
throw new FileNotFoundException($"File not found: {file}", file);
|
||||
var text = file.LoadAsText();
|
||||
this.Compile(text);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,139 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public class SymbolizationReader
|
||||
{
|
||||
#region Read Script Words
|
||||
|
||||
private string buffer = "";
|
||||
private int lineContext = 1;
|
||||
private bool isOutOfStringline = true;
|
||||
public Dictionary<int, List<Variable>> ScriptWords = new() { { 1, new() } };
|
||||
|
||||
private static HashSet<char> ControllerCharSet = new()
|
||||
{
|
||||
'{','}','(',')','[',']',
|
||||
'+','-','*','/','%',
|
||||
'=',
|
||||
'!','<','>','&','|',
|
||||
'^',
|
||||
':',',','.','?',/*'\'','"',*/
|
||||
'@','#','$',
|
||||
';'
|
||||
};
|
||||
|
||||
public void ReadChar(char ch)
|
||||
{
|
||||
if (isOutOfStringline)
|
||||
{
|
||||
if (ControllerCharSet.Contains(ch))
|
||||
CompleteSingleSymbol(ch);
|
||||
else if (ch == '\n')
|
||||
CompleteLine();
|
||||
else if (char.IsWhiteSpace(ch) || ch == '\r' || ch == '\t')
|
||||
CompleteWord();
|
||||
else if (buffer.Length == 0 && ch == '"')
|
||||
BeginString();
|
||||
else if (char.IsLetter(ch) || char.IsDigit(ch))
|
||||
PushChar(ch);
|
||||
else
|
||||
throw new NotImplementedException($"{lineContext + 1} line, {buffer} + <{ch}> not implemented");
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((buffer.Length > 0 && buffer.Last() == '\\') || ch != '"')
|
||||
PushChar(ch);
|
||||
else if (ch == '"')
|
||||
EndString();
|
||||
else
|
||||
throw new NotImplementedException($"{lineContext + 1} line, \"{buffer}\" + \'{ch}\' not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
void CompleteSingleSymbol(char ch)
|
||||
{
|
||||
CompleteWord();
|
||||
buffer = ch.ToString();
|
||||
CompleteWord();
|
||||
}
|
||||
|
||||
void PushChar(char ch)
|
||||
{
|
||||
buffer += ch;
|
||||
}
|
||||
void CompleteWord()
|
||||
{
|
||||
if (buffer.Length > 0)
|
||||
{
|
||||
if (ScriptWords.TryGetValue(lineContext, out var line) == false)
|
||||
{
|
||||
ScriptWords.Add(lineContext, line = new());
|
||||
}
|
||||
if (Internal.Keyword.Keywords.TryGetValue(buffer, out var keyword))
|
||||
{
|
||||
line.Add(keyword.Clone() as Internal.Variable);
|
||||
}
|
||||
else
|
||||
{
|
||||
line.Add(new ScriptWordVariable(buffer));
|
||||
}
|
||||
buffer = "";
|
||||
}
|
||||
}
|
||||
void CompleteLine()
|
||||
{
|
||||
CompleteWord();
|
||||
lineContext++; ;
|
||||
}
|
||||
void BeginString()
|
||||
{
|
||||
if (buffer.Length > 0)
|
||||
throw new InvalidGrammarException($"String must start with nothing: {buffer}");
|
||||
isOutOfStringline = false;
|
||||
}
|
||||
void EndString()
|
||||
{
|
||||
isOutOfStringline = true;
|
||||
CompleteWord();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Read Scope Words
|
||||
|
||||
public void CompleteScopeWord(SymbolizationContext rootContext)
|
||||
{
|
||||
int wordCounter = 1;
|
||||
Internal.Keyword currentKey = null;
|
||||
foreach(var line in ScriptWords)
|
||||
{
|
||||
foreach (var word in line.Value)
|
||||
{
|
||||
if (currentKey == null)
|
||||
{
|
||||
if(currentKey is Internal.Keyword cky)
|
||||
{
|
||||
currentKey = cky;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidGrammarException($"Line {line.Key}, word {wordCounter}: Expected a keyword, but got {word}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
currentKey = currentKey.ControlContext(rootContext, word);
|
||||
}
|
||||
wordCounter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.Symbolization
|
||||
{
|
||||
public class SymbolizationRunner
|
||||
{
|
||||
private SymbolizationContext Context;
|
||||
|
||||
public void Compile(string path)
|
||||
{
|
||||
Context = new();
|
||||
Context.Compile(new ToolFile(path));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -1,24 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using Convention;
|
||||
using Convention;
|
||||
using Convention.EasySave;
|
||||
using Convention.Symbolization;
|
||||
using Convention.RScript;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
public class Program
|
||||
{
|
||||
static class Test
|
||||
{
|
||||
public static object Func(object x)
|
||||
{
|
||||
Console.WriteLine(x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var context = new SymbolizationRunner();
|
||||
try
|
||||
RScriptEngine engine = new();
|
||||
RScriptImportClass import = new()
|
||||
{
|
||||
context.Compile("example_script.txt");
|
||||
Console.WriteLine("Script compiled successfully.");
|
||||
}
|
||||
catch (FileNotFoundException ex)
|
||||
typeof(Math),
|
||||
typeof(ExpressionMath),
|
||||
typeof(Test)
|
||||
};
|
||||
|
||||
|
||||
var result = engine.Compile(@"
|
||||
int i= 2;
|
||||
int count = 0;
|
||||
label(test);
|
||||
goto(true,func1);
|
||||
Func(i);
|
||||
goto(100>i,test);
|
||||
|
||||
goto(context.ExistNamespace(""x""),end);
|
||||
namespace(x)
|
||||
{
|
||||
Console.WriteLine($"Error: {ex.Message}");
|
||||
}
|
||||
Func(""xxx"");
|
||||
}
|
||||
|
||||
namespace(func1)
|
||||
{
|
||||
i = Pow(i,2);
|
||||
count = count + 1;
|
||||
Func(count);
|
||||
}
|
||||
|
||||
label(end);
|
||||
", import);
|
||||
var data = RScriptSerializer.SerializeClass(result);
|
||||
var file = new ToolFile("F:\\test_after_run.dat");
|
||||
file.SaveAsBinary(data);
|
||||
data = file.LoadAsBinary();
|
||||
engine.Run(RScriptSerializer.DeserializeClass(data), import);
|
||||
return;
|
||||
var data2 = RScriptSerializer.SerializeClass(engine.GetCompileResultFromCurrent());
|
||||
var file2 = new ToolFile("F:\\test_after_run.dat");
|
||||
file2.SaveAsBinary(data2);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user