2024-04-12 19:01:37 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
|
|
|
|
using Canon.Core.CodeGenerators;
|
2024-04-22 21:26:34 +08:00
|
|
|
|
using Canon.Core.GrammarParser;
|
2024-04-12 19:01:37 +08:00
|
|
|
|
using Canon.Core.LexicalParser;
|
|
|
|
|
using Canon.Core.SyntaxNodes;
|
2024-04-18 16:34:32 +08:00
|
|
|
|
using Canon.Tests.Utils;
|
2024-04-21 22:24:35 +08:00
|
|
|
|
using Xunit.Abstractions;
|
2024-04-12 19:01:37 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Tests.CCodeGeneratorTests;
|
|
|
|
|
|
|
|
|
|
public class BasicTests
|
|
|
|
|
{
|
|
|
|
|
private readonly IGrammarParser _parser = GeneratedGrammarParser.Instance;
|
2024-04-18 16:34:32 +08:00
|
|
|
|
private readonly ILexer _lexer = new Lexer();
|
2024-04-21 22:24:35 +08:00
|
|
|
|
private readonly ITestOutputHelper _outputHelper;
|
|
|
|
|
|
|
|
|
|
public BasicTests(ITestOutputHelper outputHelper)
|
|
|
|
|
{
|
|
|
|
|
_outputHelper = outputHelper;
|
|
|
|
|
}
|
2024-04-12 19:01:37 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ProgramStructTest()
|
|
|
|
|
{
|
|
|
|
|
CCodeBuilder builder = new();
|
|
|
|
|
|
|
|
|
|
const string program = """
|
|
|
|
|
program DoNothing;
|
|
|
|
|
begin
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
2024-04-18 16:34:32 +08:00
|
|
|
|
IEnumerable<SemanticToken> tokens = _lexer.Tokenize(new StringSourceReader(program));
|
2024-04-12 19:01:37 +08:00
|
|
|
|
|
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
|
|
|
|
root.GenerateCCode(builder);
|
|
|
|
|
|
2024-04-21 22:24:35 +08:00
|
|
|
|
string result = builder.Build();
|
|
|
|
|
_outputHelper.WriteLine(result);
|
|
|
|
|
Assert.Equal("#include <PascalCoreLib.h> int main(){statement; return 0;}", result);
|
2024-04-12 19:01:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|