2024-04-26 10:18:49 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
2024-04-21 22:24:35 +08:00
|
|
|
|
using Canon.Core.Enums;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Core.SyntaxNodes;
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class SimpleExpressionGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required SimpleExpression SimpleExpression { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class RelationGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required SimpleExpression Left { get; init; }
|
|
|
|
|
|
|
|
|
|
public required RelationOperator Operator { get; init; }
|
|
|
|
|
|
|
|
|
|
public required SimpleExpression Right { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public class Expression : NonTerminatedSyntaxNode
|
|
|
|
|
{
|
|
|
|
|
public override NonTerminatorType Type => NonTerminatorType.Expression;
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 11:56:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否为FOR语句中的起始语句
|
|
|
|
|
/// </summary>
|
2024-04-30 14:43:14 +08:00
|
|
|
|
public bool IsForConditionBegin { get; set; }
|
2024-05-04 11:56:06 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否为FOR语句中的结束语句
|
|
|
|
|
/// </summary>
|
2024-04-30 14:43:14 +08:00
|
|
|
|
public bool IsForConditionEnd { get; set; }
|
|
|
|
|
|
2024-05-04 11:56:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否为IF语句中的条件语句
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsIfCondition { get; set; }
|
|
|
|
|
|
2024-05-10 16:45:21 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否为条件判断语句
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsCondition { get; set; }
|
|
|
|
|
|
2024-05-10 13:52:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否为WHILE语句中的条件语句
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsWhileCondition { get; set; }
|
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 直接赋值产生式的事件
|
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<SimpleExpressionGeneratorEventArgs>? OnSimpleExpressionGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 关系产生式的事件
|
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<RelationGeneratorEventArgs>? OnRelationGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public static Expression Create(List<SyntaxNodeBase> children)
|
|
|
|
|
{
|
|
|
|
|
return new Expression { Children = children };
|
|
|
|
|
}
|
2024-04-21 22:24:35 +08:00
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
private void RaiseEvent()
|
|
|
|
|
{
|
|
|
|
|
if (Children.Count == 1)
|
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
OnSimpleExpressionGenerator?.Invoke(this, new SimpleExpressionGeneratorEventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
SimpleExpression = Children[0].Convert<SimpleExpression>()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
OnRelationGenerator?.Invoke(this, new RelationGeneratorEventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
Left = Children[0].Convert<SimpleExpression>(),
|
|
|
|
|
Operator = Children[1].Convert<RelationOperator>(),
|
|
|
|
|
Right = Children[2].Convert<SimpleExpression>()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnSimpleExpressionGenerator = null;
|
|
|
|
|
OnRelationGenerator = null;
|
|
|
|
|
}
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|