From 6783429ab65de80d151eb7d57ce58db5dbc14837 Mon Sep 17 00:00:00 2001 From: ninemine <106434473+NINEMINEsigma@users.noreply.github.com> Date: Fri, 4 Jul 2025 21:39:02 +0800 Subject: [PATCH] =?UTF-8?q?EP=200.1.1=20=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[Symbolization]/Detail/Functional.cs | 35 ++++++++-- Convention/[Symbolization]/Detail/Keyword.cs | 61 ++++++++++++----- .../[Symbolization]/Detail/Modification.cs | 21 ------ .../Runner/SymbolizationContext.cs | 8 +-- .../Runner/SymbolizationReader.cs | 68 ++++++++++++++++--- [Test]/Program.cs | 8 +-- 6 files changed, 134 insertions(+), 67 deletions(-) delete mode 100644 Convention/[Symbolization]/Detail/Modification.cs diff --git a/Convention/[Symbolization]/Detail/Functional.cs b/Convention/[Symbolization]/Detail/Functional.cs index 78582b8..5262426 100644 --- a/Convention/[Symbolization]/Detail/Functional.cs +++ b/Convention/[Symbolization]/Detail/Functional.cs @@ -8,9 +8,9 @@ namespace Convention.Symbolization.Internal { public readonly FunctionSymbol FunctionInfo; - protected Function(string symbolName,string functionName, Type returnType, Type[] parameterTypes, Modification[] modifiers) : base(symbolName) + protected Function(string symbolName,string functionName, Type returnType, Type[] parameterTypes) : base(symbolName) { - FunctionInfo = new(functionName, returnType, parameterTypes, modifiers); + FunctionInfo = new(functionName, returnType, parameterTypes); } public abstract Variable Invoke(SymbolizationContext context, Variable[] parameters); } @@ -20,7 +20,7 @@ namespace Convention.Symbolization.Internal public readonly MethodInfo methodInfo; public DelegationalFunction(string symbolName, MethodInfo methodInfo) - : base(symbolName, methodInfo.Name, methodInfo.ReturnType, methodInfo.GetParameters().ToList().ConvertAll(x => x.ParameterType).ToArray(), Array.Empty()) + : base(symbolName, methodInfo.Name, methodInfo.ReturnType, methodInfo.GetParameters().ToList().ConvertAll(x => x.ParameterType).ToArray()) { this.methodInfo = methodInfo!; } @@ -44,22 +44,43 @@ namespace Convention.Symbolization.Internal } } + 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 Modification[] Modifiers; public readonly string FullName; - public FunctionSymbol(string symbolName, Type returnType, Type[] parameterTypes, Modification[] modifiers) + public FunctionSymbol(string symbolName, Type returnType, Type[] parameterTypes) { this.FunctionName = symbolName; this.ReturnType = returnType; this.ParameterTypes = parameterTypes; - this.Modifiers = modifiers; - this.FullName = $"{returnType.FullName} {symbolName}({string.Join(',', parameterTypes.ToList().ConvertAll(x => x.FullName))}) {string.Join(' ', modifiers.ToList().ConvertAll(x => x.ToString()))}"; + this.FullName = $"{returnType.FullName} {symbolName}({string.Join(',', parameterTypes.ToList().ConvertAll(x => x.FullName))})"; } public bool Equals(FunctionSymbol other) diff --git a/Convention/[Symbolization]/Detail/Keyword.cs b/Convention/[Symbolization]/Detail/Keyword.cs index fd10e49..eda7f1b 100644 --- a/Convention/[Symbolization]/Detail/Keyword.cs +++ b/Convention/[Symbolization]/Detail/Keyword.cs @@ -1,34 +1,31 @@ using System; +using System.IO; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Convention.Symbolization.Internal { - public abstract class Keyword : Variable + public abstract class Keyword : CloneableVariable { - protected Keyword(string keyword, Type realType) : base(new(keyword, realType)) + protected Keyword(string keyword) : base(keyword) { - Keywords.Add(keyword, this); + Keywords.TryAdd(keyword, this); } public static readonly Dictionary 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 : Keyword where T:Keyword,new() + public abstract class Keyword : Keyword where T : Keyword, new() { private static T MyInstance = new(); - public static T Instance - { - get - { - return MyInstance; - } - } - - protected Keyword(string keyword) : base(keyword, typeof(T)) + protected Keyword(string keyword) : base(keyword) { } @@ -50,6 +47,36 @@ namespace Convention.Symbolization.Keyword 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; + } } /// @@ -63,7 +90,7 @@ namespace Convention.Symbolization.Keyword } /// - /// FunctionName(parameter-list) -> return-type { ... return return-type-instance; } + /// FunctionName(parameter-list) return-type { ... return return-type-instance; } /// public sealed class FunctionDef : Internal.Keyword { diff --git a/Convention/[Symbolization]/Detail/Modification.cs b/Convention/[Symbolization]/Detail/Modification.cs deleted file mode 100644 index 16866d4..0000000 --- a/Convention/[Symbolization]/Detail/Modification.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -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]/Runner/SymbolizationContext.cs b/Convention/[Symbolization]/Runner/SymbolizationContext.cs index d528456..f1ce415 100644 --- a/Convention/[Symbolization]/Runner/SymbolizationContext.cs +++ b/Convention/[Symbolization]/Runner/SymbolizationContext.cs @@ -15,8 +15,6 @@ namespace Convention.Symbolization } private readonly SymbolizationContext ParentContext; - private Dictionary> ScriptWords; - public readonly Dictionary> ScriptCommands = new(); public readonly Internal.Namespace CurrentNamespace; public void Compile(string allText) @@ -25,9 +23,9 @@ namespace Convention.Symbolization Internal.SymbolizationReader reader = new(); foreach (char ch in allText) reader.ReadChar(ch); - ScriptWords = reader.ScriptWords; - // Turn the script words into commands - // TODO + var ScriptWords = reader.ScriptWords; + // Turn the script words into scope words + reader.CompleteScopeWord(this); } public void Compile(ToolFile file) diff --git a/Convention/[Symbolization]/Runner/SymbolizationReader.cs b/Convention/[Symbolization]/Runner/SymbolizationReader.cs index ae105d2..d84e39c 100644 --- a/Convention/[Symbolization]/Runner/SymbolizationReader.cs +++ b/Convention/[Symbolization]/Runner/SymbolizationReader.cs @@ -8,10 +8,12 @@ namespace Convention.Symbolization.Internal { public class SymbolizationReader { + #region Read Script Words + private string buffer = ""; - private int lineContext = 0; + private int lineContext = 1; private bool isOutOfStringline = true; - public Dictionary> ScriptWords = new() { { 0, new() } }; + public Dictionary> ScriptWords = new() { { 1, new() } }; private static HashSet ControllerCharSet = new() { @@ -22,6 +24,7 @@ namespace Convention.Symbolization.Internal '^', ':',',','.','?',/*'\'','"',*/ '@','#','$', + ';' }; public void ReadChar(char ch) @@ -30,16 +33,16 @@ namespace Convention.Symbolization.Internal { if (ControllerCharSet.Contains(ch)) CompleteSingleSymbol(ch); - else if (char.IsWhiteSpace(ch) || ch == '\n' || ch == '\r' || ch == '\t') + else if (ch == '\n') + CompleteLine(); + else if (char.IsWhiteSpace(ch) || 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"); + throw new NotImplementedException($"{lineContext + 1} line, {buffer} + <{ch}> not implemented"); } else { @@ -48,7 +51,7 @@ namespace Convention.Symbolization.Internal else if (ch == '"') EndString(); else - throw new NotImplementedException($"{lineContext+1} line, \"{buffer}\" + \'{ch}\' not implemented"); + throw new NotImplementedException($"{lineContext + 1} line, \"{buffer}\" + \'{ch}\' not implemented"); } } @@ -67,16 +70,25 @@ namespace Convention.Symbolization.Internal { if (buffer.Length > 0) { - ScriptWords[lineContext].Add(new ScriptWordVariable(buffer)); + 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(); - ScriptWords[lineContext].Add(new ScriptWordVariable(";")); - lineContext++; - ScriptWords.Add(lineContext, new()); + lineContext++; ; } void BeginString() { @@ -89,5 +101,39 @@ namespace Convention.Symbolization.Internal 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 } } diff --git a/[Test]/Program.cs b/[Test]/Program.cs index 32dac28..4520452 100644 --- a/[Test]/Program.cs +++ b/[Test]/Program.cs @@ -10,19 +10,15 @@ public class Program { static void Main(string[] args) { - // Create a new SymbolizationContext - var context = new SymbolizationContext(); - // Compile a script from a file - var toolFile = new ToolFile("example_script.txt"); + var context = new SymbolizationRunner(); try { - context.Compile(toolFile); + context.Compile("example_script.txt"); Console.WriteLine("Script compiled successfully."); } catch (FileNotFoundException ex) { Console.WriteLine($"Error: {ex.Message}"); - toolFile.Create(); } } } \ No newline at end of file