Pop() public method

public Pop ( ) : object
return object
Ejemplo n.º 1
0
 private static void assemble(ScliptingEnv e)
 {
     var separator = ScliptingUtil.ToString(e.Pop());
     var item = e.Pop();
     var list = item is List<object> ? ((List<object>) item).Select(obj => ScliptingUtil.ToString(obj)) : ScliptingUtil.ToString(item).Select(ch => (object) ch.ToString());
     e.CurrentStack.Add(list.JoinString(separator));
 }
Ejemplo n.º 2
0
        public override IEnumerable<Position> Execute(ScliptingEnv environment)
        {
            yield return new Position(Index, 1);

            var regex = ScliptingUtil.ToString(environment.Pop());
            switch (_matchType)
            {
                case matchType.Regex: break;
                case matchType.CaseSensitiveSubstring: regex = Regex.Escape(regex); break;
                case matchType.CaseInsensitiveSubstring: regex = "(?i:{0})".Fmt(Regex.Escape(regex)); break;
            }

            var input = ScliptingUtil.ToString(environment.CurrentStack.Last());
            List<Match> matches = null;
            Match match = null;

            if (_firstMatchOnly)
                match = Regex.Match(input, regex, RegexOptions.Singleline);
            else
                matches = Regex.Matches(input, regex, RegexOptions.Singleline).Cast<Match>().ToList();
            var pushResult = true;

            if (((_firstMatchOnly && !match.Success) || (!_firstMatchOnly && matches.Count == 0)) && ElseBlock != null)
            {
                // Jump to the “else” instruction
                yield return new Position(ElseIndex, 1);
                if (ElseBlockPops)
                    environment.Pop();
                else
                    pushResult = false;
                foreach (var instruction in ElseBlock)
                    foreach (var position in instruction.Execute(environment))
                        yield return position;
            }
            else
            {
                if (PrimaryBlockPops)
                    environment.Pop();
                var offset = 0;
                for (int i = 0; i < (_firstMatchOnly ? match.Success ? 1 : 0 : matches.Count); i++)
                {
                    var m = _firstMatchOnly ? match : matches[i];
                    environment.RegexObjects.Add(new RegexMatch(input, regex, offset, m));
                    if (i > 0)
                        yield return new Position(Index, 1);
                    foreach (var instruction in PrimaryBlock)
                        foreach (var position in instruction.Execute(environment))
                            yield return position;
                    yield return new Position(Index + Count - 1, 1);
                    var subst = ScliptingUtil.ToString(environment.Pop());
                    input = input.Substring(0, m.Index + offset) + subst + input.Substring(m.Index + offset + m.Length);
                    offset += subst.Length - m.Length;
                    environment.RegexObjects.RemoveAt(environment.RegexObjects.Count - 1);
                }
            }

            // “End” instruction
            environment.RegexObjects.Add(new RegexMatch(input, regex, 0, null));
            yield return new Position(Index + Count - 1, 1);
            environment.RegexObjects.RemoveAt(environment.RegexObjects.Count - 1);
            if (pushResult)
                environment.CurrentStack.Add(input);
        }
Ejemplo n.º 3
0
        private static void annihilate(ScliptingEnv e)
        {
            var bigInteger = ScliptingUtil.ToInt(e.Pop());
            if (bigInteger < 0 || bigInteger > int.MaxValue)
                return;
            var listOrString = e.Pop();

            var integer = (int) bigInteger;
            var list = listOrString as List<object>;
            if (list != null)
            {
                var newList = new List<object>(list);
                if (integer < newList.Count)
                    newList.RemoveAt(integer);
                e.CurrentStack.Add(newList);
            }
            else
            {
                // assume string
                var input = ScliptingUtil.ToString(listOrString);
                // make sure we push the original object (not convert to string) if out of range
                e.CurrentStack.Add(integer >= input.Length ? listOrString : input.Substring(0, integer) + input.Substring(integer + 1));
            }
        }
Ejemplo n.º 4
0
        public override IEnumerable<Position> Execute(ScliptingEnv environment)
        {
            yield return new Position(Index, 1);
            var item = environment.CurrentStack.Last();
            if (SatisfiesCondition(item))
            {
                if (PrimaryBlockPops)
                    environment.Pop();
                foreach (var instruction in PrimaryBlock)
                    foreach (var position in instruction.Execute(environment))
                        yield return position;
            }
            else
            {
                // Jump to the “else” instruction
                if (ElseBlock != null)
                    yield return new Position(ElseIndex, 1);
                if ((ElseBlock == null && PrimaryBlockPops) || (ElseBlock != null && ElseBlockPops))
                    environment.Pop();
                if (ElseBlock != null)
                    foreach (var instruction in ElseBlock)
                        foreach (var position in instruction.Execute(environment))
                            yield return position;
            }

            // “End” instruction
            yield return new Position(Index + Count - 1, 1);
        }
Ejemplo n.º 5
0
        public override IEnumerable<Position> Execute(ScliptingEnv environment)
        {
            yield return new Position(Index, Count);

            switch (Instruction)
            {
                case ListStringInstruction.RetrievePop:
                case ListStringInstruction.RetrieveNoPop:
                    {
                        if (GetIndexFromStack)
                            ListStringIndex = (int) ScliptingUtil.ToInt(environment.Pop());
                        var listOrString = Instruction == ListStringInstruction.RetrievePop ? environment.Pop() : environment.CurrentStack.Last();
                        operation(listOrString, false, false,
                            (s, i) => { environment.CurrentStack.Add(i >= 0 && i < s.Length ? s[i].ToString() : ""); },
                            (l, i) => { environment.CurrentStack.Add(i >= 0 && i < l.Count ? l[i] : ""); });
                    }
                    break;
                case ListStringInstruction.Insert:
                    {
                        var item = environment.Pop();
                        if (GetIndexFromStack)
                            ListStringIndex = (int) ScliptingUtil.ToInt(environment.Pop());
                        var listOrString = environment.Pop();
                        operation(listOrString, true, false,
                            (s, i) =>
                            {
                                var j = Backwards ? i + 1 : i;
                                var itemAsString = ScliptingUtil.ToString(item);
                                var itemAsChar = itemAsString.Length == 0 ? ' ' : itemAsString[0];
                                environment.CurrentStack.Add(s.Substring(0, j) + itemAsChar + s.Substring(j));
                            },
                            (l, i) =>
                            {
                                l.Insert(Backwards ? i + 1 : i, item);
                                environment.CurrentStack.Add(l);
                            });
                    }
                    break;
                case ListStringInstruction.Delete:
                    {
                        if (GetIndexFromStack)
                            ListStringIndex = (int) ScliptingUtil.ToInt(environment.Pop());
                        var listOrString = environment.Pop();
                        operation(listOrString, false, false,
                            (s, i) =>
                            {
                                // make sure we push the original object (not convert to string) if out of range
                                environment.CurrentStack.Add(i >= 0 && i < s.Length ? s.Remove(i, 1) : listOrString);
                            },
                            (l, i) =>
                            {
                                if (i >= 0 && i < l.Count)
                                    l.RemoveAt(i);
                                environment.CurrentStack.Add(l);
                            });
                    }
                    break;
                case ListStringInstruction.RetrieveDelete:
                    {
                        if (GetIndexFromStack)
                            ListStringIndex = (int) ScliptingUtil.ToInt(environment.Pop());
                        var listOrString = environment.Pop();
                        operation(listOrString, false, false,
                            (s, i) =>
                            {
                                var ch = (i >= 0 && i < s.Length ? s[i].ToString() : "");
                                // make sure we push the original object (not convert to string) if out of range
                                environment.CurrentStack.Add(i >= 0 && i < s.Length ? s.Remove(i, 1) : listOrString);
                                environment.CurrentStack.Add(ch);
                            },
                            (l, i) =>
                            {
                                var item = i >= 0 && i < l.Count ? l[i] : "";
                                if (i >= 0 && i < l.Count)
                                    l.RemoveAt(i);
                                environment.CurrentStack.Add(l);
                                environment.CurrentStack.Add(item);
                            });
                    }
                    break;
                case ListStringInstruction.Replace:
                    {
                        var item = environment.Pop();
                        if (GetIndexFromStack)
                            ListStringIndex = (int) ScliptingUtil.ToInt(environment.Pop());
                        var listOrString = environment.Pop();
                        operation(listOrString, false, true,
                            (s, i) =>
                            {
                                var itemAsString = ScliptingUtil.ToString(item);
                                var itemAsChar = itemAsString.Length == 0 ? ' ' : itemAsString[0];
                                environment.CurrentStack.Add(s.Substring(0, i) + itemAsChar + s.Substring(i + 1));
                            },
                            (l, i) =>
                            {
                                l[i] = item;
                                environment.CurrentStack.Add(l);
                            });
                    }
                    break;
                case ListStringInstruction.Exchange:
                    {
                        var item = environment.Pop();
                        if (GetIndexFromStack)
                            ListStringIndex = (int) ScliptingUtil.ToInt(environment.Pop());
                        var listOrString = environment.Pop();
                        operation(listOrString, false, true,
                            (s, i) =>
                            {
                                var itemAsString = ScliptingUtil.ToString(item);
                                var itemAsChar = itemAsString.Length == 0 ? ' ' : itemAsString[0];
                                var prevChar = s[i].ToString();
                                environment.CurrentStack.Add(s.Substring(0, i) + itemAsChar + s.Substring(i + 1));
                                environment.CurrentStack.Add(prevChar);
                            },
                            (l, i) =>
                            {
                                environment.CurrentStack.Add(l);
                                environment.CurrentStack.Add(l[i]);
                                l[i] = item;    // sneaky: modify list after putting it on the stack...
                            });
                    }
                    break;

                default:
                    throw new InvalidOperationException("Invalid list/string manipulation instruction encountered.");
            }
        }
Ejemplo n.º 6
0
 public override IEnumerable<Position> Execute(ScliptingEnv environment)
 {
     yield return new Position(Index, Count);
     environment.CurrentStack.Add(new Function { FunctionCode = this, CapturedItem = Capture ? environment.Pop() : null });
 }
Ejemplo n.º 7
0
 public override IEnumerable<Position> Execute(ScliptingEnv environment)
 {
     yield return new Position(Index, 1);
     var b = ScliptingUtil.ToInt(environment.Pop());
     var a = ScliptingUtil.ToInt(environment.Pop());
     if (Backwards ? (a < b) : (a > b))
     {
         // “Else” block
         if (ElseBlock != null)
         {
             yield return new Position(ElseIndex, 1);
             foreach (var instr in ElseBlock)
                 foreach (var pos in instr.Execute(environment))
                     yield return pos;
         }
         // “End” instruction
         yield return new Position(Index + Count - 1, 1);
     }
     else
     {
         for (var i = a; Backwards ? (i >= b) : (i <= b); i += Backwards ? -1 : 1)
         {
             yield return new Position(Index, 1);
             environment.CurrentStack.Add(i);
             foreach (var instr in PrimaryBlock)
                 foreach (var pos in instr.Execute(environment))
                     yield return pos;
             // “End” instruction
             yield return new Position(Index + Count - 1, 1);
         }
     }
 }
Ejemplo n.º 8
0
 public override IEnumerable<Position> Execute(ScliptingEnv environment)
 {
     yield return new Position(Index, 1);
     var orig = environment.CurrentStack.Last();
     var list =
         orig is List<object> ? (List<object>) orig :
         orig is byte[] ? ((byte[]) orig).Select(b => (object) (BigInteger) b) :
         ScliptingUtil.ToString(orig).Select(ch => (object) ch.ToString());
     bool any = false;
     foreach (var item in list)
     {
         if (!any && PrimaryBlockPops)
             environment.Pop();
         any = true;
         yield return new Position(Index, 1);
         environment.CurrentStack.Add(item);
         foreach (var instr in PrimaryBlock)
             foreach (var pos in instr.Execute(environment))
                 yield return pos;
         // “End” instruction
         yield return new Position(Index + Count - 1, 1);
     }
     if (!any)
     {
         // Jump to the “else” instruction
         if (ElseBlock != null)
             yield return new Position(ElseIndex, 1);
         if ((ElseBlock == null && PrimaryBlockPops) || (ElseBlock != null && ElseBlockPops))
             environment.Pop();
         if (ElseBlock != null)
             foreach (var instr in ElseBlock)
                 foreach (var pos in instr.Execute(environment))
                     yield return pos;
         // “End” instruction
         yield return new Position(Index + Count - 1, 1);
     }
 }
Ejemplo n.º 9
0
 public override IEnumerable<Position> Execute(ScliptingEnv environment)
 {
     yield return new Position(Index, 1);
     var item = Instruction == Instruction.Perform ? environment.CurrentStack.Last() : environment.Pop();
     var fnc = item as Function;
     if (fnc != null)
     {
         yield return new Position(fnc.FunctionCode.Index, 1);
         if (fnc.CapturedItem != null)
             environment.CurrentStack.Add(fnc.CapturedItem);
         foreach (var instr in fnc.FunctionCode.PrimaryBlock)
             foreach (var pos in instr.Execute(environment))
                 yield return pos;
         yield return new Position(fnc.FunctionCode.Index + fnc.FunctionCode.Count - 1, 1);
     }
     if (Instruction == Instruction.Handle)
     {
         yield return new Position(Index, 1);
         environment.CurrentStack.Add(item);
     }
 }
Ejemplo n.º 10
0
        public override IEnumerable<Position> Execute(ScliptingEnv environment)
        {
            yield return new Position(Index, 1);
            var item = environment.CurrentStack.Last();
            var isList = item is List<object>;
            var list = isList ? ((List<object>) item).ToList() : (ScliptingUtil.ToString(item).Select(ch => (object) ch.ToString()).ToList());
            var newList = new List<object>();

            if (list.Count == 0 && ElseBlock != null)
            {
                // Jump to the “else” instruction
                yield return new Position(ElseIndex, 1);
                if (ElseBlockPops)
                    environment.Pop();
                foreach (var instruction in ElseBlock)
                    foreach (var position in instruction.Execute(environment))
                        yield return position;
                // “End” instruction
                yield return new Position(Index + Count - 1, 1);
            }
            else
            {
                if (PrimaryBlockPops)
                    environment.Pop();
                while (list.Count > 0)
                {
                    yield return new Position(Index, 1);
                    var index = Backward ? list.Count - 1 : 0;
                    environment.CurrentStack.Add(list[index]);
                    foreach (var instruction in PrimaryBlock)
                        foreach (var position in instruction.Execute(environment))
                            yield return position;
                    yield return new Position(Index + Count - 1, 1);
                    var subItem = environment.Pop();
                    if (ScliptingUtil.IsTrue(subItem))
                        break;
                    newList.Insert(Backward ? 0 : newList.Count, list[index]);
                    list.RemoveAt(index);
                }

                ScliptingUtil.FlipIf(Backward,
                    () => environment.CurrentStack.Add(isList ? (object) newList : ScliptingUtil.ToString(newList)),
                    () => environment.CurrentStack.Add(isList ? (object) list : ScliptingUtil.ToString(list)));
            }
        }