add: constant parser and variable parser.
This commit is contained in:
62
CanonSharp.Tests/ParserTests/ConstDeclarationTests.cs
Normal file
62
CanonSharp.Tests/ParserTests/ConstDeclarationTests.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using CanonSharp.Pascal.Parser;
|
||||
using CanonSharp.Pascal.SyntaxTree;
|
||||
using CanonSharp.Tests.Utils;
|
||||
|
||||
namespace CanonSharp.Tests.ParserTests;
|
||||
|
||||
public class ConstDeclarationTests : GrammarParserTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void BooleanConstValueTest()
|
||||
{
|
||||
BooleanValueNode node = RunParser<BooleanValueNode>(GrammarParser.ConstValueParser(), "true");
|
||||
Assert.True(node.Value);
|
||||
|
||||
node = RunParser<BooleanValueNode>(GrammarParser.ConstValueParser(), "false");
|
||||
Assert.False(node.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CharConstValueTest()
|
||||
{
|
||||
CharValueNode node = RunParser<CharValueNode>(GrammarParser.ConstValueParser(), "'a'");
|
||||
Assert.Equal('a', node.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NumberConstValueTest()
|
||||
{
|
||||
IntegerValueNode integerValueNode = RunParser<IntegerValueNode>(GrammarParser.ConstValueParser(), "250");
|
||||
Assert.Equal(250, integerValueNode.Value);
|
||||
|
||||
FloatValueNode floatValueNode = RunParser<FloatValueNode>(GrammarParser.ConstValueParser(), "100.5");
|
||||
Assert.Equal(100.5, floatValueNode.Value);
|
||||
|
||||
UnaryOperatorNode node = RunParser<UnaryOperatorNode>(GrammarParser.ConstValueParser(), "+250");
|
||||
Assert.Equal(UnaryOperatorType.Plus, node.OperatorType);
|
||||
node = RunParser<UnaryOperatorNode>(GrammarParser.ConstValueParser(), "-100.5");
|
||||
Assert.Equal(UnaryOperatorType.Minus, node.OperatorType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstDeclarationTest()
|
||||
{
|
||||
ConstantNode node = RunParser<ConstantNode>(GrammarParser.ConstDeclarationParser(), "a = true");
|
||||
|
||||
Assert.Equal("a", node.Identifier.LiteralValue);
|
||||
Assert.True(node.Value.Convert<BooleanValueNode>().Value);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("", 0)]
|
||||
[InlineData("const INFINITE = 100;", 1)]
|
||||
[InlineData("const a = true; b = false", 2)]
|
||||
[InlineData("const code1 = 100.4;code2 = 'a'", 2)]
|
||||
public void ConstDeclarationsCountTest(string input, int count)
|
||||
{
|
||||
BlockNode blockNode = RunParser<BlockNode>(GrammarParser.ConstDeclarationsParser(), input);
|
||||
List<ConstantNode> constantNodes = blockNode.Statements.Select(node => node.Convert<ConstantNode>()).ToList();
|
||||
|
||||
Assert.Equal(count, constantNodes.Count);
|
||||
}
|
||||
}
|
@@ -38,4 +38,32 @@ public class ProgramParserTests : GrammarParserTestBase
|
||||
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.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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user