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