47 lines
945 B
C#
47 lines
945 B
C#
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
|
||
|
|
namespace Rokojori;
|
||
|
|
|
||
|
|
public class ExpressionParser:ParserPhase
|
||
|
|
{
|
||
|
|
public List<PrecedenceLevel> levels = [];
|
||
|
|
|
||
|
|
public override void Process( Parser parser )
|
||
|
|
{
|
||
|
|
ProcessExpression( parser.root );
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ProcessExpression( ASTNode expressionParent )
|
||
|
|
{
|
||
|
|
var parser = expressionParent.parser;
|
||
|
|
|
||
|
|
for ( int i = 0; i < levels.Count; i++ )
|
||
|
|
{
|
||
|
|
var changed = levels[ i ].Process( expressionParent );
|
||
|
|
|
||
|
|
if ( parser.hasErrors )
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( changed )
|
||
|
|
{
|
||
|
|
i = -1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
protected PrecedenceLevel AddPrecedenceLevel( params ExpressionOperator[] operators )
|
||
|
|
{
|
||
|
|
var level = new PrecedenceLevel();
|
||
|
|
level.operators.AddRange( operators );
|
||
|
|
|
||
|
|
var isFromRight = operators.Find( op => ! op.IsFromLeft() ) != null;
|
||
|
|
level.fromLeft = ! isFromRight;
|
||
|
|
|
||
|
|
this.levels.Add( level );
|
||
|
|
return level;
|
||
|
|
}
|
||
|
|
}
|