Beispiel #1
0
 public object Process(Parser parser, SortedDictionary <string, object> parameters)
 {
     if (parser.CurrenToken.Type == TokenType.KwCase)
     {
         parser.NextToken();
         var _evaluation = (ExpressionNode) new AssignmentExpression().Process(parser, parameters);
         if (parser.CurrenToken.Type == TokenType.PmColon)
         {
             parser.NextToken();
             var _case = new CaseNode {
                 EvaluationNode = _evaluation
             };
             var _caseCode = (List <StatementNode>) new StatementList().Process(parser, parameters);
             _case.CodeNode = _caseCode;
             return(_case);
         }
         else
         {
             throw new ParserException("This was expected : in the case clause, Received: [" +
                                       parser.CurrenToken.LexemeVal + "], Row: " + parser.CurrenToken.Row
                                       + ", Column: " + parser.CurrenToken.Column);
         }
     }
     else
     {
         throw new ParserException("This was expected the tokent: case, Received: [" +
                                   parser.CurrenToken.LexemeVal + "], Row: " + parser.CurrenToken.Row
                                   + ", Column: " + parser.CurrenToken.Column);
     }
 }
Beispiel #2
0
 public override void Visit(CaseNode node)
 {
     StringBuilder.Append($"case {node.CaseName}");
     StringBuilder.Append(" {");
     node.CaseActionNode.Accept(this);
     StringBuilder.Append("}");
 }
Beispiel #3
0
 public override void Visit(CaseNode node)
 {
     if (node.WhenThenExpressions.Length == 0)
     {
         ReportLackOfWhenThenExpression(node);
     }
 }
Beispiel #4
0
 /// <summary>
 ///     Visit Case node in BFS manner.
 /// </summary>
 /// <param name="node">Case node that will be visited.</param>
 public void Visit(CaseNode node)
 {
     node.Accept(_visitor);
     foreach (var item in node.Descendants)
     {
         item.Accept(this);
     }
 }
 public void Visit(CaseNode node)
 {
     IndentedAppendLine($"case {node.SwitchName}.{node.CaseName}:");
     indentLevel++;
     node.CaseActionNode.Accept(this);
     IndentedAppendLine("break;");
     indentLevel--;
 }
 public override void Visit(CaseNode node)
 {
     if (_fields.Select(f => f.Expression.ToString()).Contains(node.ToString()))
     {
         Nodes.Push(new AccessColumnNode(node.ToString(), string.Empty, node.ReturnType, TextSpan.Empty));
     }
     else
     {
         base.Visit(node);
     }
 }
        public void Visit(CaseNode node)
        {
            node.Else.Accept(this);

            for (int i = node.WhenThenPairs.Length - 1; i >= 0; --i)
            {
                node.WhenThenPairs[i].When.Accept(this);
                node.WhenThenPairs[i].Then.Accept(this);
            }

            node.Accept(_visitor);
        }
Beispiel #8
0
        private Node parseCase()
        {
            Token token = expect(typeof(CaseToken))
            ;
            CaseToken caseToken = (CaseToken)token;
            CaseNode  node      = new CaseNode();

            node.setLineNumber(caseToken.getLineNumber());
            node.setFileName(filename);
            node.setValue(caseToken.getValue());
            node.setConditions(whenBlock());
            return(node);
        }
        public void Visit(CaseNode node)
        {
            var whenThenPairs = new List <(Node When, Node Then)>();

            for (int i = 0; i < node.WhenThenPairs.Length; ++i)
            {
                var then = Nodes.Pop();
                var when = Nodes.Pop();
                whenThenPairs.Add((when, then));
            }

            var elseNode = Nodes.Pop();

            Nodes.Push(new CaseNode(whenThenPairs.ToArray(), elseNode, elseNode.ReturnType));
        }
        public void CheckSemantic(CaseNode node, IScope scope = null)
        {
            CheckSemantic(node.Expression, scope);

            IType type = null;

            foreach (var caseOptionNode in node.CaseOptionNodes)
            {
                CheckSemantic(caseOptionNode, scope);

                type = type is null ?
                       caseOptionNode.ComputedType :
                       CoolType.Lca((CoolType)type, (CoolType)caseOptionNode.ComputedType);
            }
            node.ComputedType = type;
        }
        public void Visit(CaseNode node)
        {
            var oldMethodAccessType = _visitor.SetMethodAccessType(MethodAccessType.ResultQuery);

            node.Else.Accept(this);

            for (int i = node.WhenThenPairs.Length - 1; i >= 0; --i)
            {
                node.WhenThenPairs[i].When.Accept(this);
                node.WhenThenPairs[i].Then.Accept(this);
            }

            node.Accept(_visitor);

            _visitor.SetMethodAccessType(oldMethodAccessType);
        }
Beispiel #12
0
        public override ASTNode VisitCase([NotNull] CoolParser.CaseContext context)
        {
            CaseNode node = new CaseNode(context)
            {
                ExpressionCase = Visit(context.expression(0)) as ExpressionNode
            };

            var formals     = context.formal().Select(x => Visit(x)).ToList();
            var expressions = context.expression().Skip(1).Select(x => Visit(x)).ToList();

            for (int i = 0; i < formals.Count; ++i)
            {
                node.Branches.Add((formals[i] as FormalNode, expressions[i] as ExpressionNode));
            }

            return(node);
        }
Beispiel #13
0
        public void Visit(CaseNode node)
        {
            node.CaseBase.Accept(this);

            int branchSelected = -1;
            var typeExp0       = node.CaseBase.staticType;
            var typeExpK       = scope.GetType(node.Branchs[0].formal.type.Text);

            for (int i = 0; i < node.Branchs.Count; ++i)
            {
                if (!scope.IsDefinedType(node.Branchs[i].formal.type.Text, out TypeInfo type))
                {
                    errors.Add(ErrorSemantic.NotDeclaredType(node.Branchs[i].formal.type));
                }

                var typeK = scope.GetType(node.Branchs[i].formal.type.Text);

                var scopeBranch = scope.CreateChild();
                scopeBranch.Define(node.Branchs[i].formal.id.text, typeK);

                node.Branchs[i].expr.Accept(new SemanticVisit(errors, scopeBranch));

                typeExpK = node.Branchs[i].expr.staticType;

                if (branchSelected == -1 && typeExp0 <= typeK)
                {
                    branchSelected = i;
                }

                if (i == 0)
                {
                    node.staticType = node.Branchs[0].expr.staticType;
                }
                node.staticType = SemanticAlgorithm.LowerCommonAncestor(node.staticType, typeExpK);
            }
            node.Select = branchSelected;

            if (node.Select == -1)
            {
                errors.Add(ErrorSemantic.NotMatchedBranch(node));
            }
        }
Beispiel #14
0
        public void Visit(CaseNode node)
        {
            node.ExpressionCase.Accept(this);

            int branchSelected = -1;
            var typeExp0       = node.ExpressionCase.StaticType;
            var typeExpK       = scope.GetType(node.Branches[0].Formal.Type.Text);

            for (int i = 0; i < node.Branches.Count; ++i)
            {
                if (!scope.IsDefinedType(node.Branches[i].Formal.Type.Text, out TypeInfo type))
                {
                    errors.Add(SemanticError.NotDeclaredType(node.Branches[i].Formal.Type));
                }

                var typeK = scope.GetType(node.Branches[i].Formal.Type.Text);

                var scopeBranch = scope.CreateChild();
                scopeBranch.Define(node.Branches[i].Formal.Id.Text, typeK);

                node.Branches[i].Expression.Accept(new Tour2(scopeBranch, errors));

                typeExpK = node.Branches[i].Expression.StaticType;

                if (branchSelected == -1 && typeExp0 <= typeK)
                {
                    branchSelected = i;
                }

                if (i == 0)
                {
                    node.StaticType = node.Branches[0].Expression.StaticType;
                }
                node.StaticType = Algorithm.LowerCommonAncestor(node.StaticType, typeExpK);
            }
            node.BranchSelected = branchSelected;

            if (node.BranchSelected == -1)
            {
                errors.Add(SemanticError.NotMatchedBranch(node));
            }
        }
        public override ASTNode VisitCase([NotNull] CoolParser.CaseContext context)
        {
            var caseNode = new CaseNode(context.Start.Line, context.Start.Column)
            {
                Cases = new List <AttributeNode>()
            };

            caseNode.Sentence = Visit(context.expr(0)) as ExpressionNode;

            var formals = (from f in context.formal() select Visit(f) as FormalNode).ToList();
            var dexprs  = (from de in context.expr().Skip(1) select Visit(de) as ExpressionNode).ToList();

            for (int i = 0; i < formals.Count; i++)
            {
                caseNode.Cases.Add(
                    new AttributeNode(formals[i].Line, formals[i].Column)
                {
                    Formal = formals[i],
                    DessignateExpression = dexprs[i]
                });
            }

            return(caseNode);
        }
 public void Visit(CaseNode node)
 {
 }
Beispiel #17
0
 /// <summary>
 ///     Visit Case node.
 /// </summary>
 /// <param name="node">Case node of AST</param>
 public abstract void Visit(CaseNode node);
Beispiel #18
0
 public static string NotMatchedBranch(CaseNode node)
 {
     return($"(line: {node.line}, column: {node.column})" +
            $" Ningun case ha sido matcheado."
            );
 }
 public static string NotMatchedBranch(CaseNode node)
 {
     return($"({node.Line}, {node.Column}) - Semantic Error:" +
            $" At least one branch must be matched."
            );
 }
Beispiel #20
0
 private void ReportLackOfWhenThenExpression(CaseNode node)
 {
     AddSyntaxError(node.FullSpan, string.Format(AnalysisMessage.LackOfWhenThenExpression, node),
                    SyntaxErrorKind.LackOfExpression);
 }
 public void AddItemToBoard(CaseNode cn)
 {
     nodes.Add(cn);
     Instantiate(cn);
 }
Beispiel #22
0
 public virtual void Visit(CaseNode node)
 {
 }
Beispiel #23
0
 public void Visit(CaseNode node)
 {
     throw new NotImplementedException();
 }
Beispiel #24
0
        public void Visit(CaseNode node)
        {
            string static_type = node.ExpressionCase.StaticType.Text;

            int result = VariableManager.PeekVariableCounter();
            int expr   = VariableManager.IncrementVariableCounter();

            VariableManager.PushVariableCounter();
            node.ExpressionCase.Accept(this);
            VariableManager.PopVariableCounter();

            //VariableManager.IncrementVariableCounter();

            if (static_type == "String" ||
                static_type == "Int" ||
                static_type == "Bool")
            {
                int    index = node.Branches.FindIndex((x) => x.Formal.Type.Text == static_type);
                string v     = node.Branches[index].Formal.Id.Text;

                VariableManager.PushVariable(v, node.Branches[index].Formal.Type.Text);// v <- expr

                int t = VariableManager.IncrementVariableCounter();
                VariableManager.PushVariableCounter();

                node.Branches[index].Expression.Accept(this);

                VariableManager.PopVariableCounter();

                VariableManager.PopVariable(v);

                IC.Add(new AssignmentVariableToVariable(VariableManager.PeekVariableCounter(), t));
            }
            else
            {
                string tag = IC.Count.ToString();

                List <(FormalNode Formal, ExpressionNode Expression)> sorted = new List <(FormalNode Formal, ExpressionNode Expression)>();
                sorted.AddRange(node.Branches);
                sorted.Sort((x, y) => (Scope.GetType(x.Formal.Type.Text) <= Scope.GetType(y.Formal.Type.Text) ? -1 : 1));

                for (int i = 0; i < sorted.Count; ++i)
                {
                    //same that expr integer
                    VariableManager.PushVariable(sorted[i].Formal.Id.Text, sorted[i].Formal.Type.Text);

                    string branch_type = sorted[i].Formal.Type.Text;
                    VariableManager.PushVariableCounter();
                    VariableManager.IncrementVariableCounter();

                    IC.Add(new LabelLine("_case", tag + "." + i));
                    IC.Add(new AssignmentStringToVariable(VariableManager.VariableCounter, branch_type));
                    IC.Add(new BinaryOperation(VariableManager.VariableCounter, expr, VariableManager.VariableCounter, "inherit"));
                    IC.Add(new ConditionalJump(VariableManager.VariableCounter, new LabelLine("_case", tag + "." + (i + 1))));


                    if ((branch_type == "Int" ||
                         branch_type == "Bool" ||
                         branch_type == "String"))
                    {
                        if (static_type == "Object")
                        {
                            IC.Add(new AssignmentMemoryToVariable(expr, expr, VirtualTable.GetSizeClass(branch_type)));

                            VariableManager.PushVariableCounter();
                            sorted[i].Expression.Accept(this);
                            VariableManager.PopVariableCounter();

                            IC.Add(new AssignmentVariableToVariable(result, VariableManager.PeekVariableCounter()));
                            IC.Add(new GotoJump(new LabelLine("_endcase", tag)));
                        }
                    }
                    else
                    {
                        VariableManager.PushVariableCounter();
                        sorted[i].Expression.Accept(this);
                        VariableManager.PopVariableCounter();

                        IC.Add(new AssignmentVariableToVariable(result, VariableManager.PeekVariableCounter()));
                        IC.Add(new GotoJump(new LabelLine("_endcase", tag)));
                    }



                    VariableManager.PopVariableCounter();

                    VariableManager.PopVariable(sorted[i].Formal.Id.Text);
                }

                IC.Add(new LabelLine("_case", tag + "." + sorted.Count));
                IC.Add(new GotoJump(new LabelLine("_caseselectionexception")));

                IC.Add(new LabelLine("_endcase", tag));
            }
        }
        public void GenerateCode(CaseNode node, ICIL_CodeGenerator codeGenerator)
        {
            node.Holder = codeGenerator.DefineVariable();
            codeGenerator.AddLocalVariable(
                new CIL_LocalVariable((Variable)node.Holder));

            //genCode (expr0)
            GenerateCode(node.Expression, codeGenerator);

            var type = codeGenerator.DefineVariable();

            codeGenerator.AddLocalVariable(
                new CIL_LocalVariable(type));

            //type = TypeOF expr0.Holder
            codeGenerator.AddInstruction(
                new TypeOf(type, (Variable)node.Expression.Holder));

            var boolean = codeGenerator.DefineVariable();

            codeGenerator.AddLocalVariable(
                new CIL_LocalVariable(boolean));

            // boolean = type == -1 (is null)
            codeGenerator.AddInstruction(
                new CilEqualValue(boolean, type, -1));

            // If boolean is true throw exception
            codeGenerator.AddInstruction(
                new ConditionalJump(boolean, true, $"{CoolExceptionType.DispatchOnVoidException}"));

            #region 'Code BranchSelector'

            var loop = codeGenerator.DefineLabel();

            // Loop:
            codeGenerator.AddInstruction(
                new Label(loop));

            var end = codeGenerator.DefineLabel();

            foreach (var caseOption in node.CaseOptionNodes)
            {
                // boolean = x == type_k
                codeGenerator.AddInstruction(
                    new CilEqualValue(boolean, type, caseOption.TypeId));

                //Define next_label
                var nextBranch = codeGenerator.DefineLabel();

                //if !boolean Goto label
                codeGenerator.AddInstruction(
                    new ConditionalJump(boolean, false, nextBranch));

                //Define Variable id_k
                caseOption.Symbol.Holder = codeGenerator.DefineVariable();

                codeGenerator.AddLocalVariable(
                    new CIL_LocalVariable((Variable)caseOption.Symbol.Holder));

                // id_k <- expr0
                codeGenerator.AddInstruction(
                    new Assign((Variable)caseOption.Symbol.Holder, node.Expression.Holder));

                // genCode (expr_k)
                GenerateCode(caseOption.Expression, codeGenerator);

                // set value at return
                codeGenerator.AddInstruction(
                    new Assign((Variable)node.Holder, caseOption.Expression.Holder));

                // Goto END
                codeGenerator.AddInstruction(
                    new Goto(end));

                // Next branch :
                codeGenerator.AddInstruction(
                    new Label(nextBranch));
            }

            // Set Parent
            // type = Inherance_Tree[type] ó
            // type = GetIndex Inherance_Tree type
            codeGenerator.AddInstruction(
                new GetIndex(type, Constant.InheritanceTree, type));

            // boolean = type == -1
            codeGenerator.AddInstruction(
                new CilEqualValue(boolean, type, -1));

            // if boolean is true throw exception
            codeGenerator.AddInstruction(
                new ConditionalJump(boolean, true, $"{CoolExceptionType.ExecutionOfACaseStatementWithoutAMatchingBranchException}"));

            //Goto loop
            codeGenerator.AddInstruction(
                new Goto(loop));

            // Label END
            codeGenerator.AddInstruction(
                new Label(end));

            #endregion
        }
 public NodeExecutionContext(CaseNode node, TestContext parent, TestCaseInfo self, object testObject)
     : base(node, parent, self, testObject)
 {
 }
Beispiel #27
0
 public virtual XzaarExpression Visit(CaseNode @case)
 {
     return(null);
 }
Beispiel #28
0
 public override void Visit(CaseNode node) => ProduceDebuggerInstructions(node, n => base.Visit(n));
 public static string NotMatchedBranch(CaseNode node)
 {
     return($"(Line: {node.Line}, Column: {node.Column})" +
            $" At least one branch must be matched."
            );
 }
        public void Visit(CaseNode node)
        {
            var static_type = node.CaseBase.staticType.Text;
            int result      = VariableManager.PeekVariableCounter();
            int expr        = VariableManager.IncrementVariableCounter();

            VariableManager.PushVariableCounter();
            node.CaseBase.Accept(this);
            VariableManager.PopVariableCounter();

            if (static_type == "String" || static_type == "Int" || static_type == "Bool")
            {
                int    index = node.Branchs.FindIndex((x) => x.formal.type.Text == static_type);
                string v     = node.Branchs[index].formal.id.text;

                VariableManager.PushVariable(v, node.Branchs[index].formal.type.Text);

                int count = VariableManager.IncrementVariableCounter();

                VariableManager.PushVariableCounter();
                node.Branchs[index].expr.Accept(this);
                VariableManager.PopVariableCounter();

                VariableManager.PopVariable(v);

                Code.Add(new AssignVarToVarCodeLine(VariableManager.PeekVariableCounter(), count));
            }
            else
            {
                string tag = Code.Count.ToString();

                List <(FormalNode formal, ExprNode expr)> sorted = new List <(FormalNode formal, ExprNode expr)>();
                sorted.AddRange(node.Branchs);
                sorted.Sort((x, y) => (Scope.GetType(x.formal.type.Text) <= Scope.GetType(y.formal.type.Text) ? -1 : 1));

                for (int i = 0; i < sorted.Count; ++i)
                {
                    VariableManager.PushVariable(sorted[i].formal.id.text, sorted[i].formal.type.Text);

                    string branch_type = sorted[i].formal.type.Text;
                    VariableManager.PushVariableCounter();
                    VariableManager.IncrementVariableCounter();

                    Code.Add(new LabelCodeLine("_case", tag + "." + i));
                    Code.Add(new AssignStrToVarCodeLine(VariableManager.VariableCounter, branch_type));
                    Code.Add(new BinaryOpCodeLine(VariableManager.VariableCounter, expr, VariableManager.VariableCounter, "inherit"));
                    Code.Add(new CondJumpCodeLine(VariableManager.VariableCounter, new LabelCodeLine("_case", tag + "." + (i + 1))));

                    if ((branch_type == "Int" || branch_type == "Bool" || branch_type == "String"))
                    {
                        if (static_type == "Object")
                        {
                            Code.Add(new AssignMemToVarCodeLine(expr, expr, ClassManager.GetClassSize(branch_type)));

                            VariableManager.PushVariableCounter();
                            sorted[i].expr.Accept(this);
                            VariableManager.PopVariableCounter();

                            Code.Add(new AssignVarToVarCodeLine(result, VariableManager.PeekVariableCounter()));
                            Code.Add(new GotoCodeLine(new LabelCodeLine("_endcase", tag)));
                        }
                    }
                    else
                    {
                        VariableManager.PushVariableCounter();
                        sorted[i].expr.Accept(this);
                        VariableManager.PopVariableCounter();

                        Code.Add(new AssignVarToVarCodeLine(result, VariableManager.PeekVariableCounter()));
                        Code.Add(new GotoCodeLine(new LabelCodeLine("_endcase", tag)));
                    }
                    VariableManager.PopVariableCounter();
                    VariableManager.PopVariable(sorted[i].formal.id.text);
                }

                Code.Add(new LabelCodeLine("_case", tag + "." + sorted.Count));
                Code.Add(new GotoCodeLine(new LabelCodeLine("_caseselectionexception")));
                Code.Add(new LabelCodeLine("_endcase", tag));
            }
        }