Compare commits

..

30 Commits

Author SHA1 Message Date
b1c36c08fb EP RScript/内置辅助解释 2025-10-21 15:31:57 +08:00
d2bd58568a EP RScript/新增跳转缓存 2025-10-21 10:26:37 +08:00
15dd1c9aa4 EP RScript/优化跳转 2025-10-17 16:43:15 +08:00
21f3c580eb 修复了File中有关二进制的错误 2025-10-17 16:25:18 +08:00
09b334ca58 修复一些内容 2025-10-17 15:47:20 +08:00
4dc6691650 EP RScript 2025-10-16 20:15:59 +08:00
22b8c1838a EP RScript/修复遗漏 2025-10-16 17:24:06 +08:00
b9012674d6 EP RScript/解离出Runner 2025-10-16 15:21:33 +08:00
8a4edfcb79 EP RScript/允许变量声明时赋值 2025-10-16 11:58:47 +08:00
8d6f96b99a Save 2025-10-16 11:29:50 +08:00
a91c9741e4 加入自动类型转换 2025-10-16 10:25:31 +08:00
9d7fbf9786 Update RScript 2025-10-16 00:39:45 +08:00
8e8edd8724 Update RScript/新增NamedSpace为命名空间命名 2025-10-15 16:50:47 +08:00
83ecbfbfd3 Submodule RScript 2025-10-13 19:44:56 +08:00
b5e2f4cc16 准备将RScript作为独立依赖 2025-10-13 19:43:54 +08:00
bacfcd550f EP RScript 修复因跳跃导致的变量层次混乱 2025-10-11 17:24:40 +08:00
f850030d10 Save 2025-10-11 09:53:30 +08:00
d58efb13e2 Save 2025-10-10 18:01:38 +08:00
c07c64be1e EP RScript 完成控制流, 基本完成 2025-10-10 11:04:43 +08:00
f8d81d9198 EP RScript 仍存在一些问题, 但是能够运行 2025-10-09 18:02:00 +08:00
a8cfb012fc EP RScript 2025-09-27 22:05:04 +08:00
5041bb5600 Update Architecture.cs 2025-07-31 15:41:31 +08:00
a72c6b285b 剥离Symbolization 2025-07-24 09:35:49 +08:00
632e58cfce BS 0.1.0 File 2025-07-16 15:25:11 +08:00
b8836df198 BS 0.1.0 修复Architecture.TimelineUpdate无法立即更新所有能够更新的错误 2025-07-14 22:09:35 +08:00
e8286f711f EP Symbolization 完善关键字行为 2025-07-08 00:17:44 +08:00
71fc384095 EP Symbolization 构建作用域 2025-07-07 00:28:37 +08:00
ninemine
522010e7cd EP Symbolization 重构line-word-index 2025-07-05 16:57:59 +08:00
ninemine
8123ed4b15 EP Symbolization 完成ScopeWords解析 2025-07-05 15:10:24 +08:00
ninemine
acada40141 EP Symboliztion +Namespace 2025-07-04 23:00:41 +08:00
21 changed files with 973 additions and 1108 deletions

View File

@@ -1,7 +1,5 @@
---
description:
globs:
alwaysApply: true
alwaysApply: false
---
## RIPER-5 + O1 思维 + 代理执行协议

6
.gitmodules vendored Normal file
View 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

View File

@@ -5,6 +5,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Convention</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>

1
Convention/[FLEE] Submodule

Submodule Convention/[FLEE] added at 47b12f4bc0

1
Convention/[RScript] Submodule

Submodule Convention/[RScript] added at 4860aa251e

View File

@@ -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,16 +368,21 @@ namespace Convention
public static void UpdateTimeline()
{
foreach (var pair in TimelineQuenes)
for (bool stats = true; stats;)
{
var timeline = pair.Value;
if(timeline.Quene[timeline.Context].predicate())
stats = false;
foreach (var pair in TimelineQuenes)
{
foreach (var action in timeline.Quene[timeline.Context].actions)
var timeline = pair.Value;
if (timeline.Quene[timeline.Context].predicate())
{
action();
stats = true;
foreach (var action in timeline.Quene[timeline.Context].actions)
{
action();
}
timeline.Context++;
}
timeline.Context++;
}
}
}

View File

@@ -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

View File

@@ -1,6 +0,0 @@
using System;
namespace Convention
{
}

View File

@@ -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) { }
}
}

View File

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

View File

@@ -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")
{
}
}
}

View File

@@ -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;
}
}
}

View File

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

View File

@@ -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;
}
}
}

View File

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

View File

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

View File

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

View File

@@ -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
}
}

View File

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

View File

@@ -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 void Main(string[] args)
static class Test
{
var context = new SymbolizationRunner();
try
public static object Func(object x)
{
context.Compile("example_script.txt");
Console.WriteLine("Script compiled successfully.");
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine(x);
return x;
}
}
static void Main(string[] args)
{
RScriptEngine engine = new();
RScriptImportClass import = new()
{
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)
{
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);
}
}