2024-08-15 16:18:32 +08:00
|
|
|
|
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.IdentifierName);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-08-16 17:07:53 +08:00
|
|
|
|
|
|
|
|
|
[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.IdentifierName);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-08-15 16:18:32 +08:00
|
|
|
|
}
|