Exemple #1
0
        //Remove an item from a given list.
        public override object VisitStat_list_remove([NotNull] algoParser.Stat_list_removeContext context)
        {
            //Get the value that needs to be removed.
            AlgoValue toRemove = (AlgoValue)VisitExpr(context.expr());

            //Get the variable.
            var listVar = Particles.ParseParticleBlock(this, context, context.IDENTIFIER(), context.particle());

            if (listVar == null)
            {
                Error.Fatal(context, "No variable was returned to remove an item from, so can't remove an item from it.");
                return(null);
            }

            //Variable exists, so get the list value from it.
            if (listVar.Type != AlgoValueType.List)
            {
                Error.Fatal(context, "Variable given is not list, so can't remove an item from it.");
                return(null);
            }

            //Get the value of the list.
            List <AlgoValue> toSet = (List <AlgoValue>)listVar.Value;

            if (context.FROM_SYM() != null)
            {
                //Remove a given value from the list.
                //Check if the list contains the value.
                if (!toSet.Any(x => x.Value.Equals(toRemove.Value)))
                {
                    Error.Fatal(context, "The list selected does not contain an item with value given to remove.");
                    return(null);
                }

                //Remove the value.
                int index = toSet.FindIndex(x => x._Equals(toRemove));
                toSet.RemoveAt(index);
            }
            else
            {
                //Is the index valid?
                if (toRemove.Type != AlgoValueType.Integer)
                {
                    Error.Fatal(context, "The index given to remove at is not an integer.");
                    return(null);
                }

                if ((BigInteger)toRemove.Value < 0 || (BigInteger)toRemove.Value > toSet.Count)
                {
                    Error.Fatal(context, "Index to remove out of range for the given list.");
                }

                //Yes, remove there.
                toSet.RemoveAt(int.Parse(((BigInteger)toRemove.Value).ToString()));
            }

            return(null);
        }
Exemple #2
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="algoParser.stat_list_remove"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitStat_list_remove([NotNull] algoParser.Stat_list_removeContext context)
 {
     return(VisitChildren(context));
 }
Exemple #3
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="algoParser.stat_list_remove"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitStat_list_remove([NotNull] algoParser.Stat_list_removeContext context)
 {
 }