feat: 语法树的访问者和类型检测访问者 (#56)

Reviewed-on: PostGuard/Canon#56
This commit is contained in:
2024-04-26 10:18:49 +08:00
parent b20c3234c5
commit 17dbcccb59
63 changed files with 2757 additions and 704 deletions

View File

@@ -0,0 +1,18 @@
using Canon.Core.Abstractions;
using Canon.Core.GrammarParser;
using Canon.Core.LexicalParser;
using Canon.Core.SyntaxNodes;
namespace Canon.Tests.Utils;
public static class CompilerHelpers
{
public static ProgramStruct Analyse(string program)
{
ILexer lexer = new Lexer();
IGrammarParser grammarParser = GeneratedGrammarParser.Instance;
IEnumerable<SemanticToken> tokens = lexer.Tokenize(new StringSourceReader(program));
return grammarParser.Analyse(tokens);
}
}

View File

@@ -0,0 +1,85 @@
using System.Text;
using Canon.Core.Abstractions;
using Canon.Core.SyntaxNodes;
namespace Canon.Tests.Utils;
public class SampleSyntaxTreeVisitor : SyntaxNodeVisitor
{
private readonly StringBuilder _builder = new();
public override string ToString()
{
return _builder.ToString();
}
public override void PreVisit(ProgramStruct programStruct)
{
_builder.Append(programStruct).Append('\n');
}
public override void PostVisit(ProgramStruct programStruct)
{
_builder.Append(programStruct).Append('\n');
}
public override void PreVisit(ProgramHead programHead)
{
_builder.Append(programHead).Append('\n');
}
public override void PostVisit(ProgramHead programHead)
{
_builder.Append(programHead).Append('\n');
}
public override void PreVisit(ProgramBody programBody)
{
_builder.Append(programBody).Append('\n');
}
public override void PostVisit(ProgramBody programBody)
{
_builder.Append(programBody).Append('\n');
}
public override void PreVisit(SubprogramDeclarations subprogramDeclarations)
{
_builder.Append(subprogramDeclarations).Append('\n');
}
public override void PostVisit(SubprogramDeclarations subprogramDeclarations)
{
_builder.Append(subprogramDeclarations).Append('\n');
}
public override void PreVisit(CompoundStatement compoundStatement)
{
_builder.Append(compoundStatement).Append('\n');
}
public override void PostVisit(CompoundStatement compoundStatement)
{
_builder.Append(compoundStatement).Append('\n');
}
public override void PreVisit(StatementList statementList)
{
_builder.Append(statementList).Append('\n');
}
public override void PostVisit(StatementList statementList)
{
_builder.Append(statementList).Append('\n');
}
public override void PreVisit(Statement statement)
{
_builder.Append(statement).Append('\n');
}
public override void PostVisit(Statement statement)
{
_builder.Append(statement).Append('\n');
}
}