daily update on 20250220 by nitload youmu, building AST generator, correct type naming

This commit is contained in:
Nitload
2025-02-21 08:11:26 +08:00
parent ccef0de6f6
commit 83bde12d22
98 changed files with 4182 additions and 1855 deletions
@@ -0,0 +1,90 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="DefaultSchema.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.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static StgSharp.Script.Express.ExpCompile;
namespace StgSharp.Script.Express
{
internal partial class ExpSchema_Nitload : ExpSchema
{
private static readonly ExpSchema_Nitload _onlyInstance = new ExpSchema_Nitload(
);
private ExpSchema_Nitload()
: base()
{
Name = "Nitload";
}
public static ExpSchema Only
{
get => _onlyInstance;
}
private void Init()
{
IEnumerable<KeyValuePair<string, ExpTypeSource>> builtinTypeSource = [
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Int ),
new ExpBuiltinType( KeyWord.Int, typeof( int ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Real ),
new ExpBuiltinType( KeyWord.Real, typeof( float ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Bool ),
new ExpBuiltinType( KeyWord.Bool, typeof( bool ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Logic ),
new ExpBuiltinType( KeyWord.Logic, typeof( ExpLogic ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.String ),
new ExpBuiltinType( KeyWord.String, typeof( string ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Binary ),
new ExpBuiltinType( KeyWord.Binary, typeof( BitArray ) ) ),
];
TypeDict = new Dictionary<string, ExpTypeSource>(
builtinTypeSource, _multiplexer ).ToFrozenDictionary();
}
}
}
@@ -33,6 +33,7 @@ using CommunityToolkit.HighPerformance.Buffers;
using StgSharp.Collections;
using System;
using System.Collections;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -44,10 +45,11 @@ namespace StgSharp.Script.Express
public static partial class ExpCompile
{
private static string[] _operatorsWithLetter;
private static ConcurrentStringHashMultiplexer _multiplexer;
private static FrozenDictionary<string, ExpElementInstanceBase> _builtinConst;
private static FrozenDictionary<string, ExpTypeSource> _builtinType;
private static FrozenDictionary<string, ExpOperandToken> _operators;
private static FrozenDictionary<string, ExpBinaryOperatorNode> _operators;
private static StringPool _compileStringCache;
public static FrozenDictionary<string, ExpElementInstanceBase> BuiltinConst => _builtinConst;
@@ -82,30 +84,38 @@ namespace StgSharp.Script.Express
_compileStringCache = new StringPool();
_multiplexer = new ConcurrentStringHashMultiplexer();
_builtinType = new Dictionary<string, ExpTypeSource>( _multiplexer ) {
{ PoolString( KeyWord.Int ), new ExpBuiltinType(
KeyWord.Int,
typeof( int ) ) }, { PoolString( KeyWord.Real ), new ExpBuiltinType(
KeyWord.Real,
typeof( float ) ) }, { PoolString(
KeyWord.Bool ), new ExpBuiltinType(
KeyWord.Bool,
typeof( bool ) ) }, { PoolString(
KeyWord.Binary ), new ExpBuiltinType(
KeyWord.Binary,
typeof( bool ) ) }, { PoolString(
KeyWord.String ), new ExpBuiltinType(
KeyWord.String,
typeof( string ) ) }, { PoolString(
KeyWord.Logic ), new ExpBuiltinType(
KeyWord.Logic,
typeof( ExpLogic ) ) }
}.ToFrozenDictionary();
IEnumerable<KeyValuePair<string, ExpTypeSource>> builtinTypeSource = [
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Int ),
new ExpBuiltinType( KeyWord.Int, typeof( int ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Real ),
new ExpBuiltinType( KeyWord.Real, typeof( float ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Bool ),
new ExpBuiltinType( KeyWord.Bool, typeof( bool ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Logic ),
new ExpBuiltinType( KeyWord.Logic, typeof( ExpLogic ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.String ),
new ExpBuiltinType( KeyWord.String, typeof( string ) ) ),
new KeyValuePair<string, ExpTypeSource>(
PoolString( KeyWord.Binary ),
new ExpBuiltinType( KeyWord.Binary, typeof( BitArray ) ) ),
_operators = new Dictionary<string, ExpOperandToken>( _multiplexer )
];
_builtinType = new Dictionary<string, ExpTypeSource>(
builtinTypeSource, _multiplexer ) .ToFrozenDictionary();
_operators = new Dictionary<string, ExpBinaryOperatorNode>(
_multiplexer )
{
{ PoolString( KeyWord.ADD ), new ExpOperandToken( KeyWord.ADD ) },
{ PoolString( KeyWord.SUB ), new ExpOperandToken( KeyWord.SUB ) }
{ PoolString( KeyWord.ADD ), new ExpBinaryOperatorNode(
KeyWord.ADD ) },
{ PoolString( KeyWord.SUB ), new ExpBinaryOperatorNode(
KeyWord.SUB ) }
}.ToFrozenDictionary();
_builtinConst = new Dictionary<string, ExpElementInstanceBase>(
@@ -116,7 +126,28 @@ namespace StgSharp.Script.Express
PoolString( "E" ), MathF.E ) },
}.ToFrozenDictionary();
PoolKeywords();
InitPrecedence();
_operatorsWithLetter = [
KeyWord.ADD,
KeyWord.SUB,
KeyWord.MUL,
KeyWord.DIV,
KeyWord.AND,
KeyWord.OR,
KeyWord.XOR,
KeyWord.ANDOR,
KeyWord.NOT,
];
}
public static bool LetterIsOperator( string str )
{
if( char.IsLetter( str[ 0 ] ) ) {
return Array.IndexOf( _operatorsWithLetter, str ) != -1;
}
return true;
}
public static string PoolString( string str )
@@ -124,6 +155,11 @@ namespace StgSharp.Script.Express
return _compileStringCache.GetOrAdd( str );
}
public static string PoolString( ReadOnlySpan<char> str )
{
return _compileStringCache.GetOrAdd( str );
}
public static void SkipComment( IScriptSourceProvider source ) { }
public static bool TryGetBuiltinConst(
@@ -40,42 +40,67 @@ namespace StgSharp.Script.Express
public static partial class ExpCompile
{
private static FrozenDictionary<string, int> _operatorPrecedence;
public static bool TryGetOperatorPrecedence(
string operatorName,
out int precedence )
{
return _operatorPrecedence.TryGetValue(
operatorName, out precedence );
}
public static bool TryGetSuperTypeOperatorPrecedence(
string operatorName,
out int precedence )
{
return _superTypeOperatorPrecedent.TryGetValue(
operatorName, out precedence );
}
private static void InitPrecedence()
{
KeyValuePair<string, int>[] source = [
new KeyValuePair<string, int>( "[]", 1 ),
new KeyValuePair<string, int>( ".", 1 ),
new KeyValuePair<string, int>( "\\", 1 ),
new KeyValuePair<string, int>( "unary+", 2 ),
new KeyValuePair<string, int>( "unary-", 2 ),
new KeyValuePair<string, int>( "NOT", 2 ),
new KeyValuePair<string, int>( "**", 3 ),
new KeyValuePair<string, int>( "*", 4 ),
new KeyValuePair<string, int>( "/", 4 ),
new KeyValuePair<string, int>( "AND", 4 ),
new KeyValuePair<string, int>( "MOD", 4 ),
new KeyValuePair<string, int>( "DIV", 4 ),
new KeyValuePair<string, int>( "||", 4 ),
new KeyValuePair<string, int>( "+", 5 ),
new KeyValuePair<string, int>( "-", 5 ),
new KeyValuePair<string, int>( "OR", 5 ),
new KeyValuePair<string, int>( "XOR", 5 ),
new KeyValuePair<string, int>( "=", 6 ),
new KeyValuePair<string, int>( "<>", 6 ),
new KeyValuePair<string, int>( "<=", 6 ),
new KeyValuePair<string, int>( ">=", 6 ),
new KeyValuePair<string, int>( ">", 6 ),
new KeyValuePair<string, int>( "<", 6 ),
new KeyValuePair<string, int>( ":=:", 6 ),
new KeyValuePair<string, int>( ":<>:", 6 ),
new KeyValuePair<string, int>( "IN", 6 ),
new KeyValuePair<string, int>( "LIKE", 6 ),
new KeyValuePair<string, int>( PoolString( "[" ), 1 ),
new KeyValuePair<string, int>( PoolString( "." ), 1 ),
new KeyValuePair<string, int>( PoolString( "\\" ), 1 ),
new KeyValuePair<string, int>( PoolString( "unary+" ), 2 ),
new KeyValuePair<string, int>( PoolString( "unary-" ), 2 ),
new KeyValuePair<string, int>( PoolString( "NOT" ), 2 ),
new KeyValuePair<string, int>( PoolString( "**" ), 3 ),
new KeyValuePair<string, int>( PoolString( "*" ), 4 ),
new KeyValuePair<string, int>( PoolString( "/" ), 4 ),
new KeyValuePair<string, int>( PoolString( "AND" ), 4 ),
new KeyValuePair<string, int>( PoolString( "MOD" ), 4 ),
new KeyValuePair<string, int>( PoolString( "DIV" ), 4 ),
new KeyValuePair<string, int>( PoolString( "||" ), 4 ),
new KeyValuePair<string, int>( PoolString( "+" ), 5 ),
new KeyValuePair<string, int>( PoolString( "-" ), 5 ),
new KeyValuePair<string, int>( PoolString( "OR" ), 5 ),
new KeyValuePair<string, int>( PoolString( "XOR" ), 5 ),
new KeyValuePair<string, int>( PoolString( "=" ), 6 ),
new KeyValuePair<string, int>( PoolString( "<>" ), 6 ),
new KeyValuePair<string, int>( PoolString( "<=" ), 6 ),
new KeyValuePair<string, int>( PoolString( ">=" ), 6 ),
new KeyValuePair<string, int>( PoolString( ">" ), 6 ),
new KeyValuePair<string, int>( PoolString( "<" ), 6 ),
new KeyValuePair<string, int>( PoolString( ":=:" ), 6 ),
new KeyValuePair<string, int>( PoolString( ":<>:" ), 6 ),
new KeyValuePair<string, int>( PoolString( "IN" ), 6 ),
new KeyValuePair<string, int>( PoolString( "LIKE" ), 6 ),
];
_operatorPrecedence = new Dictionary<string, int>(
source, _multiplexer ).ToFrozenDictionary();
source = [
new KeyValuePair<string, int>( "ONEOF", 1 ),
new KeyValuePair<string, int>( "AND", 2 ),
new KeyValuePair<string, int>( "ANDOR", 3 ),
];
_superTypeOperatorPrecedent = new Dictionary<string, int>(
source, _multiplexer ).ToFrozenDictionary();
}
#pragma warning disable CS8618
private static FrozenDictionary<string, int> _superTypeOperatorPrecedent;
private static FrozenDictionary<string, int> _operatorPrecedence;
#pragma warning restore CS8618
}
}
@@ -31,6 +31,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@@ -39,6 +40,23 @@ namespace StgSharp.Script.Express
public static partial class ExpCompile
{
private static void PoolKeywords()
{
FieldInfo[] fields = typeof( KeyWord ).GetFields(
BindingFlags.Public );
ParallelLoopResult result = Parallel.ForEach(
fields, static( field ) => {
if( field.FieldType == typeof( string ) ) {
string? str =
field.GetValue( null ) as string;
if( str != null ) {
_compileStringCache.GetOrAdd( str );
}
}
} );
while( !result.IsCompleted ) { }
}
public static class KeyWord
{
@@ -56,6 +74,7 @@ namespace StgSharp.Script.Express
SUB = "SUB",
MUL = "MUL",
DIV = "DIV",
NOT = "NOT",
Assignment = ":=",
Equal = "=",
Mod = "MOD",
@@ -66,9 +85,9 @@ namespace StgSharp.Script.Express
Insert = "INSERT",
Remove = "REMOVE",
ANDOR = "ANDOR",
GetIndex = "[]",
UnaryAdd = "unary+",
UnarySub = "unary-",
GetIndex = "[",
UnaryAdd = "+",
UnarySub = "-",
Exp = "**";
}