Esempio n. 1
0
        public object VisitSwitchAST([NotNull] SwitchASTContext context)
        {
            var cases = context.@case().ToList();

            int firstLine = LineCount;

            cases.ForEach(@case =>
            {
                Visit(context.expr());
                Visit(@case);
            });

            if (context.statement() != null)
            {
                Visit(context.statement());
            }

            int lastLine = LineCount - 1;

            var breaksIndex = CodeLines.Select((s, i) => (s.Split(' ')[1].Equals("BREAK") && i >= firstLine && i <= lastLine)? i : -1).Where(i => i != -1);



            breaksIndex.ToList().ForEach(i =>
            {
                SetLineOnRealIndexOf(i, $"JUMP_ABSOLUTE {LineCount}");
            });

            return(null);
        }
Esempio n. 2
0
        public object VisitSwitchAST([NotNull] SwitchASTContext context)
        {
            var cases = context.@case();

            List <Pair <string, IToken> > returnedTypes = new List <Pair <string, IToken> >();

            if (context.expr() != null)
            {
                string type = Visit(context.expr()) as string;
                if (type != null && types.IndexOf(type) < 4)
                {
                    if (type == "none")
                    {
                        InsertError(context.expr().Start, $"La expresión no puede ser la constante 'null'");
                    }
                    else
                    {
                        cases.ToList().ForEach(@case =>
                        {
                            var list = Visit(@case) as List <Pair <string, IToken> >;
                            if (list != null)
                            {
                                returnedTypes.AddRange(list);
                            }

                            var caseType = (@case as CaseASTContext).typeString;
                            if (caseType != type && (caseType != "num" || (type != "int" && type != "float")))
                            {
                                InsertError(@case.Start, $"La expresión en el case debe ser de tipo '{type}'");
                            }
                        });

                        context.typeString = type;
                    }
                }
                else
                {
                    InsertError(context.expr().Start, $"El tipo de la expresión para el switch debe ser simple");
                }
            }

            return(returnedTypes);
        }