Compare commits
10 Commits
6783429ab6
...
a8cfb012fc
Author | SHA1 | Date | |
---|---|---|---|
a8cfb012fc | |||
5041bb5600 | |||
a72c6b285b | |||
632e58cfce | |||
b8836df198 | |||
e8286f711f | |||
71fc384095 | |||
![]() |
522010e7cd | ||
![]() |
8123ed4b15 | ||
![]() |
acada40141 |
@@ -5,6 +5,7 @@
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Convention</RootNamespace>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
325
Convention/[RScript]/ScriptContent.cs
Normal file
325
Convention/[RScript]/ScriptContent.cs
Normal file
@@ -0,0 +1,325 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Convention.RScript
|
||||
{
|
||||
public partial class ScriptContent
|
||||
{
|
||||
public object RuntimeBindingTarget;
|
||||
|
||||
public struct RuntimeContext
|
||||
{
|
||||
public int iterator;
|
||||
public bool isResetIterator;
|
||||
|
||||
public static RuntimeContext Create()
|
||||
{
|
||||
return new RuntimeContext()
|
||||
{
|
||||
iterator = 0,
|
||||
isResetIterator = false
|
||||
};
|
||||
}
|
||||
|
||||
public RuntimeContext Clone()
|
||||
{
|
||||
return new()
|
||||
{
|
||||
iterator = iterator,
|
||||
isResetIterator = isResetIterator
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class ScriptDataEntry
|
||||
{
|
||||
public string text;
|
||||
|
||||
public string command;
|
||||
public string[] parameters;
|
||||
public int iterator;
|
||||
|
||||
public Func<RuntimeContext, RuntimeContext> actor;
|
||||
}
|
||||
|
||||
private List<ScriptDataEntry> Commands = new();
|
||||
|
||||
// 预处理工具
|
||||
|
||||
public static int LastIndexOfNextTerminalTail(string text, int i, string terminal)
|
||||
{
|
||||
return text.IndexOf(terminal, i) + terminal.Length - 1;
|
||||
}
|
||||
|
||||
public static bool StartWithSpecialTerminal(string text,int i)
|
||||
{
|
||||
switch (text[i])
|
||||
{
|
||||
case '{':
|
||||
case '}':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public const string SingleLineTerminal = "//";
|
||||
public const string MultiLineBeginTerminal = "/*";
|
||||
public const string MultiLineEndTerminal = "*/";
|
||||
public const string TextTerminal = "\"";
|
||||
|
||||
public static bool StartWithSingleLineTerminal(string text, int i)
|
||||
{
|
||||
int length = SingleLineTerminal.Length;
|
||||
if (text.Length - i < length)
|
||||
return false;
|
||||
return string.Compare(text, i, SingleLineTerminal, 0, length) == 0;
|
||||
}
|
||||
|
||||
public static bool StartWithMultiLineTerminal(string text, int i)
|
||||
{
|
||||
int length = MultiLineBeginTerminal.Length;
|
||||
if (text.Length - i < length)
|
||||
return false;
|
||||
return string.Compare(text, i, MultiLineBeginTerminal, 0, length) == 0;
|
||||
}
|
||||
|
||||
public static bool StartWithTextTerminal(string text, int i)
|
||||
{
|
||||
int length = TextTerminal.Length;
|
||||
if (text.Length - i < length)
|
||||
return false;
|
||||
return string.Compare(text, i, TextTerminal, 0, length) == 0;
|
||||
}
|
||||
|
||||
public static int LastIndexOfNextTextTerminalTail(string text, int i,out string buffer)
|
||||
{
|
||||
char terminal = TextTerminal[0];
|
||||
int __head = i + 1;
|
||||
buffer = "";
|
||||
for (int __tail = text.Length; __head < __tail && text[__head] != terminal;)
|
||||
{
|
||||
if (text[__head] == '\\')
|
||||
{
|
||||
switch (text[__head+1])
|
||||
{
|
||||
case 'n':
|
||||
{
|
||||
buffer += '\n';
|
||||
}
|
||||
break;
|
||||
case 't':
|
||||
{
|
||||
buffer += '\t';
|
||||
}
|
||||
break;
|
||||
case 'r':
|
||||
{
|
||||
buffer += '\r';
|
||||
}
|
||||
break;
|
||||
case '0':
|
||||
{
|
||||
buffer += '\0';
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
buffer += text[__head + 1];
|
||||
}
|
||||
break;
|
||||
}
|
||||
__head += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer += text[__head];
|
||||
__head++;
|
||||
}
|
||||
}
|
||||
return __head;
|
||||
}
|
||||
|
||||
// 文本预处理
|
||||
private void ScriptTextPreprocessing(string script)
|
||||
{
|
||||
string buffer = "";
|
||||
void PushBuffer()
|
||||
{
|
||||
if (string.IsNullOrEmpty(buffer) == false)
|
||||
{
|
||||
Commands.Add(new()
|
||||
{
|
||||
text = buffer.Trim(),
|
||||
iterator = Commands.Count
|
||||
});
|
||||
}
|
||||
buffer = "";
|
||||
}
|
||||
Func<int, bool> loopPr = x => x < script.Length;
|
||||
for (int i = 0; loopPr(i); i++)
|
||||
{
|
||||
// 切断语句\
|
||||
if (script[i] == ';')
|
||||
{
|
||||
PushBuffer();
|
||||
}
|
||||
// 读入特殊标记符
|
||||
else if (StartWithSpecialTerminal(script, i))
|
||||
{
|
||||
PushBuffer();
|
||||
buffer += script[i];
|
||||
PushBuffer();
|
||||
}
|
||||
// 跳过单行注释
|
||||
else if (StartWithSingleLineTerminal(script, i))
|
||||
{
|
||||
i = LastIndexOfNextTerminalTail(script, i, "\n");
|
||||
}
|
||||
// 跳过多行注释
|
||||
else if (StartWithMultiLineTerminal(script, i))
|
||||
{
|
||||
i = LastIndexOfNextTerminalTail(script, i, MultiLineEndTerminal);
|
||||
}
|
||||
// 读入文本
|
||||
else if (StartWithTextTerminal(script, i))
|
||||
{
|
||||
i = LastIndexOfNextTextTerminalTail(script, i, out var temp);
|
||||
buffer += temp;
|
||||
}
|
||||
// 读入
|
||||
else if (loopPr(i))
|
||||
buffer += script[i];
|
||||
}
|
||||
// 存在未完成的语句
|
||||
if (string.IsNullOrEmpty(buffer) == false)
|
||||
throw new ArgumentException("The script did not end with the correct terminator.");
|
||||
}
|
||||
|
||||
// 指令预处理工具
|
||||
|
||||
private Dictionary<int, ScriptDataEntry> CommandIndexs = new();
|
||||
private Dictionary<string, int> LabelIndexs = new();
|
||||
|
||||
public const string GotoTerminal = "goto";
|
||||
public const string LabelTerminal = "label";
|
||||
public const string IfTerminal = "if";
|
||||
|
||||
// 指令预处理
|
||||
private void CommandPreprocessing()
|
||||
{
|
||||
static GroupCollection Match(ScriptDataEntry entry, [StringSyntax(StringSyntaxAttribute.Regex)] string pattern)
|
||||
{
|
||||
Match match = Regex.Match(entry.text, pattern);
|
||||
if (!match.Success)
|
||||
throw new ArgumentException($"Script: \"{entry.text}\"<command iterator: {entry.iterator}> is invalid statement");
|
||||
return match.Groups;
|
||||
}
|
||||
|
||||
// 加入自由映射
|
||||
foreach (var entry in Commands)
|
||||
{
|
||||
CommandIndexs.Add(entry.iterator, entry);
|
||||
}
|
||||
// 匹配
|
||||
foreach (var entry in Commands)
|
||||
{
|
||||
if (
|
||||
// goto label-name;
|
||||
entry.text.StartsWith(GotoTerminal)||
|
||||
// label label-name;
|
||||
entry.text.StartsWith(LabelTerminal)
|
||||
)
|
||||
{
|
||||
var groups = Match(entry, @"^(\S*)\s+([^\s]+?)\s*;$");
|
||||
entry.command = groups[1].Value;
|
||||
entry.parameters = new string[] { groups[2].Value };
|
||||
// touch label
|
||||
if (entry.command == LabelTerminal)
|
||||
{
|
||||
LabelIndexs.Add(groups[2].Value, entry.iterator);
|
||||
}
|
||||
}
|
||||
else if (
|
||||
// if(expr)
|
||||
entry.text.StartsWith(IfTerminal)
|
||||
)
|
||||
{
|
||||
var groups = Match(entry, @"^(\S*)\s*(.*?)$");
|
||||
entry.command = groups[1].Value;
|
||||
entry.parameters = new string[] { groups[2].Value };
|
||||
}
|
||||
else
|
||||
{
|
||||
var groups = Match(entry, @"^(\w+)\s*\(\s*(.*?)\s*\)\s*;$");
|
||||
entry.command = groups[1].Value;
|
||||
|
||||
static string[] ParseArguments(string argumentsString)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(argumentsString))
|
||||
return new string[0];
|
||||
|
||||
// 处理字符串字面量和普通参数的正则表达式
|
||||
string argPattern = @"""(?:[^""\\]|\\.)*""|[^,]+";
|
||||
|
||||
return Regex.Matches(argumentsString, argPattern)
|
||||
.Cast<Match>()
|
||||
.Select(m => m.Value.Trim())
|
||||
.Where(arg => !string.IsNullOrEmpty(arg))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
entry.parameters = ParseArguments(groups[2].Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 指令转化
|
||||
|
||||
private void LoopForTC2AC()
|
||||
{
|
||||
Dictionary<string, MethodInfo> cache = (from method in RuntimeBindingTarget.GetType().GetMethods()
|
||||
select new KeyValuePair<string, MethodInfo>(method.Name, method)).ToDictionary();
|
||||
|
||||
for (int index = 0, e = Commands.Count; index < e; index++)
|
||||
{
|
||||
var entry = Commands[index];
|
||||
// Keywords
|
||||
if (entry.command == GotoTerminal)
|
||||
{
|
||||
entry.actor = context =>
|
||||
{
|
||||
var next = context.Clone();
|
||||
next.iterator = LabelIndexs[entry.parameters[0]];
|
||||
next.isResetIterator = true;
|
||||
return next;
|
||||
};
|
||||
}
|
||||
// Custom Binding
|
||||
else if(cache.TryGetValue(entry.command,out var methodInfo))
|
||||
{
|
||||
entry.actor = context =>
|
||||
{
|
||||
|
||||
return context.Clone();
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void LoadScript(string script)
|
||||
{
|
||||
ScriptTextPreprocessing(script);
|
||||
CommandPreprocessing();
|
||||
LoopForTC2AC();
|
||||
}
|
||||
}
|
||||
}
|
29
Convention/[RScript]/ScriptRunner.cs
Normal file
29
Convention/[RScript]/ScriptRunner.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.RScript
|
||||
{
|
||||
public class ScriptRunner
|
||||
{
|
||||
public ScriptContent BuildNewContent(object target)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
RuntimeBindingTarget = target,
|
||||
};
|
||||
}
|
||||
|
||||
public ScriptContent BuildNewContent()
|
||||
{
|
||||
return BuildNewContent(null);
|
||||
}
|
||||
|
||||
public void RunScriptFromContent(ScriptContent content)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
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));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
214
[Test]/FileTest.cs
Normal file
214
[Test]/FileTest.cs
Normal file
@@ -0,0 +1,214 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Convention;
|
||||
|
||||
namespace Convention.Test
|
||||
{
|
||||
public class FileTest
|
||||
{
|
||||
public static void RunTests()
|
||||
{
|
||||
Console.WriteLine("=== ToolFile 功能测试 ===");
|
||||
|
||||
// 测试基本文件操作
|
||||
TestBasicOperations();
|
||||
|
||||
// 测试文件加载和保存
|
||||
TestLoadAndSave();
|
||||
|
||||
// 测试压缩和解压
|
||||
TestCompression();
|
||||
|
||||
// 测试加密和解密
|
||||
TestEncryption();
|
||||
|
||||
// 测试哈希计算
|
||||
TestHash();
|
||||
|
||||
// 测试备份功能
|
||||
TestBackup();
|
||||
|
||||
// 测试权限管理
|
||||
TestPermissions();
|
||||
|
||||
Console.WriteLine("=== 所有测试完成 ===");
|
||||
}
|
||||
|
||||
private static void TestBasicOperations()
|
||||
{
|
||||
Console.WriteLine("\n--- 基本文件操作测试 ---");
|
||||
|
||||
// 创建测试文件
|
||||
var testFile = new ToolFile("test_file.txt");
|
||||
testFile.SaveAsText("这是一个测试文件的内容");
|
||||
|
||||
Console.WriteLine($"文件存在: {testFile.Exists()}");
|
||||
Console.WriteLine($"文件大小: {testFile.GetSize()} 字节");
|
||||
Console.WriteLine($"文件名: {testFile.GetFilename()}");
|
||||
Console.WriteLine($"文件扩展名: {testFile.GetExtension()}");
|
||||
Console.WriteLine($"文件目录: {testFile.GetDir()}");
|
||||
Console.WriteLine($"文件时间戳: {testFile.GetTimestamp()}");
|
||||
|
||||
// 清理
|
||||
testFile.Remove();
|
||||
}
|
||||
|
||||
private static void TestLoadAndSave()
|
||||
{
|
||||
Console.WriteLine("\n--- 文件加载和保存测试 ---");
|
||||
|
||||
// 测试 JSON 保存和加载
|
||||
var jsonFile = new ToolFile("test_data.json");
|
||||
var testData = new { name = "测试", value = 123, items = new[] { "item1", "item2" } };
|
||||
jsonFile.SaveAsRawJson(testData);
|
||||
|
||||
var loadedData = jsonFile.LoadAsRawJson<dynamic>();
|
||||
Console.WriteLine($"JSON 数据加载成功: {loadedData != null}");
|
||||
|
||||
// 测试 CSV 保存和加载
|
||||
var csvFile = new ToolFile("test_data.csv");
|
||||
var csvData = new List<string[]>
|
||||
{
|
||||
new[] { "姓名", "年龄", "城市" },
|
||||
new[] { "张三", "25", "北京" },
|
||||
new[] { "李四", "30", "上海" }
|
||||
};
|
||||
csvFile.SaveAsCsv(csvData);
|
||||
|
||||
var loadedCsv = csvFile.LoadAsCsv();
|
||||
Console.WriteLine($"CSV 数据加载成功: {loadedCsv.Count} 行");
|
||||
|
||||
// 清理
|
||||
jsonFile.Remove();
|
||||
csvFile.Remove();
|
||||
}
|
||||
|
||||
private static void TestCompression()
|
||||
{
|
||||
Console.WriteLine("\n--- 压缩和解压测试 ---");
|
||||
|
||||
// 创建测试文件
|
||||
var testFile = new ToolFile("test_compress.txt");
|
||||
testFile.SaveAsText("这是一个用于压缩测试的文件内容。".PadRight(1000, 'x'));
|
||||
|
||||
// 压缩文件
|
||||
var compressedFile = testFile.Compress();
|
||||
Console.WriteLine($"压缩成功: {compressedFile.Exists()}");
|
||||
Console.WriteLine($"压缩后大小: {compressedFile.GetSize()} 字节");
|
||||
|
||||
// 解压文件
|
||||
var decompressedDir = compressedFile.Decompress();
|
||||
Console.WriteLine($"解压成功: {decompressedDir.Exists()}");
|
||||
|
||||
// 清理
|
||||
testFile.Remove();
|
||||
compressedFile.Remove();
|
||||
if (decompressedDir.Exists())
|
||||
decompressedDir.Remove();
|
||||
}
|
||||
|
||||
private static void TestEncryption()
|
||||
{
|
||||
Console.WriteLine("\n--- 加密和解密测试 ---");
|
||||
|
||||
// 创建测试文件
|
||||
var testFile = new ToolFile("test_encrypt.txt");
|
||||
testFile.SaveAsText("这是一个用于加密测试的敏感数据");
|
||||
|
||||
// 加密文件
|
||||
testFile.Encrypt("mysecretkey123");
|
||||
Console.WriteLine($"加密成功: {testFile.Exists()}");
|
||||
|
||||
// 解密文件
|
||||
testFile.Decrypt("mysecretkey123");
|
||||
var decryptedContent = testFile.LoadAsText();
|
||||
Console.WriteLine($"解密成功: {decryptedContent.Contains("敏感数据")}");
|
||||
|
||||
// 清理
|
||||
testFile.Remove();
|
||||
}
|
||||
|
||||
private static void TestHash()
|
||||
{
|
||||
Console.WriteLine("\n--- 哈希计算测试 ---");
|
||||
|
||||
// 创建测试文件
|
||||
var testFile = new ToolFile("test_hash.txt");
|
||||
testFile.SaveAsText("这是一个用于哈希测试的文件内容");
|
||||
|
||||
// 计算不同算法的哈希值
|
||||
var md5Hash = testFile.CalculateHash("MD5");
|
||||
var sha256Hash = testFile.CalculateHash("SHA256");
|
||||
|
||||
Console.WriteLine($"MD5 哈希: {md5Hash}");
|
||||
Console.WriteLine($"SHA256 哈希: {sha256Hash}");
|
||||
|
||||
// 验证哈希值
|
||||
var isValid = testFile.VerifyHash(md5Hash, "MD5");
|
||||
Console.WriteLine($"哈希验证: {isValid}");
|
||||
|
||||
// 保存哈希值到文件
|
||||
var hashFile = testFile.SaveHash("MD5");
|
||||
Console.WriteLine($"哈希文件保存: {hashFile.Exists()}");
|
||||
|
||||
// 清理
|
||||
testFile.Remove();
|
||||
hashFile.Remove();
|
||||
}
|
||||
|
||||
private static void TestBackup()
|
||||
{
|
||||
Console.WriteLine("\n--- 备份功能测试 ---");
|
||||
|
||||
// 创建测试文件
|
||||
var testFile = new ToolFile("test_backup.txt");
|
||||
testFile.SaveAsText("这是一个用于备份测试的文件内容");
|
||||
|
||||
// 创建备份
|
||||
var backupFile = testFile.CreateBackup();
|
||||
Console.WriteLine($"备份创建成功: {backupFile.Exists()}");
|
||||
|
||||
// 列出备份
|
||||
var backups = testFile.ListBackups();
|
||||
Console.WriteLine($"备份数量: {backups.Count}");
|
||||
|
||||
// 恢复备份
|
||||
var restoredFile = testFile.RestoreBackup(backupFile.GetFullPath(), "restored_file.txt");
|
||||
Console.WriteLine($"备份恢复成功: {restoredFile.Exists()}");
|
||||
|
||||
// 清理
|
||||
testFile.Remove();
|
||||
backupFile.Remove();
|
||||
restoredFile.Remove();
|
||||
}
|
||||
|
||||
private static void TestPermissions()
|
||||
{
|
||||
Console.WriteLine("\n--- 权限管理测试 ---");
|
||||
|
||||
// 创建测试文件
|
||||
var testFile = new ToolFile("test_permissions.txt");
|
||||
testFile.SaveAsText("这是一个用于权限测试的文件内容");
|
||||
|
||||
// 获取权限
|
||||
var permissions = testFile.GetPermissions();
|
||||
Console.WriteLine($"读取权限: {permissions["read"]}");
|
||||
Console.WriteLine($"写入权限: {permissions["write"]}");
|
||||
Console.WriteLine($"隐藏属性: {permissions["hidden"]}");
|
||||
|
||||
// 设置权限
|
||||
testFile.SetPermissions(hidden: true);
|
||||
var newPermissions = testFile.GetPermissions();
|
||||
Console.WriteLine($"设置隐藏后: {newPermissions["hidden"]}");
|
||||
|
||||
// 检查权限
|
||||
Console.WriteLine($"可读: {testFile.IsReadable()}");
|
||||
Console.WriteLine($"可写: {testFile.IsWritable()}");
|
||||
Console.WriteLine($"隐藏: {testFile.IsHidden()}");
|
||||
|
||||
// 清理
|
||||
testFile.Remove();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,24 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using Convention;
|
||||
using Convention.EasySave;
|
||||
using Convention.Symbolization;
|
||||
using Convention.Test;
|
||||
|
||||
public class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var context = new SymbolizationRunner();
|
||||
try
|
||||
{
|
||||
context.Compile("example_script.txt");
|
||||
Console.WriteLine("Script compiled successfully.");
|
||||
}
|
||||
catch (FileNotFoundException ex)
|
||||
{
|
||||
Console.WriteLine($"Error: {ex.Message}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user