Ejemplo n.º 1
0
        private IodineObject Choice(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length <= 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }
            IodineObject collection = args [0].GetIterator(vm);
            int          count      = 0;

            collection.IterReset(vm);

            while (collection.IterMoveNext(vm))
            {
                collection.IterGetCurrent(vm);
                count++;
            }

            int choice = rgn.Next(0, count);

            count = 0;

            collection.IterReset(vm);
            while (collection.IterMoveNext(vm))
            {
                IodineObject o = collection.IterGetCurrent(vm);
                if (count == choice)
                {
                    return(o);
                }
                count++;
            }

            return(null);
        }
Ejemplo n.º 2
0
        private IodineObject Enumerate(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length == 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(IodineNull.Instance);
            }

            IodineList   list       = new IodineList(new IodineObject[] { });
            IodineObject collection = args [0].GetIterator(vm);

            collection.IterReset(vm);

            int counter = 0;

            while (collection.IterMoveNext(vm))
            {
                IodineObject o = collection.IterGetCurrent(vm);
                list.Add(new IodineTuple(new IodineObject[] {
                    new IodineInteger(counter++),
                    o
                }));
            }
            return(list);
        }
Ejemplo n.º 3
0
            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()));
            }
Ejemplo n.º 4
0
 private static IEnumerator internalSkip(VirtualMachine vm, IodineObject iterator, long count)
 {
     iterator.IterReset (vm);
     long i = 0;
     while (iterator.IterMoveNext (vm)) {
         IodineObject obj = iterator.IterGetCurrent (vm);
         if (i < count)
             yield return obj;
         i++;
     }
 }
Ejemplo n.º 5
0
        public override IodineObject Add(VirtualMachine vm, IodineObject right)
        {
            var list = new IodineList(Objects.ToArray());

            right.IterReset(vm);
            while (right.IterMoveNext(vm))
            {
                var o = right.IterGetCurrent(vm);
                list.Add(o);
            }
            return(list);
        }
Ejemplo n.º 6
0
        private static IEnumerator internalSkip(VirtualMachine vm, IodineObject iterator, long count)
        {
            iterator.IterReset(vm);
            long i = 0;

            while (iterator.IterMoveNext(vm))
            {
                IodineObject obj = iterator.IterGetCurrent(vm);
                if (i < count)
                {
                    yield return(obj);
                }
                i++;
            }
        }
Ejemplo n.º 7
0
        private IodineObject each(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 2)
            {
                vm.RaiseException(new IodineArgumentException(2));
                return(null);
            }
            IodineObject iter = args [0];
            IodineObject func = args [1];

            iter.IterReset(vm);
            while (iter.IterMoveNext(vm))
            {
                func.Invoke(vm, new IodineObject[] { iter.IterGetCurrent(vm) });
            }
            return(null);
        }
Ejemplo n.º 8
0
 private static IEnumerator internalSkipWhile(VirtualMachine vm, IodineObject iterator,
                                              IodineObject func)
 {
     iterator.IterReset(vm);
     while (iterator.IterMoveNext(vm))
     {
         IodineObject obj = iterator.IterGetCurrent(vm);
         if (!func.Invoke(vm, new IodineObject[] { obj }).IsTrue())
         {
             yield return(obj);
         }
         else
         {
             break;
         }
     }
 }
Ejemplo n.º 9
0
        private IodineObject addRange(VirtualMachine vm, IodineObject self, IodineObject[] arguments)
        {
            if (arguments.Length <= 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }
            IodineObject collection = arguments [0];

            collection.IterReset(vm);
            while (collection.IterMoveNext(vm))
            {
                IodineObject o = collection.IterGetCurrent(vm);
                Add(o);
            }
            return(null);
        }
Ejemplo n.º 10
0
            public override IodineObject Invoke(VirtualMachine vm, IodineObject[] args)
            {
                IodineList list = new IodineList(new IodineObject[0]);

                if (args.Length > 0)
                {
                    foreach (IodineObject arg in args)
                    {
                        IodineObject collection = arg.GetIterator(vm);
                        collection.IterReset(vm);
                        while (collection.IterMoveNext(vm))
                        {
                            IodineObject o = collection.IterGetCurrent(vm);
                            list.Add(o);
                        }
                    }
                }
                return(list);
            }
Ejemplo n.º 11
0
        private IodineObject join(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            StringBuilder accum      = new StringBuilder();
            IodineObject  collection = args [0];

            collection.IterReset(vm);
            string last = "";
            string sep  = "";

            while (collection.IterMoveNext(vm))
            {
                IodineObject o = collection.IterGetCurrent(vm);
                accum.AppendFormat("{0}{1}", last, sep);
                last = o.ToString();
                sep  = this.Value;
            }
            accum.Append(last);
            return(new IodineString(accum.ToString()));
        }
Ejemplo n.º 12
0
            private IodineObject AddRange(VirtualMachine vm, IodineObject self, IodineObject[] arguments)
            {
                IodineList thisObj = self as IodineList;

                if (arguments.Length <= 0)
                {
                    vm.RaiseException(new IodineArgumentException(1));
                    return(null);
                }
                IodineObject collection = arguments [0].GetIterator(vm);

                collection.IterReset(vm);
                while (collection.IterMoveNext(vm))
                {
                    IodineObject o = collection.IterGetCurrent(vm);
                    thisObj.Add(o);
                }
                return(thisObj);
            }
Ejemplo n.º 13
0
        private IodineObject sum(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }

            IodineObject initial    = args.Length > 1 ? args [1] : new IodineInteger(0);
            IodineObject collection = args [0];

            collection.IterReset(vm);
            while (collection.IterMoveNext(vm))
            {
                IodineObject o = collection.IterGetCurrent(vm);
                initial = initial.Add(vm, o);
            }
            return(initial);
        }
Ejemplo n.º 14
0
        private IodineObject map(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length <= 1)
            {
                vm.RaiseException(new IodineArgumentException(2));
                return(null);
            }

            IodineList   list       = new IodineList(new IodineObject[] { });
            IodineObject collection = args [0];
            IodineObject func       = args [1];

            collection.IterReset(vm);
            while (collection.IterMoveNext(vm))
            {
                IodineObject o = collection.IterGetCurrent(vm);
                list.Add(func.Invoke(vm, new IodineObject[] { o }));
            }
            return(list);
        }
Ejemplo n.º 15
0
        private IodineObject Sort(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(IodineNull.Instance);
            }

            IodineObject collection = args [0].GetIterator(vm);
            IodineObject func       = null;

            if (args.Length > 1)
            {
                func = args [1];
            }

            List <IodineObject> items = new List <IodineObject> ();

            collection.IterReset(vm);

            while (collection.IterMoveNext(vm))
            {
                IodineObject item = collection.IterGetCurrent(vm);
                items.Add(item);
            }

            items.Sort((x, y) => {
                if (func != null)
                {
                    x = func.Invoke(vm, new IodineObject[] { x });
                    y = func.Invoke(vm, new IodineObject[] { y });
                }
                IodineInteger i = x.Compare(vm, y) as IodineInteger;
                return((int)i.Value);
            });

            return(new IodineTuple(items.ToArray()));
        }
Ejemplo n.º 16
0
        private IodineObject Filter(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length <= 1)
            {
                vm.RaiseException(new IodineArgumentException(2));
                return(IodineNull.Instance);
            }

            IodineList   list       = new IodineList(new IodineObject[] { });
            IodineObject collection = args [0].GetIterator(vm);
            IodineObject func       = args [1];

            collection.IterReset(vm);

            while (collection.IterMoveNext(vm))
            {
                IodineObject o = collection.IterGetCurrent(vm);
                if (func.Invoke(vm, new IodineObject[] { o }).IsTrue())
                {
                    list.Add(o);
                }
            }
            return(list);
        }
Ejemplo n.º 17
0
        private IodineObject reduce(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length <= 1)
            {
                vm.RaiseException(new IodineArgumentException(2));
                return(null);
            }

            IodineObject result     = args.Length > 2 ? args [1] : null;
            IodineObject collection = args [0];
            IodineObject func       = args.Length > 2 ? args [2] : args [1];

            collection.IterReset(vm);
            while (collection.IterMoveNext(vm))
            {
                IodineObject o = collection.IterGetCurrent(vm);
                if (result == null)
                {
                    result = o;
                }
                result = func.Invoke(vm, new IodineObject[] { result, o });
            }
            return(result);
        }
Ejemplo n.º 18
0
		private static IEnumerator internalSkipWhile (VirtualMachine vm, IodineObject iterator,
			IodineObject func)
		{
			iterator.IterReset (vm);
			while (iterator.IterMoveNext (vm)) {
				IodineObject obj = iterator.IterGetCurrent (vm);
				if (!func.Invoke (vm, new IodineObject[] {obj}).IsTrue ())
					yield return obj;
				else
					break;
			}
		}
Ejemplo n.º 19
0
        private void EvalInstruction()
        {
            if (instruction.Location != null)
            {
                currentLocation = instruction.Location;
            }

            switch (instruction.OperationCode)
            {
            case Opcode.Pop:
            {
                Pop();
                break;
            }

            case Opcode.Dup:
            {
                IodineObject val = Pop();
                Push(val);
                Push(val);
                break;
            }

            case Opcode.LoadConst:
            {
                Push(Top.Module.ConstantPool [instruction.Argument]);
                break;
            }

            case Opcode.LoadNull:
            {
                Push(IodineNull.Instance);
                break;
            }

            case Opcode.LoadSelf:
            {
                Push(Top.Self);
                break;
            }

            case Opcode.LoadTrue:
            {
                Push(IodineBool.True);
                break;
            }

            case Opcode.LoadException:
            {
                Push(lastException);
                break;
            }

            case Opcode.LoadFalse:
            {
                Push(IodineBool.False);
                break;
            }

            case Opcode.StoreLocal:
            {
                string name = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
                Top.StoreLocal(name, Pop());
                break;
            }

            case Opcode.LoadLocal:
            {
                string name = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
                Push(Top.LoadLocal(name));
                break;
            }

            case Opcode.StoreGlobal:
            {
                string name = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
                Top.Module.SetAttribute(this, name, Pop());
                break;
            }

            case Opcode.LoadGlobal:
            {
                string name = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
                if (name == "_")
                {
                    Push(Top.Module);
                }
                else if (Top.Module.Attributes.ContainsKey(name))
                {
                    Push(Top.Module.GetAttribute(this, name));
                }
                else
                {
                    RaiseException(new IodineAttributeNotFoundException(name));
                }
                break;
            }

            case Opcode.StoreAttribute:
            {
                IodineObject target    = Pop();
                IodineObject value     = Pop();
                string       attribute = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
                if (target.Attributes.ContainsKey(attribute) &&
                    target.Attributes [attribute] is IIodineProperty)
                {
                    IIodineProperty property = (IIodineProperty)target.Attributes [attribute];
                    property.Set(this, value);
                    break;
                }
                target.SetAttribute(this, attribute, value);
                break;
            }

            case Opcode.LoadAttribute:
            {
                IodineObject target    = Pop();
                string       attribute = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
                if (target.Attributes.ContainsKey(attribute) &&
                    target.Attributes [attribute] is IIodineProperty)
                {
                    IIodineProperty property = (IIodineProperty)target.Attributes [attribute];
                    Push(property.Get(this));
                    break;
                }
                Push(target.GetAttribute(this, attribute));
                break;
            }

            case Opcode.LoadAttributeOrNull:
            {
                IodineObject target    = Pop();
                string       attribute = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
                if (target.Attributes.ContainsKey(attribute))
                {
                    Push(target.GetAttribute(this, attribute));
                }
                else
                {
                    Push(IodineNull.Instance);
                }
                break;
            }

            case Opcode.StoreIndex:
            {
                IodineObject index  = Pop();
                IodineObject target = Pop();
                IodineObject value  = Pop();
                target.SetIndex(this, index, value);
                break;
            }

            case Opcode.LoadIndex:
            {
                IodineObject index  = Pop();
                IodineObject target = Pop();
                Push(target.GetIndex(this, index));
                break;
            }

            case Opcode.CastLocal:
            {
                IodineTypeDefinition type = Pop() as IodineTypeDefinition;
                string       name         = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
                IodineObject o            = Top.LoadLocal(name);
                if (type == null)
                {
                    RaiseException(new IodineTypeException("TypeDef"));
                    break;
                }
                if (o.InstanceOf(type))
                {
                    Push(o);
                }
                else
                {
                    RaiseException(new IodineTypeException(type.Name));
                }
                break;
            }

            case Opcode.BinOp:
            {
                IodineObject op2 = Pop();
                IodineObject op1 = Pop();
                Push(op1.PerformBinaryOperation(this,
                                                (BinaryOperation)instruction.Argument,
                                                op2
                                                ));
                break;
            }

            case Opcode.UnaryOp:
            {
                Push(Pop().PerformUnaryOperation(this,
                                                 (UnaryOperation)instruction.Argument));
                break;
            }

            case Opcode.Invoke:
            {
                IodineObject   target    = Pop();
                IodineObject[] arguments = new IodineObject[instruction.Argument];
                for (int i = 1; i <= instruction.Argument; i++)
                {
                    arguments [instruction.Argument - i] = Pop();
                }
                Push(target.Invoke(this, arguments));
                break;
            }

            case Opcode.InvokeVar:
            {
                IodineObject        target    = Pop();
                List <IodineObject> arguments = new List <IodineObject> ();
                IodineTuple         tuple     = Pop() as IodineTuple;
                if (tuple == null)
                {
                    RaiseException(new IodineTypeException("Tuple"));
                    break;
                }
                for (int i = 0; i < instruction.Argument; i++)
                {
                    arguments.Add(Pop());
                }
                arguments.AddRange(tuple.Objects);
                Push(target.Invoke(this, arguments.ToArray()));
                break;
            }

            case Opcode.InvokeSuper:
            {
                IodineTypeDefinition target    = Pop() as IodineTypeDefinition;
                IodineObject[]       arguments = new IodineObject[instruction.Argument];
                for (int i = 1; i <= instruction.Argument; i++)
                {
                    arguments [instruction.Argument - i] = Pop();
                }

                target.Inherit(this, Top.Self, arguments);
                break;
            }

            case Opcode.Return:
            {
                Top.InstructionPointer = int.MaxValue;
                break;
            }

            case Opcode.Yield:
            {
                Top.Yielded = true;
                break;
            }

            case Opcode.JumpIfTrue:
            {
                if (Pop().IsTrue())
                {
                    Top.InstructionPointer = instruction.Argument;
                }
                break;
            }

            case Opcode.JumpIfFalse:
            {
                if (!Pop().IsTrue())
                {
                    Top.InstructionPointer = instruction.Argument;
                }
                break;
            }

            case Opcode.Jump:
            {
                Top.InstructionPointer = instruction.Argument;
                break;
            }

            case Opcode.BuildClass:
            {
                IodineName   name        = Pop() as IodineName;
                IodineString doc         = Pop() as IodineString;
                IodineMethod constructor = Pop() as IodineMethod;
                //CodeObject initializer = Pop as CodeObject;
                IodineTypeDefinition baseClass  = Pop() as IodineTypeDefinition;
                IodineTuple          interfaces = Pop() as IodineTuple;
                IodineClass          clazz      = new IodineClass(name.ToString(), new CodeObject(), constructor);

                if (baseClass != null)
                {
                    clazz.BaseClass = baseClass;
                    baseClass.BindAttributes(clazz);
                }

                for (int i = 0; i < instruction.Argument; i++)
                {
                    IodineObject val = Pop();
                    IodineObject key = Pop();

                    clazz.Attributes [val.ToString()] = key;
                }

                foreach (IodineObject obj in interfaces.Objects)
                {
                    IodineContract contract = obj as IodineContract;
                    if (!contract.InstanceOf(clazz))
                    {
                        //RaiseException (new IodineTypeException (contract.Name));
                        break;
                    }
                }

                clazz.SetAttribute("__doc__", doc);

                Push(clazz);
                break;
            }

            case Opcode.BuildMixin:
            {
                IodineName name = Pop() as IodineName;

                IodineMixin mixin = new IodineMixin(name.ToString());

                for (int i = 0; i < instruction.Argument; i++)
                {
                    IodineObject val = Pop();
                    IodineObject key = Pop();

                    mixin.Attributes [val.ToString()] = key;
                }

                Push(mixin);
                break;
            }

            case Opcode.BuildEnum:
            {
                IodineName name = Pop() as IodineName;

                IodineEnum ienum = new IodineEnum(name.ToString());
                for (int i = 0; i < instruction.Argument; i++)
                {
                    IodineInteger val = Pop() as IodineInteger;
                    IodineName    key = Pop() as IodineName;
                    ienum.AddItem(key.ToString(), (int)val.Value);
                }

                Push(ienum);
                break;
            }

            case Opcode.BuildContract:
            {
                IodineName name = Pop() as IodineName;

                IodineContract contract = new IodineContract(name.ToString());
                for (int i = 0; i < instruction.Argument; i++)
                {
                    IodineMethod val = Pop() as IodineMethod;
                    contract.AddMethod(val);
                }

                Push(contract);
                break;
            }

            case Opcode.BuildTrait:
            {
                IodineName name = Pop() as IodineName;

                IodineTrait trait = new IodineTrait(name.ToString());
                for (int i = 0; i < instruction.Argument; i++)
                {
                    IodineMethod val = Pop() as IodineMethod;
                    trait.AddMethod(val);
                }

                Push(trait);
                break;
            }

            case Opcode.BuildHash:
            {
                IodineDictionary hash = new IodineDictionary();
                for (int i = 0; i < instruction.Argument; i++)
                {
                    IodineObject val = Pop();
                    IodineObject key = Pop();
                    hash.Set(key, val);
                }
                Push(hash);
                break;
            }

            case Opcode.BuildList:
            {
                IodineObject[] items = new IodineObject[instruction.Argument];
                for (int i = 1; i <= instruction.Argument; i++)
                {
                    items [instruction.Argument - i] = Pop();
                }
                Push(new IodineList(items));
                break;
            }

            case Opcode.BuildTuple:
            {
                IodineObject[] items = new IodineObject[instruction.Argument];
                for (int i = 1; i <= instruction.Argument; i++)
                {
                    items [instruction.Argument - i] = Pop();
                }
                Push(new IodineTuple(items));
                break;
            }

            case Opcode.BuildClosure:
            {
                IodineObject obj    = Pop();
                IodineMethod method = obj as IodineMethod;
                Push(new IodineClosure(Top, method));
                break;
            }

            case Opcode.BuildGenExpr:
            {
                CodeObject method = Pop() as CodeObject;
                Push(new IodineGeneratorExpr(Top, method));
                break;
            }

            case Opcode.Slice:
            {
                IodineObject target = Pop();

                IodineInteger[] arguments = new IodineInteger[3];

                for (int i = 0; i < 3; i++)
                {
                    IodineObject obj = Pop();
                    arguments [i] = obj as IodineInteger;

                    if (obj != IodineNull.Instance && arguments [i] == null)
                    {
                        RaiseException(new IodineTypeException("Int"));
                        break;
                    }
                }

                IodineSlice slice = new IodineSlice(arguments [0], arguments [1], arguments [2]);

                Push(target.Slice(this, slice));

                break;
            }

            case Opcode.MatchPattern:
            {
                IodineObject collection = Pop().GetIterator(this);

                IodineObject[] items = new IodineObject[instruction.Argument];
                for (int i = 1; i <= instruction.Argument; i++)
                {
                    items [instruction.Argument - i] = Pop();
                }


                int index = 0;

                collection.IterReset(this);

                while (collection.IterMoveNext(this) && index < items.Length)
                {
                    IodineObject o = collection.IterGetCurrent(this);

                    if (items [index] is IodineTypeDefinition)
                    {
                        if (!o.InstanceOf(items [index] as IodineTypeDefinition))
                        {
                            Push(IodineBool.False);
                            break;
                        }
                    }
                    else
                    {
                        if (!o.Equals(items [index]))
                        {
                            Push(IodineBool.False);
                            break;
                        }
                    }

                    index++;
                }

                Push(IodineBool.Create(index == items.Length));

                break;
            }

            case Opcode.Unwrap:
            {
                IodineObject container = Pop();

                IodineObject value = container.Unwrap(this);

                if (instruction.Argument > 0)
                {
                    IodineInteger len = value.Len(this) as IodineInteger;

                    if (len == null || len.Value != instruction.Argument)
                    {
                        Push(IodineBool.False);
                        break;
                    }
                }

                Push(value);
                Push(IodineBool.True);

                break;
            }

            case Opcode.Unpack:
            {
                IodineTuple tuple = Pop() as IodineTuple;

                if (tuple == null)
                {
                    RaiseException(new IodineTypeException("Tuple"));
                    break;
                }

                if (tuple.Objects.Length != instruction.Argument)
                {
                    RaiseException(new IodineUnpackException(instruction.Argument));
                    break;
                }
                for (int i = tuple.Objects.Length - 1; i >= 0; i--)
                {
                    Push(tuple.Objects [i]);
                }
                break;
            }

            case Opcode.GetIter:
            {
                Push(Pop().GetIterator(this));
                break;
            }

            case Opcode.IterGetNext:
            {
                Push(Pop().IterGetCurrent(this));
                break;
            }

            case Opcode.IterMoveNext:
            {
                Push(IodineBool.Create(Pop().IterMoveNext(this)));
                break;
            }

            case Opcode.IterReset:
            {
                Pop().IterReset(this);
                break;
            }

            case Opcode.PushExceptionHandler:
            {
                Top.ExceptionHandlers.Push(new IodineExceptionHandler(frameCount, instruction.Argument));
                break;
            }

            case Opcode.PopExceptionHandler:
            {
                Top.ExceptionHandlers.Pop();
                break;
            }

            case Opcode.InstanceOf:
            {
                IodineObject         o    = Pop();
                IodineTypeDefinition type = Pop() as IodineTypeDefinition;
                if (type == null)
                {
                    RaiseException(new IodineTypeException("TypeDef"));
                    break;
                }
                Push(IodineBool.Create(o.InstanceOf(type)));
                break;
            }

            case Opcode.DynamicCast:
            {
                IodineObject         o    = Pop();
                IodineTypeDefinition type = Pop() as IodineTypeDefinition;
                if (type == null)
                {
                    RaiseException(new IodineTypeException("TypeDef"));
                    break;
                }
                if (o.InstanceOf(type))
                {
                    Push(o);
                }
                else
                {
                    Push(IodineNull.Instance);
                }
                break;
            }

            case Opcode.NullCoalesce:
            {
                IodineObject o1 = Pop();
                IodineObject o2 = Pop();
                if (o1 is IodineNull)
                {
                    Push(o2);
                }
                else
                {
                    Push(o1);
                }
                break;
            }

            case Opcode.BeginExcept:
            {
                bool rethrow = true;
                for (int i = 1; i <= instruction.Argument; i++)
                {
                    IodineTypeDefinition type = Pop() as IodineTypeDefinition;
                    if (type == null)
                    {
                        RaiseException(new IodineTypeException("TypeDef"));
                        break;
                    }

                    if (lastException.InstanceOf(type))
                    {
                        rethrow = false;
                        break;
                    }
                }
                if (rethrow)
                {
                    RaiseException(lastException);
                }
                break;
            }

            case Opcode.Raise:
            {
                IodineObject e = Pop();
                if (e.InstanceOf(IodineException.TypeDefinition))
                {
                    RaiseException(e);
                }
                else
                {
                    RaiseException(new IodineTypeException("Exception"));
                }
                break;
            }

            case Opcode.SwitchLookup:
            {
                Dictionary <int, IodineObject> lookup = new Dictionary <int, IodineObject> ();
                int needle = Pop().GetHashCode();
                for (int i = 0; i < instruction.Argument; i++)
                {
                    IodineObject value = Pop();
                    IodineObject key   = Pop();
                    lookup [key.GetHashCode()] = value;
                }
                if (lookup.ContainsKey(needle))
                {
                    lookup [needle].Invoke(this, new IodineObject[] { });
                    Push(IodineBool.True);
                }
                else
                {
                    Push(IodineBool.False);
                }
                break;
            }

            case Opcode.BeginWith:
            {
                IodineObject obj = Pop();
                obj.Enter(this);
                Top.DisposableObjects.Push(obj);
                break;
            }

            case Opcode.EndWith:
            {
                Top.DisposableObjects.Pop().Exit(this);
                break;
            }

            case Opcode.IncludeMixin:
            {
                IodineObject obj  = Pop();
                IodineObject type = Pop();

                foreach (KeyValuePair <string, IodineObject> attr in obj.Attributes)
                {
                    type.SetAttribute(attr.Key, attr.Value);
                }
                break;
            }

            case Opcode.ApplyMixin:
            {
                IodineObject type  = Pop();
                IodineMixin  mixin = Top.Module.ConstantPool [instruction.Argument] as IodineMixin;

                foreach (KeyValuePair <string, IodineObject> attr in mixin.Attributes)
                {
                    type.SetAttribute(attr.Key, attr.Value);
                }
                break;
            }

            case Opcode.BuildFunction:
            {
                MethodFlags flags = (MethodFlags)instruction.Argument;

                IodineString name       = Pop() as IodineString;
                IodineString doc        = Pop() as IodineString;
                CodeObject   bytecode   = Pop() as CodeObject;
                IodineTuple  parameters = Pop() as IodineTuple;

                IodineObject[] defaultValues      = new IodineObject[] { };
                int            defaultValuesStart = 0;

                if (flags.HasFlag(MethodFlags.HasDefaultParameters))
                {
                    IodineTuple   defaultValuesTuple = Pop() as IodineTuple;
                    IodineInteger startInt           = Pop() as IodineInteger;
                    defaultValues      = defaultValuesTuple.Objects;
                    defaultValuesStart = (int)startInt.Value;
                }

                IodineMethod method = new IodineMethod(
                    Top.Module,
                    name,
                    bytecode,
                    parameters,
                    flags,
                    defaultValues,
                    defaultValuesStart
                    );

                method.SetAttribute("__doc__", doc);

                Push(method);

                break;
            }
            }
        }
Ejemplo n.º 20
0
 public override IodineObject Add(VirtualMachine vm, IodineObject right)
 {
     IodineList list = new IodineList (Objects.ToArray ());
     right.IterReset (vm);
     while (right.IterMoveNext (vm)) {
         IodineObject o = right.IterGetCurrent (vm);
         list.Add (o);
     }
     return list;
 }
Ejemplo n.º 21
0
        private IodineObject Hex(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            string[] lut = new string[] {
                "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f",
                "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f",
                "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f",
                "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f",
                "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f",
                "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f",
                "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f",
                "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f",
                "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8a", "8b", "8c", "8d", "8e", "8f",
                "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f",
                "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af",
                "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf",
                "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf",
                "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df",
                "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef",
                "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff"
            };

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


            StringBuilder accum = new StringBuilder();

            if (args [0] is IodineBytes)
            {
                IodineBytes bytes = args [0] as IodineBytes;

                foreach (byte b in bytes.Value)
                {
                    accum.Append(lut [b]);
                }

                return(new IodineString(accum.ToString()));
            }

            if (args [0] is IodineString)
            {
                IodineString str = args [0] as IodineString;

                foreach (byte b in str.Value)
                {
                    accum.Append(lut [b]);
                }

                return(new IodineString(accum.ToString()));
            }

            IodineObject iterator = args [0].GetIterator(vm);


            if (iterator != null)
            {
                while (iterator.IterMoveNext(vm))
                {
                    IodineInteger b = iterator.IterGetCurrent(vm) as IodineInteger;

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

                    accum.Append(lut [b.Value & 0xFF]);
                }
            }

            return(new IodineString(accum.ToString()));
        }