From 1a1db3554b10ed4a707671695d5100eee621af75 Mon Sep 17 00:00:00 2001 From: Nitload Date: Sun, 30 Mar 2025 08:07:45 +0800 Subject: [PATCH] Daily update on 20250330-080745 by Nitload Youmu. Additional Message : CASEOF block in full function, next is Collection operation, relative keywords processing and REPEAT block. Signed-off-by: Nitload --- .../net8.0/StgSharp.Model.AssemblyInfo.cs | 2 +- .../net8.0/StgSharp.Model.sourcelink.json | 2 +- .../Express/ExpAstNode/ExpSwitchNode.cs | 100 ++++++++----- .../Express/ExpSyntaxAnalyzer.CaseOf.cs | 131 ++++++++++++++++++ .../Express/ExpSyntaxAnalyzer.If.cs | 1 - StgSharp.Script/Express/ExpSyntaxAnalyzer.cs | 3 + .../net8.0/StgSharp.Script.AssemblyInfo.cs | 2 +- .../net8.0/StgSharp.Script.sourcelink.json | 2 +- StgSharpDebug/ExpLinearExpressionTest.cs | 8 +- .../net8.0/StgSharpDebug.AssemblyInfo.cs | 2 +- .../net8.0/StgSharpDebug.sourcelink.json | 2 +- 11 files changed, 208 insertions(+), 47 deletions(-) create mode 100644 StgSharp.Script/Express/ExpSyntaxAnalyzer.CaseOf.cs diff --git a/StgSharp.Model/obj/x64/Debug/net8.0/StgSharp.Model.AssemblyInfo.cs b/StgSharp.Model/obj/x64/Debug/net8.0/StgSharp.Model.AssemblyInfo.cs index 9b865113..5a5ef2b9 100644 --- a/StgSharp.Model/obj/x64/Debug/net8.0/StgSharp.Model.AssemblyInfo.cs +++ b/StgSharp.Model/obj/x64/Debug/net8.0/StgSharp.Model.AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("StgSharp.Model")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+36b6ad5a526ca6ef66d826f614e8476a99fe52f7")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+17a226c20653a1c92f6a3b420d6388c31f6a8c1e")] [assembly: System.Reflection.AssemblyProductAttribute("StgSharp.Model")] [assembly: System.Reflection.AssemblyTitleAttribute("StgSharp.Model")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/StgSharp.Model/obj/x64/Debug/net8.0/StgSharp.Model.sourcelink.json b/StgSharp.Model/obj/x64/Debug/net8.0/StgSharp.Model.sourcelink.json index c19f6c51..15b457c1 100644 --- a/StgSharp.Model/obj/x64/Debug/net8.0/StgSharp.Model.sourcelink.json +++ b/StgSharp.Model/obj/x64/Debug/net8.0/StgSharp.Model.sourcelink.json @@ -1 +1 @@ -{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/36b6ad5a526ca6ef66d826f614e8476a99fe52f7/*"}} \ No newline at end of file +{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/17a226c20653a1c92f6a3b420d6388c31f6a8c1e/*"}} \ No newline at end of file diff --git a/StgSharp.Script/Express/ExpAstNode/ExpSwitchNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpSwitchNode.cs index 2bb1adcb..5ba30f9b 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpSwitchNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpSwitchNode.cs @@ -29,6 +29,7 @@ //----------------------------------------------------------------------- //----------------------------------------------------------------------- using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.Text; @@ -37,27 +38,75 @@ namespace StgSharp.Script.Express public class ExpSwitchNode : ExpNode { - private Dictionary _cases; private ExpNode _default; private ExpNode _target; + private FrozenDictionary _cases; + public ExpSwitchNode( - Token source, - ExpNode target, - ExpNode defaultCase, - params (ExpNode label, ExpNode op)[] cases ) + Token source, + ExpNode target, + ExpNode defaultCase, + params (ExpNode label, ExpNode op)[] cases ) : base( source ) { _target = target; _default = defaultCase; - _cases = new Dictionary(); - foreach( (ExpNode label, ExpNode op ) item in cases ) { - if( item.op == null ) { - _cases.Add( item.label, null! ); - } else { - _cases.Add( item.label, item.op ); + Dictionary casesDic = new Dictionary(); + foreach( (ExpNode label, ExpNode op ) item in cases ) + { + if( item.op == null ) + { + casesDic.Add( item.label, null! ); + } else + { + casesDic.Add( item.label, item.op ); } } + _cases = casesDic.ToFrozenDictionary(); + + + string str = @"switch( {0} ){"; + StringBuilder codeFormatBuilder = new StringBuilder(); + codeFormatBuilder.AppendLine( str ); + for( int i = 0; i < casesDic.Count; i++ ) + { + int index = i * 2 + 1; + codeFormatBuilder.AppendLine( + $$""" + case {{{index}}}: + {{{index + 1}}} + break; + """ ); + } + CodeConvertTemplate = codeFormatBuilder.ToString(); + } + + public ExpSwitchNode( + Token source, + ExpNode target, + ExpNode defaultCase, + IDictionary cases ) + : base( source ) + { + _target = target; + _default = defaultCase; + _cases = cases.ToFrozenDictionary(); + + string str = @"switch( {0} ){"; + StringBuilder codeFormatBuilder = new StringBuilder(); + codeFormatBuilder.AppendLine( str ); + for( int i = 0; i < cases.Count; i++ ) + { + int index = i * 2 + 1; + codeFormatBuilder.AppendLine( + $$""" + case {{{index}}}: + {{{index + 1}}} + break; + """ ); + } + CodeConvertTemplate = codeFormatBuilder.ToString(); } public ExpNode this[ ExpNode index ] @@ -71,37 +120,12 @@ namespace StgSharp.Script.Express } } - public override ExpNode Left => throw new NotImplementedException(); + public override ExpNode Left => Empty; - public override ExpNode Right => throw new NotImplementedException(); + public override ExpNode Right => Empty; public override IExpElementSource EqualityTypeConvert => null!; - public void AddOperationToDefault( ExpNode expressionRoot ) - { - if( expressionRoot != null ) { - _default.AppendNode( expressionRoot ); - } - } - - /// - /// Adds an expression to certain . The added expression will be run - /// at last. - /// - /// Label of case to add operation - /// Root token of the expression to be added. - /// Cannot fine the case label in current witch expression. - public void AppendOperationToCase( - ExpNode label, - ExpNode expressionRoot ) - { - if( _cases.TryGetValue( label, out ExpNode? op ) ) { - op.AppendNode( expressionRoot ); - } else { - throw new ExpCaseNotFoundException( this, label ); - } - } - public bool ContainsCase( ExpNode token ) { return _cases.ContainsKey( token ); diff --git a/StgSharp.Script/Express/ExpSyntaxAnalyzer.CaseOf.cs b/StgSharp.Script/Express/ExpSyntaxAnalyzer.CaseOf.cs new file mode 100644 index 00000000..af15c72a --- /dev/null +++ b/StgSharp.Script/Express/ExpSyntaxAnalyzer.CaseOf.cs @@ -0,0 +1,131 @@ +//----------------------------------------------------------------------- +//----------------------------------------------------------------------- +// file="ExpSyntaxAnalyzer.CaseOf.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +//----------------------------------------------------------------------- +//----------------------------------------------------------------------- +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using ExpKeyword = StgSharp.Script.Express.ExpressCompile.Keyword; + +namespace StgSharp.Script.Express +{ + public partial class ExpSyntaxAnalyzer + { + + private bool AppendToken_CaseOf( Token t ) + { + Token op; + ExpNode root; + switch( t.Value ) + { + case ExpKeyword.Case: + CaseOfStateCache state = new CaseOfStateCache( t ); + _cache.CurrentDepthMark.StateCache = state; + state.CurrentState = t.Value; + return true; + case ExpKeyword.Of: + state = _cache.StateOfCurrentDepth(); + while( _cache.TryPopOperator( out op ) ) { + ConvertAndPushOneOperator( op ); + } + _cache.PopOperand( out _, out root ); + state.CaseExpression = root; + state.CurrentState = t.Value; + return true; + case ":": + state = _cache.StateOfCurrentDepth(); + if( state.CurrentState != ExpKeyword.Of && state.CurrentState != + ExpKeyword.Otherwise ) + { + root = _cache.PackAllStatements(); + state.CloseCurrentCase( root ); + } + while( _cache.TryPopOperator( out op ) ) { + ConvertAndPushOneOperator( op ); + } + if( !_cache.PopOperand( out _, out root ) ) + { + // sth wrong, case expression cannot generate; + } + state.CurrentCase = root; + return true; + case ExpKeyword.Otherwise: + state = _cache.StateOfCurrentDepth(); + root = _cache.PackAllStatements(); + state.CloseCurrentCase( root ); + state.CurrentState = t.Value; + return true; + case ExpKeyword.EndCase: + state = _cache.StateOfCurrentDepth(); + root = _cache.PackAllStatements(); + state.CloseCurrentCase( root ); + root = new ExpSwitchNode( + state.Begin, state.CaseExpression, state.DefaultCase, state.Cases ); + _cache.DecreaseDepth(); + _cache.StatementsInDepth.Push( root ); + return true; + default: + AppendToken_common( t ); + return true; + } + } + + private class CaseOfStateCache : ICompileDepthState + { + + public CaseOfStateCache( Token t ) + { + Begin = t; + } + + public Dictionary Cases { get; } = new Dictionary(); + + public ExpNode CaseExpression { get; set; } + + public ExpNode DefaultCase { get; set; } + + public ExpNode CurrentCase { get; set; } + + public string CurrentState { get; set; } + + public Token Begin { get; init; } + + public void CloseCurrentCase( ExpNode root ) + { + Cases.Add( CurrentCase, root ); + } + + } + + } +} diff --git a/StgSharp.Script/Express/ExpSyntaxAnalyzer.If.cs b/StgSharp.Script/Express/ExpSyntaxAnalyzer.If.cs index 33db18d3..03bcd137 100644 --- a/StgSharp.Script/Express/ExpSyntaxAnalyzer.If.cs +++ b/StgSharp.Script/Express/ExpSyntaxAnalyzer.If.cs @@ -105,7 +105,6 @@ namespace StgSharp.Script.Express state.ExpressionIfFalse ); _cache.DecreaseDepth(); _cache.StatementsInDepth.Push( ifNode ); - _cache.PushOperand( ifNode ); return true; default: AppendToken_common( t ); diff --git a/StgSharp.Script/Express/ExpSyntaxAnalyzer.cs b/StgSharp.Script/Express/ExpSyntaxAnalyzer.cs index 60676213..c2e14002 100644 --- a/StgSharp.Script/Express/ExpSyntaxAnalyzer.cs +++ b/StgSharp.Script/Express/ExpSyntaxAnalyzer.cs @@ -84,6 +84,9 @@ namespace StgSharp.Script.Express case ExpCompileStateCode.IfBranch: AppendToken_IfBranch( expToken ); break; + case ExpCompileStateCode.CaseBranch: + AppendToken_CaseOf( expToken ); + break; default: AppendToken_common( expToken ); break; diff --git a/StgSharp.Script/obj/x64/Debug/net8.0/StgSharp.Script.AssemblyInfo.cs b/StgSharp.Script/obj/x64/Debug/net8.0/StgSharp.Script.AssemblyInfo.cs index 6f01ea61..3fd63c86 100644 --- a/StgSharp.Script/obj/x64/Debug/net8.0/StgSharp.Script.AssemblyInfo.cs +++ b/StgSharp.Script/obj/x64/Debug/net8.0/StgSharp.Script.AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("StgSharp.Script")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+36b6ad5a526ca6ef66d826f614e8476a99fe52f7")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+17a226c20653a1c92f6a3b420d6388c31f6a8c1e")] [assembly: System.Reflection.AssemblyProductAttribute("StgSharp.Script")] [assembly: System.Reflection.AssemblyTitleAttribute("StgSharp.Script")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/StgSharp.Script/obj/x64/Debug/net8.0/StgSharp.Script.sourcelink.json b/StgSharp.Script/obj/x64/Debug/net8.0/StgSharp.Script.sourcelink.json index c19f6c51..15b457c1 100644 --- a/StgSharp.Script/obj/x64/Debug/net8.0/StgSharp.Script.sourcelink.json +++ b/StgSharp.Script/obj/x64/Debug/net8.0/StgSharp.Script.sourcelink.json @@ -1 +1 @@ -{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/36b6ad5a526ca6ef66d826f614e8476a99fe52f7/*"}} \ No newline at end of file +{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/17a226c20653a1c92f6a3b420d6388c31f6a8c1e/*"}} \ No newline at end of file diff --git a/StgSharpDebug/ExpLinearExpressionTest.cs b/StgSharpDebug/ExpLinearExpressionTest.cs index f44faa26..a6cbf33d 100644 --- a/StgSharpDebug/ExpLinearExpressionTest.cs +++ b/StgSharpDebug/ExpLinearExpressionTest.cs @@ -126,11 +126,15 @@ namespace StgSharp (**) a := b + (c + d) - a; IF a = b THEN - a := a * e; + a := a * a; ELSE - a := c * e; + a := c * a; END_IF (**) + CASE a OF + 1 : a := 1 + e ; + OTHERWISE : a:=a; + END_CASE """; public static void ExpLinearExpressionTest() diff --git a/StgSharpDebug/obj/x64/Debug/net8.0/StgSharpDebug.AssemblyInfo.cs b/StgSharpDebug/obj/x64/Debug/net8.0/StgSharpDebug.AssemblyInfo.cs index bbcfd883..524bb584 100644 --- a/StgSharpDebug/obj/x64/Debug/net8.0/StgSharpDebug.AssemblyInfo.cs +++ b/StgSharpDebug/obj/x64/Debug/net8.0/StgSharpDebug.AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("StgSharpDebug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+36b6ad5a526ca6ef66d826f614e8476a99fe52f7")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+17a226c20653a1c92f6a3b420d6388c31f6a8c1e")] [assembly: System.Reflection.AssemblyProductAttribute("StgSharpDebug")] [assembly: System.Reflection.AssemblyTitleAttribute("StgSharpDebug")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/StgSharpDebug/obj/x64/Debug/net8.0/StgSharpDebug.sourcelink.json b/StgSharpDebug/obj/x64/Debug/net8.0/StgSharpDebug.sourcelink.json index c19f6c51..15b457c1 100644 --- a/StgSharpDebug/obj/x64/Debug/net8.0/StgSharpDebug.sourcelink.json +++ b/StgSharpDebug/obj/x64/Debug/net8.0/StgSharpDebug.sourcelink.json @@ -1 +1 @@ -{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/36b6ad5a526ca6ef66d826f614e8476a99fe52f7/*"}} \ No newline at end of file +{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/17a226c20653a1c92f6a3b420d6388c31f6a8c1e/*"}} \ No newline at end of file