using Canon.Core.Abstractions;
using Canon.Core.Enums;
using Canon.Core.LexicalParser;
namespace Canon.Core.SyntaxNodes;
public class AssignGeneratorEventArgs : EventArgs
{
public required Variable Variable { get; init; }
public required Expression Expression { get; init; }
}
public class IfGeneratorEventArgs : EventArgs
{
public required Expression Condition { get; init; }
public required Statement Sentence { get; init; }
public required ElsePart ElseSentence { get; init; }
}
public class ForGeneratorEventArgs : EventArgs
{
public required IdentifierSemanticToken Iterator { get; init; }
public required Expression Begin { get; init; }
public required Expression End { get; init; }
public required Statement Sentence { get; init; }
public required TerminatedSyntaxNode Do { get; init; }
}
public class WhileGeneratorEventArgs : EventArgs
{
public required Expression Condition { get; init; }
public required Statement Sentence { get; init; }
public required TerminatedSyntaxNode Do { get; init; }
}
public class Statement : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Statement;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseEvent();
}
///
/// 使用赋值产生式的事件
///
public event EventHandler? OnAssignGenerator;
///
/// 使用If产生式的事件
///
public event EventHandler? OnIfGenerator;
///
/// 使用For产生式的事件
///
public event EventHandler? OnForGenerator;
///
/// 使用While产生式的事件
///
public event EventHandler? OnWhileGenerator;
public static Statement Create(List children)
{
return new Statement { Children = children };
}
private void RaiseEvent()
{
if (Children.Count == 3)
{
OnAssignGenerator?.Invoke(this,
new AssignGeneratorEventArgs
{
Variable = Children[0].Convert(), Expression = Children[2].Convert()
});
}
else if (Children.Count == 4)
{
OnWhileGenerator?.Invoke(this,
new WhileGeneratorEventArgs
{
Condition = Children[1].Convert(),
Do = Children[2].Convert(),
Sentence = Children[3].Convert(),
});
}
else if (Children.Count == 5)
{
OnIfGenerator?.Invoke(this,
new IfGeneratorEventArgs
{
Condition = Children[1].Convert(),
Sentence = Children[3].Convert(),
ElseSentence = Children[4].Convert()
});
}
else if (Children.Count == 8)
{
OnForGenerator?.Invoke(this,
new ForGeneratorEventArgs
{
Iterator = Children[1].Convert().Token.Convert(),
Begin = Children[3].Convert(),
End = Children[5].Convert(),
Do = Children[6].Convert(),
Sentence = Children[7].Convert()
});
}
}
}