using Canon.Core.Abstractions;
using Canon.Core.Enums;
namespace Canon.Core.SyntaxNodes;
public class FactorGeneratorEventArgs : EventArgs
{
public required Factor Factor { get; init; }
}
public class MultiplyGeneratorEventArgs : EventArgs
{
public required Term Left { get; init; }
public required MultiplyOperator Operator { get; init; }
public required Factor Right { get; init; }
}
public class Term : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Term;
///
/// 是否为条件判断语句
///
public bool IsCondition { get; set; }
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseEvent();
}
///
/// 直接赋值产生式的事件
///
public event EventHandler? OnFactorGenerator;
///
/// 乘法产生式的事件
///
public event EventHandler? OnMultiplyGenerator;
public static Term Create(List children)
{
return new Term { Children = children };
}
private void RaiseEvent()
{
if (Children.Count == 1)
{
OnFactorGenerator?.Invoke(this, new FactorGeneratorEventArgs { Factor = Children[0].Convert() });
}
else
{
OnMultiplyGenerator?.Invoke(this,
new MultiplyGeneratorEventArgs
{
Left = Children[0].Convert(),
Operator = Children[1].Convert(),
Right = Children[2].Convert()
});
}
OnFactorGenerator = null;
OnMultiplyGenerator = null;
}
}