Daily update on 20250303-043627 by Nitload Youmu. Additional Message : remove node dependency to context, node instance ctor switched to factory mode,

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2025-03-03 04:36:27 +08:00
parent d12bbbd873
commit 825e68853c
26 changed files with 440 additions and 238 deletions
@@ -37,40 +37,23 @@ using System.Threading.Tasks;
namespace StgSharp.Script.Express
{
public class ExpBinaryOperatorNode : ExpBaseNode
public class ExpBinaryOperatorNode : ExpNode
{
private ExpBaseNode _left;
private ExpBaseNode _right;
private ExpInstantiableElementBase _returnType;
private ExpInstantiableElement _returnType;
public ExpBinaryOperatorNode( string name ) : base( name, null! ) { }
private ExpNode _left;
private ExpNode _right;
public ExpBinaryOperatorNode(
string source,
ExpSchema context,
ExpBaseNode left,
ExpBaseNode right )
: base( source, context )
{
if( !left.EqualityTypeConvert
.IsConvertable( right.EqualityTypeConvert ) ) {
throw new InvalidCastException(
$"Cannot convert element {left.EqualityTypeConvert} to {right.EqualityTypeConvert}" );
}
_left = left;
_right = right;
}
private ExpBinaryOperatorNode( string name ) : base( name ) { }
public override ExpBaseNode Left => _left;
public override ExpInstantiableElement EqualityTypeConvert => _returnType;
public override ExpBaseNode Right => _right;
public override ExpNode Left => _left;
public override ExpInstantiableElementBase EqualityTypeConvert => _returnType;
public override ExpNode Right => _right;
public static ExpBinaryOperatorNode Add(
ExpBaseNode left,
ExpBaseNode right )
public static ExpBinaryOperatorNode Add( ExpNode left, ExpNode right )
{
if( !left.IsNumber && !right.IsNumber ) {
throw new ExpInvalidTypeException(
@@ -88,8 +71,8 @@ namespace StgSharp.Script.Express
}
public static ExpBinaryOperatorNode Assign(
ExpBaseNode left,
ExpBaseNode right )
ExpNode left,
ExpNode right )
{
if( left.EqualityTypeConvert != right.EqualityTypeConvert ) {
throw new ExpInvalidTypeException(
@@ -98,15 +81,13 @@ namespace StgSharp.Script.Express
}
return new ExpBinaryOperatorNode( ExpCompile.KeyWord.Assignment )
{
_returnType = ExpTypeSource.,
_returnType = ExpInstantiableElement.Void,
_nodeFlag = ExpNodeFlag.Operator_Binary | left._nodeFlag,
CodeConvertTemplate = "{0} & {1}",
};
}
public static ExpBinaryOperatorNode Div(
ExpBaseNode left,
ExpBaseNode right )
public static ExpBinaryOperatorNode Div( ExpNode left, ExpNode right )
{
if( !left.IsNumber && !right.IsNumber ) {
throw new ExpInvalidTypeException(
@@ -123,9 +104,7 @@ namespace StgSharp.Script.Express
};
}
public static ExpBinaryOperatorNode Mul(
ExpBaseNode left,
ExpBaseNode right )
public static ExpBinaryOperatorNode Mul( ExpNode left, ExpNode right )
{
if( !left.IsNumber && !right.IsNumber ) {
throw new ExpInvalidTypeException(
@@ -142,9 +121,7 @@ namespace StgSharp.Script.Express
};
}
public static ExpBinaryOperatorNode Sub(
ExpBaseNode left,
ExpBaseNode right )
public static ExpBinaryOperatorNode Sub( ExpNode left, ExpNode right )
{
if( !left.IsNumber && !right.IsNumber ) {
throw new ExpInvalidTypeException(
@@ -36,32 +36,33 @@ using System.Threading.Tasks;
namespace StgSharp.Script.Express
{
internal class ExpFunctionCallingNode : ExpBaseNode
internal class ExpFunctionCallingNode : ExpNode
{
private ExpBaseNode _parameter;
private ExpFunctionSource _function;
private ExpNode _parameter;
public ExpFunctionCallingNode(
string functionName,
int line,
int column,
ExpSchema context,
ExpBaseNode firstParameter )
: base( $"{functionName}_{line}_{column}", context )
ExpFunctionSource function,
ExpNode firstParameter )
: base( $"{function.Name}_call" )
{
_function = function;
_parameter = firstParameter;
if( !context.TryGetFunction( functionName, out _function ) ) {
foreach( var item in context. ) { }
}
}
public override ExpBaseNode Left => throw new NotImplementedException();
public override ExpBaseNode Right => _parameter;
public override ExpInstantiableElementBase EqualityTypeConvert => throw new NotImplementedException(
public override ExpInstantiableElement EqualityTypeConvert => throw new NotImplementedException(
);
public override ExpNode Left => Empty;
public override ExpNode Right => _parameter;
public void AppendParameter( ExpNode parameter )
{
_parameter.AppendNode( parameter );
}
}
}
+19 -14
View File
@@ -36,41 +36,46 @@ using System.Text;
namespace StgSharp.Script.Express
{
public sealed class ExpIfNode : ExpBaseNode
public sealed class ExpIfNode : ExpNode
{
private ExpBaseNode _falseOpBegin;
private ExpBaseNode _rule;
private ExpBaseNode _trueOpBegin ;
private ExpNode _falseOpBegin;
private ExpNode _rule;
private ExpNode _trueOpBegin ;
public ExpIfNode(
string name,
ExpSchema context,
ExpBaseNode rule,
ExpBaseNode rootOperationIfTrue,
ExpBaseNode rootOperationIfFalse )
: base( name, context )
ExpNode rule,
ExpNode rootOperationIfTrue,
ExpNode rootOperationIfFalse )
: base( name )
{
_rule = rule;
_trueOpBegin = rootOperationIfFalse;
_falseOpBegin = rootOperationIfFalse;
CodeConvertTemplate = """
if( {0} ) {
{1}
} else {
{2}
}
""";
}
public override ExpBaseNode Left => _trueOpBegin;
public override ExpNode Left => _trueOpBegin;
public override ExpBaseNode Right => _falseOpBegin;
public override ExpNode Right => _falseOpBegin;
public override IExpElementSource EqualityTypeConvert => null!;
public void AppendFalseOperation( ExpBaseNode expressionRoot )
public void AppendFalseOperation( ExpNode expressionRoot )
{
AssertSchemaInclude( expressionRoot );
_falseOpBegin.AppendNode( expressionRoot );
}
public void AppendTrueOperation( ExpBaseNode expressionRoot )
public void AppendTrueOperation( ExpNode expressionRoot )
{
AssertSchemaInclude( expressionRoot );
_trueOpBegin.AppendNode( expressionRoot );
}
@@ -36,29 +36,29 @@ using System.Threading.Tasks;
namespace StgSharp.Script.Express
{
public class ExpIndexOfNode : ExpBaseNode
public class ExpIndexOfNode : ExpNode
{
private ExpBaseNode _indexExpression;
private ExpCollectionInstanceBase _collection;
private ExpNode _indexExpression;
public ExpIndexOfNode(
ExpSchema context,
ExpCollectionInstanceBase instance,
ExpBaseNode indexExpression )
: base( ExpCompile.PoolString( "[" ), context )
ExpNode indexExpression )
: base( ExpCompile.PoolString( "[" ) )
{
_collection = instance;
_indexExpression = indexExpression;
CodeConvertTemplate = @"{0}[{1}]";
}
public override ExpBaseNode Left => _collection;
public override ExpNode Left => _collection;
public override ExpBaseNode Right => throw new NotImplementedException();
public override ExpNode Right => _indexExpression;
public override IExpElementSource EqualityTypeConvert => throw new NotImplementedException(
);
public override IExpElementSource EqualityTypeConvert => _collection.EqualityTypeConvert;
}
}
@@ -51,7 +51,7 @@ namespace StgSharp.Script.Express
private readonly T _value;
public ExpLiteralGenericNode( string name, ExpSchema context, T value )
: base( name, context )
: base( name )
{
_value = value;
switch( value ) {
@@ -93,9 +93,9 @@ namespace StgSharp.Script.Express
}
}
public override ExpBaseNode Left => Empty;
public override ExpNode Left => Empty;
public override ExpBaseNode Right => throw new NotImplementedException();
public override ExpNode Right => throw new NotImplementedException();
public override IExpElementSource EqualityTypeConvert => _type;
@@ -36,22 +36,24 @@ using System.Text;
namespace StgSharp.Script.Express
{
public class ExpRepeatToken : ExpBaseNode
public class ExpRepeatToken : ExpNode
{
private ExpBaseNode _operationBegin;
//TODO: Current code cannot represent full functionality defined in ISO10303, need rewrite.
private ExpElementInstanceBase _variable, _begin, _end, _increment;
private ExpNode _operationBegin;
public ExpRepeatToken(
string name,
ExpSchema context,
ExpBaseNode operationBegin,
ExpNode operationBegin,
ExpElementInstanceBase variable,
ExpElementInstanceBase begin,
ExpElementInstanceBase end,
ExpElementInstanceBase increment )
: base( name, context )
: base( name )
{
if( variable.NodeFlag != begin.NodeFlag || begin.NodeFlag !=
end.NodeFlag || ( increment != null && end.NodeFlag !=
@@ -81,13 +83,13 @@ namespace StgSharp.Script.Express
_operationBegin = operationBegin;
}
public override ExpBaseNode Left => throw new NotImplementedException();
public override ExpNode Left => throw new NotImplementedException();
public override ExpBaseNode Right => _operationBegin;
public override ExpNode Right => _operationBegin;
public override IExpElementSource EqualityTypeConvert => null!;
public void AppendRepeatedOperation( ExpBaseNode node )
public void AppendRepeatedOperation( ExpNode node )
{
_operationBegin.AppendNode( node );
}
@@ -34,25 +34,25 @@ using System.Text;
namespace StgSharp.Script.Express
{
public class ExpSwitchNode : ExpBaseNode
public class ExpSwitchNode : ExpNode
{
private Dictionary<ExpBaseNode, ExpBaseNode> _cases;
private ExpBaseNode _default;
private ExpBaseNode _target;
private Dictionary<ExpNode, ExpNode> _cases;
private ExpNode _default;
private ExpNode _target;
public ExpSwitchNode(
string name,
ExpSchema context,
ExpBaseNode target,
ExpBaseNode defaultCase,
params (ExpBaseNode label, ExpBaseNode op)[] cases )
: base( name, context )
ExpNode target,
ExpNode defaultCase,
params (ExpNode label, ExpNode op)[] cases )
: base( name )
{
_target = target;
_default = defaultCase;
_cases = new Dictionary<ExpBaseNode, ExpBaseNode>();
foreach( (ExpBaseNode label, ExpBaseNode op ) item in cases ) {
_cases = new Dictionary<ExpNode, ExpNode>();
foreach( (ExpNode label, ExpNode op ) item in cases ) {
if( item.op == null ) {
_cases.Add( item.label, null! );
} else {
@@ -61,24 +61,24 @@ namespace StgSharp.Script.Express
}
}
public ExpBaseNode this[ ExpBaseNode index ]
public ExpNode this[ ExpNode index ]
{
get
{
if( _cases.TryGetValue( index, out ExpBaseNode? ret ) ) {
if( _cases.TryGetValue( index, out ExpNode? ret ) ) {
return ret;
}
return _default;
}
}
public override ExpBaseNode Left => throw new NotImplementedException();
public override ExpNode Left => throw new NotImplementedException();
public override ExpBaseNode Right => throw new NotImplementedException();
public override ExpNode Right => throw new NotImplementedException();
public override IExpElementSource EqualityTypeConvert => null!;
public void AddOperationToDefault( ExpBaseNode expressionRoot )
public void AddOperationToDefault( ExpNode expressionRoot )
{
if( expressionRoot != null ) {
_default.AppendNode( expressionRoot );
@@ -93,18 +93,17 @@ namespace StgSharp.Script.Express
/// <param name="expressionRoot"> Root token of the expression to be added. </param>
/// <exception cref="ExpCaseNotFoundException"> Cannot fine the case label in current witch expression. </exception>
public void AppendOperationToCase(
ExpBaseNode label,
ExpBaseNode expressionRoot )
ExpNode label,
ExpNode expressionRoot )
{
AssertSchemaInclude( expressionRoot );
if( _cases.TryGetValue( label, out ExpBaseNode? op ) ) {
if( _cases.TryGetValue( label, out ExpNode? op ) ) {
op.AppendNode( expressionRoot );
} else {
throw new ExpCaseNotFoundException( this, label );
}
}
public bool ContainsCase( ExpBaseNode token )
public bool ContainsCase( ExpNode token )
{
return _cases.ContainsKey( token );
}
@@ -36,25 +36,42 @@ using System.Threading.Tasks;
namespace StgSharp.Script.Express
{
public class ExpUnaryOperatorNode : ExpBaseNode
public class ExpUnaryOperatorNode : ExpNode
{
private ExpBaseNode _operand;
private ExpNode _operand;
private ExpUnaryOperatorNode( string name ) : base( name ) { }
public ExpUnaryOperatorNode(
string name,
ExpBaseNode operand,
ExpNode operand,
ExpSchema context )
: base( name, context )
: base( name )
{
_operand = operand;
}
public override ExpBaseNode Left => null!;
public override ExpNode Left => null!;
public override ExpBaseNode Right => _operand;
public override ExpNode Right => _operand;
public override IExpElementSource EqualityTypeConvert => _operand.EqualityTypeConvert;
public static ExpUnaryOperatorNode UnaryNotNegation( 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}",
};
}
}
}
@@ -0,0 +1,143 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="_ExpNode.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 CommunityToolkit.HighPerformance.Buffers;
using System;
using System.Collections.Generic;
using System.Text;
namespace StgSharp.Script.Express
{
public abstract class ExpNode : IASTNode<ExpNode, IExpElementSource>
{
protected internal ExpNodeFlag _nodeFlag;
protected internal string _name;
protected ExpNode( string name )
{
_name = name;
Next = this;
Previous = this;
}
public bool IsNumber
{
get { return ( _nodeFlag | ExpNodeFlag.BuiltinType_Number ) != 0; }
}
public abstract ExpNode Left
{
get;
}
public static ExpNode Empty
{
get => ExpEmptyNode._only;
}
public ExpNode Next
{
get;
set;
}
public abstract ExpNode Right
{
get;
}
public ExpNode Previous
{
get;
set;
}
public abstract IExpElementSource EqualityTypeConvert
{
get;
}
public long NodeFlag => ( long )_nodeFlag;
public string CodeConvertTemplate
{
get;
init;
}
public string Name => _name;
public virtual void AppendNode( ExpNode nextToken )
{
Next.Previous = nextToken;
nextToken.Next = Next;
Next = nextToken;
Next.Previous = this;
}
public ExpNodeNextEnumerator GetEnumerator()
{
return new ExpNodeNextEnumerator( this );
}
public static bool IsNullOrEmpty( ExpNode node )
{
return node == ExpEmptyNode._only || node == null;
}
public static bool VerifyTypeConvertable( ExpNode left, ExpNode right )
{
IExpElementSource tLeft = left.EqualityTypeConvert,
tRight = right.EqualityTypeConvert;
if( tLeft == null || tRight == null ) {
return false;
}
return tLeft.IsConvertable( tRight );
}
private class ExpEmptyNode : ExpNode
{
internal static readonly ExpEmptyNode _only = new();
public ExpEmptyNode() : base( string.Empty ) { }
public override ExpInstantiableElement EqualityTypeConvert => ExpInstantiableElement.Void;
public override ExpNode Left => _only;
public override ExpNode Right => _only;
}
}
}