Daily update on 20250310-064733 by Nitload Youmu. Additional Message : Change: Generic node removed, five non-generic node as replacement

Change: Preprocess to token scheduled, less dependency on type prediction, which put to LINK or C# compiling pase
Design: the EXPRESS lang feature "FIXED" keyword not supported, builtin type binary not supported, not used in STEP AP203 and much too complex to make a full marshal implementation in C#.
Add: Node generating method to generate literal node

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2025-03-10 06:47:33 +08:00
parent 6f494f25ef
commit 16b813646d
31 changed files with 387 additions and 217 deletions
@@ -30,6 +30,7 @@
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
@@ -45,7 +46,7 @@ namespace StgSharp.Script.Express
private ExpNode _left;
private ExpNode _right;
private ExpBinaryOperatorNode( string name ) : base( name ) { }
private ExpBinaryOperatorNode( Token source ) : base( source ) { }
public override ExpInstantiableElement EqualityTypeConvert => _returnType;
@@ -53,24 +54,28 @@ namespace StgSharp.Script.Express
public override ExpNode Right => _right;
public static ExpBinaryOperatorNode Add( ExpNode left, ExpNode right )
public static ExpBinaryOperatorNode Add(
Token source,
ExpNode left,
ExpNode right )
{
if( !left.IsNumber && !right.IsNumber ) {
throw new ExpInvalidTypeException(
"Number", left.EqualityTypeConvert.Name );
}
bool isFloat =
( left._nodeFlag | ExpNodeFlag.BuiltinType_Float ) == 0 || ( right._nodeFlag | ExpNodeFlag.BuiltinType_Float ) ==
( left._nodeFlag | ExpNodeFlag.BuiltinType_Real ) == 0 || ( right._nodeFlag | ExpNodeFlag.BuiltinType_Real ) ==
0;
return new ExpBinaryOperatorNode( ExpCompile.KeyWord.ADD )
return new ExpBinaryOperatorNode( source )
{
_returnType = isFloat ? null : null,
_returnType = isFloat ? ExpCompile.ExpReal : ExpCompile.ExpInt,
_nodeFlag = ExpNodeFlag.Operator_Binary | ExpNodeFlag.BuiltinType_Number,
CodeConvertTemplate = "{0} + {1}",
};
}
public static ExpBinaryOperatorNode Assign(
Token source,
ExpNode left,
ExpNode right )
{
@@ -79,7 +84,7 @@ namespace StgSharp.Script.Express
left.EqualityTypeConvert.Name,
right.EqualityTypeConvert.Name );
}
return new ExpBinaryOperatorNode( ExpCompile.KeyWord.Assignment )
return new ExpBinaryOperatorNode( source )
{
_returnType = ExpInstantiableElement.Void,
_nodeFlag = ExpNodeFlag.Operator_Binary | left._nodeFlag,
@@ -87,52 +92,61 @@ namespace StgSharp.Script.Express
};
}
public static ExpBinaryOperatorNode Div( ExpNode left, ExpNode right )
public static ExpBinaryOperatorNode Div(
Token source,
ExpNode left,
ExpNode right )
{
if( !left.IsNumber && !right.IsNumber ) {
throw new ExpInvalidTypeException(
"Number", left.EqualityTypeConvert.Name );
}
bool isFloat =
( left._nodeFlag | ExpNodeFlag.BuiltinType_Float ) == 0 || ( right._nodeFlag | ExpNodeFlag.BuiltinType_Float ) ==
( left._nodeFlag | ExpNodeFlag.BuiltinType_Real ) == 0 || ( right._nodeFlag | ExpNodeFlag.BuiltinType_Real ) ==
0;
return new ExpBinaryOperatorNode( ExpCompile.KeyWord.DIV )
return new ExpBinaryOperatorNode( source )
{
_returnType = isFloat ? null : null,
_returnType = isFloat ? ExpCompile.ExpReal : ExpCompile.ExpInt,
_nodeFlag = ExpNodeFlag.Operator_Binary | ExpNodeFlag.BuiltinType_Number,
CodeConvertTemplate = "{0} / {1}",
};
}
public static ExpBinaryOperatorNode Mul( ExpNode left, ExpNode right )
public static ExpBinaryOperatorNode Mul(
Token source,
ExpNode left,
ExpNode right )
{
if( !left.IsNumber && !right.IsNumber ) {
throw new ExpInvalidTypeException(
"Number", left.EqualityTypeConvert.Name );
}
bool isFloat =
( left._nodeFlag | ExpNodeFlag.BuiltinType_Float ) == 0 || ( right._nodeFlag | ExpNodeFlag.BuiltinType_Float ) ==
( left._nodeFlag | ExpNodeFlag.BuiltinType_Real ) == 0 || ( right._nodeFlag | ExpNodeFlag.BuiltinType_Real ) ==
0;
return new ExpBinaryOperatorNode( ExpCompile.KeyWord.MUL )
return new ExpBinaryOperatorNode( source )
{
_returnType = isFloat ? null : null,
_returnType = isFloat ? ExpCompile.ExpReal : ExpCompile.ExpInt,
_nodeFlag = ExpNodeFlag.Operator_Binary | ExpNodeFlag.BuiltinType_Number,
CodeConvertTemplate = "{0} * {1}",
};
}
public static ExpBinaryOperatorNode Sub( ExpNode left, ExpNode right )
public static ExpBinaryOperatorNode Sub(
Token source,
ExpNode left,
ExpNode right )
{
if( !left.IsNumber && !right.IsNumber ) {
throw new ExpInvalidTypeException(
"Number", left.EqualityTypeConvert.Name );
}
bool isFloat =
( left._nodeFlag | ExpNodeFlag.BuiltinType_Float ) == 0 || ( right._nodeFlag | ExpNodeFlag.BuiltinType_Float ) ==
( left._nodeFlag | ExpNodeFlag.BuiltinType_Real ) == 0 || ( right._nodeFlag | ExpNodeFlag.BuiltinType_Real ) ==
0;
return new ExpBinaryOperatorNode( ExpCompile.KeyWord.SUB )
return new ExpBinaryOperatorNode( source )
{
_returnType = isFloat ? : null,
_returnType = isFloat ? ExpCompile.ExpReal : ExpCompile.ExpInt,
_nodeFlag = ExpNodeFlag.Operator_Binary | ExpNodeFlag.BuiltinType_Number,
CodeConvertTemplate = "{0} - {1}",
};
@@ -42,9 +42,9 @@ namespace StgSharp.Script.Express
private ExpInstantiableElement _element;
public ExpElementInstanceNode(
string varName,
Token source,
ExpInstantiableElement element )
: base( varName )
: base( source )
{
_element = element;
}
@@ -34,13 +34,12 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Script.Express.ExpAstNode
namespace StgSharp.Script.Express
{
public class ExpElementMemberNameNode : ExpNode
{
public ExpElementMemberNameNode( string memberName )
: base( memberName ) { }
public ExpElementMemberNameNode( Token source ) : base( source ) { }
public override ExpNode Left => Empty;
@@ -44,9 +44,10 @@ namespace StgSharp.Script.Express
private ExpNode _parameter;
public ExpFunctionCallingNode(
Token source,
ExpFunctionSource function,
ExpNode firstParameter )
: base( $"{function.Name}_call" )
: base( source )
{
_function = function;
_parameter = firstParameter;
@@ -44,12 +44,12 @@ namespace StgSharp.Script.Express
private ExpNode _trueOpBegin ;
public ExpIfNode(
string name,
Token source,
ExpSchema context,
ExpNode rule,
ExpNode rootOperationIfTrue,
ExpNode rootOperationIfFalse )
: base( name )
: base( source )
{
_rule = rule;
_trueOpBegin = rootOperationIfFalse;
@@ -44,9 +44,10 @@ namespace StgSharp.Script.Express
private ExpNode _indexExpression;
private ExpIndexOfNode(
Token source,
ExpCollectionInstanceBase instance,
ExpNode indexExpression )
: base( ExpCompile.PoolString( "[" ) )
: base( source )
{
_collection = instance;
_indexExpression = indexExpression;
@@ -60,6 +61,7 @@ namespace StgSharp.Script.Express
public override IExpElementSource EqualityTypeConvert => _collection.EqualityTypeConvert;
public static ExpIndexOfNode Create(
Token source,
ExpCollectionInstanceBase instance,
ExpNode indexExpression )
{
@@ -67,7 +69,7 @@ namespace StgSharp.Script.Express
throw new ExpInvalidTypeException(
"Number", indexExpression.EqualityTypeConvert.Name );
}
return new ExpIndexOfNode( instance, indexExpression );
return new ExpIndexOfNode( source, instance, indexExpression );
}
}
@@ -0,0 +1,50 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpInstanceReferenceNode.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;
namespace StgSharp.Script.Express
{
public class ExpInstanceReferenceNode : ExpNode
{
public override ExpNode Left => throw new NotImplementedException();
public override ExpNode Right => throw new NotImplementedException();
public override IExpElementSource EqualityTypeConvert => throw new NotImplementedException(
);
}
}
@@ -37,71 +37,121 @@ using System.Text;
namespace StgSharp.Script.Express
{
public class ExpLiteralNode
public class ExpIntNode : ExpElementInstanceBase
{
private readonly IValueBox box;
private int _value;
public ExpIntNode( Token source, int value )
: base( source )
{
_value = value;
_nodeFlag = ExpNodeFlag.BuiltinType_Int | ExpNodeFlag.Element_Type;
}
public override IExpElementSource EqualityTypeConvert => ExpSchema.BuiltinSchema
.TryGetType(
ExpCompile.KeyWord.Int, out ExpTypeSource? type ) ?
type : throw new ExpCompileNotInitializedException();
public int Value => _value;
public override string TypeName => ExpCompile.KeyWord.Int;
}
public class ExpLiteralGenericNode<T> : ExpElementInstanceBase
public class ExpStringNode : ExpElementInstanceBase
{
private readonly ExpBuiltinType _type;
private readonly T _value;
private readonly string _value;
public ExpLiteralGenericNode( string name, T value )
: base( name )
public ExpStringNode( Token source, string value )
: base( source )
{
_value = value;
switch( value ) {
case float f:
ExpSchema.BuiltinSchema
.TryGetType(
ExpCompile.KeyWord.Real,
out ExpTypeSource? type );
_type = ( type as ExpBuiltinType )!;
_nodeFlag = ExpNodeFlag.BuiltinType_Float | ExpNodeFlag.Element_Const;
break;
case int i:
ExpSchema.BuiltinSchema
.TryGetType( ExpCompile.KeyWord.Int, out type );
_type = ( type as ExpBuiltinType )!;
_nodeFlag = ExpNodeFlag.BuiltinType_Int | ExpNodeFlag.Element_Const;
break;
case BitArray b:
ExpSchema.BuiltinSchema
.TryGetType( ExpCompile.KeyWord.Binary, out type );
_type = ( type as ExpBuiltinType )!;
_nodeFlag = ExpNodeFlag.BuiltinType_Binary | ExpNodeFlag.Element_Const;
break;
case string s:
ExpSchema.BuiltinSchema
.TryGetType( ExpCompile.KeyWord.String, out type );
_type = ( type as ExpBuiltinType )!;
_nodeFlag = ExpNodeFlag.BuiltinType_Int | ExpNodeFlag.Element_Const;
break;
case ExpLogic l:
ExpSchema.BuiltinSchema
.TryGetType( ExpCompile.KeyWord.Logic, out type );
_type = ( type as ExpBuiltinType )!;
_nodeFlag = ExpNodeFlag.BuiltinType_Int | ExpNodeFlag.Element_Const;
break;
default:
throw new ExpInvalidTypeException(
"Builtin Type", value!.GetType().Name );
}
_nodeFlag = ExpNodeFlag.BuiltinType_String | ExpNodeFlag.Element_Type;
}
public override ExpNode Left => Empty;
public override IExpElementSource EqualityTypeConvert => ExpSchema.BuiltinSchema
.TryGetType(
ExpCompile.KeyWord.String,
out ExpTypeSource? type ) ?
type : throw new ExpCompileNotInitializedException();
public override ExpNode Right => throw new NotImplementedException();
public override string TypeName => EqualityTypeConvert.Name;
public override IExpElementSource EqualityTypeConvert => _type;
public string Value => _value;
public override string TypeName => _type.Name;
}
public T value => _value;
public class ExpBoolNode : ExpElementInstanceBase
{
private readonly bool _value;
public ExpBoolNode( Token source, bool value )
: base( source )
{
_value = value;
_nodeFlag = ExpNodeFlag.BuiltinType_Boolean | ExpNodeFlag.Element_Type;
}
public bool Value => _value;
public override IExpElementSource EqualityTypeConvert => ExpSchema.BuiltinSchema
.TryGetType(
ExpCompile.KeyWord.Bool, out ExpTypeSource? type ) ?
type : throw new ExpCompileNotInitializedException();
public override string TypeName => EqualityTypeConvert.Name;
}
public class ExpRealNumberNode : ExpElementInstanceBase
{
private readonly float _value;
public ExpRealNumberNode( Token source, float value )
: base( source )
{
_value = value;
_nodeFlag = ExpNodeFlag.BuiltinType_Real | ExpNodeFlag.Element_Type;
}
public float Value => _value;
public override IExpElementSource EqualityTypeConvert => ExpSchema.BuiltinSchema
.TryGetType(
ExpCompile.KeyWord.String,
out ExpTypeSource? type ) ?
type : throw new ExpCompileNotInitializedException();
public override string TypeName => EqualityTypeConvert.Name;
}
public class ExpLogicValueNode : ExpElementInstanceBase
{
private readonly ExpLogic _value;
public ExpLogicValueNode( Token source, ExpLogic value )
: base( source )
{
_value = value;
_nodeFlag = ExpNodeFlag.BuiltinType_Logic | ExpNodeFlag.Element_Type;
}
public ExpLogic Value => _value;
public override IExpElementSource EqualityTypeConvert => ExpSchema.BuiltinSchema
.TryGetType(
ExpCompile.KeyWord.Logic,
out ExpTypeSource? type ) ?
type : throw new ExpCompileNotInitializedException();
public override string TypeName => EqualityTypeConvert.Name;
}
}
@@ -46,14 +46,13 @@ namespace StgSharp.Script.Express
private ExpNode _operationBegin;
public ExpRepeatToken(
string name,
ExpSchema context,
Token source,
ExpNode operationBegin,
ExpElementInstanceBase variable,
ExpElementInstanceBase begin,
ExpElementInstanceBase end,
ExpElementInstanceBase increment )
: base( name )
: base( source )
{
if( variable.NodeFlag != begin.NodeFlag || begin.NodeFlag !=
end.NodeFlag || ( increment != null && end.NodeFlag !=
@@ -62,13 +61,19 @@ namespace StgSharp.Script.Express
}
if( increment == null ) {
switch( ( ExpNodeFlag )variable.NodeFlag | ExpNodeFlag.BuiltinType_Any ) {
case ExpNodeFlag.BuiltinType_Float:
case ExpNodeFlag.BuiltinType_Real:
increment = new ExpLiteralGenericNode<float>(
$"{name}_defaultIncrement", 1.0f );
new Token(
ExpCompile.PoolString( "1.0f" ), source.Line,
-1, TokenFlag.Number ),
1.0f );
break;
case ExpNodeFlag.BuiltinType_Int:
increment = new ExpLiteralGenericNode<int>(
$"{name}_defaultIncrement", 1 );
new Token(
ExpCompile.PoolString( "1" ), source.Line, -1,
TokenFlag.Number ),
1 );
break;
default:
throw new ExpInvalidTypeException(
@@ -42,12 +42,11 @@ namespace StgSharp.Script.Express
private ExpNode _target;
public ExpSwitchNode(
string name,
ExpSchema context,
Token source,
ExpNode target,
ExpNode defaultCase,
params (ExpNode label, ExpNode op)[] cases )
: base( name )
: base( source )
{
_target = target;
_default = defaultCase;
@@ -41,13 +41,13 @@ namespace StgSharp.Script.Express
private ExpNode _operand;
private ExpUnaryOperatorNode( string name ) : base( name ) { }
private ExpUnaryOperatorNode( Token source ) : base( source ) { }
public ExpUnaryOperatorNode(
string name,
Token source,
ExpNode operand,
ExpSchema context )
: base( name )
: base( source )
{
_operand = operand;
}
@@ -58,44 +58,50 @@ namespace StgSharp.Script.Express
public override IExpElementSource EqualityTypeConvert => _operand.EqualityTypeConvert;
public static ExpUnaryOperatorNode UnaryMinus( ExpNode operand )
public static ExpUnaryOperatorNode UnaryMinus(
Token source,
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 )
( operand._nodeFlag | ExpNodeFlag.BuiltinType_Real ) == 0;
return new ExpUnaryOperatorNode( source )
{
_nodeFlag = ExpNodeFlag.Operator_Unary | ExpNodeFlag.BuiltinType_Number,
CodeConvertTemplate = "- {0}",
};
}
public static ExpUnaryOperatorNode UnaryNot( ExpNode operand )
public static ExpUnaryOperatorNode UnaryNot(
Token source,
ExpNode operand )
{
if( ( operand._nodeFlag | ( ExpNodeFlag.BuiltinType_Logic | ExpNodeFlag.BuiltinType_Logic ) ) ==
0 ) {
throw new ExpInvalidTypeException(
"Bool or Logic", operand.EqualityTypeConvert.Name );
}
return new ExpUnaryOperatorNode( ExpCompile.KeyWord.NOT )
return new ExpUnaryOperatorNode( source )
{
_nodeFlag = ExpNodeFlag.Operator_Unary | ExpNodeFlag.BuiltinType_Logic,
CodeConvertTemplate = "! {0}",
};
}
public static ExpUnaryOperatorNode UnaryPlus( ExpNode operand )
public static ExpUnaryOperatorNode UnaryPlus(
Token source,
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 )
( operand._nodeFlag | ExpNodeFlag.BuiltinType_Real ) == 0;
return new ExpUnaryOperatorNode( source )
{
_nodeFlag = ExpNodeFlag.Operator_Unary | ExpNodeFlag.BuiltinType_Number,
CodeConvertTemplate = "+ {0}",
+19 -7
View File
@@ -41,11 +41,11 @@ namespace StgSharp.Script.Express
{
protected internal ExpNodeFlag _nodeFlag;
protected internal string _name;
protected internal Token _source;
protected ExpNode( string name )
protected ExpNode( Token source )
{
_name = name;
_source = source;
Next = this;
Previous = this;
}
@@ -87,6 +87,10 @@ namespace StgSharp.Script.Express
get;
}
public int Line => _source.Line;
public int Column => _source.Column;
public long NodeFlag => ( long )_nodeFlag;
public string CodeConvertTemplate
@@ -114,9 +118,15 @@ namespace StgSharp.Script.Express
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static ExpNode NonOperation( string name )
public static ExpNode NonOperation( Token source )
{
return new ExpEmptyNode( name );
return new ExpEmptyNode( source.Line, source.Column );
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static ExpNode NonOperation( int line, int column )
{
return new ExpEmptyNode( line, column );
}
public void PrependNode( ExpNode previousNode )
@@ -146,9 +156,11 @@ namespace StgSharp.Script.Express
internal static readonly ExpEmptyNode _only = new();
public ExpEmptyNode() : base( string.Empty ) { }
public ExpEmptyNode() : base( Token.Empty ) { }
public ExpEmptyNode( string name ) : base( name ) { }
public ExpEmptyNode( int line, int column )
: base(
new Token( string.Empty, line, column, TokenFlag.None ) ) { }
public override ExpInstantiableElement EqualityTypeConvert => ExpInstantiableElement.Void;