public void Visit(GridAssignmentStatementNode node) { ValueAstEvaluator valueVisitor = new ValueAstEvaluator(sender); Cell c = valueVisitor.Visit(node.GridPoint); // Extract result Symbol state = Stbl.st.Retrieve(node.Identifier.Label); if (state is StateSymbol s) { sender.SetCell(c, s); } else { throw new TheLanguageErrorException($"Grid assignment statement. Unexpected type for \'{ node.Identifier.Label }\' got { state } expected State"); } }
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); }
public void Visit(MemberAssignmentStatementNode node) { ValueAstEvaluator valueEvaluator = new ValueAstEvaluator(sender); Cell cell; // Get cell if (node.GridPoint != null) { cell = valueEvaluator.Visit(node.GridPoint); } else { cell = sender.GetCurrentCell(); } // Evaluate expression or string object result; if (node.Value is ExpressionNode exprNode) { if (exprNode is ComparisonNode) { result = new ComparisonExpressionAstEvaluator().Visit(exprNode); } else { result = new MathExpressionAstEvaluator().Visit(exprNode); } } else { result = ((StringValueNode)node.Value).Value; } // Retrieve state member for the next cell Cell nextCell = sender.GetNextCell(cell.Pos); MemberSymbol member = nextCell.State.RetrieveMember(node.MemberID.Label); if (member != null) { try { // Set new value switch (result) { case int i: member.SetValue(i); break; case string s: member.SetValue(s); break; default: throw new TheLanguageErrorException($"Cannot assign value of type \'{ result.GetType() }\' to a member"); } } catch (TheLanguageErrorException e) { throw new TheLanguageErrorException($"Member .{ member.Label }", e); } } else { throw new TheLanguageErrorException($"Unknown member \'{ node.MemberID.Label }\'"); } }