Esempio n. 1
0
        // Declaration
        public override TypeNode Visit(DeclarationNode node, object obj)
        {
            Symbol duplicate = SymbolTable.RetrieveSymbol(node.Id.Text);

            // If declaration has assignment, get the type of the assignment
            if (node.AssignmentExpression != null)
            {
                TypeNode assignmentType = Visit(node.AssignmentExpression, node.Type);

                // If assignment type is not the same type as the declaration, log error
                if (assignmentType != node.Type)
                {
                    ErrorLogger.LogError(new IncompatibleTypesError(assignmentType, node.Type, node.AssignmentExpression.SourcePosition));
                    return(new ErrorTypeNode(node.SourcePosition));
                }
            }

            // Enter in symbol table if not a duplicate, else log error
            if (duplicate == null)
            {
                SymbolTable.EnterSymbol(node.Id.Text, node.Type);
            }
            else
            {
                ErrorLogger.LogError(new VariableAlreadyDeclaredError(node.Id.Text, node.Id.SourcePosition));
                node.Type = new ErrorTypeNode(node.SourcePosition);
            }
            return(node.Type);
        }
        // Declaration
        public override ASTNode VisitDclStmt(CLUBSParser.DclStmtContext context)
        {
            SourcePosition sourcePosition = new SourcePosition(context.start);

            DeclarationNode node;
            TypeNode        type = GetTypeNode(context.type().kind.Type, sourcePosition);
            IdentifierNode  id   = new IdentifierNode(context.id.Text, new SourcePosition(context.id));

            // Check if declaration is a set.
            if (context.set != null)
            {
                // If set, type should be wrapped in a set type.
                node = new DeclarationNode(new SetTypeNode(type, sourcePosition), id, sourcePosition);
            }
            else
            {
                // Else, type should be as is.
                node = new DeclarationNode(type, id, sourcePosition);
            }

            // Visit assignment if has any
            if (context.assignment != null)
            {
                node.AssignmentExpression = Visit(context.assignment) as ExpressionNode;
            }

            return(node);
        }
Esempio n. 3
0
        // Declaration
        public override string Visit(DeclarationNode node, object obj)
        {
            StringBuilder builder = new StringBuilder();

            // Append declaration of variable with a type-based default initialization
            builder.Append($"{Visit(node.Type)} {node.Id} = {node.Type.GetInitializationString(node.Id.Text)}");

            // If declaration has assignment, append new statement with assignment to the newly declared variable
            if (node.AssignmentExpression != null)
            {
                builder.Append($"{node.Id} = {Visit(node.AssignmentExpression, node.Id)};\n");
            }

            SymbolTable.EnterSymbol(node.Id.Text, node.Type);

            return(builder.ToString());
        }
Esempio n. 4
0
 public abstract T Visit(DeclarationNode node, object obj);