From c5777f4bfe31231c2026f5e0ffd291d5db793843 Mon Sep 17 00:00:00 2001 From: Nitload Date: Wed, 5 Mar 2025 05:25:11 +0800 Subject: [PATCH] Daily update on 20250305-052510 by Nitload Youmu. Additional Message : message Signed-off-by: Nitload --- StgSharp.Script/Express/CompileEnv/KeyWord.cs | 4 +- .../ExpAstNode/ExpUnaryOperatorNode.cs | 17 ++++- .../Express/ExpAstNode/_ExpNode.cs | 25 ++++++- .../Express/NodeGenerator/ExpNodeGenerator.cs | 69 +++++------------ .../NodeGenerator/FunctionCallingGenerator.cs | 74 +++++++++++++++++++ .../NodeGenerator/GenerateUnaryToken.cs | 47 +++++++++--- StgSharp.Script/IASTNode.cs | 2 + .../net8.0/StgSharp.Script.AssemblyInfo.cs | 2 +- 8 files changed, 171 insertions(+), 69 deletions(-) create mode 100644 StgSharp.Script/Express/NodeGenerator/FunctionCallingGenerator.cs diff --git a/StgSharp.Script/Express/CompileEnv/KeyWord.cs b/StgSharp.Script/Express/CompileEnv/KeyWord.cs index fe54b5ee..aa79bacf 100644 --- a/StgSharp.Script/Express/CompileEnv/KeyWord.cs +++ b/StgSharp.Script/Express/CompileEnv/KeyWord.cs @@ -83,8 +83,8 @@ namespace StgSharp.Script.Express Remove = "REMOVE", ANDOR = "ANDOR", GetIndex = "[", - UnaryAdd = "+", - UnarySub = "-", + UnaryPlus = "+", + UnaryMinus = "-", Exp = "**"; } diff --git a/StgSharp.Script/Express/ExpAstNode/ExpUnaryOperatorNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpUnaryOperatorNode.cs index d59a1fde..200babdd 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpUnaryOperatorNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpUnaryOperatorNode.cs @@ -58,7 +58,7 @@ namespace StgSharp.Script.Express public override IExpElementSource EqualityTypeConvert => _operand.EqualityTypeConvert; - public static ExpUnaryOperatorNode UnaryNegation( ExpNode operand ) + public static ExpUnaryOperatorNode UnaryMinus( ExpNode operand ) { if( !operand.IsNumber ) { throw new ExpInvalidTypeException( @@ -87,5 +87,20 @@ namespace StgSharp.Script.Express }; } + public static ExpUnaryOperatorNode UnaryPlus( ExpNode operand ) + { + if( !operand.IsNumber ) { + throw new ExpInvalidTypeException( + "Number", operand.EqualityTypeConvert.Name ); + } + bool isFloat = + ( operand._nodeFlag | ExpNodeFlag.BuiltinType_Float ) == 0; + return new ExpUnaryOperatorNode( ExpCompile.KeyWord.SUB ) + { + _nodeFlag = ExpNodeFlag.Operator_Unary | ExpNodeFlag.BuiltinType_Number, + CodeConvertTemplate = "+ {0}", + }; + } + } } diff --git a/StgSharp.Script/Express/ExpAstNode/_ExpNode.cs b/StgSharp.Script/Express/ExpAstNode/_ExpNode.cs index 2f1437eb..0f554fc9 100644 --- a/StgSharp.Script/Express/ExpAstNode/_ExpNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/_ExpNode.cs @@ -32,6 +32,7 @@ using CommunityToolkit.HighPerformance.Buffers; using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Text; namespace StgSharp.Script.Express @@ -96,11 +97,11 @@ namespace StgSharp.Script.Express public string Name => _name; - public virtual void AppendNode( ExpNode nextToken ) + public void AppendNode( ExpNode nextNode ) { - Next.Previous = nextToken; - nextToken.Next = Next; - Next = nextToken; + Next.Previous = nextNode; + nextNode.Next = Next; + Next = nextNode; Next.Previous = this; } @@ -114,6 +115,20 @@ namespace StgSharp.Script.Express return node == ExpEmptyNode._only || node == null; } + [MethodImpl( MethodImplOptions.AggressiveInlining )] + public static ExpNode NonOperation( string name ) + { + return new ExpEmptyNode( name ); + } + + public void PrependNode( ExpNode previousNode ) + { + Previous.Next = previousNode; + previousNode.Previous = Previous; + Previous = previousNode; + previousNode.Next = this; + } + public static bool VerifyTypeConvertable( ExpNode left, ExpNode right ) { IExpElementSource tLeft = left.EqualityTypeConvert, @@ -131,6 +146,8 @@ namespace StgSharp.Script.Express public ExpEmptyNode() : base( string.Empty ) { } + public ExpEmptyNode( string name ) : base( name ) { } + public override ExpInstantiableElement EqualityTypeConvert => ExpInstantiableElement.Void; public override ExpNode Left => _only; diff --git a/StgSharp.Script/Express/NodeGenerator/ExpNodeGenerator.cs b/StgSharp.Script/Express/NodeGenerator/ExpNodeGenerator.cs index 182a6641..d88c5857 100644 --- a/StgSharp.Script/Express/NodeGenerator/ExpNodeGenerator.cs +++ b/StgSharp.Script/Express/NodeGenerator/ExpNodeGenerator.cs @@ -66,88 +66,55 @@ namespace StgSharp.Script.Express /// meets separators or operators. /// /// - public void AppendToken( Token expToken ) + public ExpNode AppendToken( Token expToken ) { switch( expToken.Flag ) { case TokenFlag.Symbol_Unary or TokenFlag.Symbol_Binary: - _operandsToken.Push( expToken ); break; case TokenFlag.Number or TokenFlag.String or TokenFlag.Member: break; - case TokenFlag.Separator_Single: + case TokenFlag.Separator_Single: // a , ; found break; - case TokenFlag.Separator_Left: + case TokenFlag.Separator_Left: //a ( [ { found break; - case TokenFlag.Separator_Right: + case TokenFlag.Separator_Right: //a } ] ) found break; - case TokenFlag.Index_Left: + case TokenFlag.Index_Left: // a [ found break; - case TokenFlag.Index_Right: + case TokenFlag.Index_Right: // a ] found break; default: break; } } - public void CacheNextOperandNode( ExpNode node ) + public ExpNode GetNextOperandCache() { - _operandsNode.Push( node ); + if( _operandsNode.Count != 0 ) { + return _operandsNode.Pop(); + } + if( _operandsToken.Count != 0 ) { + Token token = _operandsToken.Pop(); + } + return ExpNode.Empty; } - public AbstractSyntaxTree CloseAndDispose() + //ConvertOneOperator not finish + private void ConvertOneOperator() { - if( _operandsToken.Count != 0 ) { - // convert rest to AST - } - return new AbstractSyntaxTree(); - } - /* - public ExpNode GenerateNode( Token t ) - { - switch( t.Flag ) { + Token @operator = _operators.Pop(); + switch( @operator.Flag ) { case TokenFlag.Symbol_Unary: break; case TokenFlag.Symbol_Binary: break; - case TokenFlag.Number: - break; - case TokenFlag.String: - break; - case TokenFlag.Member: - break; - case TokenFlag.Separator_Single: - break; - case TokenFlag.Separator_Left: - break; - case TokenFlag.Separator_Right: - break; case TokenFlag.Index_Left: break; - case TokenFlag.Index_Right: - break; default: break; } } - /**/ - - public int GetNextOperandCache( out Token? token, out ExpNode? node ) - { - if( _operandsNode.Count != 0 ) { - node = _operandsNode.Pop(); - token = null!; - return 2; - } - if( _operandsToken.Count != 0 ) { - token = _operandsToken.Pop(); - node = null!; - return 1; - } - token = null; - node = null!; - return 0; - } } } diff --git a/StgSharp.Script/Express/NodeGenerator/FunctionCallingGenerator.cs b/StgSharp.Script/Express/NodeGenerator/FunctionCallingGenerator.cs new file mode 100644 index 00000000..94fbd0de --- /dev/null +++ b/StgSharp.Script/Express/NodeGenerator/FunctionCallingGenerator.cs @@ -0,0 +1,74 @@ +//----------------------------------------------------------------------- +//----------------------------------------------------------------------- +// file="FunctionCallingGenerator.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.Linq; +using System.Text; +using System.Threading.Tasks; + +using static StgSharp.Script.Express.ExpCompile; + +namespace StgSharp.Script.Express +{ + public partial class ExpNodeGenerator + { + + private bool _isCallingFunction = false; + private Stack _callDepth = new Stack(); + + private void EnterFunctionCalling( Token funcNameToken ) + { + _isCallingFunction = true; + _callDepth.Push( _operandsToken.Count ); + } + + private void ExitFunctionCalling() + { + _callDepth.Pop(); + if( _callDepth.Count == 0 ) { + _isCallingFunction = false; + } + } + + //TODO cannot process occasion of void input or null at first + private void MergeFunctionCallingParameter() + { + if( _callDepth.Peek() == _operandsToken.Count ) { + _operandsNode.Peek() + .PrependNode( + ExpNode.NonOperation( PoolString( "FuncParam" ) ) ); + } else { + //TODO compile the param expression + } + } + + } +} diff --git a/StgSharp.Script/Express/NodeGenerator/GenerateUnaryToken.cs b/StgSharp.Script/Express/NodeGenerator/GenerateUnaryToken.cs index 53fc4c48..e15a2ab7 100644 --- a/StgSharp.Script/Express/NodeGenerator/GenerateUnaryToken.cs +++ b/StgSharp.Script/Express/NodeGenerator/GenerateUnaryToken.cs @@ -38,21 +38,48 @@ namespace StgSharp.Script.Express { public partial class ExpNodeGenerator { - /* - public ExpNode GenerateUnaryToken( Token t ) + + public bool TryGenerateBinaryNode( Token t, out ExpNode node ) { + ExpNode left = GetNextOperandCache(); + ExpNode right = GetNextOperandCache(); switch( t.Value ) { - case ExpCompile.KeyWord.UnaryAdd: - break; - case ExpCompile.KeyWord.UnarySub: - break; - case ExpCompile.KeyWord.NOT: - break; + case ExpCompile.KeyWord.ADD: + node = ExpBinaryOperatorNode.Add( left, right ); + return true; + case ExpCompile.KeyWord.SUB: + node = ExpBinaryOperatorNode.Sub( left, right ); + return true; + case ExpCompile.KeyWord.MUL: + node = ExpBinaryOperatorNode.Mul( left, right ); + return true; + case ExpCompile.KeyWord.DIV: + node = ExpBinaryOperatorNode.Div( left, right ); + return true; default: - break; + node = ExpNode.Empty; + return false; + } + } + + public bool TryGenerateUnaryNode( Token t, out ExpNode node ) + { + ExpNode operand = GetNextOperandCache(); + switch( t.Value ) { + case ExpCompile.KeyWord.UnaryPlus: + node = ExpUnaryOperatorNode.UnaryPlus( operand ); + return true; + case ExpCompile.KeyWord.UnaryMinus: + node = ExpUnaryOperatorNode.UnaryMinus( operand ); + return true; + case ExpCompile.KeyWord.NOT: + node = ExpUnaryOperatorNode.UnaryNot( operand ); + return true; + default: + node = ExpNode.Empty; + return false; } } - /**/ } } diff --git a/StgSharp.Script/IASTNode.cs b/StgSharp.Script/IASTNode.cs index 23d6ead8..de4b0b08 100644 --- a/StgSharp.Script/IASTNode.cs +++ b/StgSharp.Script/IASTNode.cs @@ -86,5 +86,7 @@ namespace StgSharp.Script void AppendNode( TNode nextToken ); + void PrependNode( TNode previusNode ); + } } 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 a4bdd447..9f3994d3 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+825e68853cd1738287eecc8a4390ee9c78550308")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5d4f516a2836a307ab61298b3d96c20a81324634")] [assembly: System.Reflection.AssemblyProductAttribute("StgSharp.Script")] [assembly: System.Reflection.AssemblyTitleAttribute("StgSharp.Script")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]