private IodineObject RandInt(VirtualMachine vm, IodineObject self, IodineObject[] args)
 {
     if (args.Length <= 0)
     {
         return(new IodineInteger(rgn.Next(Int32.MinValue, Int32.MaxValue)));
     }
     else
     {
         int start = 0;
         int end   = 0;
         if (args.Length <= 1)
         {
             IodineInteger integer = args [0] as IodineInteger;
             if (integer == null)
             {
                 vm.RaiseException(new IodineTypeException("Int"));
                 return(null);
             }
             end = (int)integer.Value;
         }
         else
         {
             IodineInteger startInteger = args [0] as IodineInteger;
             IodineInteger endInteger   = args [1] as IodineInteger;
             if (startInteger == null || endInteger == null)
             {
                 vm.RaiseException(new IodineTypeException("Int"));
                 return(null);
             }
             start = (int)startInteger.Value;
             end   = (int)endInteger.Value;
         }
         return(new IodineInteger(rgn.Next(start, end)));
     }
 }
        public IodineSlice(IodineInteger start, IodineInteger stop, IodineInteger stride)
            : base(SliceTypeDefinition)
        {
            if (start != null)
            {
                Start = (int)start.Value;
            }
            else
            {
                DefaultStart = true;
            }

            if (stop != null)
            {
                Stop = (int)stop.Value;
            }
            else
            {
                DefaultStop = true;
            }

            Stride = (int)stride.Value;

            SetAttribute("start", (IodineObject)start ?? IodineNull.Instance);
            SetAttribute("stop", (IodineObject)stop ?? IodineNull.Instance);
            SetAttribute("stride", stride);
        }
        /**
         * Iodine Function: Bytes.substr (self, start, [length])
         * Description: Returns the substring of this value starting at start
         */
        private IodineObject Substring(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length == 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }

            if (args.Length == 1)
            {
                IodineInteger i1 = args [0] as IodineInteger;
                if (i1 == null)
                {
                    vm.RaiseException(new IodineTypeException("Int"));
                    return(null);
                }
                return(Substring(vm, i1));
            }
            else
            {
                IodineInteger i1 = args [0] as IodineInteger;
                IodineInteger i2 = args [1] as IodineInteger;

                if (i1 == null || i2 == null)
                {
                    vm.RaiseException(new IodineTypeException("Int"));
                    return(null);
                }

                return(Substring(vm, i1, i2));
            }
        }
Beispiel #4
0
            public override IodineObject Invoke(VirtualMachine vm, IodineObject[] args)
            {
                if (args.Length <= 0)
                {
                    vm.RaiseException(new IodineArgumentException(1));
                }

                if (args [0] is IodineFloat)
                {
                    IodineFloat fp = args [0] as IodineFloat;
                    return(new IodineBigInt((long)fp.Value));
                }

                if (args [0] is IodineInteger)
                {
                    IodineInteger integer = args [0] as IodineInteger;
                    return(new IodineBigInt(new BigInteger(integer.Value)));
                }

                BigInteger value;

                if (!BigInteger.TryParse(args [0].ToString(), out value))
                {
                    vm.RaiseException(new IodineTypeCastException("Int"));
                    return(null);
                }
                else
                {
                    return(new IodineBigInt(value));
                }
            }
Beispiel #5
0
        private IodineObject GetFrame(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length == 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }

            IodineInteger index = args [0] as IodineInteger;

            if (index == null)
            {
                vm.RaiseException(new IodineTypeException("Int"));
                return(null);
            }

            StackFrame top = vm.Top;
            int        i   = 0;

            while (top != null && i != index.Value)
            {
                top = top.Parent;
                i++;
            }

            if (top == null)
            {
                return(IodineNull.Instance);
            }

            return(new IodineStackFrameWrapper(top));
        }
            public override IodineObject Invoke(VirtualMachine vm, IodineObject[] args)
            {
                if (args.Length <= 0)
                {
                    vm.RaiseException(new IodineArgumentException(1));
                }

                if (args [0] is IodineString)
                {
                    return(new IodineBytes(args [0].ToString()));
                }

                IodineObject iter = args [0];

                iter.IterReset(vm);

                List <byte> bytes = new List <byte> ();

                while (iter.IterMoveNext(vm))
                {
                    IodineInteger b = iter.IterGetCurrent(vm) as IodineInteger;

                    if (b == null)
                    {
                        vm.RaiseException(new IodineException("Int"));
                        return(null);
                    }
                    bytes.Add((byte)(b.Value & 0xFF));
                }

                return(new IodineBytes(bytes.ToArray()));
            }
Beispiel #7
0
        /**
         * Iodine Function: _warn (type, msg)
         * Description: Internal low level function for issuing warnings
         * See modules/warnings.id
         */
        private IodineObject Warn(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length <= 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }

            IodineInteger warnType = args [0] as IodineInteger;
            IodineString  warnMsg  = args [1] as IodineString;

            if (warnType == null)
            {
                vm.RaiseException(new IodineTypeException("Int"));
                return(null);
            }

            if (warnMsg == null)
            {
                vm.RaiseException(new IodineTypeException("Str"));
                return(null);
            }

            vm.Context.Warn((WarningType)warnType.Value, warnMsg.ToString());
            return(null);
        }
            private IodineObject PadLeft(VirtualMachine vm, IodineObject self, IodineObject[] args)
            {
                IodineString thisObj = self as IodineString;

                char ch = ' ';

                if (args.Length == 0)
                {
                    vm.RaiseException(new IodineArgumentException(1));
                    return(null);
                }

                IodineInteger width = args [0] as IodineInteger;

                if (width == null)
                {
                    vm.RaiseException(new IodineTypeException("Int"));
                    return(null);
                }

                if (args.Length > 1)
                {
                    IodineString chStr = args [0] as IodineString;

                    if (chStr == null)
                    {
                        vm.RaiseException(new IodineTypeException("Str"));
                        return(null);
                    }

                    ch = chStr.Value [0];
                }

                return(new IodineString(thisObj.Value.PadLeft((int)width.Value, ch)));
            }
Beispiel #9
0
            private IodineObject RemoveAt(VirtualMachine vm, IodineObject self, IodineObject[] arguments)
            {
                IodineList thisObj = self as IodineList;

                if (arguments.Length <= 0)
                {
                    vm.RaiseException(new IodineArgumentException(1));
                    return(null);
                }
                IodineInteger index = arguments [0] as IodineInteger;

                if (index != null)
                {
                    if (index.Value < thisObj.Objects.Count)
                    {
                        thisObj.Objects.RemoveAt((int)index.Value);
                    }
                    else
                    {
                        vm.RaiseException(new IodineKeyNotFound());
                        return(null);
                    }
                    return(thisObj);
                }
                vm.RaiseException(new IodineTypeException("Int"));
                return(null);
            }
 public bool TryToConvertFromPrimative(object obj, out IodineObject result)
 {
     if (obj is IConvertible) {
         result = new IodineInteger (Convert.ToInt64 (obj));
         return true;
     }
     result = null;
     return false;
 }
Beispiel #11
0
            private IodineObject Splice(VirtualMachine vm, IodineObject self, IodineObject[] arguments)
            {
                IodineList thisObj = self as IodineList;

                if (arguments.Length <= 0)
                {
                    vm.RaiseException(new IodineArgumentException(1));
                    return(null);
                }

                int start = 0;
                int end   = thisObj.Objects.Count;

                IodineInteger startInt = arguments [0] as IodineInteger;

                if (startInt == null)
                {
                    vm.RaiseException(new IodineTypeException("Int"));
                    return(null);
                }
                start = (int)startInt.Value;

                if (arguments.Length >= 2)
                {
                    IodineInteger endInt = arguments [1] as IodineInteger;
                    if (endInt == null)
                    {
                        vm.RaiseException(new IodineTypeException("Int"));
                        return(null);
                    }
                    end = (int)endInt.Value;
                }

                if (start < 0)
                {
                    start = thisObj.Objects.Count - start;
                }
                if (end < 0)
                {
                    end = thisObj.Objects.Count - end;
                }

                IodineList retList = new IodineList(new IodineObject[] { });

                for (int i = start; i < end; i++)
                {
                    if (i < 0 || i > thisObj.Objects.Count)
                    {
                        vm.RaiseException(new IodineIndexException());
                        return(null);
                    }
                    retList.Add(thisObj.Objects [i]);
                }

                return(retList);
            }
Beispiel #12
0
        public override IodineObject GreaterThanOrEqual(VirtualMachine vm, IodineObject right)
        {
            IodineInteger intVal = right as IodineInteger;

            if (intVal == null)
            {
                vm.RaiseException(new IodineTypeException("Right hand side must be of type Int!"));
            }
            return(IodineBool.Create(Value >= intVal.Value));
        }
Beispiel #13
0
            public override IodineObject Invoke(VirtualMachine vm, IodineObject[] args)
            {
                if (args.Length <= 0)
                {
                    vm.RaiseException(new IodineArgumentException(1));
                    return(null);
                }
                IodineInteger size = args [0] as IodineInteger;

                return(new IodineByteArray(new byte [size.Value]));
            }
Beispiel #14
0
        public override bool Equals(IodineObject obj)
        {
            IodineInteger intVal = obj as IodineInteger;

            if (intVal != null)
            {
                return(intVal.Value == Value);
            }

            return(false);
        }
        public override IodineObject GetIndex(VirtualMachine vm, IodineObject key)
        {
            IodineInteger index = key as IodineInteger;

            if (index.Value < Objects.Length)
            {
                return(Objects [(int)index.Value]);
            }
            vm.RaiseException(new IodineIndexException());
            return(null);
        }
Beispiel #16
0
        private IodineObject sleep(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length <= 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
            }
            IodineInteger time = args [0] as IodineInteger;

            System.Threading.Thread.Sleep((int)time.Value);
            return(null);
        }
Beispiel #17
0
        private IodineObject take(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 2)
            {
                vm.RaiseException(new IodineArgumentException(2));
                return(null);
            }
            IodineInteger count = args [1] as IodineInteger;

            return(new InternalGenerator(() => internalTake(vm, args [0], count.Value)));
        }
Beispiel #18
0
        public override IodineObject Add(VirtualMachine vm, IodineObject right)
        {
            IodineInteger intVal = right as IodineInteger;

            if (intVal == null)
            {
                vm.RaiseException(new IodineTypeException("Right hand side must be of type Int!"));
                return(null);
            }
            return(new IodineInteger(Value + intVal.Value));
        }
Beispiel #19
0
        private IodineObject chr(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length <= 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }
            IodineInteger ascii = args [0] as IodineInteger;

            return(new IodineString(((char)(int)ascii.Value).ToString()));
        }
        private IodineObject Substring(VirtualMachine vm, IodineInteger i1)
        {
            byte[] newBytes = new byte[Value.Length - (int)i1.Value];
            int    nI       = 0;

            for (int i = (int)i1.Value; i < newBytes.Length; i++)
            {
                newBytes [nI++] = Value [i];
            }

            return(new IodineBytes(newBytes));
        }
        private IodineObject Substring(VirtualMachine vm, IodineInteger i1, IodineInteger i2)
        {
            byte[] newBytes = new byte[(int)i2.Value];

            int nI = 0;

            for (int i = (int)i1.Value; nI < (int)i2.Value; i++)
            {
                newBytes [nI++] = Value [i];
            }

            return(new IodineBytes(newBytes));
        }
Beispiel #22
0
        public override void SetIndex(VirtualMachine vm, IodineObject key, IodineObject value)
        {
            IodineInteger index = key as IodineInteger;
            IodineInteger val   = value as IodineInteger;

            if (index.Value < Array.Length)
            {
                Array [(int)index.Value] = (byte)(val.Value & 0xFF);
            }
            else
            {
                vm.RaiseException(new IodineIndexException());
            }
        }
Beispiel #23
0
        private IodineObject range(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            long start = 0;
            long end   = 0;
            long step  = 1;

            if (args.Length <= 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }
            if (args.Length == 1)
            {
                IodineInteger stepObj = args [0] as IodineInteger;
                if (stepObj == null)
                {
                    vm.RaiseException(new IodineTypeException("Int"));
                    return(null);
                }
                end = stepObj.Value;
            }
            else if (args.Length == 2)
            {
                IodineInteger startObj = args [0] as IodineInteger;
                IodineInteger endObj   = args [1] as IodineInteger;
                if (startObj == null || endObj == null)
                {
                    vm.RaiseException(new IodineTypeException("Int"));
                    return(null);
                }
                start = startObj.Value;
                end   = endObj.Value;
            }
            else
            {
                IodineInteger startObj = args [0] as IodineInteger;
                IodineInteger endObj   = args [1] as IodineInteger;
                IodineInteger stepObj  = args [2] as IodineInteger;
                if (startObj == null || endObj == null || stepObj == null)
                {
                    vm.RaiseException(new IodineTypeException("Int"));
                    return(null);
                }
                start = startObj.Value;
                end   = endObj.Value;
                step  = stepObj.Value;
            }
            return(new IodineRange(start, end, step));
        }
Beispiel #24
0
                public override IodineObject Invoke(VirtualMachine vm, IodineObject[] args)
                {
                    if (args.Length == 0)
                    {
                        return(new IodineSemaphore(1));
                    }
                    IodineInteger semaphore = args [0] as IodineInteger;

                    if (semaphore == null)
                    {
                        vm.RaiseException(new IodineTypeException("Integer"));
                        return(null);
                    }
                    return(new IodineSemaphore((int)semaphore.Value));
                }
        public override IodineObject GetIndex(VirtualMachine vm, IodineObject key)
        {
            IodineInteger index = key as IodineInteger;

            if (index == null)
            {
                vm.RaiseException(new IodineTypeException("Int"));
                return(null);
            }
            if (index.Value >= this.Value.Length)
            {
                vm.RaiseException(new IodineIndexException());
                return(null);
            }
            return(new IodineInteger((long)Value [(int)index.Value]));
        }
Beispiel #26
0
        public override IodineObject Xor(VirtualMachine vm, IodineObject right)
        {
            IodineInteger intVal = right as IodineInteger;
            IodineBigInt  bigVal = right as IodineBigInt;

            if (intVal == null)
            {
                if (bigVal != null)
                {
                    return(new IodineBigInt(Value ^ bigVal.Value));
                }
                vm.RaiseException(new IodineTypeException("Right hand side must be of type Int!"));
                return(null);
            }
            return(new IodineInteger(Value ^ intVal.Value));
        }
Beispiel #27
0
        /**
         * Iodine Function: _setWarnMask (val)
         * Description: Internal low level function for settings the current warning mask
         * See modules/warnings.id
         */
        private IodineObject SetWarnMask(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length == 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }

            IodineInteger value = args [0] as IodineInteger;

            if (value == null)
            {
                vm.RaiseException(new IodineTypeException("Int"));
                return(null);
            }

            vm.Context.WarningFilter = (WarningType)value.Value;
            return(null);
        }
Beispiel #28
0
        public override void SetIndex(VirtualMachine vm, IodineObject key, IodineObject value)
        {
            IodineInteger index = key as IodineInteger;

            if (index == null)
            {
                vm.RaiseException(new IodineTypeException("Int"));
                return;
            }

            if (index.Value < Objects.Count)
            {
                this.Objects [(int)index.Value] = value;
            }
            else
            {
                vm.RaiseException(new IodineIndexException());
            }
        }
Beispiel #29
0
        private IodineObject removeAt(VirtualMachine vm, IodineObject self, IodineObject[] arguments)
        {
            if (arguments.Length <= 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }
            IodineInteger index = arguments [0] as IodineInteger;

            if (index != null)
            {
                Objects.RemoveAt((int)index.Value);
            }
            else
            {
                vm.RaiseException(new IodineTypeException("Int"));
            }
            return(null);
        }
Beispiel #30
0
        private IodineObject exit(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length <= 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }

            IodineInteger exitCode = args [0] as IodineInteger;

            if (exitCode == null)
            {
                vm.RaiseException(new IodineTypeException("Int"));
                return(null);
            }

            Environment.Exit((int)exitCode.Value);
            return(null);
        }
Beispiel #31
0
        private IodineObject write(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (Closed)
            {
                vm.RaiseException(new IodineIOException("Stream has been closed!"));
                return(null);
            }

            if (!CanWrite)
            {
                vm.RaiseException(new IodineIOException("Can not write to stream!"));
                return(null);
            }

            foreach (IodineObject obj in args)
            {
                if (obj is IodineString)
                {
                    write(obj.ToString());
                }
                else if (obj is IodineBytes)
                {
                    IodineBytes arr = obj as IodineBytes;
                    File.Write(arr.Value, 0, arr.Value.Length);
                }
                else if (obj is IodineInteger)
                {
                    IodineInteger intVal = obj as IodineInteger;
                    write((byte)intVal.Value);
                }
                else if (obj is IodineByteArray)
                {
                    IodineByteArray arr = obj as IodineByteArray;
                    File.Write(arr.Array, 0, arr.Array.Length);
                }
                else
                {
                    vm.RaiseException(new IodineTypeException(""));
                }
            }
            return(null);
        }