feat: CanonSharp Benchmark. (#4)

Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/4
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
This commit is contained in:
2024-08-19 14:37:34 +08:00
committed by 任昌骏
parent cf19f8197e
commit 89ce313b77
165 changed files with 11578 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class AddOperator : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.AddOperator;
public SemanticToken OperatorToken => Children[0].Convert<TerminatedSyntaxNode>().Token;
public static AddOperator Create(List<SyntaxNodeBase> children)
{
return new AddOperator { Children = children };
}
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
}

View File

@@ -0,0 +1,24 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class BasicType : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.BasicType;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static BasicType Create(List<SyntaxNodeBase> children)
{
return new BasicType { Children = children };
}
}

View File

@@ -0,0 +1,29 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class CompoundStatement : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.CompoundStatement;
/// <summary>
/// 是否为主函数部分
/// </summary>
public bool IsMain { get; set; }
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static CompoundStatement Create(List<SyntaxNodeBase> children)
{
return new CompoundStatement { Children = children };
}
}

View File

@@ -0,0 +1,66 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ConstDeclaration : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ConstDeclaration;
/// <summary>
/// 是否递归的声明下一个ConstDeclaration
/// </summary>
public bool IsRecursive { get; private init; }
/// <summary>
/// 获得声明的常量
/// </summary>
public (IdentifierSemanticToken, ConstValue) ConstValue => GetConstValue();
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static ConstDeclaration Create(List<SyntaxNodeBase> children)
{
bool isRecursive;
if (children.Count == 3)
{
isRecursive = false;
}
else if (children.Count == 5)
{
isRecursive = true;
}
else
{
throw new InvalidOperationException();
}
return new ConstDeclaration { Children = children, IsRecursive = isRecursive };
}
private static IdentifierSemanticToken ConvertToIdentifierSemanticToken(SyntaxNodeBase node)
{
return (IdentifierSemanticToken)node.Convert<TerminatedSyntaxNode>().Token;
}
private (IdentifierSemanticToken, ConstValue) GetConstValue()
{
if (IsRecursive)
{
return (ConvertToIdentifierSemanticToken(Children[2]), Children[4].Convert<ConstValue>());
}
else
{
return (ConvertToIdentifierSemanticToken(Children[0]), Children[2].Convert<ConstValue>());
}
}
}

View File

@@ -0,0 +1,24 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ConstDeclarations : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ConstDeclarations;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static ConstDeclarations Create(List<SyntaxNodeBase> children)
{
return new ConstDeclarations { Children = children };
}
}

View File

@@ -0,0 +1,106 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
/// <summary>
/// 使用数值产生式事件的事件参数
/// </summary>
public class NumberConstValueEventArgs : EventArgs
{
/// <summary>
/// 是否含有负号
/// </summary>
public bool IsNegative { get; init; }
/// <summary>
/// 数值记号
/// </summary>
public required NumberSemanticToken Token { get; init; }
}
/// <summary>
/// 使用字符产生式事件的事件参数
/// </summary>
public class CharacterConstValueEventArgs : EventArgs
{
/// <summary>
/// 字符记号
/// </summary>
public required CharacterSemanticToken Token { get; init; }
}
public class ConstValue : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ConstValue;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseGeneratorEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseGeneratorEvent();
}
public string ValueString { get; set; } = string.Empty;
/// <summary>
/// 使用数值产生式的事件
/// </summary>
public event EventHandler<NumberConstValueEventArgs>? OnNumberGenerator;
/// <summary>
/// 使用字符产生式的事件
/// </summary>
public event EventHandler<CharacterConstValueEventArgs>? OnCharacterGenerator;
private void RaiseGeneratorEvent()
{
if (Children.Count == 2)
{
OperatorSemanticToken operatorSemanticToken = Children[0].Convert<TerminatedSyntaxNode>().Token
.Convert<OperatorSemanticToken>();
NumberSemanticToken numberSemanticToken = Children[1].Convert<TerminatedSyntaxNode>().Token
.Convert<NumberSemanticToken>();
OnNumberGenerator?.Invoke(this, new NumberConstValueEventArgs
{
Token = numberSemanticToken,
IsNegative = operatorSemanticToken.OperatorType == OperatorType.Minus
});
return;
}
SemanticToken token = Children[0].Convert<TerminatedSyntaxNode>().Token;
if (token.TokenType == SemanticTokenType.Number)
{
OnNumberGenerator?.Invoke(this,
new NumberConstValueEventArgs
{
Token = token.Convert<NumberSemanticToken>()
});
}
else
{
OnCharacterGenerator?.Invoke(this, new CharacterConstValueEventArgs
{
Token = token.Convert<CharacterSemanticToken>()
});
}
OnNumberGenerator = null;
OnCharacterGenerator = null;
}
public static ConstValue Create(List<SyntaxNodeBase> children)
{
return new ConstValue { Children = children };
}
}

View File

@@ -0,0 +1,24 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ElsePart : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ElsePart;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static ElsePart Create(List<SyntaxNodeBase> children)
{
return new ElsePart { Children = children };
}
}

View File

@@ -0,0 +1,98 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class SimpleExpressionGeneratorEventArgs : EventArgs
{
public required SimpleExpression SimpleExpression { get; init; }
}
public class RelationGeneratorEventArgs : EventArgs
{
public required SimpleExpression Left { get; init; }
public required RelationOperator Operator { get; init; }
public required SimpleExpression Right { get; init; }
}
public class Expression : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Expression;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseEvent();
}
/// <summary>
/// 是否为FOR语句中的起始语句
/// </summary>
public bool IsForConditionBegin { get; set; }
/// <summary>
/// 是否为FOR语句中的结束语句
/// </summary>
public bool IsForConditionEnd { get; set; }
/// <summary>
/// 是否为IF语句中的条件语句
/// </summary>
public bool IsIfCondition { get; set; }
/// <summary>
/// 是否为条件判断语句
/// </summary>
public bool IsCondition { get; set; }
/// <summary>
/// 是否为WHILE语句中的条件语句
/// </summary>
public bool IsWhileCondition { get; set; }
/// <summary>
/// 直接赋值产生式的事件
/// </summary>
public event EventHandler<SimpleExpressionGeneratorEventArgs>? OnSimpleExpressionGenerator;
/// <summary>
/// 关系产生式的事件
/// </summary>
public event EventHandler<RelationGeneratorEventArgs>? OnRelationGenerator;
public static Expression Create(List<SyntaxNodeBase> children)
{
return new Expression { Children = children };
}
private void RaiseEvent()
{
if (Children.Count == 1)
{
OnSimpleExpressionGenerator?.Invoke(this, new SimpleExpressionGeneratorEventArgs
{
SimpleExpression = Children[0].Convert<SimpleExpression>()
});
}
else
{
OnRelationGenerator?.Invoke(this, new RelationGeneratorEventArgs
{
Left = Children[0].Convert<SimpleExpression>(),
Operator = Children[1].Convert<RelationOperator>(),
Right = Children[2].Convert<SimpleExpression>()
});
}
OnSimpleExpressionGenerator = null;
OnRelationGenerator = null;
}
}

View File

@@ -0,0 +1,75 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class OnExpressionListEventArgs : EventArgs
{
public required ExpressionList ExpressionList { get; init; }
}
public class ExpressionList : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ExpressionList;
/// <summary>
/// 子表达式列表
/// </summary>
public List<Expression> Expressions { get; } = [];
/// <summary>
/// 当前ExpressionList中的Expression定义
/// </summary>
public required Expression Expression { get; init; }
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseEvent();
}
/// <summary>
/// 使用ExpressionList产生式的时间
/// </summary>
public event EventHandler<OnExpressionListEventArgs>? OnExpressionList;
public static ExpressionList Create(List<SyntaxNodeBase> children)
{
ExpressionList result;
if (children.Count == 1)
{
result = new ExpressionList { Expression = children[0].Convert<Expression>(), Children = children };
result.Expressions.Add(children[0].Convert<Expression>());
}
else
{
result = new ExpressionList { Expression = children[2].Convert<Expression>(), Children = children };
foreach (Expression expression in children[0].Convert<ExpressionList>().Expressions)
{
result.Expressions.Add(expression);
}
result.Expressions.Add(children[2].Convert<Expression>());
}
return result;
}
private void RaiseEvent()
{
if (Children.Count == 3)
{
OnExpressionList?.Invoke(this,
new OnExpressionListEventArgs { ExpressionList = Children[0].Convert<ExpressionList>() });
}
OnExpressionList = null;
}
}

View File

@@ -0,0 +1,219 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class NumberGeneratorEventArgs : EventArgs
{
public required NumberSemanticToken Token { get; init; }
}
public class VariableGeneratorEventArgs : EventArgs
{
public required Variable Variable { get; init; }
}
public class ParethnesisGeneratorEventArgs : EventArgs
{
public required Expression Expression { get; init; }
}
public class ProcedureCallGeneratorEventArgs : EventArgs
{
public required IdentifierSemanticToken ProcedureName { get; init; }
public List<Expression> Parameters { get; } = [];
}
public class NotGeneratorEventArgs : EventArgs
{
public required Factor Factor { get; init; }
}
public class UminusGeneratorEventArgs : EventArgs
{
public required Factor Factor { get; init; }
}
public class PlusGeneratorEventArgs : EventArgs
{
public required Factor Factor { get; init; }
}
public class BooleanGeneratorEventArgs : EventArgs
{
public required bool Value { get; init; }
}
public class Factor : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Factor;
/// <summary>
/// 是否为条件判断语句
/// </summary>
public bool IsCondition { get; set; }
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<NumberGeneratorEventArgs>? OnNumberGenerator;
/// <summary>
/// 使用括号产生式的事件
/// </summary>
public event EventHandler<ParethnesisGeneratorEventArgs>? OnParethnesisGenerator;
/// <summary>
/// 使用变量产生式的事件
/// </summary>
public event EventHandler<VariableGeneratorEventArgs>? OnVariableGenerator;
/// <summary>
/// 使用否定产生式的事件
/// </summary>
public event EventHandler<NotGeneratorEventArgs>? OnNotGenerator;
/// <summary>
/// 使用负号产生式的事件
/// </summary>
public event EventHandler<UminusGeneratorEventArgs>? OnUminusGenerator;
/// <summary>
/// 使用加号产生式的事件
/// </summary>
public event EventHandler<PlusGeneratorEventArgs>? OnPlusGenerator;
/// <summary>
/// 使用布尔值产生式的事件
/// </summary>
public event EventHandler<BooleanGeneratorEventArgs>? OnBooleanGenerator;
/// <summary>
/// 过程调用产生式的事件
/// </summary>
public event EventHandler<ProcedureCallGeneratorEventArgs>? OnProcedureCallGenerator;
public static Factor Create(List<SyntaxNodeBase> children)
{
return new Factor { Children = children };
}
private void RaiseEvent()
{
if (Children.Count == 1)
{
if (Children[0].IsTerminated)
{
SemanticToken token = Children[0].Convert<TerminatedSyntaxNode>().Token;
switch (token.TokenType)
{
// factor -> num
case SemanticTokenType.Number:
OnNumberGenerator?.Invoke(this,
new NumberGeneratorEventArgs { Token = token.Convert<NumberSemanticToken>() });
break;
// factor -> true | false
case SemanticTokenType.Keyword:
KeywordSemanticToken keywordSemanticToken = token.Convert<KeywordSemanticToken>();
switch (keywordSemanticToken.KeywordType)
{
case KeywordType.True:
OnBooleanGenerator?.Invoke(this, new BooleanGeneratorEventArgs { Value = true });
break;
case KeywordType.False:
OnBooleanGenerator?.Invoke(this, new BooleanGeneratorEventArgs { Value = false });
break;
}
break;
}
}
else
{
OnVariableGenerator?.Invoke(this,
new VariableGeneratorEventArgs { Variable = Children[0].Convert<Variable>() });
}
}
else if (Children.Count == 3)
{
TerminatedSyntaxNode terminatedSyntaxNode = Children[0].Convert<TerminatedSyntaxNode>();
// factor -> ( expression )
if (terminatedSyntaxNode.Token.TokenType == SemanticTokenType.Delimiter)
{
OnParethnesisGenerator?.Invoke(this,
new ParethnesisGeneratorEventArgs { Expression = Children[1].Convert<Expression>() });
}
else
{
// factor -> id ( )
OnProcedureCallGenerator?.Invoke(this,
new ProcedureCallGeneratorEventArgs
{
ProcedureName = terminatedSyntaxNode.Token.Convert<IdentifierSemanticToken>()
});
}
}
else if (Children.Count == 4)
{
// factor -> id ( expressionList)
ProcedureCallGeneratorEventArgs eventArgs = new()
{
ProcedureName =
Children[0].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>(),
};
eventArgs.Parameters.AddRange(Children[2].Convert<ExpressionList>().Expressions);
OnProcedureCallGenerator?.Invoke(this, eventArgs);
}
else
{
SemanticToken token = Children[0].Convert<TerminatedSyntaxNode>().Token;
Factor factor = Children[1].Convert<Factor>();
if (token.TokenType == SemanticTokenType.Keyword)
{
// factor -> not factor
OnNotGenerator?.Invoke(this, new NotGeneratorEventArgs { Factor = factor });
}
else
{
OperatorSemanticToken operatorSemanticToken = token.Convert<OperatorSemanticToken>();
switch (operatorSemanticToken.OperatorType)
{
// factor -> + factor
case OperatorType.Plus:
OnPlusGenerator?.Invoke(this, new PlusGeneratorEventArgs { Factor = factor });
break;
// factor -> - factor
case OperatorType.Minus:
OnUminusGenerator?.Invoke(this, new UminusGeneratorEventArgs { Factor = factor });
break;
}
}
}
OnNumberGenerator = null;
OnVariableGenerator = null;
OnParethnesisGenerator = null;
OnProcedureCallGenerator = null;
OnNotGenerator = null;
OnUminusGenerator = null;
}
}

View File

@@ -0,0 +1,24 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class FormalParameter : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.FormalParameter;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static FormalParameter Create(List<SyntaxNodeBase> children)
{
return new FormalParameter { Children = children };
}
}

View File

@@ -0,0 +1,78 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class IdentifierGeneratorEventArgs : EventArgs
{
public required IdentifierSemanticToken IdentifierToken { get; init; }
public required IdentifierList IdentifierList { get; init; }
}
public class TypeGeneratorEventArgs : EventArgs
{
public required TypeSyntaxNode TypeSyntaxNode { get; init; }
}
public class IdentifierList : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.IdentifierList;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseEvent();
}
/// <summary>
/// 是否为参数中的引用参数
/// </summary>
public bool IsReference { get; set; }
/// <summary>
/// 是否在过程定义中使用
/// </summary>
public bool IsProcedure { get; set; }
/// <summary>
/// 是否为变量定义中使用
/// </summary>
public bool IsVariableDefinition { get; set; }
public event EventHandler<IdentifierGeneratorEventArgs>? OnIdentifierGenerator;
public event EventHandler<TypeGeneratorEventArgs>? OnTypeGenerator;
public static IdentifierList Create(List<SyntaxNodeBase> children)
{
return new IdentifierList { Children = children };
}
private void RaiseEvent()
{
if (Children.Count == 2)
{
OnTypeGenerator?.Invoke(this,
new TypeGeneratorEventArgs { TypeSyntaxNode = Children[1].Convert<TypeSyntaxNode>() });
}
else
{
OnIdentifierGenerator?.Invoke(this, new IdentifierGeneratorEventArgs
{
IdentifierToken = Children[1].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>(),
IdentifierList = Children[2].Convert<IdentifierList>()
});
}
OnTypeGenerator = null;
OnIdentifierGenerator = null;
}
}

View File

@@ -0,0 +1,68 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class IndexGeneratorEventArgs : EventArgs
{
public required ExpressionList IndexParameters { get; init; }
}
public class IdentifierVarPart : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.IdVarPart;
/// <summary>
/// 数组索引的个数
/// </summary>
public int IndexCount { get; set; }
/// <summary>
/// 索引中的表达式
/// </summary>
public List<Expression> Expressions { get; } = [];
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<IndexGeneratorEventArgs>? OnIndexGenerator;
public static IdentifierVarPart Create(List<SyntaxNodeBase> children)
{
IdentifierVarPart result = new() { Children = children };
if (children.Count == 3)
{
ExpressionList expressionList = children[1].Convert<ExpressionList>();
result.Expressions.AddRange(expressionList.Expressions);
}
return result;
}
private void RaiseEvent()
{
if (Children.Count == 3)
{
OnIndexGenerator?.Invoke(this, new IndexGeneratorEventArgs()
{
IndexParameters = Children[1].Convert<ExpressionList>()
});
}
OnIndexGenerator = null;
}
}

View File

@@ -0,0 +1,27 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class MultiplyOperator : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.MultiplyOperator;
public SemanticToken OperatorToken => Children[0].Convert<TerminatedSyntaxNode>().Token;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static MultiplyOperator Create(List<SyntaxNodeBase> children)
{
return new MultiplyOperator { Children = children };
}
}

View File

@@ -0,0 +1,42 @@
using System.Collections;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public abstract class NonTerminatedSyntaxNode : SyntaxNodeBase, IEnumerable<SyntaxNodeBase>
{
public override bool IsTerminated => false;
public abstract NonTerminatorType Type { get; }
public required List<SyntaxNodeBase> Children { get; init; }
public IEnumerator<SyntaxNodeBase> GetEnumerator()
{
yield return this;
foreach (SyntaxNodeBase child in Children)
{
if (child.IsTerminated)
{
yield return child;
}
else
{
NonTerminatedSyntaxNode nonTerminatedNode = child.Convert<NonTerminatedSyntaxNode>();
foreach (SyntaxNodeBase node in nonTerminatedNode)
{
yield return node;
}
}
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public override string ToString()
{
return Type.ToString();
}
}

View File

@@ -0,0 +1,45 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class Parameter : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Parameter;
/// <summary>
/// 是否为引用变量
/// </summary>
public bool IsVar { get; private init; }
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static Parameter Create(List<SyntaxNodeBase> children)
{
NonTerminatedSyntaxNode node = children[0].Convert<NonTerminatedSyntaxNode>();
bool isVar;
if (node.Type == NonTerminatorType.VarParameter)
{
isVar = true;
}
else if (node.Type == NonTerminatorType.ValueParameter)
{
isVar = false;
}
else
{
throw new InvalidOperationException();
}
return new Parameter { Children = children, IsVar = isVar };
}
}

View File

@@ -0,0 +1,24 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ParameterList : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ParameterList;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static ParameterList Create(List<SyntaxNodeBase> children)
{
return new ParameterList { Children = children };
}
}

View File

@@ -0,0 +1,68 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class Period : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Period;
/// <summary>
/// 所有定义的Period
/// </summary>
public List<Period> Periods { get; } = [];
/// <summary>
/// 数组的开始下标和结束下标
/// </summary>
public (NumberSemanticToken, NumberSemanticToken) Range
{
get
{
if (Children.Count == 3)
{
return (Children[0].Convert<TerminatedSyntaxNode>().Token.Convert<NumberSemanticToken>(),
Children[2].Convert<TerminatedSyntaxNode>().Token.Convert<NumberSemanticToken>());
}
else
{
return (Children[2].Convert<TerminatedSyntaxNode>().Token.Convert<NumberSemanticToken>(),
Children[4].Convert<TerminatedSyntaxNode>().Token.Convert<NumberSemanticToken>());
}
}
}
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static Period Create(List<SyntaxNodeBase> children)
{
Period result = new() { Children = children };
if (children.Count == 3)
{
result.Periods.Add(result);
}
else
{
Period child = children[0].Convert<Period>();
foreach (Period period in child.Periods)
{
result.Periods.Add(period);
}
result.Periods.Add(result);
}
return result;
}
}

View File

@@ -0,0 +1,81 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ParameterGeneratorEventArgs : EventArgs
{
public required ExpressionList Parameters { get; init; }
}
public class NoParameterGeneratorEventArgs : EventArgs;
public class ProcedureCall : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ProcedureCall;
/// <summary>
/// 调用函数的名称
/// </summary>
public IdentifierSemanticToken ProcedureId
=> Children[0].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>();
/// <summary>
/// 调用函数的参数
/// </summary>
public List<Expression> Parameters { get; } = [];
/// <summary>
/// 调用函数时含有参数的事件
/// </summary>
public event EventHandler<ParameterGeneratorEventArgs>? OnParameterGenerator;
/// <summary>
/// 调用函数是没有参数的事件
/// </summary>
public event EventHandler<NoParameterGeneratorEventArgs>? OnNoParameterGenerator;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseEvent();
}
public static ProcedureCall Create(List<SyntaxNodeBase> children)
{
ProcedureCall result = new() { Children = children };
if (children.Count == 4)
{
result.Parameters.AddRange(children[2].Convert<ExpressionList>().Expressions);
}
return result;
}
private void RaiseEvent()
{
if (Children.Count == 4)
{
OnParameterGenerator?.Invoke(this, new ParameterGeneratorEventArgs
{
Parameters = Children[2].Convert<ExpressionList>()
});
}
else
{
OnNoParameterGenerator?.Invoke(this, new NoParameterGeneratorEventArgs());
}
OnParameterGenerator = null;
OnNoParameterGenerator = null;
}
}

View File

@@ -0,0 +1,29 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ProgramBody : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ProgramBody;
/// <summary>
/// 语句声明
/// </summary>
public CompoundStatement CompoundStatement => Children[3].Convert<CompoundStatement>();
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static ProgramBody Create(List<SyntaxNodeBase> children)
{
return new ProgramBody { Children = children };
}
}

View File

@@ -0,0 +1,31 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ProgramHead : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ProgramHead;
/// <summary>
/// 程序名称
/// </summary>
public IdentifierSemanticToken ProgramName
=> (IdentifierSemanticToken)Children[1].Convert<TerminatedSyntaxNode>().Token;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static ProgramHead Create(List<SyntaxNodeBase> children)
{
return new ProgramHead { Children = children };
}
}

View File

@@ -0,0 +1,29 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ProgramStruct : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ProgramStruct;
/// <summary>
/// 程序头
/// </summary>
public ProgramHead Head => Children[0].Convert<ProgramHead>();
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static ProgramStruct Create(List<SyntaxNodeBase> children)
{
return new ProgramStruct { Children = children };
}
}

View File

@@ -0,0 +1,27 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class RelationOperator : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.RelationOperator;
public SemanticToken OperatorToken => Children[0].Convert<TerminatedSyntaxNode>().Token;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static RelationOperator Create(List<SyntaxNodeBase> children)
{
return new RelationOperator { Children = children };
}
}

View File

@@ -0,0 +1,76 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class TermGeneratorEventArgs : EventArgs
{
public required Term Term { get; init; }
}
public class AddGeneratorEventArgs : EventArgs
{
public required SimpleExpression Left { get; init; }
public required AddOperator Operator { get; init; }
public required Term Right { get; init; }
}
public class SimpleExpression : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.SimpleExpression;
/// <summary>
/// 是否为条件判断语句
/// </summary>
public bool IsCondition { get; set; }
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<TermGeneratorEventArgs>? OnTermGenerator;
/// <summary>
/// 加法产生式的事件
/// </summary>
public event EventHandler<AddGeneratorEventArgs>? OnAddGenerator;
public static SimpleExpression Create(List<SyntaxNodeBase> children)
{
return new SimpleExpression { Children = children };
}
private void RaiseEvent()
{
if (Children.Count == 1)
{
OnTermGenerator?.Invoke(this, new TermGeneratorEventArgs { Term = Children[0].Convert<Term>() });
}
else
{
OnAddGenerator?.Invoke(this,
new AddGeneratorEventArgs
{
Left = Children[0].Convert<SimpleExpression>(),
Operator = Children[1].Convert<AddOperator>(),
Right = Children[2].Convert<Term>()
});
}
OnTermGenerator = null;
OnAddGenerator = null;
}
}

View File

@@ -0,0 +1,129 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class AssignGeneratorEventArgs : EventArgs
{
public required Variable Variable { get; init; }
public required Expression Expression { get; init; }
}
public class IfGeneratorEventArgs : EventArgs
{
public required Expression Condition { get; init; }
public required Statement Sentence { get; init; }
public required ElsePart ElseSentence { get; init; }
}
public class ForGeneratorEventArgs : EventArgs
{
public required IdentifierSemanticToken Iterator { get; init; }
public required Expression Begin { get; init; }
public required Expression End { get; init; }
public required Statement Sentence { get; init; }
public required TerminatedSyntaxNode Do { get; init; }
}
public class WhileGeneratorEventArgs : EventArgs
{
public required Expression Condition { get; init; }
public required Statement Sentence { get; init; }
public required TerminatedSyntaxNode Do { get; init; }
}
public class Statement : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Statement;
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<AssignGeneratorEventArgs>? OnAssignGenerator;
/// <summary>
/// 使用If产生式的事件
/// </summary>
public event EventHandler<IfGeneratorEventArgs>? OnIfGenerator;
/// <summary>
/// 使用For产生式的事件
/// </summary>
public event EventHandler<ForGeneratorEventArgs>? OnForGenerator;
/// <summary>
/// 使用While产生式的事件
/// </summary>
public event EventHandler<WhileGeneratorEventArgs>? OnWhileGenerator;
public static Statement Create(List<SyntaxNodeBase> children)
{
return new Statement { Children = children };
}
private void RaiseEvent()
{
if (Children.Count == 3)
{
OnAssignGenerator?.Invoke(this,
new AssignGeneratorEventArgs
{
Variable = Children[0].Convert<Variable>(), Expression = Children[2].Convert<Expression>()
});
}
else if (Children.Count == 4)
{
OnWhileGenerator?.Invoke(this,
new WhileGeneratorEventArgs
{
Condition = Children[1].Convert<Expression>(),
Do = Children[2].Convert<TerminatedSyntaxNode>(),
Sentence = Children[3].Convert<Statement>(),
});
}
else if (Children.Count == 5)
{
OnIfGenerator?.Invoke(this,
new IfGeneratorEventArgs
{
Condition = Children[1].Convert<Expression>(),
Sentence = Children[3].Convert<Statement>(),
ElseSentence = Children[4].Convert<ElsePart>()
});
}
else if (Children.Count == 8)
{
OnForGenerator?.Invoke(this,
new ForGeneratorEventArgs
{
Iterator = Children[1].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>(),
Begin = Children[3].Convert<Expression>(),
End = Children[5].Convert<Expression>(),
Do = Children[6].Convert<TerminatedSyntaxNode>(),
Sentence = Children[7].Convert<Statement>()
});
}
}
}

View File

@@ -0,0 +1,24 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class StatementList : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.StatementList;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static StatementList Create(List<SyntaxNodeBase> children)
{
return new StatementList { Children = children};
}
}

View File

@@ -0,0 +1,24 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class Subprogram : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Subprogram;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static Subprogram Create(List<SyntaxNodeBase> children)
{
return new Subprogram { Children = children };
}
}

View File

@@ -0,0 +1,24 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class SubprogramBody : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.SubprogramBody;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static SubprogramBody Create(List<SyntaxNodeBase> children)
{
return new SubprogramBody() { Children = children };
}
}

View File

@@ -0,0 +1,25 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class SubprogramDeclarations : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.SubprogramDeclarations;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static SubprogramDeclarations Create(List<SyntaxNodeBase> children)
{
return new SubprogramDeclarations { Children = children };
}
}

View File

@@ -0,0 +1,83 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ProcedureGeneratorEventArgs : EventArgs;
public class FunctionGeneratorEventArgs : EventArgs
{
public required BasicType ReturnType { get; init; }
}
public class SubprogramHead : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.SubprogramHead;
/// <summary>
/// 过程定义还是函数定义
/// </summary>
public bool IsProcedure { get; private init; }
/// <summary>
/// 子程序的名称
/// </summary>
public IdentifierSemanticToken SubprogramName =>
Children[1].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>();
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseEvent();
}
public event EventHandler<ProcedureGeneratorEventArgs>? OnProcedureGenerator;
public event EventHandler<FunctionGeneratorEventArgs>? OnFunctionGenerator;
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 };
}
private void RaiseEvent()
{
if (IsProcedure)
{
OnProcedureGenerator?.Invoke(this, new ProcedureGeneratorEventArgs());
}
else
{
OnFunctionGenerator?.Invoke(this,
new FunctionGeneratorEventArgs { ReturnType = Children[4].Convert<BasicType>() });
}
OnProcedureGenerator = null;
OnFunctionGenerator = null;
}
}

View File

@@ -0,0 +1,117 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public abstract class SyntaxNodeBase
{
public abstract bool IsTerminated { get; }
public abstract void PreVisit(SyntaxNodeVisitor visitor);
public abstract void PostVisit(SyntaxNodeVisitor visitor);
public T Convert<T>() where T : SyntaxNodeBase
{
T? result = this as T;
if (result is null)
{
throw new InvalidOperationException("Can't cast into target SyntaxNode");
}
return result;
}
public override string ToString()
{
return IsTerminated.ToString();
}
public static SyntaxNodeBase Create(SemanticToken token)
{
return new TerminatedSyntaxNode { Token = token };
}
public static SyntaxNodeBase Create(NonTerminatorType type, List<SyntaxNodeBase> children)
{
switch (type)
{
case NonTerminatorType.ProgramStruct:
return ProgramStruct.Create(children);
case NonTerminatorType.ProgramHead:
return ProgramHead.Create(children);
case NonTerminatorType.ProgramBody:
return ProgramBody.Create(children);
case NonTerminatorType.IdentifierList:
return IdentifierList.Create(children);
case NonTerminatorType.VarDeclarations:
return VarDeclarations.Create(children);
case NonTerminatorType.SubprogramDeclarations:
return SubprogramDeclarations.Create(children);
case NonTerminatorType.CompoundStatement:
return CompoundStatement.Create(children);
case NonTerminatorType.ConstValue:
return ConstValue.Create(children);
case NonTerminatorType.ConstDeclaration:
return ConstDeclaration.Create(children);
case NonTerminatorType.ConstDeclarations:
return ConstDeclarations.Create(children);
case NonTerminatorType.VarDeclaration:
return VarDeclaration.Create(children);
case NonTerminatorType.Type:
return TypeSyntaxNode.Create(children);
case NonTerminatorType.BasicType:
return BasicType.Create(children);
case NonTerminatorType.Period:
return Period.Create(children);
case NonTerminatorType.Subprogram:
return Subprogram.Create(children);
case NonTerminatorType.SubprogramHead:
return SubprogramHead.Create(children);
case NonTerminatorType.SubprogramBody:
return SubprogramBody.Create(children);
case NonTerminatorType.FormalParameter:
return FormalParameter.Create(children);
case NonTerminatorType.ParameterList:
return ParameterList.Create(children);
case NonTerminatorType.Parameter:
return Parameter.Create(children);
case NonTerminatorType.VarParameter:
return VarParameter.Create(children);
case NonTerminatorType.ValueParameter:
return ValueParameter.Create(children);
case NonTerminatorType.StatementList:
return StatementList.Create(children);
case NonTerminatorType.Statement:
return Statement.Create(children);
case NonTerminatorType.Variable:
return Variable.Create(children);
case NonTerminatorType.Expression:
return Expression.Create(children);
case NonTerminatorType.ProcedureCall:
return ProcedureCall.Create(children);
case NonTerminatorType.ElsePart:
return ElsePart.Create(children);
case NonTerminatorType.ExpressionList:
return ExpressionList.Create(children);
case NonTerminatorType.SimpleExpression:
return SimpleExpression.Create(children);
case NonTerminatorType.Term:
return Term.Create(children);
case NonTerminatorType.Factor:
return Factor.Create(children);
case NonTerminatorType.AddOperator:
return AddOperator.Create(children);
case NonTerminatorType.MultiplyOperator:
return MultiplyOperator.Create(children);
case NonTerminatorType.RelationOperator:
return RelationOperator.Create(children);
case NonTerminatorType.IdVarPart:
return IdentifierVarPart.Create(children);
default:
throw new InvalidOperationException();
}
}
}

View File

@@ -0,0 +1,76 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class FactorGeneratorEventArgs : EventArgs
{
public required Factor Factor { get; init; }
}
public class MultiplyGeneratorEventArgs : EventArgs
{
public required Term Left { get; init; }
public required MultiplyOperator Operator { get; init; }
public required Factor Right { get; init; }
}
public class Term : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Term;
/// <summary>
/// 是否为条件判断语句
/// </summary>
public bool IsCondition { get; set; }
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<FactorGeneratorEventArgs>? OnFactorGenerator;
/// <summary>
/// 乘法产生式的事件
/// </summary>
public event EventHandler<MultiplyGeneratorEventArgs>? OnMultiplyGenerator;
public static Term Create(List<SyntaxNodeBase> children)
{
return new Term { Children = children };
}
private void RaiseEvent()
{
if (Children.Count == 1)
{
OnFactorGenerator?.Invoke(this, new FactorGeneratorEventArgs { Factor = Children[0].Convert<Factor>() });
}
else
{
OnMultiplyGenerator?.Invoke(this,
new MultiplyGeneratorEventArgs
{
Left = Children[0].Convert<Term>(),
Operator = Children[1].Convert<MultiplyOperator>(),
Right = Children[2].Convert<Factor>()
});
}
OnFactorGenerator = null;
OnMultiplyGenerator = null;
}
}

View File

@@ -0,0 +1,36 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class TerminatedSyntaxNode : SyntaxNodeBase
{
public override bool IsTerminated => true;
/// <summary>
/// 是否为For循环定义中的DO节点
/// </summary>
public bool IsForNode { get; set; }
/// <summary>
/// 是否为While循环定义中的DO节点
/// </summary>
public bool IsWhileNode { get; set; }
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public required SemanticToken Token { get; init; }
public override string ToString()
{
return Token.LiteralValue;
}
}

View File

@@ -0,0 +1,64 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class BasicTypeGeneratorEventArgs : EventArgs
{
public required BasicType BasicType { get; init; }
}
public class ArrayTypeGeneratorEventArgs : EventArgs
{
public required Period Period { get; init; }
public required BasicType BasicType { get; init; }
}
public class TypeSyntaxNode : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Type;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseEvent();
}
public event EventHandler<BasicTypeGeneratorEventArgs>? OnBasicTypeGenerator;
public event EventHandler<ArrayTypeGeneratorEventArgs>? OnArrayTypeGenerator;
public static TypeSyntaxNode Create(List<SyntaxNodeBase> children)
{
return new TypeSyntaxNode { Children = children };
}
private void RaiseEvent()
{
if (Children.Count == 1)
{
OnBasicTypeGenerator?.Invoke(this, new BasicTypeGeneratorEventArgs
{
BasicType = Children[0].Convert<BasicType>()
});
}
else
{
OnArrayTypeGenerator?.Invoke(this, new ArrayTypeGeneratorEventArgs
{
Period = Children[2].Convert<Period>(),
BasicType = Children[5].Convert<BasicType>()
});
}
OnBasicTypeGenerator = null;
OnArrayTypeGenerator = null;
}
}

View File

@@ -0,0 +1,35 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class ValueParameter : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ValueParameter;
/// <summary>
/// 是否为参数中的引用参数
/// </summary>
public bool IsReference { get; set; }
public IdentifierSemanticToken Token =>
Children[0].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>();
public IdentifierList IdentifierList => Children[1].Convert<IdentifierList>();
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static ValueParameter Create(List<SyntaxNodeBase> children)
{
return new ValueParameter { Children = children };
}
}

View File

@@ -0,0 +1,46 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class VarDeclaration : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.VarDeclaration;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public required IdentifierSemanticToken Token { get; init; }
public required IdentifierList IdentifierList { get; init; }
public static VarDeclaration Create(List<SyntaxNodeBase> children)
{
if (children.Count == 2)
{
return new VarDeclaration
{
Children = children,
Token = children[0].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>(),
IdentifierList = children[1].Convert<IdentifierList>()
};
}
else
{
return new VarDeclaration
{
Children = children,
Token = children[2].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>(),
IdentifierList = children[3].Convert<IdentifierList>()
};
}
}
}

View File

@@ -0,0 +1,24 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class VarDeclarations : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.VarDeclarations;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static VarDeclarations Create(List<SyntaxNodeBase> children)
{
return new VarDeclarations { Children = children };
}
}

View File

@@ -0,0 +1,26 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class VarParameter : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.VarParameter;
public ValueParameter ValueParameter => Children[1].Convert<ValueParameter>();
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static VarParameter Create(List<SyntaxNodeBase> children)
{
return new VarParameter { Children = children };
}
}

View File

@@ -0,0 +1,36 @@
using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class Variable : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Variable;
/// <summary>
/// 变量的名称
/// </summary>
public IdentifierSemanticToken Identifier =>
(IdentifierSemanticToken)Children[0].Convert<TerminatedSyntaxNode>().Token;
/// <summary>
/// 声明数组访问的部分
/// </summary>
public IdentifierVarPart VarPart => Children[1].Convert<IdentifierVarPart>();
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
}
public static Variable Create(List<SyntaxNodeBase> children)
{
return new Variable { Children = children };
}
}