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:
@@ -0,0 +1,82 @@
|
||||
using CanonSharp.Benchmark.Canon.Core.Enums;
|
||||
using CanonSharp.Benchmark.Canon.Core.Exceptions;
|
||||
using CanonSharp.Benchmark.Canon.Core.GrammarParser;
|
||||
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
|
||||
using CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
|
||||
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// 语法分析器接口
|
||||
/// </summary>
|
||||
public interface IGrammarParser
|
||||
{
|
||||
public ITransformer BeginTransformer { get; }
|
||||
|
||||
public NonTerminator Begin { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 分析指定的词法记号流并构建对应的语法树
|
||||
/// </summary>
|
||||
/// <param name="tokens">输入的词法记号流</param>
|
||||
/// <returns>语法树的根节点</returns>
|
||||
/// <exception cref="InvalidOperationException">语法分析错误</exception>
|
||||
public ProgramStruct Analyse(IEnumerable<SemanticToken> tokens)
|
||||
{
|
||||
Stack<AnalyseState> stack = [];
|
||||
stack.Push(new AnalyseState(BeginTransformer, SyntaxNodeBase.Create(SemanticToken.End)));
|
||||
|
||||
using IEnumerator<SemanticToken> enumerator = tokens.GetEnumerator();
|
||||
if (!enumerator.MoveNext())
|
||||
{
|
||||
throw new InvalidOperationException("Input token list is empty");
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
AnalyseState top = stack.Peek();
|
||||
|
||||
// 首先尝试进行移进
|
||||
if (top.State.ShiftTable.TryGetValue(enumerator.Current, out ITransformer? next))
|
||||
{
|
||||
stack.Push(new AnalyseState(next, SyntaxNodeBase.Create(enumerator.Current)));
|
||||
if (enumerator.MoveNext())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new GrammarException(stack.Peek().State);
|
||||
}
|
||||
}
|
||||
|
||||
// 再进行归约
|
||||
if (top.State.ReduceTable.TryGetValue(enumerator.Current, out ReduceInformation? information))
|
||||
{
|
||||
if (information.Left == Begin)
|
||||
{
|
||||
// 如果是归约到起始符
|
||||
// 那么就直接返回不继续进行归约
|
||||
return top.Node.Convert<ProgramStruct>();
|
||||
}
|
||||
|
||||
List<SyntaxNodeBase> children = [];
|
||||
NonTerminatorType leftType = information.Left.Type;
|
||||
for (int i = 0; i < information.Length; i++)
|
||||
{
|
||||
children.Add(stack.Pop().Node);
|
||||
}
|
||||
|
||||
// 为了符合生成式的顺序而倒序
|
||||
children.Reverse();
|
||||
stack.Push(new AnalyseState(stack.Peek().State.ShiftTable[information.Left],
|
||||
SyntaxNodeBase.Create(leftType, children)));
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new GrammarException(stack.Peek().State, enumerator.Current);
|
||||
}
|
||||
}
|
||||
|
||||
private record AnalyseState(ITransformer State, SyntaxNodeBase Node);
|
||||
}
|
11
CanonSharp.Benchmark/Canon.Core/Abstractions/ILexer.cs
Normal file
11
CanonSharp.Benchmark/Canon.Core/Abstractions/ILexer.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
|
||||
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// 词法分析器接口
|
||||
/// </summary>
|
||||
public interface ILexer
|
||||
{
|
||||
public IEnumerable<SemanticToken> Tokenize(ISourceReader reader);
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// 读取源代码的接口
|
||||
/// </summary>
|
||||
public interface ISourceReader
|
||||
{
|
||||
public char Current { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 源文件名称
|
||||
/// </summary>
|
||||
public string FileName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前读取字符的行号
|
||||
/// </summary>
|
||||
public uint Line { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前读取字符的列号
|
||||
/// </summary>
|
||||
public uint Pos { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 回退一个字符
|
||||
/// </summary>
|
||||
/// <returns>回退是否成功</returns>
|
||||
public bool Retract();
|
||||
|
||||
/// <summary>
|
||||
/// 前进一个字符
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool MoveNext();
|
||||
|
||||
/// <summary>
|
||||
/// 读取下一个字符但是移进
|
||||
/// </summary>
|
||||
/// <param name="c">读取到的下一个字符</param>
|
||||
/// <returns>是否能够读取下一个字符</returns>
|
||||
public bool TryPeekChar([NotNullWhen(true)] out char? c);
|
||||
}
|
33
CanonSharp.Benchmark/Canon.Core/Abstractions/ITransformer.cs
Normal file
33
CanonSharp.Benchmark/Canon.Core/Abstractions/ITransformer.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using CanonSharp.Benchmark.Canon.Core.GrammarParser;
|
||||
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// 进行归约需要的信息
|
||||
/// </summary>
|
||||
/// <param name="Length">归约的长度</param>
|
||||
/// <param name="Left">归约得到的左部符号</param>
|
||||
public record ReduceInformation(int Length, NonTerminator Left)
|
||||
{
|
||||
public string GenerateCode()
|
||||
{
|
||||
return $"new ReduceInformation({Length}, {Left.GenerateCode()})";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 状态的各种迁移信息
|
||||
/// </summary>
|
||||
public interface ITransformer
|
||||
{
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 进行移进的信息
|
||||
/// </summary>
|
||||
public IDictionary<TerminatorBase, ITransformer> ShiftTable { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 进行归约的信息
|
||||
/// </summary>
|
||||
public IDictionary<Terminator, ReduceInformation> ReduceTable { get; }
|
||||
}
|
@@ -0,0 +1,302 @@
|
||||
using CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
|
||||
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Abstractions;
|
||||
|
||||
public abstract class SyntaxNodeVisitor
|
||||
{
|
||||
public virtual void PreVisit(AddOperator addOperator)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(AddOperator addOperator)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(BasicType basicType)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(BasicType basicType)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(CompoundStatement compoundStatement)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(CompoundStatement compoundStatement)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ConstDeclaration constDeclaration)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ConstDeclaration constDeclaration)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ConstDeclarations constDeclarations)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ConstDeclarations constDeclarations)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ConstValue constValue)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ConstValue constValue)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ElsePart elsePart)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ElsePart elsePart)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(Expression expression)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(Expression expression)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ExpressionList expressionList)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ExpressionList expressionList)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(Factor factor)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(Factor factor)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(FormalParameter formalParameter)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(FormalParameter formalParameter)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(IdentifierList identifierList)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(IdentifierList identifierList)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(IdentifierVarPart identifierVarPart)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(IdentifierVarPart identifierVarPart)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(MultiplyOperator multiplyOperator)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(MultiplyOperator multiplyOperator)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(Parameter parameter)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(Parameter parameter)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ParameterList parameterList)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ParameterList parameterList)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(Period period)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(Period period)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ProcedureCall procedureCall)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ProcedureCall procedureCall)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ProgramBody programBody)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ProgramBody programBody)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ProgramHead programHead)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ProgramHead programHead)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ProgramStruct programStruct)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ProgramStruct programStruct)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(RelationOperator relationOperator)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(RelationOperator relationOperator)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(SimpleExpression simpleExpression)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(SimpleExpression simpleExpression)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(Statement statement)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(Statement statement)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(StatementList statementList)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(StatementList statementList)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(Subprogram subprogram)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(Subprogram subprogram)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(SubprogramBody subprogramBody)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(SubprogramBody subprogramBody)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(SubprogramDeclarations subprogramDeclarations)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(SubprogramDeclarations subprogramDeclarations)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(SubprogramHead subprogramHead)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(SubprogramHead subprogramHead)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(Term term)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(Term term)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(TypeSyntaxNode typeSyntaxNode)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(TypeSyntaxNode typeSyntaxNode)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(ValueParameter valueParameter)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(ValueParameter valueParameter)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(VarDeclaration varDeclaration)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(VarDeclaration varDeclaration)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(VarDeclarations varDeclarations)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(VarDeclarations varDeclarations)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(Variable variable)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(Variable variable)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(VarParameter varParameter)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(VarParameter varParameter)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreVisit(TerminatedSyntaxNode terminatedSyntaxNode)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PostVisit(TerminatedSyntaxNode terminatedSyntaxNode)
|
||||
{
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user