2024-04-26 10:18:49 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
|
|
|
|
using Canon.Core.CodeGenerators;
|
2024-04-21 22:24:35 +08:00
|
|
|
|
using Canon.Core.Enums;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
using Canon.Core.SemanticParser;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Core.SyntaxNodes;
|
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
public class OnSimpleExpressionGeneratorEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public required SimpleExpression SimpleExpression { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OnRelationGeneratorEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 直接赋值产生式的事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<OnSimpleExpressionGeneratorEventArgs>? OnSimpleExpressionGenerator;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 关系产生式的事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<OnRelationGeneratorEventArgs>? OnRelationGenerator;
|
|
|
|
|
|
|
|
|
|
private PascalType? _expressionType;
|
|
|
|
|
|
|
|
|
|
public PascalType ExprssionType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_expressionType is null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _expressionType;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_expressionType = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
OnSimpleExpressionGenerator?.Invoke(this, new OnSimpleExpressionGeneratorEventArgs
|
|
|
|
|
{
|
|
|
|
|
SimpleExpression = Children[0].Convert<SimpleExpression>()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OnRelationGenerator?.Invoke(this, new OnRelationGeneratorEventArgs
|
|
|
|
|
{
|
|
|
|
|
Left = Children[0].Convert<SimpleExpression>(),
|
|
|
|
|
Operator = Children[1].Convert<RelationOperator>(),
|
|
|
|
|
Right = Children[2].Convert<SimpleExpression>()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnSimpleExpressionGenerator = null;
|
|
|
|
|
OnRelationGenerator = null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 22:24:35 +08:00
|
|
|
|
public override void GenerateCCode(CCodeBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
foreach (var child in Children)
|
|
|
|
|
{
|
|
|
|
|
child.GenerateCCode(builder);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|