From 6806d90829e66c11489b70bf058640f07c41a113 Mon Sep 17 00:00:00 2001 From: ninemine <106434473+NINEMINEsigma@users.noreply.github.com> Date: Thu, 3 Jul 2025 21:50:26 +0800 Subject: [PATCH] =?UTF-8?q?EP=200.1.1=20=E8=AF=AD=E5=8F=A5=E5=88=86?= =?UTF-8?q?=E7=95=8C=E7=BA=BF=E5=B0=9A=E6=9C=AA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Collection Types/EasySaveStackType.cs | 2 +- .../[Runtime]/EasySave/Types/EasySaveType.cs | 2 +- Convention/[Runtime]/File.cs | 27 +----- Convention/[Runtime]/GlobalConfig.cs | 10 +- Convention/[Symbolization]/Detail/Keyword.cs | 2 +- .../[Symbolization]/Detail/Modification.cs | 9 ++ Convention/[Symbolization]/Detail/Reader.cs | 13 --- .../Detail/ScriptWordVariable.cs | 17 +--- .../Runner/SymbolizationContext.cs | 41 ++++++++ .../Runner/SymbolizationReader.cs | 93 +++++++++++++++++++ Convention/[Symbolization]/Symbolization.cs | 47 +--------- [Test]/Program.cs | 35 +++---- 12 files changed, 175 insertions(+), 123 deletions(-) delete mode 100644 Convention/[Symbolization]/Detail/Reader.cs create mode 100644 Convention/[Symbolization]/Runner/SymbolizationContext.cs create mode 100644 Convention/[Symbolization]/Runner/SymbolizationReader.cs diff --git a/Convention/[Runtime]/EasySave/Types/Collection Types/EasySaveStackType.cs b/Convention/[Runtime]/EasySave/Types/Collection Types/EasySaveStackType.cs index fb0eb0d..59db683 100644 --- a/Convention/[Runtime]/EasySave/Types/Collection Types/EasySaveStackType.cs +++ b/Convention/[Runtime]/EasySave/Types/Collection Types/EasySaveStackType.cs @@ -45,7 +45,7 @@ namespace Convention.EasySave.Types { if(!reader.StartReadCollectionItem()) break; - stack.Push(reader.Read(elementType)); + stack.CompleteLine(reader.Read(elementType)); if(reader.EndReadCollectionItem()) break; } diff --git a/Convention/[Runtime]/EasySave/Types/EasySaveType.cs b/Convention/[Runtime]/EasySave/Types/EasySaveType.cs index 4f979c6..20ab8af 100644 --- a/Convention/[Runtime]/EasySave/Types/EasySaveType.cs +++ b/Convention/[Runtime]/EasySave/Types/EasySaveType.cs @@ -126,7 +126,7 @@ namespace Convention.EasySave.Types } else if (type == typeof(EasySaveStackType)) { - var method = baseType.type.GetMethod("Push"); + var method = baseType.type.GetMethod("CompleteLine"); foreach (var item in loaded) method.Invoke(obj, new object[] { item }); } diff --git a/Convention/[Runtime]/File.cs b/Convention/[Runtime]/File.cs index e9631ce..a8d0807 100644 --- a/Convention/[Runtime]/File.cs +++ b/Convention/[Runtime]/File.cs @@ -11,7 +11,6 @@ namespace Convention { private string FullPath; private FileSystemInfo OriginInfo; - public FileStream OriginControlStream { get; private set; } public ToolFile(string path) { FullPath = path; @@ -127,19 +126,10 @@ namespace Convention } private void SaveAsText(string data) { - if (OriginControlStream != null && OriginControlStream.CanWrite) - { - using var sw = new StreamWriter(OriginControlStream); - sw.Write(data); - sw.Flush(); - } - else - { - using var fs = new FileStream(FullPath, FileMode.CreateNew, FileAccess.Write); - using var sw = new StreamWriter(fs); - sw.Write(data); - sw.Flush(); - } + using var fs = new FileStream(FullPath, FileMode.CreateNew, FileAccess.Write); + using var sw = new StreamWriter(fs); + sw.Write(data); + sw.Flush(); } public static void SaveDataAsBinary(string path, byte[] outdata, FileStream Stream = null) { @@ -158,7 +148,7 @@ namespace Convention public void SaveAsBinary(byte[] data) { - SaveDataAsBinary(FullPath, data, OriginControlStream); + SaveDataAsBinary(FullPath, data, (OriginInfo as FileInfo).OpenWrite()); } #endregion @@ -201,15 +191,8 @@ namespace Convention Refresh(); return this; } - public ToolFile Open(FileMode mode) - { - this.Close(); - OriginControlStream = new FileStream(this.FullPath, mode); - return this; - } public ToolFile Close() { - OriginControlStream?.Close(); return this; } public ToolFile Create() diff --git a/Convention/[Runtime]/GlobalConfig.cs b/Convention/[Runtime]/GlobalConfig.cs index d7717fd..4cf8f18 100644 --- a/Convention/[Runtime]/GlobalConfig.cs +++ b/Convention/[Runtime]/GlobalConfig.cs @@ -16,7 +16,6 @@ namespace Convention public static void GenerateEmptyConfigJson(ToolFile file) { - file.Open(System.IO.FileMode.CreateNew); file.SaveAsRawJson>(new() { { "properties",new Dictionary() } @@ -71,12 +70,9 @@ namespace Convention var file = DataDir | path; if (file.Exists()) { - try - { - file.Open(System.IO.FileMode.Create); - return true; - } - catch (Exception) { } + file.Delete(); + file.Create(); + return true; } return false; } diff --git a/Convention/[Symbolization]/Detail/Keyword.cs b/Convention/[Symbolization]/Detail/Keyword.cs index cc09a61..fd10e49 100644 --- a/Convention/[Symbolization]/Detail/Keyword.cs +++ b/Convention/[Symbolization]/Detail/Keyword.cs @@ -43,7 +43,7 @@ namespace Convention.Symbolization.Internal namespace Convention.Symbolization.Keyword { /// - /// namespace-expression + /// file /// public sealed class Import : Internal.Keyword { diff --git a/Convention/[Symbolization]/Detail/Modification.cs b/Convention/[Symbolization]/Detail/Modification.cs index b93e692..16866d4 100644 --- a/Convention/[Symbolization]/Detail/Modification.cs +++ b/Convention/[Symbolization]/Detail/Modification.cs @@ -8,5 +8,14 @@ namespace Convention.Symbolization.Internal { public abstract class Modification : Variable { + protected Modification(string modificationalName, Type type) : base(new(modificationalName, type)) + { + } + } + public abstract class Modification : Modification where T : Modification + { + protected Modification(string modificationalName) : base(modificationalName, typeof(T)) + { + } } } diff --git a/Convention/[Symbolization]/Detail/Reader.cs b/Convention/[Symbolization]/Detail/Reader.cs deleted file mode 100644 index 1b022e1..0000000 --- a/Convention/[Symbolization]/Detail/Reader.cs +++ /dev/null @@ -1,13 +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 - { - - } -} diff --git a/Convention/[Symbolization]/Detail/ScriptWordVariable.cs b/Convention/[Symbolization]/Detail/ScriptWordVariable.cs index e9a7c43..a032bf8 100644 --- a/Convention/[Symbolization]/Detail/ScriptWordVariable.cs +++ b/Convention/[Symbolization]/Detail/ScriptWordVariable.cs @@ -6,7 +6,7 @@ public ScriptWordVariable(string word) : base("") { - this.word = word; + this.word = (string)word.Clone(); } public override ScriptWordVariable CloneVariable(string targetSymbolName) @@ -19,20 +19,9 @@ return other is not null && word.Equals(other.word); } - public Keyword GetKeyword() + public override string ToString() { - if(Keyword.Keywords.TryGetValue(word, out var keyword)) - return keyword; - return null; - } - - public Variable GetVariable(SymbolizationContext context) - { - if (context.Variables.TryGetValue(word, out var variable)) - { - return variable; - } - return null; + return word; } } } diff --git a/Convention/[Symbolization]/Runner/SymbolizationContext.cs b/Convention/[Symbolization]/Runner/SymbolizationContext.cs new file mode 100644 index 0000000..d528456 --- /dev/null +++ b/Convention/[Symbolization]/Runner/SymbolizationContext.cs @@ -0,0 +1,41 @@ +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; + private Dictionary> ScriptWords; + public readonly Dictionary> ScriptCommands = new(); + 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); + ScriptWords = reader.ScriptWords; + // Turn the script words into commands + // TODO + } + + public void Compile(ToolFile file) + { + if (file.Exists() == false) + throw new FileNotFoundException($"File not found: {file}", file); + var text = file.LoadAsText(); + this.Compile(text); + } + } +} diff --git a/Convention/[Symbolization]/Runner/SymbolizationReader.cs b/Convention/[Symbolization]/Runner/SymbolizationReader.cs new file mode 100644 index 0000000..ae105d2 --- /dev/null +++ b/Convention/[Symbolization]/Runner/SymbolizationReader.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Convention.Symbolization.Internal +{ + public class SymbolizationReader + { + private string buffer = ""; + private int lineContext = 0; + private bool isOutOfStringline = true; + public Dictionary> ScriptWords = new() { { 0, new() } }; + + private static HashSet ControllerCharSet = new() + { + '{','}','(',')','[',']', + '+','-','*','/','%', + '=', + '!','<','>','&','|', + '^', + ':',',','.','?',/*'\'','"',*/ + '@','#','$', + }; + + public void ReadChar(char ch) + { + if (isOutOfStringline) + { + if (ControllerCharSet.Contains(ch)) + CompleteSingleSymbol(ch); + else if (char.IsWhiteSpace(ch) || ch == '\n' || ch == '\r' || ch == '\t') + CompleteWord(); + else if (buffer.Length == 0 && ch == '"') + BeginString(); + else if (ch == ';') + CompleteLine(); + 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) + { + ScriptWords[lineContext].Add(new ScriptWordVariable(buffer)); + buffer = ""; + } + } + void CompleteLine() + { + CompleteWord(); + ScriptWords[lineContext].Add(new ScriptWordVariable(";")); + lineContext++; + ScriptWords.Add(lineContext, new()); + } + void BeginString() + { + if (buffer.Length > 0) + throw new InvalidGrammarException($"String must start with nothing: {buffer}"); + isOutOfStringline = false; + } + void EndString() + { + isOutOfStringline = true; + CompleteWord(); + } + } +} diff --git a/Convention/[Symbolization]/Symbolization.cs b/Convention/[Symbolization]/Symbolization.cs index 1a208c1..dedcdc5 100644 --- a/Convention/[Symbolization]/Symbolization.cs +++ b/Convention/[Symbolization]/Symbolization.cs @@ -1,56 +1,17 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Text; using System.Threading.Tasks; 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; - } - - public readonly SymbolizationContext ParentContext; - public readonly Dictionary> ScriptWords = new(); - public readonly Dictionary> ScriptCommands = new(); - public readonly Dictionary Variables = new(); - public readonly Internal.Namespace CurrentNamespace; - - public SymbolizationContext Compile() - { - return this; - } - } - public class SymbolizationRunner { - private readonly Internal.Namespace GlobalNamespace; - private readonly SymbolizationContext Context; + private SymbolizationContext Context; - public SymbolizationRunner(SymbolizationContext context) + public void Compile(string path) { - GlobalNamespace = Internal.Namespace.CreateRootNamespace(); - Context = context; - } - public SymbolizationRunner() :this(new()){ } - - public Exception Compile() - { - try - { - - } - catch (Exception ex) - { - return ex; - } - return null; + Context = new(); + Context.Compile(new ToolFile(path)); } } diff --git a/[Test]/Program.cs b/[Test]/Program.cs index 2e64fd5..32dac28 100644 --- a/[Test]/Program.cs +++ b/[Test]/Program.cs @@ -4,32 +4,25 @@ using System.IO; using System.Threading; using Convention; using Convention.EasySave; +using Convention.Symbolization; public class Program { - class Vector2 - { - public double x, y; - } - class Vector2X2 - { - public double x, y; - public Vector2 next; - } - static void Main(string[] args) { - Directory.CreateDirectory(PlatformIndicator.PersistentDataPath); - Console.WriteLine(PlatformIndicator.PersistentDataPath); - EasySave.Save("key", new Vector2X2() + // Create a new SymbolizationContext + var context = new SymbolizationContext(); + // Compile a script from a file + var toolFile = new ToolFile("example_script.txt"); + try { - x = 10, - y = 20, - next = new() - { - x = 30, - y = 40 - } - }, "./test.json"); + context.Compile(toolFile); + Console.WriteLine("Script compiled successfully."); + } + catch (FileNotFoundException ex) + { + Console.WriteLine($"Error: {ex.Message}"); + toolFile.Create(); + } } } \ No newline at end of file