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
|
|
|
|
using Canon.Core.LexicalParser;
|
|
|
|
|
|
|
|
|
|
namespace Canon.Core.SyntaxNodes;
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class ProcedureGeneratorEventArgs : EventArgs;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class FunctionGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required BasicType ReturnType { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public class SubprogramHead : NonTerminatedSyntaxNode
|
|
|
|
|
{
|
|
|
|
|
public override NonTerminatorType Type => NonTerminatorType.SubprogramHead;
|
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 过程定义还是函数定义
|
|
|
|
|
/// </summary>
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public bool IsProcedure { get; private init; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 子程序的名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IdentifierSemanticToken SubprogramName =>
|
2024-04-26 10:18:49 +08:00
|
|
|
|
Children[1].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>();
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
public FormalParameter Parameters => Children[2].Convert<FormalParameter>();
|
|
|
|
|
|
|
|
|
|
public override void PreVisit(SyntaxNodeVisitor visitor)
|
|
|
|
|
{
|
|
|
|
|
visitor.PreVisit(this);
|
|
|
|
|
RaiseEvent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void PostVisit(SyntaxNodeVisitor visitor)
|
|
|
|
|
{
|
|
|
|
|
visitor.PostVisit(this);
|
|
|
|
|
RaiseEvent();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<ProcedureGeneratorEventArgs>? OnProcedureGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<FunctionGeneratorEventArgs>? OnFunctionGenerator;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
|
|
|
|
public static SubprogramHead Create(List<SyntaxNodeBase> children)
|
|
|
|
|
{
|
|
|
|
|
bool isProcedure;
|
|
|
|
|
|
|
|
|
|
TerminatedSyntaxNode node = children[0].Convert<TerminatedSyntaxNode>();
|
|
|
|
|
KeywordSemanticToken token = (KeywordSemanticToken)node.Token;
|
|
|
|
|
|
|
|
|
|
if (token.KeywordType == KeywordType.Procedure)
|
|
|
|
|
{
|
|
|
|
|
isProcedure = true;
|
|
|
|
|
}
|
|
|
|
|
else if (token.KeywordType == KeywordType.Function)
|
|
|
|
|
{
|
|
|
|
|
isProcedure = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new SubprogramHead { Children = children, IsProcedure = isProcedure };
|
|
|
|
|
}
|
2024-04-21 22:24:35 +08:00
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
private void RaiseEvent()
|
2024-04-21 22:24:35 +08:00
|
|
|
|
{
|
|
|
|
|
if (IsProcedure)
|
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
OnProcedureGenerator?.Invoke(this, new ProcedureGeneratorEventArgs());
|
2024-04-21 22:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-04-26 10:18:49 +08:00
|
|
|
|
OnFunctionGenerator?.Invoke(this,
|
2024-05-01 21:06:27 +08:00
|
|
|
|
new FunctionGeneratorEventArgs { ReturnType = Children[4].Convert<BasicType>() });
|
2024-04-21 22:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
OnProcedureGenerator = null;
|
|
|
|
|
OnFunctionGenerator = null;
|
2024-04-21 22:24:35 +08:00
|
|
|
|
}
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|