diff --git a/StgSharp.Script/Express/Builtin/ExpCollection.cs b/StgSharp.Script/Express/Builtin/ExpCollection.cs index f5f2f0e7..18a6373f 100644 --- a/StgSharp.Script/Express/Builtin/ExpCollection.cs +++ b/StgSharp.Script/Express/Builtin/ExpCollection.cs @@ -37,7 +37,7 @@ using System.Text; namespace StgSharp.Script.Express { - public abstract class ExpCollectionBase : ExpInstantiableElementBase, IEnumerable + public abstract class ExpCollectionBase : ExpInstantiableElement, IEnumerable { protected internal string _accurateType; @@ -69,7 +69,7 @@ namespace StgSharp.Script.Express public abstract IEnumerator GetEnumerator(); public override sealed bool IsConvertable( - ExpInstantiableElementBase targetType ) + ExpInstantiableElement targetType ) { return false; } @@ -105,7 +105,7 @@ namespace StgSharp.Script.Express public override string Name => ExpCompile.PoolString( "ARRAY" ); - public override ExpBaseNode CreateInstanceNode() + public override ExpNode CreateInstanceNode() { throw new NotImplementedException(); } @@ -130,7 +130,7 @@ namespace StgSharp.Script.Express public override string Name => ExpCompile.PoolString( "BAG" ); - public override ExpBaseNode CreateInstanceNode() + public override ExpNode CreateInstanceNode() { throw new NotImplementedException(); } @@ -154,7 +154,7 @@ namespace StgSharp.Script.Express public override string Name => ExpCompile.PoolString( "LIST" ); - public override ExpBaseNode CreateInstanceNode() + public override ExpNode CreateInstanceNode() { throw new NotImplementedException(); } @@ -186,7 +186,7 @@ namespace StgSharp.Script.Express _collection.Add( token ); } - public override ExpBaseNode CreateInstanceNode() + public override ExpNode CreateInstanceNode() { throw new NotImplementedException(); } diff --git a/StgSharp.Script/Express/Builtin/ExpCollectionInstance.cs b/StgSharp.Script/Express/Builtin/ExpCollectionInstance.cs index 3a7b6feb..883ca42d 100644 --- a/StgSharp.Script/Express/Builtin/ExpCollectionInstance.cs +++ b/StgSharp.Script/Express/Builtin/ExpCollectionInstance.cs @@ -34,18 +34,14 @@ using System.Text; namespace StgSharp.Script.Express { - public abstract class ExpCollectionInstanceBase : ExpBaseNode + public abstract class ExpCollectionInstanceBase : ExpNode { protected internal (int min, int max) _range; protected internal ExpElementType _memberType; protected ExpCollectionInstanceBase( string name, ExpSchema context ) - : base( name, context ) { } - - public override ExpBaseNode Left => null!; - - public override ExpBaseNode Right => null!; + : base( name ) { } public abstract ExpCollectionBase CollectionValue { @@ -57,6 +53,10 @@ namespace StgSharp.Script.Express get => _memberType; } + public override ExpNode Left => null!; + + public override ExpNode Right => null!; + public int MinCapacity => _range.min; public int MaxCapacity => _range.max; @@ -69,14 +69,14 @@ namespace StgSharp.Script.Express private ExpSet _collection; public ExpSetInstance( string name, ExpSet set, ExpSchema context ) - : base( name, context ) + : base( name ) { _collection = set; } public override ExpCollectionBase CollectionValue => _collection; - public override ExpInstantiableElementBase EqualityTypeConvert => throw new NotImplementedException( + public override ExpInstantiableElement EqualityTypeConvert => throw new NotImplementedException( ); } @@ -87,14 +87,14 @@ namespace StgSharp.Script.Express private ExpBag _collection; public ExpBagInstance( string name, ExpBag bag, ExpSchema context ) - : base( name, context ) + : base( name ) { _collection = bag; } public override ExpCollectionBase CollectionValue => _collection; - public override ExpInstantiableElementBase EqualityTypeConvert => throw new NotImplementedException( + public override ExpInstantiableElement EqualityTypeConvert => throw new NotImplementedException( ); } @@ -105,14 +105,14 @@ namespace StgSharp.Script.Express private ExpList _collection; public ExpListInstance( string name, ExpList list, ExpSchema context ) - : base( name, context ) + : base( name ) { _collection = list; } public override ExpCollectionBase CollectionValue => _collection; - public override ExpInstantiableElementBase EqualityTypeConvert => throw new NotImplementedException( + public override ExpInstantiableElement EqualityTypeConvert => throw new NotImplementedException( ); } @@ -126,14 +126,14 @@ namespace StgSharp.Script.Express string name, ExpArray array, ExpSchema context ) - : base( name, context ) + : base( name ) { _collection = array; } public override ExpCollectionBase CollectionValue => _collection; - public override ExpInstantiableElementBase EqualityTypeConvert => _collection; + public override ExpInstantiableElement EqualityTypeConvert => _collection; } } diff --git a/StgSharp.Script/Express/Element/ExpConst.cs b/StgSharp.Script/Express/Element/ExpConst.cs index 42803106..f1266505 100644 --- a/StgSharp.Script/Express/Element/ExpConst.cs +++ b/StgSharp.Script/Express/Element/ExpConst.cs @@ -38,7 +38,7 @@ using System.Text; namespace StgSharp.Script.Express { - public sealed class ExpConstantCollectionSource : ExpImmutableElementBase + public sealed class ExpConstantCollectionSource : ExpImmutableElement { private Dictionary _constants; @@ -66,7 +66,7 @@ namespace StgSharp.Script.Express throw new NotImplementedException(); } - public override ExpBaseNode MakeReference( params object[] options ) + public override ExpNode MakeReference( params object[] options ) { if( options.Length == 0 || options[ 0 ] is not string str ) { return null!; diff --git a/StgSharp.Script/Express/Element/ExpEntity.cs b/StgSharp.Script/Express/Element/ExpEntity.cs index 99f72e43..b079942a 100644 --- a/StgSharp.Script/Express/Element/ExpEntity.cs +++ b/StgSharp.Script/Express/Element/ExpEntity.cs @@ -34,25 +34,24 @@ using System.Text; namespace StgSharp.Script.Express { - public class ExpEntity : ExpBaseNode + public class ExpEntity : ExpNode { - public ExpEntity( string name, ExpSchema context ) - : base( name, context ) { } + public ExpEntity( string name, ExpSchema context ) : base( name ) { } - 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 => throw new NotImplementedException( ); } - public class ExpEntitySource : ExpInstantiableElementBase + public class ExpEntitySource : ExpInstantiableElement { - private AbstractSyntaxTree _expressionTree; + private AbstractSyntaxTree _expressionTree; private ScriptSourceTransmitter _source; private string _name; @@ -73,12 +72,7 @@ namespace StgSharp.Script.Express throw new NotImplementedException(); } - public override ExpBaseNode CreateInstanceNode() - { - throw new NotImplementedException(); - } - - public override bool IsConvertable( IExpElementSource targetType ) + public override ExpNode CreateInstanceNode() { throw new NotImplementedException(); } diff --git a/StgSharp.Script/Express/Element/ExpFunction.cs b/StgSharp.Script/Express/Element/ExpFunction.cs index 00589af5..4e8248cb 100644 --- a/StgSharp.Script/Express/Element/ExpFunction.cs +++ b/StgSharp.Script/Express/Element/ExpFunction.cs @@ -34,32 +34,31 @@ using System.Text; namespace StgSharp.Script.Express { - public class ExpFunction : ExpBaseNode + public class ExpFunction : ExpNode { - private readonly ExpBaseNode _return; - private ExpFunctionSource _source; - private readonly List> _parameterList - = new List>(); - public ExpFunction( string name, ExpSchema context ) - : base( name, context ) { } + private readonly ExpNode _return; + private readonly List> _parameterList + = new List>(); - public override ExpBaseNode Left => throw new NotImplementedException(); - - public override ExpBaseNode Right => throw new NotImplementedException(); + public ExpFunction( string name, ExpSchema context ) : base( name ) { } public ExpElementType ElementType => ExpElementType.Function; - public override ExpInstantiableElementBase EqualityTypeConvert => throw new NotImplementedException( + public override ExpInstantiableElement EqualityTypeConvert => throw new NotImplementedException( ); - public void CallThisFunction( params ExpBaseNode[] parameters ) { } + public override ExpNode Left => throw new NotImplementedException(); + + public override ExpNode Right => throw new NotImplementedException(); + + public void CallThisFunction( params ExpNode[] parameters ) { } } - public class ExpFunctionSource : ExpImmutableElementBase + public class ExpFunctionSource : ExpImmutableElement { private ScriptSourceTransmitter _transmitter; @@ -95,7 +94,7 @@ namespace StgSharp.Script.Express */ } - public override ExpBaseNode MakeReference( params object[] options ) + public override ExpNode MakeReference( params object[] options ) { throw new NotImplementedException(); } diff --git a/StgSharp.Script/Express/Element/ExpRule.cs b/StgSharp.Script/Express/Element/ExpRule.cs index 628ac2ab..da41bdfe 100644 --- a/StgSharp.Script/Express/Element/ExpRule.cs +++ b/StgSharp.Script/Express/Element/ExpRule.cs @@ -41,23 +41,22 @@ namespace StgSharp.Script.Express private ExpRuleSource _source; - public ExpRule( string name, ExpSchema context ) - : base( name, context ) { } + public ExpRule( string name, ExpSchema context ) : base( name ) { } public object? value => throw new NotImplementedException(); - public override ExpBaseNode Left => throw new NotImplementedException(); - - public override ExpBaseNode Right => throw new NotImplementedException(); - - public override ExpInstantiableElementBase EqualityTypeConvert => throw new NotImplementedException( + public override ExpInstantiableElement EqualityTypeConvert => throw new NotImplementedException( ); + public override ExpNode Left => throw new NotImplementedException(); + + public override ExpNode Right => throw new NotImplementedException(); + public override string TypeName => _source.Name; } - public class ExpRuleSource : ExpImmutableElementBase + public class ExpRuleSource : ExpImmutableElement { private ScriptSourceTransmitter _transmitter; @@ -80,7 +79,7 @@ namespace StgSharp.Script.Express throw new NotImplementedException(); } - public override ExpBaseNode MakeReference( params object[] options ) + public override ExpNode MakeReference( params object[] options ) { throw new NotImplementedException(); } diff --git a/StgSharp.Script/Express/Element/ExpType.cs b/StgSharp.Script/Express/Element/ExpType.cs index 236869a9..0347eb54 100644 --- a/StgSharp.Script/Express/Element/ExpType.cs +++ b/StgSharp.Script/Express/Element/ExpType.cs @@ -35,19 +35,18 @@ using System.Collections.Generic; namespace StgSharp.Script.Express { - public class ExpType : ExpElementInstanceBase + public class ExpTypeInstance : ExpElementInstanceBase { - public ExpType( string name, ExpSchema context ) - : base( name, context ) { } + public ExpTypeInstance( string name ) : base( name ) { } - public override ExpBaseNode Left => throw new NotImplementedException(); - - public override ExpBaseNode Right => throw new NotImplementedException(); - - public override IExpElementSource EqualityTypeConvert => throw new NotImplementedException( + public override ExpInstantiableElement EqualityTypeConvert => throw new NotImplementedException( ); + public override ExpNode Left => throw new NotImplementedException(); + + public override ExpNode Right => throw new NotImplementedException(); + public override string TypeName => throw new NotImplementedException(); } @@ -80,6 +79,11 @@ namespace StgSharp.Script.Express public ExpElementType ElementType => ExpElementType.Type; + public ExpTypeSource Void + { + get => ExpVoidType.Only; + } + public IScriptSourceProvider SourceProvider => _transmitter; public string Name => _name; @@ -94,5 +98,30 @@ namespace StgSharp.Script.Express throw new NotImplementedException(); } + public static bool IsNullOrVoid( IExpElementSource type ) + { + return type == null || type == ExpVoidType.Only; + } + + private class ExpVoidType : ExpTypeSource + { + + private static readonly ExpVoidType _onlyInstance = new ExpVoidType( + ); + + private ExpVoidType() : base( string.Empty ) { } + + public static ExpVoidType Only + { + get => _onlyInstance; + } + + public override void Analyse() + { + return; + } + + } + } } \ No newline at end of file diff --git a/StgSharp.Script/Express/ExpAstNode/ExpBinaryOperatorNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpBinaryOperatorNode.cs index 1d5cf950..d52c0d68 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpBinaryOperatorNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpBinaryOperatorNode.cs @@ -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( diff --git a/StgSharp.Script/Express/ExpAstNode/ExpFunctionCallingNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpFunctionCallingNode.cs index d2885aab..0d71aa21 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpFunctionCallingNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpFunctionCallingNode.cs @@ -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 ); + } + } } diff --git a/StgSharp.Script/Express/ExpAstNode/ExpIfNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpIfNode.cs index 48bedde9..84a5c62b 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpIfNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpIfNode.cs @@ -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 ); } diff --git a/StgSharp.Script/Express/ExpAstNode/ExpIndexOfNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpIndexOfNode.cs index 589fba48..b20a8890 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpIndexOfNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpIndexOfNode.cs @@ -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; } } diff --git a/StgSharp.Script/Express/ExpAstNode/ExpLiteralGenericNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpLiteralGenericNode.cs index 0a8ca136..6bd275c4 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpLiteralGenericNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpLiteralGenericNode.cs @@ -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; diff --git a/StgSharp.Script/Express/ExpAstNode/ExpRepeatNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpRepeatNode.cs index e6fe62bb..bda6f1ea 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpRepeatNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpRepeatNode.cs @@ -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 ); } diff --git a/StgSharp.Script/Express/ExpAstNode/ExpSwitchNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpSwitchNode.cs index 25cbdc28..c55d28d7 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpSwitchNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpSwitchNode.cs @@ -34,25 +34,25 @@ using System.Text; namespace StgSharp.Script.Express { - public class ExpSwitchNode : ExpBaseNode + public class ExpSwitchNode : ExpNode { - private Dictionary _cases; - private ExpBaseNode _default; - private ExpBaseNode _target; + private Dictionary _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(); - foreach( (ExpBaseNode label, ExpBaseNode op ) item in cases ) { + _cases = new Dictionary(); + 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 /// Root token of the expression to be added. /// Cannot fine the case label in current witch expression. 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 ); } diff --git a/StgSharp.Script/Express/ExpAstNode/ExpUnaryOperatorNode.cs b/StgSharp.Script/Express/ExpAstNode/ExpUnaryOperatorNode.cs index 56adb556..9e7e449f 100644 --- a/StgSharp.Script/Express/ExpAstNode/ExpUnaryOperatorNode.cs +++ b/StgSharp.Script/Express/ExpAstNode/ExpUnaryOperatorNode.cs @@ -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}", + }; + } + } } diff --git a/StgSharp.Script/Express/ExpAstNode/_ExpNode.cs b/StgSharp.Script/Express/ExpAstNode/_ExpNode.cs new file mode 100644 index 00000000..2f1437eb --- /dev/null +++ b/StgSharp.Script/Express/ExpAstNode/_ExpNode.cs @@ -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 + { + + 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; + + } + + } +} diff --git a/StgSharp.Script/Express/ExpElementInstanceBase.cs b/StgSharp.Script/Express/ExpElementInstanceBase.cs index d0d7c324..71d2f81b 100644 --- a/StgSharp.Script/Express/ExpElementInstanceBase.cs +++ b/StgSharp.Script/Express/ExpElementInstanceBase.cs @@ -53,11 +53,10 @@ namespace StgSharp.Script.Express } - public abstract class ExpElementInstanceBase : ExpBaseNode + public abstract class ExpElementInstanceBase : ExpNode { - protected ExpElementInstanceBase( string name, ExpSchema context ) - : base( name, context ) { } + protected ExpElementInstanceBase( string name ) : base( name ) { } public ExpElementType ElementType { diff --git a/StgSharp.Script/Express/ExpElmentSource.cs b/StgSharp.Script/Express/ExpElmentSource.cs index 2a12049e..9198451c 100644 --- a/StgSharp.Script/Express/ExpElmentSource.cs +++ b/StgSharp.Script/Express/ExpElmentSource.cs @@ -58,15 +58,19 @@ namespace StgSharp.Script.Express } - public abstract class ExpInstantiableElementBase : IExpElementSource + public abstract class ExpInstantiableElement : IExpElementSource { + private List _baseList = new List( + ), + _derivationList = new List(); + public abstract ExpElementType ElementType { get; } - public static ExpInstantiableElementBase Void + public static ExpInstantiableElement Void { get => ExpVoidElement.Only; } @@ -83,12 +87,22 @@ namespace StgSharp.Script.Express public abstract void Analyse(); - public abstract ExpBaseNode CreateInstanceNode(); + public abstract ExpNode CreateInstanceNode(); - public abstract bool IsConvertable( - ExpInstantiableElementBase targetType ); + public bool IsConvertable( IExpElementSource targetType ) + { + if( targetType == null ) { + return false; + } + throw new NotImplementedException(); + } - private sealed class ExpVoidElement : ExpInstantiableElementBase + public static bool IsNullOrVoid( ExpInstantiableElement element ) + { + return element == null || element == ExpVoidElement.Only; + } + + private sealed class ExpVoidElement : ExpInstantiableElement { private static readonly ExpVoidElement _only = new ExpVoidElement(); @@ -106,22 +120,16 @@ namespace StgSharp.Script.Express return; } - public override ExpBaseNode CreateInstanceNode() + public override ExpNode CreateInstanceNode() { return default!; } - public override bool IsConvertable( - ExpInstantiableElementBase targetType ) - { - return false; - } - } } - public abstract class ExpImmutableElementBase : IExpElementSource + public abstract class ExpImmutableElement : IExpElementSource { public abstract ExpElementType ElementType @@ -146,7 +154,7 @@ namespace StgSharp.Script.Express return false; } - public abstract ExpBaseNode MakeReference( params object[] options ); + public abstract ExpNode MakeReference( params object[] options ); } } diff --git a/StgSharp.Script/Express/ExpExceptions.cs b/StgSharp.Script/Express/ExpExceptions.cs index 8299e596..b9cc3a0f 100644 --- a/StgSharp.Script/Express/ExpExceptions.cs +++ b/StgSharp.Script/Express/ExpExceptions.cs @@ -30,20 +30,14 @@ //----------------------------------------------------------------------- using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; +using System.Runtime.CompilerServices; +using System.Runtime.Serialization; using System.Text; namespace StgSharp.Script.Express { - public class ExpCompileNotInitializedException : InvalidOperationException - { - - public ExpCompileNotInitializedException() - : base( - "Attempt to compile an EXPRESS script before the init of compiler." ) { } - - } - public class ExpInvalidCharException : Exception { @@ -52,6 +46,9 @@ namespace StgSharp.Script.Express } + /// + /// Exception when cannot reading a token from a line. + /// public class ExpInvalidTokenException : Exception { @@ -60,10 +57,10 @@ namespace StgSharp.Script.Express } - public class ExpInvalidTypeDeclareEndingExceptions : Exception + public class ExpInvalidElementDeclareEndingExceptions : Exception { - public ExpInvalidTypeDeclareEndingExceptions( + public ExpInvalidElementDeclareEndingExceptions( string declaredName, string endedName ) : base( @@ -91,6 +88,15 @@ namespace StgSharp.Script.Express } + public class ExpCaseNotFoundException : ArgumentOutOfRangeException + { + + public ExpCaseNotFoundException( ExpSwitchNode token, ExpNode label ) + : base( + $"Attempt to read or write the label {label.Name} not exist in SWITCH expression {token.Name}" ) { } + + } + public class ExpInvalidCollectionMemberTypeException : InvalidCastException { @@ -109,27 +115,25 @@ namespace StgSharp.Script.Express ExpSchema context, ExpSchema source ) : base( - $"Attempted to reference an EXPRESS language expression in the current schema context '{context.Name}', " + $"but this expression comes from another schema '{source.Name}' that has not been included." ) { } + $"Attempted to refer to an EXPRESS language expression in the current schema context '{context.Name}', but this expression comes from another schema '{source.Name}' that has not been included." ) { } } - public class ExpCaseNotFoundException : ArgumentOutOfRangeException + public class ExpElementLoadException : TypeLoadException { - public ExpCaseNotFoundException( - ExpSwitchNode token, - ExpBaseNode label ) + public ExpElementLoadException( string typeName, ExpSchema context ) : base( - $"Attempt to read or write the label {label.Name} not exist in SWITCH expression {token.Name}" ) { } + $"Cannot find the element {typeName} in schema {context.Name} and its included schemas" ) { } } - public class ExpTypeNotFoundException : Exception + public class ExpCompileNotInitializedException : InvalidOperationException { - public ExpTypeNotFoundException( string typeName, ExpSchema context ) + public ExpCompileNotInitializedException() : base( - $"Cannot find the type {typeName} in schema {context.Name} and its included schemas" ) { } + "Attempt to compile an EXPRESS script before the init of compiler." ) { } } } diff --git a/StgSharp.Script/Express/ExpSchemaSource.cs b/StgSharp.Script/Express/ExpSchemaSource.cs index f5198c8e..59c19391 100644 --- a/StgSharp.Script/Express/ExpSchemaSource.cs +++ b/StgSharp.Script/Express/ExpSchemaSource.cs @@ -243,7 +243,7 @@ namespace StgSharp.Script.Express } string endedName = match.Groups[ "TypeName" ].ToString(); if( !string.Equals( endedName, _source.Name ) ) { - throw new ExpInvalidTypeDeclareEndingExceptions( + throw new ExpInvalidElementDeclareEndingExceptions( _source.Name, endedName ); } transmitter.WriteLine( "END_ENTITY" ); @@ -275,7 +275,7 @@ namespace StgSharp.Script.Express } string endedName = match.Groups[ "FunctionName" ].ToString(); if( !string.Equals( endedName, _source.Name ) ) { - throw new ExpInvalidTypeDeclareEndingExceptions( + throw new ExpInvalidElementDeclareEndingExceptions( _source.Name, endedName ); } transmitter.WriteLine( "END_FUNCTION" ); @@ -310,7 +310,7 @@ namespace StgSharp.Script.Express } string endedName = match.Groups[ "RuleName" ].ToString(); if( !string.Equals( endedName, _source.Name ) ) { - throw new ExpInvalidTypeDeclareEndingExceptions( + throw new ExpInvalidElementDeclareEndingExceptions( _source.Name, endedName ); } transmitter.WriteLine( "END_RULE" ); @@ -342,7 +342,7 @@ namespace StgSharp.Script.Express } string endedName = match.Groups[ "TypeName" ].ToString(); if( !string.Equals( endedName, _source.Name ) ) { - throw new ExpInvalidTypeDeclareEndingExceptions( + throw new ExpInvalidElementDeclareEndingExceptions( _source.Name, endedName ); } transmitter.WriteLine( "END_ENTITY" ); diff --git a/StgSharp.Script/Express/GeneratedExpSchema.cs b/StgSharp.Script/Express/GeneratedExpSchema.cs index aafde42b..6eec5506 100644 --- a/StgSharp.Script/Express/GeneratedExpSchema.cs +++ b/StgSharp.Script/Express/GeneratedExpSchema.cs @@ -62,7 +62,7 @@ namespace StgSharp.Script.Express return ret; } } - throw new ExpTypeNotFoundException( typeName, this ); + throw new ExpElementLoadException( typeName, this ); } public bool HasSchemaInclude( GeneratedExpSchema schema ) diff --git a/StgSharp.Script/Express/NodeGenerator/ExpNodeGenerator.cs b/StgSharp.Script/Express/NodeGenerator/ExpNodeGenerator.cs index a6bfedeb..f5b37586 100644 --- a/StgSharp.Script/Express/NodeGenerator/ExpNodeGenerator.cs +++ b/StgSharp.Script/Express/NodeGenerator/ExpNodeGenerator.cs @@ -46,12 +46,12 @@ namespace StgSharp.Script.Express private const int TokenGenerated = 0; private const int TokenTypeNotMatch = -1; - private ExpBaseNode _lastStatement; + private ExpNode _lastStatement; private GeneratedExpSchema _context; private IExpElementSource _local; - private List _cache; - private Stack _operandsNode; + private List _cache; + private Stack _operandsNode; private Stack _operandsToken; private Stack _operators; @@ -90,21 +90,21 @@ namespace StgSharp.Script.Express } } - public void CacheNextOperandNode( ExpBaseNode node ) + public void CacheNextOperandNode( ExpNode node ) { _operandsNode.Push( node ); } - public AbstractSyntaxTree CloseAndDispose( + public AbstractSyntaxTree CloseAndDispose( ) { if( _operandsToken.Count != 0 ) { // convert rest to AST } - return new AbstractSyntaxTree(); + return new AbstractSyntaxTree(); } - public ExpBaseNode GenerateNode( Token t ) + public ExpNode GenerateNode( Token t ) { switch( t.Flag ) { case TokenFlag.Symbol_Unary: @@ -134,7 +134,7 @@ namespace StgSharp.Script.Express public int GetNextOperandCache( out Token? token, - out ExpBaseNode? node ) + out ExpNode? node ) { if( _operandsNode.Count != 0 ) { node = _operandsNode.Pop(); diff --git a/StgSharp.Script/Express/NodeGenerator/GenerateUnaryToken.cs b/StgSharp.Script/Express/NodeGenerator/GenerateUnaryToken.cs index e6f658cf..2e5b7ba6 100644 --- a/StgSharp.Script/Express/NodeGenerator/GenerateUnaryToken.cs +++ b/StgSharp.Script/Express/NodeGenerator/GenerateUnaryToken.cs @@ -39,7 +39,7 @@ namespace StgSharp.Script.Express public partial class ExpNodeGenerator { - public ExpBaseNode GenerateUnaryToken( Token t ) + public ExpNode GenerateUnaryToken( Token t ) { switch( t.Value ) { case ExpCompile.KeyWord.UnaryAdd: diff --git a/StgSharp.Script/Express/NodeHelper/ExpNodeNextEnumerator.cs b/StgSharp.Script/Express/NodeHelper/ExpNodeNextEnumerator.cs index 5b289ebe..59af23cb 100644 --- a/StgSharp.Script/Express/NodeHelper/ExpNodeNextEnumerator.cs +++ b/StgSharp.Script/Express/NodeHelper/ExpNodeNextEnumerator.cs @@ -37,13 +37,13 @@ using System.Threading.Tasks; namespace StgSharp.Script.Express { - public class ExpNodeNextEnumerator : IEnumerator + public class ExpNodeNextEnumerator : IEnumerator { - private ExpBaseNode _begin; - private ExpBaseNode _current; + private ExpNode _begin; + private ExpNode _current; - public ExpNodeNextEnumerator( ExpBaseNode token ) + public ExpNodeNextEnumerator( ExpNode token ) { _begin = token; _current = token; @@ -56,7 +56,7 @@ namespace StgSharp.Script.Express bool IEnumerator.MoveNext() { - ExpBaseNode next = _current.Next; + ExpNode next = _current.Next; if( ReferenceEquals( next, _begin ) || next == null ) { return false; } @@ -71,7 +71,7 @@ namespace StgSharp.Script.Express object IEnumerator.Current => throw new NotImplementedException(); - ExpBaseNode IEnumerator.Current => throw new NotImplementedException( + ExpNode IEnumerator.Current => throw new NotImplementedException( ); } 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 b0f2f446..f833554a 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+b4b90da372efaff79c7c1c950ce22e7bb13223cb")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d12bbbd873ec13306fdfa5255965188674ed417c")] [assembly: System.Reflection.AssemblyProductAttribute("StgSharp.Script")] [assembly: System.Reflection.AssemblyTitleAttribute("StgSharp.Script")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/StgSharpDebug/View/NewRender.cs b/StgSharpDebug/View/NewRender.cs index 0d9d55c0..63346a2e 100644 --- a/StgSharpDebug/View/NewRender.cs +++ b/StgSharpDebug/View/NewRender.cs @@ -28,8 +28,16 @@ // //----------------------------------------------------------------------- //----------------------------------------------------------------------- +using StgSharp.Graphics; +using StgSharp.HighPerformance; + +using StgSharp.Internal; + using System; +using System.Collections.Generic; using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace StgSharp { @@ -42,5 +50,23 @@ namespace StgSharp set; } + private class NewRender : ViewRender + { + + public NewRender( ViewMain view ) : base( view ) { } + + public override void CustomizedInitialize() + { + Binding.Stream = CreateRenderStream(); + Render[ "only".AsLabel() ] = Binding.Stream; + } + + public override bool ValidateInitializing() + { + return Binding.Stream != null; + } + + } + } }