Skip to content

Commit

Permalink
Multiple match conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
elamaunt committed Jun 4, 2021
1 parent 9f93d8c commit ef9516b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
23 changes: 16 additions & 7 deletions src/GDShrapt.Reader.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,14 @@ public void MatchStatementTest()
Assert.AreEqual("x", matchStatement.Value.ToString());
Assert.AreEqual(3, matchStatement.Cases.Count);

Assert.AreEqual("1", matchStatement.Cases[0].Condition.ToString());
Assert.AreEqual("2", matchStatement.Cases[1].Condition.ToString());
Assert.AreEqual("\"test\"", matchStatement.Cases[2].Condition.ToString());
Assert.AreEqual(1, matchStatement.Cases[0].Conditions.Count);
Assert.AreEqual("1", matchStatement.Cases[0].Conditions[0].ToString());

Assert.AreEqual(1, matchStatement.Cases[1].Conditions.Count);
Assert.AreEqual("2", matchStatement.Cases[1].Conditions[0].ToString());

Assert.AreEqual(1, matchStatement.Cases[2].Conditions.Count);
Assert.AreEqual("\"test\"", matchStatement.Cases[2].Conditions[0].ToString());

Assert.AreEqual(1, matchStatement.Cases[0].Statements.Count);
Assert.AreEqual(1, matchStatement.Cases[1].Statements.Count);
Expand Down Expand Up @@ -436,11 +441,15 @@ public void MatchStatementTest2()
Assert.AreEqual("x", matchStatement.Value.ToString());
Assert.AreEqual(3, matchStatement.Cases.Count);

Assert.AreEqual("1", matchStatement.Cases[0].Condition.ToString());
Assert.AreEqual("2", matchStatement.Cases[1].Condition.ToString());
Assert.AreEqual(1, matchStatement.Cases[0].Conditions.Count);
Assert.AreEqual("1", matchStatement.Cases[0].Conditions[0].ToString());

Assert.AreEqual(1, matchStatement.Cases[1].Conditions.Count);
Assert.AreEqual("2", matchStatement.Cases[1].Conditions[0].ToString());

Assert.IsInstanceOfType(matchStatement.Cases[2].Condition, typeof(GDVariableDeclarationExpression));
Assert.AreEqual("var new_var", matchStatement.Cases[2].Condition.ToString());
Assert.AreEqual(1, matchStatement.Cases[2].Conditions.Count);
Assert.IsInstanceOfType(matchStatement.Cases[2].Conditions[0], typeof(GDVariableDeclarationExpression));
Assert.AreEqual("var new_var", matchStatement.Cases[2].Conditions[0].ToString());

Assert.AreEqual(1, matchStatement.Cases[0].Statements.Count);
Assert.AreEqual(1, matchStatement.Cases[1].Statements.Count);
Expand Down
19 changes: 7 additions & 12 deletions src/GDShrapt.Reader/Declarations/GDMatchCaseDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class GDMatchCaseDeclaration : GDNode
bool _expressionEnded;
bool _statementsChecked;

public GDExpression Condition { get; set; }
public List<GDExpression> Conditions { get; } = new List<GDExpression>();
public List<GDStatement> Statements { get; } = new List<GDStatement>();

internal GDMatchCaseDeclaration(int lineIntendation)
Expand All @@ -26,21 +26,16 @@ internal override void HandleChar(char c, GDReadingState state)
if (IsSpace(c))
return;

if (Condition == null)
if (c == ':' || _expressionEnded)
{
state.PushNode(new GDExpressionResolver(expr => Condition = expr));
state.PassChar(c);
_expressionEnded = true;
return;
}

if (!_expressionEnded)
{
if (c != ':')
return;
state.PushNode(new GDExpressionResolver(expr => Conditions.Add(expr)));

_expressionEnded = true;
return;
}
if (c != ',')
state.PassChar(c);
}

internal override void HandleLineFinish(GDReadingState state)
Expand All @@ -58,7 +53,7 @@ internal override void HandleLineFinish(GDReadingState state)

public override string ToString()
{
return $@"{Condition}:
return $@"{string.Join(", ", Conditions.Select(x => x.ToString()))}:
{string.Join("\n\t", Statements.Select(x => x.ToString()))}";
}
}
Expand Down

0 comments on commit ef9516b

Please sign in to comment.