private void OpMax(ParsedOpCode obj) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); MainStack.Push(Math.Max(a, b)); }
/// <summary> /// Multiplies top two numbers on the stack. /// </summary> /// <param name="obj"></param> private void OpMul(ParsedOpCode obj) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); MainStack.Push(a * b); }
private void OpSub(ParsedOpCode obj) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); MainStack.Push(b - a); }
private void OpNumEqual(ParsedOpCode op) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); MainStack.Push(a == b ? 1 : 0); }
private void OpBoolOr(ParsedOpCode obj) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); MainStack.Push(a != 0 || b != 0 ? 1 : 0); }
private void OpNumNotEqual(ParsedOpCode obj) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); MainStack.Push(a != b ? 1 : 0); }
private void OpXor(ParsedOpCode op) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); MainStack.Push(a ^ b); }
private void OpLessThan(ParsedOpCode obj) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); MainStack.Push(b < a ? 1 : 0); }
private void OpLeft(ParsedOpCode op) { var high = MainStack.PopInt32(); var data = MainStack.Pop(); if (data.Length == 0 || high == 0) { MainStack.Push(new byte[0]); } else if (high < 0) { throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary less than zero"); } else if (high > data.Length) { throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary greater than array length"); } else if (high < 0) { throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary less than zero"); } else { var bytes = data.Take(high).ToArray(); MainStack.Push(bytes); } }
private void OpSubStr(ParsedOpCode op) { var startIndex = MainStack.PopInt32(); var endIndexExclusive = MainStack.PopInt32(); var array = MainStack.Pop(); if (array.Length == 0) { MainStack.Push(new byte[0]); } else if (startIndex < 0 || endIndexExclusive < 0) { throw new ScriptIndexOutOfRangeException(op.Code, "Negative substring index"); } else if (startIndex > array.Length || endIndexExclusive > array.Length) { throw new ScriptIndexOutOfRangeException(op.Code, "Substring index out of bounds"); } else if (startIndex > endIndexExclusive) { throw new ScriptIndexOutOfRangeException(op.Code, "Start index is greater than end index"); } else if (startIndex == endIndexExclusive) { MainStack.Push(new byte[0]); } else { var substr = array.Skip(startIndex).Take(endIndexExclusive - startIndex).ToArray(); MainStack.Push(substr); } }
private void OpRoll() { var n = MainStack.PopInt32(); var val = MainStack.Pop(n); MainStack.Push(val); }
private void OpGreaterThanOrEqual(ParsedOpCode obj) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); MainStack.Push(b > a ? 1 : 0); }
private void OpWithin(ParsedOpCode obj) { var max = MainStack.PopInt32(); var min = MainStack.PopInt32(); var test = MainStack.PopInt32(); MainStack.Push(min <= test && test < max ? 1 : 0); }
private void OpMod(ParsedOpCode op) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); if (a == 0) { throw new ArithemeticException(op.Code, "Division by zero"); } MainStack.Push(b % a); }
private void OpRShift(ParsedOpCode op) { var a = MainStack.PopInt32(); var b = MainStack.PopInt32(); if (a < 0) { throw new ScriptIndexOutOfRangeException(op.Code, "Negative shift"); } if (a > 32) { throw new ScriptIndexOutOfRangeException(op.Code, "Shift overflow"); } MainStack.Push(b >> a); }
private void OpRotl(ParsedOpCode op) { var rotate = MainStack.PopInt32(); var value = MainStack.PopInt32(); if (rotate < 0) { throw new ScriptIndexOutOfRangeException(op.Code, "attempted to rotate by negative value"); } if (rotate > 31) { throw new ScriptIndexOutOfRangeException(op.Code, "attempted to rotate by value > 31"); } var rotatedValue = (value << rotate) | (value >> (32 - rotate)); MainStack.Push(rotatedValue); }
private void OpRight(ParsedOpCode op) { var index = MainStack.PopInt32(); var data = MainStack.Pop(); if (data.Length == 0 || index == data.Length) { MainStack.Push(new byte[0]); } else if (index < 0) { throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary less than zero"); } else if (index > data.Length) { throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary greater than array length"); } else { MainStack.Push(data.Skip(index).ToArray()); } }
private void OpAbs(ParsedOpCode obj) { var value = MainStack.PopInt32(); MainStack.Push(Math.Abs(value)); }
private void OpInvert(ParsedOpCode op) { var value = MainStack.PopInt32(); MainStack.Push(~value); }
private void Op1Add(ParsedOpCode op) { MainStack.Push(MainStack.PopInt32() + 1); }
private void Op1Sub(ParsedOpCode op) { MainStack.Push(MainStack.PopInt32() - 1); }
private void OpNegate(ParsedOpCode obj) { MainStack.Push(-MainStack.PopInt32()); }
private void Op0NotEqual(ParsedOpCode obj) { var value = MainStack.PopInt32(); MainStack.Push(value == 0 ? 0 : 1); }
private void OpNot(ParsedOpCode obj) { var value = MainStack.PopInt32(); MainStack.Push(value == 0 ? 1 : 0); }
private void OpPick() { var n = MainStack.PopInt32(); MainStack.Push(MainStack.Peek(n)); }