private IodineObject eval(VirtualMachine host, string source, IodineHashMap dict) { VirtualMachine vm = host; if (dict != null) { vm = new VirtualMachine(host.Context, new Dictionary <string, IodineObject> ()); foreach (string glob in host.Globals.Keys) { vm.Globals [glob] = host.Globals [glob]; } foreach (IodineObject key in dict.Keys.Values) { vm.Globals [key.ToString()] = dict.Dict [key.GetHashCode()]; } } IodineContext context = new IodineContext(); SourceUnit code = SourceUnit.CreateFromSource(source); IodineModule module = null; try { module = code.Compile(context); } catch (SyntaxException ex) { vm.RaiseException(new IodineSyntaxException(ex.ErrorLog)); return(null); } return(vm.InvokeMethod(module.Initializer, null, new IodineObject[] { })); }
private IodineObject eval(VirtualMachine vm, IodineObject self, IodineObject[] args) { if (args.Length <= 0) { vm.RaiseException(new IodineArgumentException(1)); return(null); } IodineString str = args [0] as IodineString; IodineHashMap map = null; if (str == null) { vm.RaiseException(new IodineTypeException("Str")); return(null); } if (args.Length >= 2) { map = args [1] as IodineHashMap; if (map == null) { vm.RaiseException(new IodineTypeException("HashMap")); return(null); } } return(eval(vm, str.ToString(), map)); }
public override IodineObject Equals(VirtualMachine vm, IodineObject right) { IodineHashMap hash = right as IodineHashMap; if (hash == null) { vm.RaiseException(new IodineTypeException("HashMap")); return(null); } return(IodineBool.Create(compareTo(hash))); }
private IodineList convertDataTable(DataTable table) { var resList = new IodineList (new IodineObject[]{}); foreach (DataRow row in table.Rows) { var items = new IodineHashMap (); foreach (DataColumn col in table.Columns) { items.Set (new IodineString(col.ColumnName), new IodineString((row [col.ColumnName]).ToString())); } resList.Add (items); } return resList; }
private IodineObject getAttributes(VirtualMachine vm, IodineObject self, IodineObject[] args) { if (args.Length < 1) { vm.RaiseException (new IodineArgumentException (1)); return null; } IodineObject o1 = args [0]; IodineHashMap map = new IodineHashMap (); foreach (string key in o1.Attributes.Keys) { map.Set (new IodineString (key), o1.Attributes [key]); } return map; }
private IodineObject getAttributes(VirtualMachine vm, IodineObject self, IodineObject[] args) { if (args.Length < 1) { vm.RaiseException(new IodineArgumentException(1)); return(null); } IodineObject o1 = args [0]; IodineHashMap map = new IodineHashMap(); foreach (string key in o1.Attributes.Keys) { map.Set(new IodineString(key), o1.Attributes [key]); } return(map); }
public override IodineObject Invoke (VirtualMachine vm, IodineObject[] args) { if (args.Length >= 1) { IodineList inputList = args [0] as IodineList; IodineHashMap ret = new IodineHashMap (); if (inputList != null) { foreach (IodineObject item in inputList.Objects) { IodineTuple kv = item as IodineTuple; if (kv != null) { ret.Set (kv.Objects [0], kv.Objects [1]); } } } return ret; } return new IodineHashMap (); }
private bool compareTo(IodineHashMap hash) { if (hash.Keys.Count != this.Keys.Count) { return(false); } foreach (int key in Keys.Keys) { if (!hash.Keys.ContainsKey(key)) { return(false); } if (hash.Dict [key].GetHashCode() != Dict [key].GetHashCode()) { return(false); } } return(true); }
public override IodineObject Invoke(VirtualMachine vm, IodineObject[] args) { if (args.Length >= 1) { IodineList inputList = args [0] as IodineList; IodineHashMap ret = new IodineHashMap(); if (inputList != null) { foreach (IodineObject item in inputList.Objects) { IodineTuple kv = item as IodineTuple; if (kv != null) { ret.Set(kv.Objects [0], kv.Objects [1]); } } } return(ret); } return(new IodineHashMap()); }
private IodineObject invoke(VirtualMachine vm, IodineObject self, IodineObject[] args) { if (args.Length <= 1) { vm.RaiseException(new IodineArgumentException(2)); return(null); } IodineHashMap hash = args [1] as IodineHashMap; Dictionary <string, IodineObject> items = new Dictionary <string, IodineObject> (); foreach (KeyValuePair <int, IodineObject> kv in hash.Keys) { items [kv.Value.ToString()] = hash.Dict [kv.Key]; } VirtualMachine newVm = new VirtualMachine(vm.Context, items); try { return(args [0].Invoke(newVm, new IodineObject[] {})); } catch (UnhandledIodineExceptionException ex) { vm.RaiseException(ex.OriginalException); return(null); } }
private IodineObject eval(VirtualMachine host, string source, IodineHashMap dict) { VirtualMachine vm = host; if (dict != null) { vm = new VirtualMachine (host.Context, new Dictionary<string, IodineObject> ()); foreach (string glob in host.Globals.Keys) { vm.Globals [glob] = host.Globals [glob]; } foreach (IodineObject key in dict.Keys.Values) { vm.Globals [key.ToString ()] = dict.Dict [key.GetHashCode ()]; } } IodineContext context = new IodineContext (); SourceUnit code = SourceUnit.CreateFromSource (source); IodineModule module = null; try { module = code.Compile (context); } catch (SyntaxException ex) { vm.RaiseException (new IodineSyntaxException (ex.ErrorLog)); return null; } return vm.InvokeMethod (module.Initializer, null, new IodineObject[]{ }); }
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; } } }
private void ExecuteInstruction() { currLoc = 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 (globalDict.ContainsKey (name)) { globalDict [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 (globalDict.ContainsKey (name)) { Push (globalDict [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: { this.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: { exceptionHandlers.Push (new IodineExceptionHandler (frameCount, instruction.Argument)); break; } case Opcode.PopExceptionHandler: { 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; } } }
private IodineObject eval(VirtualMachine host, string source, IodineHashMap dict) { VirtualMachine vm = host; if (dict != null) { vm = new VirtualMachine (new Dictionary<string, IodineObject> ()); foreach (string glob in host.Globals.Keys) { vm.Globals [glob] = host.Globals [glob]; } foreach (IodineObject key in dict.Keys.Values) { vm.Globals [key.ToString ()] = dict.Dict [key.GetHashCode ()]; } } ErrorLog log = new ErrorLog (); IodineModule module = IodineModule.CompileModuleFromSource (log, source); if (module == null || log.ErrorCount > 0) { IodineSyntaxException e = new IodineSyntaxException (log); vm.RaiseException (e); return null; } return vm.InvokeMethod (module.Initializer, null, new IodineObject[]{ }); }
private bool compareTo (IodineHashMap hash) { if (hash.Keys.Count != this.Keys.Count) return false; foreach (int key in Keys.Keys) { if (!hash.Keys.ContainsKey (key)) return false; if (hash.Dict [key].GetHashCode () != Dict [key].GetHashCode ()) return false; } return true; }