Daily update on 20250408-232053 by Nitload Youmu. Additional Message : REPEAT expression mainly complete, should add support to ELEMENT related operation to common expression like instance creating and type converting

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2025-04-08 23:20:53 +08:00
parent d270b6b1f8
commit 7b7b5ee475
27 changed files with 116 additions and 217 deletions
@@ -40,29 +40,29 @@ namespace StgSharp.Script.Express
public class ExpInstanceReferenceNode : ExpNode
{
private ExpElementInstance _instance;
internal ExpInstanceReferenceNode( Token source, ExpElementInstance instance )
: base( source )
{
_instance = instance;
_nodeFlag = ( _instance._nodeFlag | ( ExpNodeFlag.Name_Any | ExpNodeFlag.Collection_Any ) );
OriginObject = instance;
_nodeFlag = ( OriginObject._nodeFlag | ( ExpNodeFlag.Name_Any | ExpNodeFlag.Collection_Any ) );
CodeConvertTemplate = source.Value;
}
internal ExpInstanceReferenceNode( Token source, ExpInstanceReferenceNode reference )
: base( source )
{
_instance = reference._instance;
_nodeFlag = ( _instance._nodeFlag | ( ExpNodeFlag.Name_Any | ExpNodeFlag.Collection_Any ) );
OriginObject = reference.OriginObject;
_nodeFlag = ( OriginObject._nodeFlag | ( ExpNodeFlag.Name_Any | ExpNodeFlag.Collection_Any ) );
CodeConvertTemplate = source.Value;
}
public ExpElementInstance OriginObject { get ; private set; }
public override ExpNode Left => Empty;
public override ExpNode Right => Empty;
public override IExpElementSource EqualityTypeConvert => _instance.EqualityTypeConvert;
public override IExpElementSource EqualityTypeConvert => OriginObject.EqualityTypeConvert;
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static ExpInstanceReferenceNode MakeReferenceFrom(
@@ -59,7 +59,7 @@ namespace StgSharp.Script.Express
increment.NodeFlag ) ) {
throw new InvalidCastException();
}
if( ExpNode.IsNullOrEmpty( increment ) )
if( IsNullOrEmpty( increment ) )
{
switch( ( ExpNodeFlag )variable.NodeFlag | ExpNodeFlag.BuiltinType_Any )
{
@@ -84,7 +84,7 @@ namespace StgSharp.Script.Express
_variable = variable;
_begin = begin;
_end = end;
_increment = increment;
_increment = increment!;
_operationBegin = operationBegin;
_untilRule = untilRule;
_whileRule = whileRule;
@@ -106,52 +106,54 @@ namespace StgSharp.Script.Express
/// WHILE and UNTIL regulation.
/// </summary>
/// <param name="t">
/// Position of the loop
/// Position of the loop.
/// </param>
/// <param name="repeatVariable">
///
/// The variable recording increment by step.
/// </param>
/// <param name="begin">
///
/// Beginning value of the loop.
/// </param>
/// <param name="end">
///
/// When <see href="repeatVariable" /> reach the value, the loop ends.
/// </param>
/// <param name="repeatBody">
///
/// Expressions to operate multiple times.
/// </param>
/// <returns>
///
/// A <see cref="ExpRepeatNode" /> representing the repeat expression.
/// </returns>
public static ExpRepeatNode Create(
Token t,
ExpElementInstance repeatVariable,
ExpInstanceReferenceNode repeatVariable,
ExpNode begin,
ExpNode end,
ExpNode increment,
ExpNode whileRule,
ExpNode untilRule,
ExpNode body )
{
ExpNode untilRule = ExpBoolNode.False( null );
ExpNode whileRule = ExpBoolNode.True( null );
untilRule = ExpBoolNode.False( null );
whileRule = ExpBoolNode.True( null );
ExpRepeatNode ret = new ExpRepeatNode(
t, repeatVariable, begin, end, increment, body, whileRule, untilRule )
t, repeatVariable.OriginObject, begin, end, increment, body, whileRule, untilRule )
{
/*
* 0 increment variable type
* 1: variable name
* 2: beginning value
* 3: ending value
* 4: increment
* 5: body
* 6: while
* 7: until
* 0: variable name
* 1: beginning value
* 2: ending value
* 3: increment
* 4: body
* 5: while
* 6: until
*/
CodeConvertTemplate = $$"""
for({{0}} {{1}} = {{2}}; {{1}} <= {{3}}; {{1}} += {{4}})
for( {0} = {1}; {0} <= {2}; {0} += {3})
{
if(!( {{6}} )) break;
{{5}}
if( {{7}} ) break;
if(!( {5} )) break;
{4}
if( {6} ) break;
}
"""
};
@@ -174,21 +176,21 @@ namespace StgSharp.Script.Express
t, repeatVariable, begin, end, increment, body, whileRule, untilRule )
{
/*
* 0 increment variable type
* 1: variable name
* 2: beginning value
* 3: ending value
* 4: increment
* 5: body
* 6: while
* 7: until
* 0: variable name
* 1: beginning value
* 2: ending value
* 3: increment
* 4: body
* 5: while
* 6: until
*/
CodeConvertTemplate = $$"""
for({{0}} {{1}} = {{2}}; {{1}} <= {{3}}; {{1}} += {{4}})
for({{repeatVariable.TypeName}} {0} = {1}; {0} <= {2}; {0} += {3})
{
if(!( {{6}} )) break;
{{5}}
if( {{7}} ) break;
if(!( {5} )) break;
{4}
if( {6} ) break;
}
"""
};
@@ -32,6 +32,7 @@ using CommunityToolkit.HighPerformance.Buffers;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
@@ -97,7 +98,7 @@ namespace StgSharp.Script.Express
return new ExpNodeNextEnumerator( this );
}
public static bool IsNullOrEmpty( ExpNode node )
public static bool IsNullOrEmpty( [AllowNull]ExpNode node )
{
return node == ExpEmptyNode._only || node == null;
}
+13 -6
View File
@@ -39,7 +39,14 @@ using System.Text;
namespace StgSharp.Script.Express
{
public class ExpInvalidCharException : Exception
public class ExpException : Exception
{
public ExpException( string message ) : base( message ) { }
}
public class ExpInvalidCharException : ExpException
{
public ExpInvalidCharException( char c ) : base( $"An unknown char {c} occurred." ) { }
@@ -49,7 +56,7 @@ namespace StgSharp.Script.Express
}
public class ExpStringNotCloseException : Exception
public class ExpStringNotCloseException : ExpException
{
public ExpStringNotCloseException( int linenum )
@@ -57,7 +64,7 @@ namespace StgSharp.Script.Express
}
public class ExpInvalidSyntaxException : Exception
public class ExpInvalidSyntaxException : ExpException
{
public ExpInvalidSyntaxException( string message ) : base( message ) { }
@@ -76,7 +83,7 @@ namespace StgSharp.Script.Express
}
public class ExpInvalidElementDeclareEndingExceptions : Exception
public class ExpInvalidElementDeclareEndingExceptions : ExpException
{
public ExpInvalidElementDeclareEndingExceptions( string declaredName, string endedName )
@@ -85,7 +92,7 @@ namespace StgSharp.Script.Express
}
public class ExpInvalidElementNameException : Exception
public class ExpInvalidElementNameException : ExpException
{
public ExpInvalidElementNameException( string elementName, ExpSchema schema )
@@ -148,7 +155,7 @@ namespace StgSharp.Script.Express
}
public class ExpCompileException : Exception
public class ExpCompileException : ExpException
{
public ExpCompileException( Token pos, string message )
+1 -1
View File
@@ -356,7 +356,7 @@ namespace StgSharp.Script.Express
private static partial Regex GetNextWordTokenPattern();
[GeneratedRegex(
@"(?<token>-?\s*?[0-9]+(?:\.[0-9]*)?E?(?:[+-]?[0-9]+))(?<end>\s*?)(?<rest>\S|$)",
@"(?<token>-?\s*?[0-9]+(?:\.[0-9]*)?E?(?:[+-]?[0-9]+)?)(?<end>\s*?)(?<rest>\S|$)",
RegexOptions.Singleline )]
private static partial Regex GetNumberLiteralPattern();
@@ -56,6 +56,7 @@ namespace StgSharp.Script.Express
{
state = new RepeatLoopState( t );
_cache.CurrentDepthMark.StateCache = state;
state.CurrentState = t.Value;
return true;
}
switch( state.CurrentState )
@@ -86,9 +87,19 @@ namespace StgSharp.Script.Express
ExpNode root = _cache.PackAllStatements();
state.Body = root;
_cache.DecreaseDepth();
root = ExpRepeatNode.Create(
state.Position, state.IncrementVariable, state.Begin, state.End,
state.IncrementVariable, state.Body, state.UntilExpression, state.WhileExpression );
if( ExpTypeSource.IsNullOrVoid( state.IncrementVariableType ) )
{
root = ExpRepeatNode.Create(
state.Position, state.IncrementVariable, state.Begin, state.End,
state.IncrementVariable, state.Body, state.UntilExpression,
state.WhileExpression );
} else
{
root = ExpRepeatNode.Create(
state.Position, state.IncrementVariable.OriginObject, state.Begin, state.End,
state.IncrementVariable, state.Body, state.UntilExpression,
state.WhileExpression );
}
_cache.StatementsInDepth.Push( root );
_tempVariableInLoops.Remove( state.IncrementVariable.CodeConvertTemplate );
return true;
@@ -194,18 +205,29 @@ namespace StgSharp.Script.Express
return true;
case ExpKeyword.Assignment:
state.CurrentState = ExpKeyword.Assignment;
_tempVariableInLoops.Add(
state.IncrementVariable.CodeConvertTemplate, state.IncrementVariable );
return true;
default:
if( ExpTypeSource.IsNullOrVoid( state.IncrementVariableType ) )
{
throw new ExpInvalidTypeException( "Numerical", t.Value );
if( ( _local.TryGetMember( t.Value,
out ExpNode? node ) && node is ExpElementInstance instance ) || _tempVariableInLoops.TryGetValue(
t.Value, out instance ) )
{
state.IncrementVariable = ExpInstanceReferenceNode.MakeReferenceFrom(
t, instance );
return true;
} else
{
throw new ExpInvalidSyntaxException(
$"Variable {t.Value} is not declared or initialized." );
}
} else
{
state.IncrementVariable = state.IncrementVariableType
.CreateInstanceNode( t );
ExpElementInstance variable = state.IncrementVariableType
.CreateInstanceNode( t );
_tempVariableInLoops.Add( t.Value, variable );
state.IncrementVariable = ExpInstanceReferenceNode.MakeReferenceFrom(
t, variable );
return true;
}
}
@@ -240,7 +262,7 @@ namespace StgSharp.Script.Express
WhileExpression = ExpNode.Empty;
}
public ExpElementInstance IncrementVariable { get; set; }
public ExpInstanceReferenceNode IncrementVariable { get; set; }
public ExpNode StepIncrement { get; set; }
@@ -235,8 +235,10 @@ namespace StgSharp.Script.Express
{
_cache.PushOperand( ExpInstanceReferenceNode.MakeReferenceFrom( t, instance ) );
return true;
} else if( _tempVariableInLoops.TryGetValue( t.Value, out instance ) ) {
} else if( _tempVariableInLoops.TryGetValue( t.Value, out instance! ) )
{
_cache.PushOperand( ExpInstanceReferenceNode.MakeReferenceFrom( t, instance ) );
return true;
}
return false;
}
@@ -87,6 +87,9 @@ namespace StgSharp.Script.Express
case ExpCompileStateCode.CaseBranch:
AppendToken_CaseOf( expToken );
break;
case ExpCompileStateCode.RepeatLoop:
AppendToken_RepeatLoop( expToken );
break;
default:
AppendToken_common( expToken );
break;
@@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("StgSharp.Script")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+1a1db3554b10ed4a707671695d5100eee621af75")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d270b6b1f8220e14d01610bd55f11c0b4908af39")]
[assembly: System.Reflection.AssemblyProductAttribute("StgSharp.Script")]
[assembly: System.Reflection.AssemblyTitleAttribute("StgSharp.Script")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/1a1db3554b10ed4a707671695d5100eee621af75/*"}}
{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/d270b6b1f8220e14d01610bd55f11c0b4908af39/*"}}