示例#1
0
        // 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);
        }
        // Deck value expression
        public override string Visit(DeckValueNode node, object obj)
        {
            StringBuilder builder = new StringBuilder();

            // Get the name of the node passed as parameter
            IdentifierNode setId = obj as IdentifierNode;

            builder.Append($"{setId};\n");
            builder.Append($"{setId}.Clear();\n");

            int counter = 0;

            foreach (IdentifierNode id in node.Ids)
            {
                counter++;

                // The limit condition is retrieved from the type in the symbol table
                int    limit = (SymbolTable.RetrieveSymbol(id.Text).Type as SetTypeNode).ElementCount;
                string j     = $"j_{counter}";
                builder.Append($"for(int {j} = 0; {j} < {limit}; {j}++) {{\n ");
            }

            builder.Append($"Card _card = new Card();\n");

            counter = 0;

            foreach (IdentifierNode id in node.Ids)
            {
                counter++;

                string j = $"j_{counter}";
                builder.Append($"_card.CardValues.Add({id}[{j}]);\n");
            }
            builder.Append($"{setId}.Add(_card);\n");

            foreach (IdentifierNode id in node.Ids)
            {
                builder.Append("}\n");
            }

            return(builder.ToString());
        }
示例#3
0
 public abstract T Visit(DeckValueNode node, object obj);