public override void Execute(ICpu cpu) { Structure value = cpu.PopStructureEncapsulated(); // new value to set it to string suffixName = cpu.PopStack().ToString().ToUpper(); // name of suffix being set Structure popValue = cpu.PopStructureEncapsulated(); // object to which the suffix is attached. // We aren't converting the popValue to a Scalar, Boolean, or String structure here because // the referenced variable wouldn't be updated. The primitives themselves are treated as value // types instead of reference types. This is also why I removed the string unboxing // from the ISuffixed check below. var specialValue = popValue as ISuffixed; if (specialValue == null) { throw new Exception(string.Format("Values of type {0} cannot have suffixes", popValue.GetType())); } // TODO: When we refactor to make every structure use the new suffix style, this conversion // to primative can be removed. Right now there are too many structures that override the // SetSuffix method while relying on unboxing the object rahter than using Convert if (!specialValue.SetSuffix(suffixName, Structure.ToPrimitive(value))) { throw new Exception(string.Format("Suffix {0} not found on object", suffixName)); } }
public override void Execute(ICpu cpu) { Structure value = cpu.PopStructureEncapsulated(); var scalarValue = value as ScalarValue; if (scalarValue != null && scalarValue.IsValid) { cpu.PushStack(-scalarValue); return; } // Generic last-ditch to catch any sort of object that has // overloaded the unary negate operator '-'. // (For example, kOS.Suffixed.Vector and kOS.Suffixed.Direction) Type t = value.GetType(); MethodInfo negateMe = t.GetMethod("op_UnaryNegation", BindingFlags.FlattenHierarchy |BindingFlags.Static | BindingFlags.Public); if (negateMe != null) { object result = negateMe.Invoke(null, new[]{value}); cpu.PushStack(result); } else throw new KOSUnaryOperandTypeException("negate", value); }
public override void Execute(ICpu cpu) { Structure value = cpu.PopStructureEncapsulated(); Structure index = cpu.PopStructureEncapsulated(); Structure list = cpu.PopStructureEncapsulated(); if (index == null || value == null) { throw new KOSException("Neither the key nor the index of a collection may be null"); } var indexable = list as IIndexable; if (indexable == null) { throw new KOSException(string.Format("Can't set indexed elements on an object of type {0}", list.GetType())); } indexable.SetIndex(index, value); }
public override void Execute(ICpu cpu) { Structure index = cpu.PopStructureEncapsulated(); Structure collection = cpu.PopStructureEncapsulated(); Structure result; var indexable = collection as IIndexable; if (indexable != null) { result = indexable.GetIndex(index); } else { throw new Exception(string.Format("Can't iterate on an object of type {0}", collection.GetType())); } cpu.PushStack(result); }