From e8286f711f0eb2cc65ce8127032a7d69ef63e0b6 Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Tue, 8 Jul 2025 00:17:44 +0800 Subject: [PATCH] =?UTF-8?q?EP=20Symbolization=20=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E5=85=B3=E9=94=AE=E5=AD=97=E8=A1=8C=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Convention/[Symbolization]/Detail/Keyword.cs | 112 ++++++++++++++----- 1 file changed, 87 insertions(+), 25 deletions(-) diff --git a/Convention/[Symbolization]/Detail/Keyword.cs b/Convention/[Symbolization]/Detail/Keyword.cs index 0b20edd..129f4ad 100644 --- a/Convention/[Symbolization]/Detail/Keyword.cs +++ b/Convention/[Symbolization]/Detail/Keyword.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using static System.Net.Mime.MediaTypeNames; namespace Convention.Symbolization.Internal @@ -217,9 +218,7 @@ namespace Convention.Symbolization.Keyword } else if (pause == Pause.BeforeBody) { - if (next is not Internal.ScriptWordVariable swv) - throw new InvalidGrammarException($"Not expected keyword for body start, but got {next}"); - if (swv.Word == "{") + if (next is Internal.ScriptWordVariable swv && swv.Word == "{") { pause = Pause.Body; layer++; @@ -249,60 +248,68 @@ namespace Convention.Symbolization.Keyword return true; } + public abstract void BuildSpace( + SymbolizationContext context, + List spaceExpression, + Dictionary scopeStartAndEnd, + List insideScopeWords + ); + public override void BuildScope(SymbolizationContext context, List scopeWords) { - pause = Pause.BeforeExpression; - layer = 0; + var pause = Pause.BeforeExpression; + List expression = new(); + Dictionary scopeStartAndEnd = new(); + List insideScopeWords = new(); + Stack stack = new(); foreach (var word in scopeWords) { if (pause == Pause.BeforeExpression) { - if (next is not Internal.ScriptWordVariable swv) - throw new InvalidGrammarException($"Not expected a key Word: {next}"); - if (swv.Word == "(") - pause = Pause.Expression; - else - throw new InvalidGrammarException($"Expected '(' symbol for expression start, but got {next}"); + pause = Pause.Expression; } else if (pause == Pause.Expression) { - if (next is Internal.ScriptWordVariable swv && swv.Word == ")") + if (word is Internal.ScriptWordVariable swv && swv.Word == ")") { pause = Pause.BeforeBody; } + else + { + expression.Add(word); + } } else if (pause == Pause.BeforeBody) { - if (next is not Internal.ScriptWordVariable swv) - throw new InvalidGrammarException($"Not expected keyword for body start, but got {next}"); - if (swv.Word == "{") + if (word is Internal.ScriptWordVariable swv && swv.Word == "{") { pause = Pause.Body; - layer++; + stack.Push(word); } else + { pause = Pause.SingleSentence; + insideScopeWords.Add(word); + } } else if (pause == Pause.Body) { - if (next is Internal.ScriptWordVariable swv) + if (word is Internal.ScriptWordVariable swv) { if (swv.Word == "{") - layer++; + stack.Push(word); else if (swv.Word == "}") - layer--; - if (layer == 0) - return false; + scopeStartAndEnd.Add(stack.Pop(), word); } + else + insideScopeWords.Add(word); } else if (pause == Pause.SingleSentence) { - if (next is Internal.ScriptWordVariable swv && swv.Word == ";") - { - return false; - } + insideScopeWords.Add(word); } } + BuildSpace(context, expression, scopeStartAndEnd, insideScopeWords); } } @@ -315,6 +322,14 @@ namespace Convention.Symbolization.Keyword { } + public override void BuildSentence(SymbolizationContext context, List sentence) + { + ToolFile importPath = new(string.Join('/', sentence.ConvertAll(x => (x as Internal.ScriptWordVariable).Word))); + if(importPath.Exists()==false) + throw new FileNotFoundException(importPath.ToString()); + new SymbolizationContext(context).Compile(importPath); + } + public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex) { return new Import(lineIndex, wordIndex); @@ -329,6 +344,18 @@ namespace Convention.Symbolization.Keyword public Namespace(int lineIndex, int wordIndex) : base("namespace", lineIndex, wordIndex) { } + + public override void BuildSpace( + SymbolizationContext context, + List spaceExpression, + Dictionary scopeStartAndEnd, + List insideScopeWords + ) + { + //TODO + //将命名空间放在新的子上下文中编译 + } + public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex) { return new Namespace(lineIndex, wordIndex); @@ -343,6 +370,18 @@ namespace Convention.Symbolization.Keyword public FunctionDef(int lineIndex, int wordIndex) : base("def", lineIndex, wordIndex) { } + + public override void BuildSpace( + SymbolizationContext context, + List spaceExpression, + Dictionary scopeStartAndEnd, + List insideScopeWords + ) + { + //TODO + //套用语法规则实现表达式向可执行语句的转换 + } + public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex) { return new FunctionDef(lineIndex, wordIndex); @@ -357,6 +396,16 @@ namespace Convention.Symbolization.Keyword public Return(int lineIndex, int wordIndex) : base("return", lineIndex, wordIndex) { } + + public override void BuildSentence(SymbolizationContext context, List sentence) + { + var symbol = sentence[0]; + //TODO + //直接转换为可执行语句 + //在上下文中搜索被返回的symbol, 存在则返回对应对象, 不存在则返回symbol对象 + //运行时检查返回值是否满足函数签名的返回值, 否则抛出异常 + } + public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex) { return new Return(lineIndex, wordIndex); @@ -371,6 +420,19 @@ namespace Convention.Symbolization.Keyword public If(int lineIndex, int wordIndex) : base("if", lineIndex, wordIndex) { } + + public override void BuildSpace( + SymbolizationContext context, + List spaceExpression, + Dictionary scopeStartAndEnd, + List insideScopeWords + ) + { + //TODO + //检查表达式是否成功, 成功则套用函数翻译规则将作用域翻译为可执行语句 + //否则跳转到末尾 + } + public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex) { return new If(lineIndex, wordIndex);