daily update on 20240202 by nitload youmu, keep on writing xpreeess compiler

This commit is contained in:
Nitload
2025-02-02 23:59:05 +08:00
parent e687473132
commit 4ee675669f
483 changed files with 86314 additions and 2712 deletions
@@ -0,0 +1,54 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="BuiltinType.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 sealed class ExpBuiltinType : ExpTypeSource
{
private Type _marshalType;
internal ExpBuiltinType( string name, Type origin )
: base( name, null! )
{
_marshalType = origin;
}
public override void Analyse()
{
return;
}
}
}
@@ -0,0 +1,177 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpCollection.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.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StgSharp.Script.Express
{
public abstract class ExpCollectionBase : IExpElementSource, IEnumerable<ExpElementInstanceBase>
{
protected internal string _accurateType;
protected ExpCollectionBase(
string accurateType )
{
_accurateType = accurateType;
}
public string MemberType
{
get => _accurateType;
}
public abstract int Count { get; }
public ExpElementType ElementType => ExpElementType.Collection;
IScriptSourceProvider IExpElementSource.SourceProvider => null!;
public abstract string Name { get; }
public void Analyse()
{
return;
}
public abstract IEnumerator<ExpElementInstanceBase> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class ExpArray : ExpCollectionBase
{
private ExpElementInstanceBase[] _collection;
public ExpArray( string accurateType )
: base( accurateType ) { }
public ExpElementInstanceBase this[ int index ]
{
get { return _collection[ index ]; }
set
{
if( value.TypeName != MemberType ) {
throw new ExpInvalidCollectionMemberTypeException(
value, this );
}
_collection[ index ] = value;
}
}
public override int Count => _collection.Length;
public override string Name => ExpCompile.AddOrGetString("ARRAY");
public override IEnumerator<ExpElementInstanceBase> GetEnumerator()
{
return ( ( IEnumerable<ExpElementInstanceBase> )_collection )
.GetEnumerator( );
}
}
public class ExpBag : ExpCollectionBase
{
private ConcurrentBag<ExpElementInstanceBase> _collection = new ConcurrentBag<ExpElementInstanceBase>(
);
public ExpBag( string accurateType )
: base( accurateType ) { }
public override int Count => _collection.Count;
public override string Name => ExpCompile.AddOrGetString("BAG");
public override IEnumerator<ExpElementInstanceBase> GetEnumerator()
{
return _collection.GetEnumerator();
}
}
public sealed class ExpList : ExpCollectionBase
{
public ExpList(string accurateType ):base(accurateType)
{
}
private List<ExpElementInstanceBase> _collection = new List<ExpElementInstanceBase>();
public override int Count => _collection.Count;
public override string Name => ExpCompile.AddOrGetString("LIST");
public override IEnumerator<ExpElementInstanceBase> GetEnumerator()
{
return _collection.GetEnumerator();
}
}
public class ExpSet : ExpCollectionBase
{
private HashSet<ExpElementInstanceBase> _collection = new();
public ExpSet( string accurateType )
: base( accurateType ) { }
public void Add( ExpElementInstanceBase token )
{
if( token.TypeName != MemberType ) {
throw new ExpInvalidCollectionMemberTypeException( token,
this );
}
_collection.Add( token );
}
public override int Count => _collection.Count;
public override string Name => ExpCompile.AddOrGetString("SET");
public override sealed IEnumerator<ExpElementInstanceBase> GetEnumerator()
{
return ( ( IEnumerable<ExpElementInstanceBase> )_collection )
.GetEnumerator( );
}
}
}
@@ -0,0 +1,124 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpCollectionInstance.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 abstract class ExpCollectionInstanceBase : ExpToken
{
protected internal (int min, int max) _range;
protected internal ExpElementType _memberType;
protected ExpCollectionInstanceBase( string name, ExpSchema context )
: base( name, context ) { }
public abstract ExpCollectionBase CollectionValue
{
get;
}
public ExpElementType MemberType
{
get => _memberType;
}
public int MinCapacity => _range.min;
public int MaxCapacity => _range.max;
}
public sealed class ExpSetInstance : ExpCollectionInstanceBase
{
private ExpSet _collection;
public ExpSetInstance( string name, ExpSet set, ExpSchema context )
: base( name, context )
{
_collection = set;
}
public override ExpCollectionBase CollectionValue => _collection;
}
public sealed class ExpBagInstance : ExpCollectionInstanceBase
{
private ExpBag _collection;
public ExpBagInstance( string name, ExpBag bag, ExpSchema context )
: base( name, context )
{
_collection = bag;
}
public override ExpCollectionBase CollectionValue => _collection;
}
public sealed class ExpListInstance : ExpCollectionInstanceBase
{
private ExpList _collection;
public ExpListInstance( string name, ExpList list, ExpSchema context )
: base( name, context )
{
_collection = list;
}
public override ExpCollectionBase CollectionValue => _collection;
}
public sealed class ExpArrayInstance : ExpCollectionInstanceBase
{
private ExpArray _collection;
public ExpArrayInstance(
string name,
ExpArray array,
ExpSchema context )
: base( name, context )
{
_collection = array;
}
public override ExpCollectionBase CollectionValue => _collection;
}
}
@@ -0,0 +1,52 @@
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> _trueOp;
private List<ExpToken> _falseOp;
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 AddTrueOperation(ExpToken expressionRoot)
{
AssertSchemaInclude(expressionRoot);
_trueOp.Add(expressionRoot);
}
public void AddFalseOperation(ExpToken expressionRoot)
{
AssertSchemaInclude(expressionRoot);
_falseOp.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 Logic 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 T value => _value;
public override string TypeName => _type.Name;
public override bool TryGetNextLevel(
out ICollection<IExpressionToken> nextLevel )
{
nextLevel = null!;
return false;
}
}
}
@@ -0,0 +1,97 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// 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 List<ExpToken> _token = new List<ExpToken>();
private ExpElementInstanceBase _variable, _begin, _end, _increment;
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,86 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace StgSharp.Script.Express
{
public class ExpSwitchToken : ExpToken
{
private ExpToken _target;
private List<ExpToken> _default;
private Dictionary<ExpToken, List<ExpToken>> _cases;
public List<ExpToken> this[ExpToken index]
{
get
{
if (_cases.TryGetValue(index, out var ret))
{
return ret;
}
return _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 (var 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 bool ContainsCase(ExpToken token)
{
return _cases.ContainsKey(token);
}
/// <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 var op))
{
op.Add(expressionRoot);
}
else
{
throw new ExpCaseNotFoundException(this, label);
}
}
public void AddOperationToDefault(ExpToken expressionRoot)
{
if (expressionRoot != null)
{
_default.Add(expressionRoot);
}
}
}
}