rokojori_action_library/Runtime/Text/Parsing/Expressions/PrecedenceLevel.cs

68 lines
1.3 KiB
C#

using System.Collections.Generic;
using System.Linq;
namespace Rokojori;
public class PrecedenceLevel
{
public bool fromLeft = true;
public List<ExpressionOperator> operators = [];
public bool Process( ASTNode parent )
{
SearchExpressionForOperators( parent );
if ( _operatorIndex == -1 )
{
return false;
}
var currentOperator = operators[ _operatorIndex ];
var changed = currentOperator.ProcessStartNode( parent, _expressionIndex );
if ( changed )
{
//console.log( "Processed", className( currentOperator ), currentOperator.tokenType, currentOperator.tokenValue );
}
return changed;
}
public bool HasHigherPrecedence( int i, int e )
{
return fromLeft ? i < e : i > e;
}
int _expressionIndex = -1;
int _operatorIndex = -1;
public void SearchExpressionForOperators( ASTNode parent )
{
_expressionIndex = fromLeft ? parent.children.Count : -1;
_operatorIndex = -1;
for ( int i = 0; i < operators.Count; i++ )
{
var index = operators[ i ].FindStartNodeIndex( parent );
if ( index == -1 )
{
continue;
}
if ( ! HasHigherPrecedence( index, this._expressionIndex ) )
{
continue;
}
_expressionIndex = index;
_operatorIndex = i;
}
}
}