public void Visit(MatchStatementNode node) { try { // Find the first caseStatement CaseStatementNode caseNode = GetFirstMatchingCase(node); // Visit the statement inside the matching caseStatement if (caseNode != null) { Visit(caseNode.Statement); } } catch (TheLanguageErrorException e) { throw new TheLanguageErrorException("Match statement", e); } }
private CaseStatementNode GetFirstMatchingCase(MatchStatementNode node) { ValueAstEvaluator valueVisitor = new ValueAstEvaluator(sender); List <ValueNode> values = new List <ValueNode>(); // Evaluate elements int i = 0; foreach (var element in node.Elements) { switch (element) { case IdentifierValueNode t: AddIdentifierElement(t, values); break; case IdentifierNode t: AddIdentifierElement(new IdentifierValueNode(t.Label), values); break; case GridValueNode t: Cell otherCell = valueVisitor.Visit(t); values.Add(new StateValueNode(otherCell.State)); break; case StringValueNode t: values.Add(t); break; case IntValueNode t: values.Add(t); break; case NumberNode t: values.Add(new IntValueNode(t.Value)); break; default: throw new Exception($"Case matching has yet to be implemented for MatchElement: [{ i }] { element.GetType() }"); } i++; } // Error handling from case matching i = 1; try { // Find first matching case foreach (CaseStatementNode c in node.CaseStatementNodes) { if (IsCaseMatching(c, values)) { return(c); } i++; } } catch (TheLanguageErrorException e) { throw new TheLanguageErrorException($"Case statement { i }", e); } return(null); }