daily update on 20250210 by nitload youmu

This commit is contained in:
Nitload
2025-02-10 12:48:02 +08:00
parent ae94c265ae
commit c23b640cae
70 changed files with 2135 additions and 805 deletions
@@ -0,0 +1,80 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpIfToken.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 StgSharp.Script.Express;
using System;
using System.Collections.Generic;
using System.Text;
namespace StgSharp.Script.Express
{
public sealed class ExpIfToken : ExpToken
{
private ExpToken _rule;
private List<ExpToken> _falseOp;
private List<ExpToken> _trueOp;
public ExpIfToken(
string name,
ExpSchema context,
ExpToken rule,
IEnumerable<ExpToken> operationIfTrue,
IEnumerable<ExpToken> operationIfFalse )
: base( name, context )
{
_rule = rule;
if( operationIfFalse == null ) {
_trueOp = new List<ExpToken>();
} else {
_trueOp = new List<ExpToken>( operationIfFalse );
}
if( operationIfFalse == null ) {
_falseOp = new List<ExpToken>();
} else {
_falseOp = new List<ExpToken>( operationIfFalse );
}
}
public void AddFalseOperation( ExpToken expressionRoot )
{
AssertSchemaInclude( expressionRoot );
_falseOp.Add( expressionRoot );
}
public void AddTrueOperation( ExpToken expressionRoot )
{
AssertSchemaInclude( expressionRoot );
_trueOp.Add( expressionRoot );
}
}
}
@@ -0,0 +1,97 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpLiteralToken.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;
using System.Collections.Generic;
using System.Text;
namespace StgSharp.Script.Express
{
public class ExpLiteralToken<T> : ExpElementInstanceBase
{
private readonly ExpBuiltinType _type;
private readonly T _value;
public ExpLiteralToken( string name, ExpSchema context, T value )
: base( name, context )
{
_value = value;
switch( value ) {
case float f:
ExpCompile.TryGetBuiltinType(
ExpCompile.KeyWord.Real, out ExpTypeSource? type );
_type = ( type as ExpBuiltinType )!;
_tokenType = ExpTokenFlag.BuiltinType_Float | ExpTokenFlag.Element_Const;
break;
case int i:
ExpCompile.TryGetBuiltinType(
ExpCompile.KeyWord.Int, out type );
_type = ( type as ExpBuiltinType )!;
_tokenType = ExpTokenFlag.BuiltinType_Int | ExpTokenFlag.Element_Const;
break;
case BitArray b:
ExpCompile.TryGetBuiltinType(
ExpCompile.KeyWord.Binary, out type );
_type = ( type as ExpBuiltinType )!;
_tokenType = ExpTokenFlag.BuiltinType_Binary | ExpTokenFlag.Element_Const;
break;
case string s:
ExpCompile.TryGetBuiltinType(
ExpCompile.KeyWord.String, out type );
_type = ( type as ExpBuiltinType )!;
_tokenType = ExpTokenFlag.BuiltinType_Int | ExpTokenFlag.Element_Const;
break;
case ExpLogic l:
ExpCompile.TryGetBuiltinType(
ExpCompile.KeyWord.Logic, out type );
_type = ( type as ExpBuiltinType )!;
_tokenType = ExpTokenFlag.BuiltinType_Int | ExpTokenFlag.Element_Const;
break;
default:
throw new ExpInvalidTypeException(
"Builtin Type", value!.GetType().Name );
}
}
public override string TypeName => _type.Name;
public T value => _value;
public override bool TryGetNextLevel(
out ICollection<IExpressionToken> nextLevel )
{
nextLevel = null!;
return false;
}
}
}
@@ -0,0 +1,98 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpRepeat.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.Runtime.Serialization;
using System.Text;
namespace StgSharp.Script.Express
{
public class ExpRepeat : ExpToken
{
public ExpRepeat( string name, ExpSchema context )
: base( name, context ) { }
}
public class ExpRepeatRule : ExpToken
{
private ExpElementInstanceBase _variable, _begin, _end, _increment;
private List<ExpToken> _token = new List<ExpToken>();
public ExpRepeatRule(
string name,
ExpSchema context,
IEnumerable<ExpToken> operation,
ExpElementInstanceBase variable,
ExpElementInstanceBase begin,
ExpElementInstanceBase end,
ExpElementInstanceBase increment )
: base( name, context )
{
if( variable.TokenTypeCode != begin.TokenTypeCode || begin.TokenTypeCode !=
end.TokenTypeCode || ( increment != null && end.TokenTypeCode !=
increment.TokenTypeCode ) ) {
throw new InvalidCastException();
}
if( increment == null ) {
switch( ( ExpTokenFlag )variable.TokenTypeCode | ExpTokenFlag.BuiltinType_Any ) {
case ExpTokenFlag.BuiltinType_Float:
increment = new ExpLiteralToken<float>(
$"{name}_defaultincrement", context, 1.0f );
break;
case ExpTokenFlag.BuiltinType_Int:
increment = new ExpLiteralToken<int>(
$"{name}_defaultincrement", context, 1 );
break;
default:
throw new ExpInvalidTypeException(
"Int or Real", "otherwise" );
}
}
_begin = begin;
_variable = variable;
_end = end;
_increment = increment;
_tokenType = ExpTokenFlag.Branch_Repeat;
}
public override bool TryGetNextLevel(
out ICollection<IExpressionToken> nextLevel )
{
nextLevel = ( _token.ToList() as ICollection<IExpressionToken> )!;
return true;
}
}
}
@@ -0,0 +1,111 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpSwitchToken.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.Text;
namespace StgSharp.Script.Express
{
public class ExpSwitchToken : ExpToken
{
private Dictionary<ExpToken, List<ExpToken>> _cases;
private ExpToken _target;
private List<ExpToken> _default;
public ExpSwitchToken(
string name,
ExpSchema context,
ExpToken target,
IEnumerable<ExpToken> @default,
params (ExpToken label, IEnumerable<ExpToken> op)[] cases )
: base( name, context )
{
_target = target;
if( @default == null ) {
_default = new List<ExpToken>();
} else {
_default = new List<ExpToken>( @default );
}
_cases = new Dictionary<ExpToken, List<ExpToken>>();
foreach( (ExpToken label, IEnumerable<ExpToken> op) item in cases ) {
if( item.op == null ) {
_cases.Add( item.label, new List<ExpToken>() );
} else {
_cases.Add( item.label, new List<ExpToken>( item.op ) );
}
}
}
public List<ExpToken> this[ ExpToken index ]
{
get
{
if( _cases.TryGetValue( index, out List<ExpToken>? ret ) ) {
return ret;
}
return _default;
}
}
/// <summary>
/// Adds an expression to certain <see langword="case" />. The added expression will be run
/// at last.
/// </summary>
/// <param name="label"> Label of case to add operation </param>
/// <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 AddOperationToCase(
ExpToken label,
ExpToken expressionRoot )
{
AssertSchemaInclude( expressionRoot );
if( _cases.TryGetValue( label, out List<ExpToken>? op ) ) {
op.Add( expressionRoot );
} else {
throw new ExpCaseNotFoundException( this, label );
}
}
public void AddOperationToDefault( ExpToken expressionRoot )
{
if( expressionRoot != null ) {
_default.Add( expressionRoot );
}
}
public bool ContainsCase( ExpToken token )
{
return _cases.ContainsKey( token );
}
}
}
@@ -0,0 +1,127 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpToken.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
{
[Flags]
public enum ExpTokenFlag : long
{
None = 0,
Name_Element = 1,
Name_Param = 2,
Name_Block = 4,
Name_Keyword = 8,
Name_Any = Name_Block | Name_Element | Name_Keyword | Name_Param,
Collection_Set = 16,
Collection_Bag = 32,
Collection_Array = 64,
Collection_List = 128,
Collection_Any = Collection_Set | Collection_Bag | Collection_Array | Collection_List,
Element_Const = 256,
Element_Entity = 512,
Element_type = 1024,
Element_Rule = 2048,
Element_Function = 4096,
Element_Any = Element_Entity | Element_Const | Element_Function | Element_Rule | Element_type,
BuiltinType_Int = 8192,
BuiltinType_Float = 16384,
BuiltinType_String = 32768,
BuiltinType_Boolean = 65536,
BuiltinType_Binary = 0x2000,
BuiltinType_Logic = 0x4000,
BuiltinType_Any = BuiltinType_Int | BuiltinType_Float | BuiltinType_String | BuiltinType_Boolean | BuiltinType_Binary | BuiltinType_Logic,
Branch_If = 0x8000,
Branch_Switch = 0x1_0000,
Branch_Repeat = 0x2_0000,
}
public ref struct ExpTokenSource
{
public ReadOnlySpan<char> Value;
public readonly int Line;
public readonly int Column;
public ExpTokenSource(ReadOnlySpan<char> chars, int lineNumber, int columnNumber)
{
Value = chars; Line = lineNumber; Column = columnNumber;
}
}
public abstract class ExpToken : IExpressionToken
{
protected internal ExpToken _previousToken;
protected internal ExpTokenFlag _tokenType;
protected internal List<ExpToken> _nextLevelTokens
= new List<ExpToken>();
protected internal string _name;
protected ExpToken( string name, ExpSchema context )
{
_name = name;
Context = context;
}
public ExpSchema Context
{
get;
private set;
}
public long TokenTypeCode => ( long )_tokenType;
public string Name => _name;
public virtual bool TryGetNextLevel(
out ICollection<IExpressionToken> nextLevel )
{
nextLevel = ( _nextLevelTokens as ICollection<IExpressionToken> )!;
return true;
}
protected void AssertSchemaInclude( ExpToken expressionRoot )
{
if( expressionRoot.Context != Context && Context.HasSchemaInclude(
expressionRoot.Context ) ) {
throw new ExpInvalidSchemaIncludeException(
Context, expressionRoot.Context );
}
}
}
}