add: Array declaration and variable indexer.

This commit is contained in:
2024-08-17 17:50:28 +08:00
parent c7077bd0fd
commit 97f9fb9ec3
12 changed files with 200 additions and 30 deletions

View File

@@ -41,15 +41,52 @@ public sealed class ExpressionParserTests : GrammarParserTestBase
[Fact]
public void IdentifierTest()
{
VariableNode node = RunParser<VariableNode>(GrammarParser.FactorParser(), "temp");
Assert.Equal("temp", node.IdentifierName);
VariableNode node = RunParser<VariableNode>(GrammarParser.VariableParser(), "temp");
Assert.Equal("temp", node.Identifier.LiteralValue);
}
[Fact]
public void ArrayIdentifierTest()
{
VariableNode node = RunParser<VariableNode>(GrammarParser.VariableParser(), "a[0]");
Assert.Equal("a", node.Identifier.LiteralValue);
Assert.Single(node.Indexers);
Assert.Equal(0, node.Indexers.First().Convert<IntegerValueNode>().Value);
node = RunParser<VariableNode>(GrammarParser.VariableParser(), "a[i + 1]");
Assert.Equal("a", node.Identifier.LiteralValue);
Assert.Single(node.Indexers);
BinaryOperatorNode binaryOperatorNode = node.Indexers.First().Convert<BinaryOperatorNode>();
Assert.Equal(BinaryOperatorType.Add, binaryOperatorNode.OperatorType);
Assert.Equal("i", binaryOperatorNode.Left.Convert<VariableNode>().Identifier.LiteralValue);
Assert.Equal(1, binaryOperatorNode.Right.Convert<IntegerValueNode>().Value);
node = RunParser<VariableNode>(GrammarParser.VariableParser(), "a[b[i + 1] - 10, c]");
Assert.Equal("a", node.Identifier.LiteralValue);
Assert.Equal(2, node.Indexers.Count);
VariableNode secondNode = node.Indexers[1].Convert<VariableNode>();
Assert.Equal("c", secondNode.Identifier.LiteralValue);
Assert.Empty(secondNode.Indexers);
BinaryOperatorNode firstNode = node.Indexers[0].Convert<BinaryOperatorNode>();
Assert.Equal(BinaryOperatorType.Subtract, firstNode.OperatorType);
Assert.Equal(10, firstNode.Right.Convert<IntegerValueNode>().Value);
VariableNode variableNode = firstNode.Left.Convert<VariableNode>();
Assert.Equal("b", variableNode.Identifier.LiteralValue);
Assert.Single(variableNode.Indexers);
binaryOperatorNode = variableNode.Indexers[0].Convert<BinaryOperatorNode>();
Assert.Equal(BinaryOperatorType.Add, binaryOperatorNode.OperatorType);
Assert.Equal("i", binaryOperatorNode.Left.Convert<VariableNode>().Identifier.LiteralValue);
Assert.Equal(1, binaryOperatorNode.Right.Convert<IntegerValueNode>().Value);
}
[Fact]
public void SingleTermTest()
{
VariableNode node = RunParser<VariableNode>(GrammarParser.TermParser(), "temp");
Assert.Equal("temp", node.IdentifierName);
Assert.Equal("temp", node.Identifier.LiteralValue);
UnaryOperatorNode unaryOperatorNode = RunParser<UnaryOperatorNode>(GrammarParser.TermParser(), "- 123");
Assert.Equal(UnaryOperatorType.Minus, unaryOperatorNode.OperatorType);
@@ -89,7 +126,7 @@ public sealed class ExpressionParserTests : GrammarParserTestBase
Assert.Equal(3, node.Right.Convert<IntegerValueNode>().Value);
BinaryOperatorNode leftNode = node.Left.Convert<BinaryOperatorNode>();
Assert.Equal(BinaryOperatorType.Multiply, leftNode.OperatorType);
Assert.Equal("temp", leftNode.Left.Convert<VariableNode>().IdentifierName);
Assert.Equal("temp", leftNode.Left.Convert<VariableNode>().Identifier.LiteralValue);
Assert.Equal(2, leftNode.Right.Convert<IntegerValueNode>().Value);
}
@@ -97,7 +134,7 @@ public sealed class ExpressionParserTests : GrammarParserTestBase
public void SimpleExpressionTest1()
{
VariableNode node = RunParser<VariableNode>(GrammarParser.SimpleExpressionParser(), "temp");
Assert.Equal("temp", node.IdentifierName);
Assert.Equal("temp", node.Identifier.LiteralValue);
UnaryOperatorNode unaryOperatorNode =
RunParser<UnaryOperatorNode>(GrammarParser.SimpleExpressionParser(), "- 123");
@@ -238,8 +275,6 @@ public sealed class ExpressionParserTests : GrammarParserTestBase
public void VariableTest1()
{
VariableNode node = RunParser<VariableNode>(GrammarParser.VariableParser(), "temp");
Assert.Equal("temp", node.IdentifierName);
Assert.Equal("temp", node.Identifier.LiteralValue);
}
}