// Deck value expression public override TypeNode Visit(DeckValueNode node, object obj) { SourcePosition sourcePosition = node.SourcePosition; SetTypeNode cardValueSet = StandardTypes.GetSetType(StandardTypes.CardValue); int count = 1; // Go through all identifiers in the deck for errors foreach (IdentifierNode id in node.Ids) { Symbol symbol = SymbolTable.RetrieveSymbol(id.Text); // If id has not been declared or the reference type is not Set OF CardValue, log error if (symbol == null) { ErrorLogger.LogError(new UndeclaredVariableError(id.Text, id.SourcePosition)); return(new ErrorTypeNode(node.SourcePosition)); } else if (symbol.Type != cardValueSet) { ErrorLogger.LogError(new ExpectedTypeError(node, cardValueSet, id.SourcePosition)); return(new ErrorTypeNode(node.SourcePosition)); } count *= (symbol.Type as SetTypeNode).ElementCount; // Get total number of cards in deck } node.Type = new SetTypeNode(new CardTypeNode(sourcePosition), sourcePosition); node.ElementCount = count; // Save the number of cards in the type for later use return(node.Type); }
// HELPER METHODS // Generic visitor for PUT actions private TypeNode VisitPutAction(PutActionNode node) { SetTypeNode deckType = StandardTypes.GetSetType(StandardTypes.Card); TypeNode sourceType = Visit(node.Source); TypeNode targetType = Visit(node.Target); // If the source type is not a Set OF Card, log error if (sourceType != deckType) { ErrorLogger.LogError(new ExpectedTypeError(deckType, node.Source.SourcePosition)); return(new ErrorTypeNode(node.SourcePosition)); } // If the target type is not a Set OF Card, log error if (targetType != deckType) { ErrorLogger.LogError(new ExpectedTypeError(deckType, node.Target.SourcePosition)); return(new ErrorTypeNode(node.SourcePosition)); } return(null); }
// Card value expression public override TypeNode Visit(CardValueExpressionNode node, object obj) { TypeNode parentType = Visit(node.Parent); TypeNode childType = Visit(node.Child); TypeNode CardValueSetType = StandardTypes.GetSetType(StandardTypes.CardValue); // If parent type is not Set OF CardValue, log error if (parentType != CardValueSetType) { ErrorLogger.LogError(new ExpectedTypeError(CardValueSetType, node.Parent.SourcePosition)); return(new ErrorTypeNode(node.SourcePosition)); } // If parent type is not CardValue, log error if (childType != StandardTypes.CardValue) { ErrorLogger.LogError(new ExpectedTypeError(StandardTypes.CardValue, node.Child.SourcePosition)); return(new ErrorTypeNode(node.SourcePosition)); } return(new CardTypeNode(node.SourcePosition)); }