2024-04-26 10:18:49 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
2024-04-21 22:24:35 +08:00
|
|
|
|
using Canon.Core.Enums;
|
|
|
|
|
using Canon.Core.LexicalParser;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Core.SyntaxNodes;
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class AssignGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required Variable Variable { get; init; }
|
|
|
|
|
|
|
|
|
|
public required Expression Expression { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class IfGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required Expression Condition { get; init; }
|
|
|
|
|
|
|
|
|
|
public required Statement Sentence { get; init; }
|
|
|
|
|
|
|
|
|
|
public required ElsePart ElseSentence { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class ForGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required IdentifierSemanticToken Iterator { get; init; }
|
|
|
|
|
|
|
|
|
|
public required Expression Begin { get; init; }
|
|
|
|
|
|
|
|
|
|
public required Expression End { get; init; }
|
|
|
|
|
|
|
|
|
|
public required Statement Sentence { get; init; }
|
2024-05-10 13:52:11 +08:00
|
|
|
|
|
|
|
|
|
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; }
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public class Statement : NonTerminatedSyntaxNode
|
|
|
|
|
{
|
|
|
|
|
public override NonTerminatorType Type => NonTerminatorType.Statement;
|
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
public override void PreVisit(SyntaxNodeVisitor visitor)
|
|
|
|
|
{
|
|
|
|
|
visitor.PreVisit(this);
|
|
|
|
|
RaiseEvent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void PostVisit(SyntaxNodeVisitor visitor)
|
|
|
|
|
{
|
|
|
|
|
visitor.PostVisit(this);
|
|
|
|
|
RaiseEvent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用赋值产生式的事件
|
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<AssignGeneratorEventArgs>? OnAssignGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用If产生式的事件
|
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<IfGeneratorEventArgs>? OnIfGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用For产生式的事件
|
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<ForGeneratorEventArgs>? OnForGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
2024-05-10 13:52:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用While产生式的事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<WhileGeneratorEventArgs>? OnWhileGenerator;
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public static Statement Create(List<SyntaxNodeBase> children)
|
|
|
|
|
{
|
|
|
|
|
return new Statement { Children = children };
|
|
|
|
|
}
|
2024-04-21 22:24:35 +08:00
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
private void RaiseEvent()
|
|
|
|
|
{
|
2024-04-28 22:02:29 +08:00
|
|
|
|
if (Children.Count == 3)
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
OnAssignGenerator?.Invoke(this,
|
|
|
|
|
new AssignGeneratorEventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
Variable = Children[0].Convert<Variable>(), Expression = Children[2].Convert<Expression>()
|
2024-04-26 10:18:49 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2024-05-10 13:52:11 +08:00
|
|
|
|
else if (Children.Count == 4)
|
|
|
|
|
{
|
|
|
|
|
OnWhileGenerator?.Invoke(this,
|
|
|
|
|
new WhileGeneratorEventArgs
|
|
|
|
|
{
|
|
|
|
|
Condition = Children[1].Convert<Expression>(),
|
|
|
|
|
Do = Children[2].Convert<TerminatedSyntaxNode>(),
|
|
|
|
|
Sentence = Children[3].Convert<Statement>(),
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-26 10:18:49 +08:00
|
|
|
|
else if (Children.Count == 5)
|
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
OnIfGenerator?.Invoke(this,
|
|
|
|
|
new IfGeneratorEventArgs
|
|
|
|
|
{
|
|
|
|
|
Condition = Children[1].Convert<Expression>(),
|
|
|
|
|
Sentence = Children[3].Convert<Statement>(),
|
|
|
|
|
ElseSentence = Children[4].Convert<ElsePart>()
|
|
|
|
|
});
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
else if (Children.Count == 8)
|
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
OnForGenerator?.Invoke(this,
|
|
|
|
|
new ForGeneratorEventArgs
|
|
|
|
|
{
|
|
|
|
|
Iterator = Children[1].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>(),
|
|
|
|
|
Begin = Children[3].Convert<Expression>(),
|
|
|
|
|
End = Children[5].Convert<Expression>(),
|
2024-05-10 13:52:11 +08:00
|
|
|
|
Do = Children[6].Convert<TerminatedSyntaxNode>(),
|
2024-05-01 21:06:27 +08:00
|
|
|
|
Sentence = Children[7].Convert<Statement>()
|
|
|
|
|
});
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|