CanonSharp/CanonSharp.Tests/ParserTests/ProgramParserTests.cs
jackfiled cf19f8197e feat: Grammar Parser (#3)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/3
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
2024-08-18 12:01:27 +08:00

230 lines
7.1 KiB
C#

using CanonSharp.Pascal.SyntaxTree;
using CanonSharp.Tests.Utils;
namespace CanonSharp.Tests.ParserTests;
public class ProgramParserTests : GrammarParserTestBase
{
[Fact]
public void ProgramParseTest1()
{
Program program = ProgramParse("""
program main;
begin
end.
""");
Assert.Equal("main", program.Head.ProgramName.LiteralValue);
Assert.Empty(program.Body.MainBlock.Statements);
}
[Fact]
public void ProgramParseTest2()
{
Program program = ProgramParse("""
program main;
begin
temp := 1 + 1;
end.
""");
Assert.Equal("main", program.Head.ProgramName.LiteralValue);
AssignNode assignNode = program.Body.MainBlock.Statements[0].Convert<AssignNode>();
Assert.Equal("temp", assignNode.Variable.Identifier.LiteralValue);
BinaryOperatorNode binaryOperatorNode = assignNode.Expression.Convert<BinaryOperatorNode>();
Assert.Equal(BinaryOperatorType.Add, binaryOperatorNode.OperatorType);
Assert.Equal(1, binaryOperatorNode.Left.Convert<IntegerValueNode>().Value);
Assert.Equal(1, binaryOperatorNode.Right.Convert<IntegerValueNode>().Value);
}
[Fact]
public void ProgramParseTest3()
{
Program program = ProgramParse("""
program main;
const a = 1; b = true;
begin
temp := 1 + 1;
end.
""");
Assert.Equal("main", program.Head.ProgramName.LiteralValue);
List<ConstantNode> constantNodes = program.Body.ConstantDeclarations.Statements
.Select(x => x.Convert<ConstantNode>()).ToList();
Assert.Equal(2, constantNodes.Count);
Assert.Equal("a", constantNodes[0].Identifier.LiteralValue);
Assert.Equal("b", constantNodes[1].Identifier.LiteralValue);
AssignNode assignNode = program.Body.MainBlock.Statements[0].Convert<AssignNode>();
Assert.Equal("temp", assignNode.Variable.Identifier.LiteralValue);
BinaryOperatorNode binaryOperatorNode = assignNode.Expression.Convert<BinaryOperatorNode>();
Assert.Equal(BinaryOperatorType.Add, binaryOperatorNode.OperatorType);
Assert.Equal(1, binaryOperatorNode.Left.Convert<IntegerValueNode>().Value);
Assert.Equal(1, binaryOperatorNode.Right.Convert<IntegerValueNode>().Value);
}
[Fact]
public void ProgramParseTest4()
{
Program program = ProgramParse("""
program main;
var a : integer;
begin
a := 1 + 1;
end.
""");
IList<VariableDeclarationNode> variableDeclarationNodes = program.Body.VariableDeclarations.Statements
.Select(x => x.Convert<VariableDeclarationNode>()).ToList();
Assert.Single(variableDeclarationNodes);
Assert.Single(variableDeclarationNodes.First().Identifiers);
Assert.Equal("a", variableDeclarationNodes.First().Identifiers.First().LiteralValue);
}
[Theory]
[InlineData("""
program DoNothing;
begin
end.
""")]
[InlineData("""
program Add;
var a : Integer;
begin
a := 1 + 1
end.
""")]
[InlineData("""
program varTest;
var a : integer;
begin
a := 9 div 1;
end.
""")]
[InlineData("""
program main;
var a : integer;
begin
a := 1 + +1;
a := 1 - -1;
a := 1 + -1;
a := 1 - +1;
end.
""")]
[InlineData("""
program main;
const a = true; b = false;
var c, d : boolean;
begin
c := true;
d := false;
end.
""")]
[InlineData("""
program arrayTest;
var a : array [0..10] of integer;
begin
a[0] := 1;
end.
""")]
[InlineData("""
program arrayTest;
var a : array [10..100, 0..10] of integer;
begin
a[10,0] := 1;
end.
""")]
[InlineData("""
program main;
begin
begin
end
end.
""")]
[InlineData("""
program main;
var i : integer;
begin
while i < 10 do
i := i + 1;
end.
""")]
[InlineData("""
program main;
procedure test;
begin
end;
begin
end.
""")]
[InlineData("""
program main;
function test(a, b : integer) : integer;
begin
test := 1;
end;
begin
end.
""")]
[InlineData("""
program main;
var i : integer;
begin
for i := 1 to 100 do
begin
doSomething(i);
end;
end.
""")]
[InlineData("""
program main;
begin
if 1 = 2 then
begin
test1;
test2 := a;
end
else
begin
doSomething;
end;
end.
""")]
[InlineData("""
program main;
var a : integer;
function test : integer;
begin
end;
begin
test;
a := test;
test();
a := test();
end.
""")]
[InlineData("""
program main;
procedure test();
begin
end;
begin
test();
end.
""")]
[InlineData("""
program main;
begin
test
end.
""")]
public void ProgramParseTest(string input)
{
ProgramParse(input);
}
}