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
@@ -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(