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 OnTermGeneratorEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public required Term Term { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OnAddGeneratorEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public required SimpleExpression Left { get; init; }
|
|
|
|
|
|
|
|
|
|
public required AddOperator Operator { get; init; }
|
|
|
|
|
|
|
|
|
|
public required Term Right { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public class SimpleExpression : NonTerminatedSyntaxNode
|
|
|
|
|
{
|
|
|
|
|
public override NonTerminatorType Type => NonTerminatorType.SimpleExpression;
|
|
|
|
|
|
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<OnTermGeneratorEventArgs>? OnTermGenerator;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 加法产生式的事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<OnAddGeneratorEventArgs>? OnAddGenerator;
|
|
|
|
|
|
|
|
|
|
private PascalType? _simpleExpressionType;
|
|
|
|
|
|
|
|
|
|
public PascalType SimpleExpressionType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_simpleExpressionType is null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _simpleExpressionType;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_simpleExpressionType = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public static SimpleExpression Create(List<SyntaxNodeBase> children)
|
|
|
|
|
{
|
|
|
|
|
return new SimpleExpression { 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)
|
|
|
|
|
{
|
|
|
|
|
OnTermGenerator?.Invoke(this, new OnTermGeneratorEventArgs
|
|
|
|
|
{
|
|
|
|
|
Term = Children[0].Convert<Term>()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OnAddGenerator?.Invoke(this, new OnAddGeneratorEventArgs
|
|
|
|
|
{
|
|
|
|
|
Left = Children[0].Convert<SimpleExpression>(),
|
|
|
|
|
Operator = Children[1].Convert<AddOperator>(),
|
|
|
|
|
Right = Children[2].Convert<Term>()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnTermGenerator = null;
|
|
|
|
|
OnAddGenerator = null;
|
|
|
|
|
}
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|