Canon/Canon.Tests/CodeGeneratorTests/DeclarationTests.cs
Lan_G 5ca947125b feat-generater (#69)
Reviewed-on: PostGuard/Canon#69
Co-authored-by: Lan_G <2911328695@qq.com>
Co-committed-by: Lan_G <2911328695@qq.com>
2024-04-30 14:43:14 +08:00

65 lines
2.3 KiB
C#

using Canon.Core.SemanticParser;
using Canon.Core.SyntaxNodes;
using Canon.Tests.Utils;
using Xunit.Abstractions;
namespace Canon.Tests.CodeGeneratorTests;
public class DeclarationTests
{
private readonly ITestOutputHelper _output;
public DeclarationTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void ConstDeclarationTest()
{
const string program = """
program main;
const a = 'a'; b = 200; c = 3.14; d = 'm';
begin
end.
""";
ProgramStruct root = CompilerHelpers.Analyse(program);
SyntaxTreeTraveller traveller = new();
CCodeGenerateVisitor visitor = new();
traveller.Travel(root, visitor);
string result = visitor.Builder.Build();
_output.WriteLine(result);
Assert.Equal("#include <stdbool.h>\n#include <stdio.h>\nconst char a = 'a';\nconst " +
"int b = 200;\nconst double c = 3.14;\nconst char d = 'm';\nint main()\n{\n;\n\nreturn 0;\n}\n",
visitor.Builder.Build());
}
[Fact]
public void VarDeclarationTest()
{
const string program = """
program main;
var a, b, c:array[3..6, 4..999, 0..7, 8..80] of real;
d, e, f:integer;
g, h:array [6..8] of boolean;
i, j:char;
m, n:integer;
begin
end.
""";
ProgramStruct root = CompilerHelpers.Analyse(program);
SyntaxTreeTraveller traveller = new();
CCodeGenerateVisitor visitor = new();
traveller.Travel(root, visitor);
string result = visitor.Builder.Build();
_output.WriteLine(result);
Assert.Equal("#include <stdbool.h>\n#include <stdio.h>\ndouble c[4][996][8][73]," +
" b[4][996][8][73], a[4][996][8][73];\nint f, e, d;\nbool h[3], g[3];\nchar j, i;" +
"\nint n, m;\nint main()\n{\n;\n\nreturn 0;\n}\n", visitor.Builder.Build());
}
}