Пример #1
0
        public override void Visit(BoolComparisonNode node)
        {
            //VisitChildren(node);
            _symbolTable.SetCurrentNode(node);
            if (node.Left != null && node.Right != null)
            {
                // Check if the nodes are boolcomparisons
                if (node.Left is BoolComparisonNode && node.Right is BoolComparisonNode)
                {
                    node.Left.Accept(this);
                    node.LeftType = node.Left.Type_enum;
                    node.Right.Accept(this);
                    node.RightType = node.Right.Type_enum;
                }
                if (node.HasChildren)
                {
                    // Extract the type from Left and right sides of a bool comparison

                    if (node.Left is ExpressionNode)
                    {
                        node.LeftType = ((ExpressionNode)node.Left).OverAllType ?? default(AllType);
                    }
                    else if (node.Left.Children[0] is PredicateNode)
                    {
                        node.LeftType = _symbolTable.RetrieveSymbol(node.Left.Children[0].Name) ?? default(AllType);
                    }
                    else if (node.Left is BoolComparisonNode)
                    {
                        node.LeftType = ((ExpressionNode)node.Left.Children[0]).OverAllType ?? default(AllType);
                    }

                    if (node.Right is ExpressionNode)
                    {
                        node.RightType = ((ExpressionNode)node.Right).OverAllType ?? default(AllType);
                    }
                    else if (node.Right.Children[0] is PredicateNode)
                    {
                        node.RightType = _symbolTable.RetrieveSymbol(node.Right.Children[0].Name) ?? default(AllType);
                    }
                    else if (node.Right is BoolComparisonNode)
                    {
                        node.RightType = ((ExpressionNode)node.Right.Children[0]).OverAllType ?? default(AllType);
                    }
                }
                if (node.RightType != AllType.UNKNOWNTYPE && node.LeftType != AllType.UNKNOWNTYPE)
                {
                    if (node.RightType != node.LeftType)
                    {
                        if (!((node.RightType == AllType.INT && node.LeftType == AllType.DECIMAL) || (node.RightType == AllType.DECIMAL && node.LeftType == AllType.INT)))
                        {
                            _symbolTable.WrongTypeConditionError();
                        }
                    }
                }
            }
            else
            {
                VisitChildren(node);
            }
        }
Пример #2
0
        public override AbstractNode VisitSimpleBoolCompOrExp([NotNull] GiraphParser.SimpleBoolCompOrExpContext context)
        {
            BoolComparisonNode BCompare = new BoolComparisonNode(context.Start.Line, context.Start.Column);

            BCompare.Type = "bool";
            // Checks if there is a prefix, if there is, add it to the Node
            if (context.simpleExpression() != null)
            {
                AbstractNode output = Visit(context.simpleExpression());
                if (output is ExpressionNode expNode && context.rightP != null && context.leftP != null)
                {
                    expNode.hasparentheses = true;
                }
                return(output);
            }
            if (context.prefix != null)
            {
                BCompare.Prefix = context.prefix.Text;
                BCompare.AdoptChildren(Visit(context.simpleBoolCompOrExp(0)));
            }
            // Check if there are left and right "()" around the boolcomparison
            if (context.rightP != null && context.leftP != null && context.simpleBoolCompOrExp() != null)
            {
                BCompare.InsideParentheses = true;
                BCompare.AdoptChildren(Visit(context.simpleBoolCompOrExp(0)));
            }
            // Checks if there is a left and right statement, because this will indicatef that the boolcomparison, has a left bool and right bool, compared by the operator.
            else if (context.right != null && context.left != null && context.simpleBoolCompOrExp() != null)
            {
                BCompare.Left         = Visit(context.left);
                BCompare.Right        = Visit(context.right);
                BCompare.Left.Parent  = BCompare;
                BCompare.Right.Parent = BCompare;

                if (context.BOOLOPERATOR() != null)
                {
                    BCompare.ComparisonOperator = context.BOOLOPERATOR().GetText();
                }
                else if (context.andOr() != null)
                {
                    BCompare.ComparisonOperator = context.andOr().GetText();
                }
            }
            // A boolcomparison can end in an expression or a predicate, this is handled here.
            else
            {
                if (context.predi != null)
                {
                    BCompare.AdoptChildren(Visit(context.predi));
                }
                else if (context.exp != null)
                {
                    BCompare.AdoptChildren(Visit(context.exp));
                }
            }
            return(BCompare);
        }
        public override void Visit(BoolComparisonNode node)
        {
            SymbolTable.SetCurrentNode(node);

            if (node.Left != null && node.Right != null)
            {
                node.Left.Accept(this);
                node.Right.Accept(this);
            }
            else
            {
                VisitChildren(node);
            }
        }
Пример #4
0
        public override AbstractNode VisitForLoop([NotNull] GiraphParser.ForLoopContext context)
        {
            ForLoopNode        ForLoop        = new ForLoopNode(context.Start.Line, context.Start.Column);
            var                contextInside  = context.forCondition().forConditionInside();
            BoolComparisonNode boolComparison = new BoolComparisonNode(context.Start.Line, context.Start.Column);

            boolComparison.ComparisonOperator = "<";
            if (contextInside.forConditionStart().forConditionDcl() != null)
            {
                ForLoop.VariableDeclaration = Visit(contextInside.forConditionStart().forConditionDcl());
                VariableNode varNode = new VariableNode(context.Start.Line, context.Start.Column);
                varNode.Name             = (ForLoop.VariableDeclaration as VariableDclNode).Name;
                varNode.Type             = (ForLoop.VariableDeclaration as VariableDclNode).Type;
                boolComparison.Left      = varNode;
                ForLoop.ToValueOperation = boolComparison;
            }
            else if (contextInside.forConditionStart().expression() != null)
            {
                ForLoop.FromValueNode    = Visit(contextInside.forConditionStart().expression());
                ForLoop.ToValueOperation = Visit(contextInside.expression(0));
            }

            boolComparison.Right = Visit(contextInside.expression(0));
            if (contextInside.expression(1) != null)
            {
                ForLoop.Increment = Visit(contextInside.expression(1));
            }
            else
            {
                ExpressionNode expNode = new ExpressionNode(context.Start.Line, context.Start.Column);
                ConstantNode   conNode = new ConstantNode(context.Start.Line, context.Start.Column);
                conNode.Type  = "int";
                conNode.Value = "1";
                expNode.ExpressionParts.Add(conNode);

                ForLoop.Increment = expNode;
                //ForLoop.Increment
            }

            // Visit all the children of the Codeblock associated with the ForLoop
            foreach (var Child in context.codeBlock().codeBlockContent())
            {
                // Adopt the children
                ForLoop.AdoptChildren(Visit(Child.GetChild(0)));
            }
            return(ForLoop);
        }
 public override void Visit(BoolComparisonNode node)
 {
     Debug.Print("BoolComparisonNode");
     if (node.HasChildren)
     {
         VisitChildren(node);
     }
     else
     {
         if (node.Left != null)
         {
             node.Left.Accept(this);
             ProgramCode.Append($" {node.ComparisonOperator} ");
             node.Right.Accept(this);
         }
     }
 }
Пример #6
0
 public abstract void Visit(BoolComparisonNode node);