/// <summary> /// Visit a parse tree produced by <see cref="algoParser.stat_list_add"/>. /// <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_add([NotNull] algoParser.Stat_list_addContext context) { return(VisitChildren(context)); }
//Adding an item to a list. public override object VisitStat_list_add([NotNull] algoParser.Stat_list_addContext context) { //Evaluate the value to be added to the list. AlgoValue toAdd = (AlgoValue)VisitExpr(context.expr()[0]); //Get the list variable from the identifier and particle. AlgoValue listVar = Particles.ParseParticleBlock(this, context, context.IDENTIFIER(), context.particle()); if (listVar == null) { Error.Fatal(context, "No list value returned to add to from particle block."); return(null); } //Check the type is actually a list. if (listVar.Type != AlgoValueType.List) { Error.Fatal(context, "Variable given is not list, so can't remove an item from it."); return(null); } //Add a value to the list, either at a specific index or to the end. List <AlgoValue> toReturn = (List <AlgoValue>)listVar.Value; if (context.AT_SYM() != null) { //Evaluate the index. AlgoValue index = (AlgoValue)VisitExpr(context.expr()[1]); //Is it an integer? if (index.Type == AlgoValueType.Integer) { Error.Fatal(context, "The index supplied to insert at was not an integer."); return(null); } //Is it 0 or above? if ((BigInteger)index.Value < 0) { Error.Fatal(context, "The index supplied is below zero, out of range."); return(null); } //Is the index greater than the length of the list? if ((BigInteger)index.Value > ((List <AlgoValue>)listVar.Value).Count || (BigInteger)index.Value > int.MaxValue) { Error.Fatal(context, "The index supplied was out of range (too large)."); return(null); } //Insert. toReturn.Insert(int.Parse(((BigInteger)index.Value).ToString()), toAdd); } else { toReturn.Add(toAdd); } //Set the variable via. reference (TEST ME!). listVar.Value = toReturn; return(null); }
/// <summary> /// Exit a parse tree produced by <see cref="algoParser.stat_list_add"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitStat_list_add([NotNull] algoParser.Stat_list_addContext context) { }