// Set value
        public override ASTNode VisitSetValueExpr(CLUBSParser.SetValueExprContext context)
        {
            SetValueNode node = new SetValueNode(new SourcePosition(context.start));

            // Visit all elements in the set and add them as Ids to the SetValueNode
            node.Ids = context.setElement().Select(id => Visit(id) as OrderedIdentifierNode).ToList();
            // Last element is added manually to get the correct order
            node.Ids.Add(new OrderedIdentifierNode(context.id.Text, Order.LAST, new SourcePosition(context.id)));

            return(node);
        }
        // Set value expression
        public override string Visit(SetValueNode 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");

            foreach (IdentifierNode id in node.Ids)
            {
                // If identifiers are not already declared, new instances will be declared
                if (SymbolTable.RetrieveSymbol(id.Text) == null)
                {
                    string typeString = Visit((node.Type as SetTypeNode).Type);
                    builder.Append($"{typeString} {id} = new {typeString}");

                    // Base types (Player, CardValue, Card) are declared with a name in constructor
                    TypeNode type = (node.Type as SetTypeNode).Type;
                    if (type is BaseTypeNode)
                    {
                        builder.Append($"(\"{id}\");\n");
                    }
                    else
                    {
                        builder.Append($"();\n");
                    }

                    SymbolTable.EnterSymbol(id.Text, type);
                }
            }

            int order = 0; // Number representing where the node is placed in the hierarchy

            foreach (OrderedIdentifierNode id in node.Ids)
            {
                builder.Append($"{id}.Order = {order};\n");
                builder.Append($"{id}.Parent = \"{setId}\";\n");
                builder.Append($"{setId}.Add({id});\n");
                if (id.Order == Order.LT)
                {
                    order++;
                }
            }
            return(builder.ToString());
        }
Пример #3
0
        // Set value expression
        public override TypeNode Visit(SetValueNode node, object obj)
        {
            TypeNode leftType;

            // Get the type of the node passed as parameter, must be Set type
            if (obj is SetTypeNode leftSet)
            {
                leftType = leftSet.Type; // Get the element type of the Set
            }
            else // If not Set type, log error
            {
                ErrorLogger.LogError(new ExpectedTypeError(node, StandardTypes.Set, node.SourcePosition));
                return(new ErrorTypeNode(node.SourcePosition));
            }

            foreach (IdentifierNode id in node.Ids)
            {
                Symbol symbol = SymbolTable.RetrieveSymbol(id.Text);

                // Declare the id if not already declared, else check whether type is compatible
                if (symbol == null)
                {
                    SymbolTable.EnterSymbol(id.Text, leftType);
                }
                else if (symbol.Type != leftType) // If compatible, log error
                {
                    ErrorLogger.LogError(new IncompatibleTypesError(symbol.Type, leftType, id.SourcePosition));
                    return(new ErrorTypeNode(node.SourcePosition));
                }
            }
            node.Type = new SetTypeNode(leftType, node.SourcePosition);

            // Save the element count in the Type for later use
            (node.Type as SetTypeNode).ElementCount = node.Ids.Count;
            (obj as SetTypeNode).ElementCount       = node.Ids.Count;
            return(node.Type);
        }
Пример #4
0
 public abstract T Visit(SetValueNode node, object obj);