public override IodineObject BindAttributes(IodineObject newStrBuf)
 {
     newStrBuf.SetAttribute("clear", new BuiltinMethodCallback(Clear, newStrBuf));
     newStrBuf.SetAttribute("append", new BuiltinMethodCallback(Append, newStrBuf));
     newStrBuf.SetAttribute("prepend", new BuiltinMethodCallback(Prepend, newStrBuf));
     return(newStrBuf);
 }
示例#2
0
 public override IodineObject BindAttributes(IodineObject obj)
 {
     obj.SetAttribute("start", new BuiltinMethodCallback(Start, obj));
     obj.SetAttribute("abort", new BuiltinMethodCallback(Abort, obj));
     obj.SetAttribute("alive", new BuiltinMethodCallback(Alive, obj));
     return(obj);
 }
示例#3
0
 public override IodineObject BindAttributes(IodineObject obj)
 {
     obj.SetAttribute("acquire", new BuiltinMethodCallback(Acquire, obj));
     obj.SetAttribute("release", new BuiltinMethodCallback(Release, obj));
     obj.SetAttribute("synchronize", new BuiltinMethodCallback(Synchronize, obj));
     return(obj);
 }
示例#4
0
        public override void Inherit(VirtualMachine vm, IodineObject self, IodineObject [] arguments)
        {
            var obj = Invoke(vm, arguments);

            foreach (KeyValuePair <string, IodineObject> kv in Attributes)
            {
                if (!self.HasAttribute(kv.Key))
                {
                    self.SetAttribute(kv.Key, kv.Value);
                }
                if (!obj.HasAttribute(kv.Key))
                {
                    obj.SetAttribute(kv.Key, kv.Value);
                }
            }
            Dictionary <string, IodineObject> childAttributes = obj.Attributes;

            foreach (KeyValuePair <string, IodineObject> kv in childAttributes)
            {
                if (kv.Value is IodineBoundMethod)
                {
                    IodineBoundMethod wrapper = (IodineBoundMethod)kv.Value;
                    wrapper.Bind(self);
                }
            }
            self.SetAttribute("__super__", obj);
            self.Base = obj;
        }
示例#5
0
            public override IodineObject BindAttributes(IodineObject obj)
            {
                IodineIterableMixin.ApplyMixin(obj);

                obj.SetAttribute("contains", new BuiltinMethodCallback(Contains, obj));
                obj.SetAttribute("substr", new BuiltinMethodCallback(Substring, obj));

                return(obj);
            }
示例#6
0
 public virtual void Inherit(VirtualMachine vm, IodineObject self, IodineObject[] arguments)
 {
     IodineObject obj = this.Invoke (vm, arguments);
     foreach (string attr in attributes.Keys) {
         if (!self.HasAttribute (attr))
             self.SetAttribute (attr, attributes [attr]);
         obj.SetAttribute (attr, attributes [attr]);
     }
     self.SetAttribute ("__super__", obj);
     self.Base = obj;
 }
示例#7
0
        public virtual void Inherit(VirtualMachine vm, IodineObject self, IodineObject[] arguments)
        {
            var obj = Invoke(vm, arguments);

            foreach (string attr in Attributes.Keys)
            {
                if (!self.HasAttribute(attr))
                {
                    self.SetAttribute(attr, Attributes [attr]);
                }
                obj.SetAttribute(attr, Attributes [attr]);
            }
            self.SetAttribute("__super__", obj);
            self.Base = obj;
        }
示例#8
0
 public IodineObject BindAttributes(IodineObject obj)
 {
     foreach (KeyValuePair<string, IodineObject> kv in attributes) {
         if (!obj.HasAttribute (kv.Key))
             obj.SetAttribute (kv.Key, kv.Value);
     }
     return obj;
 }
示例#9
0
        public override void Inherit(VirtualMachine vm, IodineObject self, IodineObject[] arguments)
        {
            IodineObject obj = Invoke(vm, arguments);

            foreach (KeyValuePair <string, IodineObject> kv in Attributes)
            {
                if (!self.HasAttribute(kv.Key))
                {
                    self.SetAttribute(kv.Key, kv.Value);
                }
                if (!obj.HasAttribute(kv.Key))
                {
                    obj.SetAttribute(kv.Key, kv.Value);
                }
            }
            self.SetAttribute("__super__", obj);
            self.Base = obj;
        }
示例#10
0
 public virtual IodineObject BindAttributes(IodineObject obj)
 {
     foreach (KeyValuePair <string, IodineObject> kv in Attributes)
     {
         if (!obj.HasAttribute(kv.Key))
         {
             obj.SetAttribute(kv.Key, kv.Value);
         }
     }
     return(obj);
 }
示例#11
0
 public override IodineObject BindAttributes(IodineObject obj)
 {
     obj.SetAttribute("write", new BuiltinMethodCallback(Write, obj));
     obj.SetAttribute("writeln", new BuiltinMethodCallback(Writeln, obj));
     obj.SetAttribute("read", new BuiltinMethodCallback(Read, obj));
     obj.SetAttribute("readln", new BuiltinMethodCallback(Readln, obj));
     obj.SetAttribute("kill", new BuiltinMethodCallback(Kill, obj));
     obj.SetAttribute("empty", new BuiltinMethodCallback(Empty, obj));
     obj.SetAttribute("alive", new BuiltinMethodCallback(Alive, obj));
     return(base.BindAttributes(obj));
 }
示例#12
0
 public override IodineObject BindAttributes(IodineObject newFile)
 {
     newFile.SetAttribute("write", new BuiltinMethodCallback(Write, newFile));
     newFile.SetAttribute("writeln", new BuiltinMethodCallback(Writeln, newFile));
     newFile.SetAttribute("read", new BuiltinMethodCallback(Read, newFile));
     newFile.SetAttribute("readln", new BuiltinMethodCallback(Readln, newFile));
     newFile.SetAttribute("close", new BuiltinMethodCallback(Close, newFile));
     newFile.SetAttribute("flush", new BuiltinMethodCallback(Flush, newFile));
     newFile.SetAttribute("readall", new BuiltinMethodCallback(ReadAll, newFile));
     return(newFile);
 }
示例#13
0
 public static void ApplyMixin(IodineObject obj)
 {
     obj.SetAttribute("each", new BuiltinMethodCallback(Each, obj));
     obj.SetAttribute("filter", new BuiltinMethodCallback(Filter, obj));
     obj.SetAttribute("first", new BuiltinMethodCallback(First, obj));
     obj.SetAttribute("map", new BuiltinMethodCallback(Map, obj));
     obj.SetAttribute("last", new BuiltinMethodCallback(Last, obj));
     obj.SetAttribute("reduce", new BuiltinMethodCallback(Reduce, obj));
 }
示例#14
0
 public override IodineObject BindAttributes(IodineObject obj)
 {
     obj.SetAttribute("contains", new BuiltinMethodCallback(Contains, obj));
     obj.SetAttribute("getSize", new BuiltinMethodCallback(GetSize, obj));
     obj.SetAttribute("clear", new BuiltinMethodCallback(Clear, obj));
     obj.SetAttribute("set", new BuiltinMethodCallback(Set, obj));
     obj.SetAttribute("get", new BuiltinMethodCallback(Get, obj));
     obj.SetAttribute("remove", new BuiltinMethodCallback(Remove, obj));
     return(obj);
 }
示例#15
0
        /// <summary>
        /// Raises an exception, throwing 'ex' as an IodineException object
        /// </summary>
        /// <param name="ex">Exception to raise.</param>
        public void RaiseException(IodineObject ex)
        {
            if (traceCallback != null)
            {
                traceCallback(TraceType.Exception, this, Top, Top.Location);
            }

            var handler = PopCurrentExceptionHandler();

            if (handler == null)   // No exception handler

            /*
             * The program has gone haywire and we ARE going to crash, however
             * we must attempt to properly dispose any objects created inside
             * Iodine's with statement
             */
            {
                StackFrame top = Top;

                while (top != null)
                {
                    while (top.DisposableObjects.Count > 0)
                    {
                        var obj = top.DisposableObjects.Pop();

                        try {
                            obj.Exit(this);  // Call __exit__
                        } catch (UnhandledIodineExceptionException) {
                            // Ignore this, we will throw one when we're done anyway
                        }
                    }
                    top = top.Parent;
                }

                throw new UnhandledIodineExceptionException(Top, ex);
            }

            ex.SetAttribute("stacktrace", new IodineString(GetStackTrace()));

            UnwindStack(frameCount - handler.Frame);

            lastException = ex;

            Top.InstructionPointer = handler.InstructionPointer;
        }
示例#16
0
        private IodineObject setAttribute(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 3)
            {
                vm.RaiseException(new IodineArgumentException(2));
                return(null);
            }
            IodineObject o1  = args [0];
            IodineString str = args [1] as IodineString;

            if (str == null)
            {
                vm.RaiseException(new IodineTypeException("Str"));
                return(null);
            }
            o1.SetAttribute(str.Value, args [2]);
            return(null);
        }
示例#17
0
 public void RaiseException(IodineObject ex)
 {
     if (exceptionHandlers.Count == 0) {
         throw new UnhandledIodineExceptionException (Top, ex);
     } else {
         IodineExceptionHandler handler = exceptionHandlers.Pop ();
         ex.SetAttribute ("stackTrace", new IodineString (Trace ()));
         Unwind (frameCount - handler.Frame);
         lastException = ex;
         Top.InstructionPointer = handler.InstructionPointer;
     }
 }
示例#18
0
 public override IodineObject BindAttributes(IodineObject obj)
 {
     obj.SetAttribute("kill", new BuiltinMethodCallback(Kill, obj));
     return(obj);
 }
示例#19
0
            public override IodineObject BindAttributes(IodineObject obj)
            {
                IodineIterableMixin.ApplyMixin(obj);

                obj.SetAttribute("lower", new BuiltinMethodCallback(Lower, obj));
                obj.SetAttribute("upper", new BuiltinMethodCallback(Upper, obj));
                obj.SetAttribute("substr", new BuiltinMethodCallback(Substring, obj));
                obj.SetAttribute("index", new BuiltinMethodCallback(IndexOf, obj));
                obj.SetAttribute("rindex", new BuiltinMethodCallback(RightIndex, obj));
                obj.SetAttribute("find", new BuiltinMethodCallback(Find, obj));
                obj.SetAttribute("rfind", new BuiltinMethodCallback(RightFind, obj));
                obj.SetAttribute("contains", new BuiltinMethodCallback(Contains, obj));
                obj.SetAttribute("replace", new BuiltinMethodCallback(Replace, obj));
                obj.SetAttribute("startswith", new BuiltinMethodCallback(StartsWith, obj));
                obj.SetAttribute("endswith", new BuiltinMethodCallback(EndsWith, obj));
                obj.SetAttribute("split", new BuiltinMethodCallback(Split, obj));
                obj.SetAttribute("join", new BuiltinMethodCallback(Join, obj));
                obj.SetAttribute("trim", new BuiltinMethodCallback(Trim, obj));
                obj.SetAttribute("format", new BuiltinMethodCallback(Format, obj));
                obj.SetAttribute("isalpha", new BuiltinMethodCallback(IsLetter, obj));
                obj.SetAttribute("isdigit", new BuiltinMethodCallback(IsDigit, obj));
                obj.SetAttribute("isalnum", new BuiltinMethodCallback(IsLetterOrDigit, obj));
                obj.SetAttribute("iswhitespace", new BuiltinMethodCallback(IsWhiteSpace, obj));
                obj.SetAttribute("issymbol", new BuiltinMethodCallback(IsSymbol, obj));
                obj.SetAttribute("ljust", new BuiltinMethodCallback(PadRight, obj));
                obj.SetAttribute("rjust", new BuiltinMethodCallback(PadLeft, obj));

                base.BindAttributes(obj);

                return(obj);
            }
示例#20
0
        public override void Inherit(VirtualMachine vm, IodineObject self, IodineObject[] arguments)
        {
            IodineObject obj = Invoke (vm, arguments);

            foreach (KeyValuePair<string, IodineObject> kv in attributes) {
                if (!self.HasAttribute (kv.Key))
                    self.SetAttribute (kv.Key, kv.Value);
                if (!obj.HasAttribute (kv.Key))
                    obj.SetAttribute (kv.Key, kv.Value);
            }
            self.SetAttribute ("__super__", obj);
            self.Base = obj;
        }
示例#21
0
 public override IodineObject BindAttributes(IodineObject newStr)
 {
     newStr.SetAttribute("lower", new BuiltinMethodCallback(Lower, newStr));
     newStr.SetAttribute("upper", new BuiltinMethodCallback(Upper, newStr));
     newStr.SetAttribute("substr", new BuiltinMethodCallback(Substring, newStr));
     newStr.SetAttribute("index", new BuiltinMethodCallback(IndexOf, newStr));
     newStr.SetAttribute("rindex", new BuiltinMethodCallback(RightIndex, newStr));
     newStr.SetAttribute("find", new BuiltinMethodCallback(Find, newStr));
     newStr.SetAttribute("rfind", new BuiltinMethodCallback(RightFind, newStr));
     newStr.SetAttribute("contains", new BuiltinMethodCallback(Contains, newStr));
     newStr.SetAttribute("replace", new BuiltinMethodCallback(Replace, newStr));
     newStr.SetAttribute("startswith", new BuiltinMethodCallback(StartsWith, newStr));
     newStr.SetAttribute("endswith", new BuiltinMethodCallback(EndsWith, newStr));
     newStr.SetAttribute("split", new BuiltinMethodCallback(Split, newStr));
     newStr.SetAttribute("join", new BuiltinMethodCallback(Join, newStr));
     newStr.SetAttribute("trim", new BuiltinMethodCallback(Trim, newStr));
     newStr.SetAttribute("format", new BuiltinMethodCallback(Format, newStr));
     newStr.SetAttribute("isalpha", new BuiltinMethodCallback(IsLetter, newStr));
     newStr.SetAttribute("isdigit", new BuiltinMethodCallback(IsDigit, newStr));
     newStr.SetAttribute("isalnum", new BuiltinMethodCallback(IsLetterOrDigit, newStr));
     newStr.SetAttribute("iswhitespace", new BuiltinMethodCallback(IsWhiteSpace, newStr));
     newStr.SetAttribute("issymbol", new BuiltinMethodCallback(IsSymbol, newStr));
     newStr.SetAttribute("ljust", new BuiltinMethodCallback(PadRight, newStr));
     newStr.SetAttribute("rjust", new BuiltinMethodCallback(PadLeft, newStr));
     return(newStr);
 }
示例#22
0
 public override IodineObject BindAttributes(IodineObject obj)
 {
     obj.SetAttribute("wait", new BuiltinMethodCallback(Wait, obj));
     obj.SetAttribute("signal", new BuiltinMethodCallback(Signal, obj));
     return(obj);
 }
示例#23
0
		public override void Inherit (VirtualMachine vm, IodineObject self, IodineObject[] arguments)
		{
			IodineObject obj = Invoke (vm, arguments);

			foreach (KeyValuePair<string, IodineObject> kv in Attributes) {
				if (!self.HasAttribute (kv.Key))
					self.SetAttribute (kv.Key, kv.Value);
				if (!obj.HasAttribute (kv.Key)) {
					obj.SetAttribute (kv.Key, kv.Value);
				}
			}
			Dictionary<string, IodineObject> childAttributes = obj.Attributes;

			foreach (KeyValuePair<string, IodineObject> kv in childAttributes) {
				if (kv.Value is IodineInstanceMethodWrapper) {
					IodineInstanceMethodWrapper wrapper = (IodineInstanceMethodWrapper)kv.Value;
					wrapper.Rewrap (self);
				}
			}
			self.SetAttribute ("__super__", obj);
			self.Base = obj;
		}
示例#24
0
            public override IodineObject BindAttributes(IodineObject newList)
            {
                base.BindAttributes(newList);

                IodineIterableMixin.ApplyMixin(newList);

                newList.SetAttribute("append", new BuiltinMethodCallback(Add, newList));
                newList.SetAttribute("prepend", new BuiltinMethodCallback(Prepend, newList));
                newList.SetAttribute("appendrange", new BuiltinMethodCallback(AddRange, newList));
                newList.SetAttribute("discard", new BuiltinMethodCallback(Discard, newList));
                newList.SetAttribute("remove", new BuiltinMethodCallback(Remove, newList));
                newList.SetAttribute("removeat", new BuiltinMethodCallback(RemoveAt, newList));
                newList.SetAttribute("contains", new BuiltinMethodCallback(Contains, newList));
                newList.SetAttribute("clear", new BuiltinMethodCallback(Clear, newList));
                newList.SetAttribute("index", new BuiltinMethodCallback(Index, newList));
                newList.SetAttribute("rindex", new BuiltinMethodCallback(RightIndex, newList));
                newList.SetAttribute("find", new BuiltinMethodCallback(Find, newList));
                newList.SetAttribute("rfind", new BuiltinMethodCallback(RightFind, newList));

                return(newList);
            }
示例#25
0
		public void RaiseException (IodineObject ex)
		{
			if (traceCallback != null) {
				traceCallback (TraceType.Exception, this, Top, currentLocation);
			}
			IodineExceptionHandler handler = PopCurrentExceptionHandler ();
			if (handler == null) { // No exception handler
				/*
				 * The program has gone haywire and we ARE going to crash, however
				 * we must attempt to properly dispose any objects created inside 
				 * Iodine's with statement
				 */
				StackFrame top = Top;
				while (top != null) {
					while (top.DisposableObjects.Count > 0) {
						IodineObject obj = top.DisposableObjects.Pop ();
						try {
							obj.Exit (this); // Call __exit__
						} catch (UnhandledIodineExceptionException) {
							// Ignore this, we will throw one when we're done anyway
						}
					}
					top = top.Parent;
				}
				throw new UnhandledIodineExceptionException (Top, ex);
			}
			ex.SetAttribute ("stackTrace", new IodineString (GetStackTrace ()));
			UnwindStack (frameCount - handler.Frame);
			lastException = ex;
			Top.InstructionPointer = handler.InstructionPointer;
		}
示例#26
0
        private void ExecuteInstruction()
        {
            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:
            {
                Top.StoreLocal(instruction.Argument, Pop());
                break;
            }

            case Opcode.LoadLocal:
            {
                Push(Top.LoadLocal(instruction.Argument));
                break;
            }

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

            case Opcode.LoadGlobal:
            {
                string name = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
                if (Top.Module.Attributes.ContainsKey(name))
                {
                    Push(Top.Module.GetAttribute(this, name));
                }
                else if (Globals.ContainsKey(name))
                {
                    Push(Globals [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.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.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    = (IodineTypeDefinition)Pop();
                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.BuildHash:
            {
                IodineHashMap hash = new IodineHashMap();
                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:
            {
                IodineMethod method = Pop() as IodineMethod;
                Push(new IodineClosure(Top, method));
                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;
            }
            }
        }
示例#27
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;
            }
            }
        }