2024-08-15 16:18:32 +08:00
|
|
|
|
using CanonSharp.Pascal.Parser;
|
|
|
|
|
using CanonSharp.Pascal.SyntaxTree;
|
|
|
|
|
using CanonSharp.Tests.Utils;
|
|
|
|
|
|
|
|
|
|
namespace CanonSharp.Tests.ParserTests;
|
|
|
|
|
|
|
|
|
|
public class StatementParserTests : GrammarParserTestBase
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void StatementTest1()
|
|
|
|
|
{
|
|
|
|
|
AssignNode node = RunParser<AssignNode>(GrammarParser.StatementParser(), "temp := 1");
|
|
|
|
|
|
2024-08-17 17:50:28 +08:00
|
|
|
|
Assert.Equal("temp", node.Variable.Identifier.LiteralValue);
|
2024-08-15 16:18:32 +08:00
|
|
|
|
Assert.Equal(1, node.Expression.Convert<IntegerValueNode>().Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData("""
|
|
|
|
|
begin
|
|
|
|
|
temp := 1 + 1;
|
|
|
|
|
flag := true and false;
|
|
|
|
|
end
|
|
|
|
|
""")]
|
|
|
|
|
[InlineData("""
|
|
|
|
|
begin
|
|
|
|
|
temp := 1 + 1;
|
|
|
|
|
flag := true and false
|
|
|
|
|
end
|
|
|
|
|
""")]
|
|
|
|
|
public void CompoundStatementTest1(string input)
|
|
|
|
|
{
|
|
|
|
|
BlockNode node = RunParser<BlockNode>(GrammarParser.CompoundStatementParser(), input);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(2, node.Statements.Count);
|
|
|
|
|
AssignNode assignNode = node.Statements[0].Convert<AssignNode>();
|
2024-08-17 17:50:28 +08:00
|
|
|
|
Assert.Equal("temp", assignNode.Variable.Identifier.LiteralValue);
|
2024-08-15 16:18:32 +08:00
|
|
|
|
|
|
|
|
|
assignNode = node.Statements[1].Convert<AssignNode>();
|
2024-08-17 17:50:28 +08:00
|
|
|
|
Assert.Equal("flag", assignNode.Variable.Identifier.LiteralValue);
|
2024-08-15 16:18:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void CompoundStatementTest2()
|
|
|
|
|
{
|
|
|
|
|
BlockNode node = RunParser<BlockNode>(GrammarParser.CompoundStatementParser(), "begin end");
|
|
|
|
|
Assert.Empty(node.Statements);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ProgramHeadTest1()
|
|
|
|
|
{
|
|
|
|
|
ProgramHead head = RunParser<ProgramHead>(GrammarParser.ProgramHeadParser(), "program main");
|
|
|
|
|
|
|
|
|
|
Assert.Equal("main", head.ProgramName.LiteralValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData("""
|
|
|
|
|
begin
|
|
|
|
|
temp := 1 + 1;
|
|
|
|
|
flag := true and false;
|
|
|
|
|
end
|
|
|
|
|
""")]
|
|
|
|
|
[InlineData("""
|
|
|
|
|
begin
|
|
|
|
|
temp := 1 + 1;
|
|
|
|
|
flag := true and false
|
|
|
|
|
end
|
|
|
|
|
""")]
|
|
|
|
|
public void ProgramBodyTest1(string input)
|
|
|
|
|
{
|
|
|
|
|
BlockNode node = RunParser<ProgramBody>(GrammarParser.ProgramBodyParser(), input).MainBlock;
|
|
|
|
|
|
|
|
|
|
Assert.Equal(2, node.Statements.Count);
|
|
|
|
|
AssignNode assignNode = node.Statements[0].Convert<AssignNode>();
|
2024-08-17 17:50:28 +08:00
|
|
|
|
Assert.Equal("temp", assignNode.Variable.Identifier.LiteralValue);
|
2024-08-15 16:18:32 +08:00
|
|
|
|
|
|
|
|
|
assignNode = node.Statements[1].Convert<AssignNode>();
|
2024-08-17 17:50:28 +08:00
|
|
|
|
Assert.Equal("flag", assignNode.Variable.Identifier.LiteralValue);
|
2024-08-15 16:18:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|