To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

Commit a3af5c17 authored by Stefan Marr's avatar Stefan Marr
Browse files

Adapted BigInteger primitives to use Integer.value_fits



- also removed redundant checks and introduced polymorphic getter for the embedded value in BigInteger and Integer
- used biginteger spelling for field name
Signed-off-by: default avatarStefan Marr <git@stefan-marr.de>
parent adb9a138
from som.primitives.primitives import Primitives
from som.vmobjects.primitive import Primitive
from som.vmobjects.integer import Integer
import math
class BigIntegerPrimitives(Primitives):
......@@ -21,122 +22,72 @@ class BigIntegerPrimitives(Primitives):
right_obj = frame.pop()
left = frame.pop()
# Check second parameter type:
if isinstance(right_obj, int):
# Second operand was Integer
right = self._universe.new_biginteger(right_obj.get_embedded_integer())
else:
right = right_obj
# Do operation and perform conversion to Integer if required
result = left.get_embedded_biginteger() + right.get_embedded_biginteger()
if isinstance(result, long):
frame.push(self._universe.new_biginteger(result))
else:
result = left.get_embedded_biginteger() + right_obj.get_embedded_value()
if Integer.value_fits(result):
frame.push(self._universe.new_integer(result))
else:
frame.push(self._universe.new_biginteger(result))
self._install_instance_primitive(Primitive("+", self._universe, _plus))
def _minus(ivkbl, frame, interpreter):
right_obj = frame.pop()
left = frame.pop()
# Check second parameter type:
if isinstance(right_obj, int):
# Second operand was Integer
right = self._universe.new_biginteger(right_obj.get_embedded_integer())
else:
right = right_obj
# Do operation and perform conversion to Integer if required
result = left.get_embedded_biginteger() - right.get_embedded_biginteger()
if isinstance(result, long):
frame.push(self._universe.new_biginteger(result))
result = left.get_embedded_biginteger() - right_obj.get_embedded_value()
if Integer.value_fits(result):
frame.push(self._universe.new_integer(result))
else:
frame.push(self._universe.new_integer(result.intValue()));
frame.push(self._universe.new_biginteger(result))
self._install_instance_primitive(Primitive("-", self._universe, _minus))
def _mult(ivkbl, frame, interpreter):
right_obj = frame.pop()
left = frame.pop()
# Check second parameter type:
if isinstance(right_obj, int):
# Second operand was Integer
right = self._universe.new_biginteger(right_obj.get_embedded_integer())
else:
right = right_obj
# Do operation and perform conversion to Integer if required
result = left.get_embedded_biginteger() * right.get_embedded_biginteger()
if isinstance(result, long):
frame.push(self._universe.new_biginteger(result))
else:
result = left.get_embedded_biginteger() * right_obj.get_embedded_value()
if Integer.value_fits(result):
frame.push(self._universe.new_integer(result))
else:
frame.push(self._universe.new_biginteger(result))
self._install_instance_primitive(Primitive("*", self._universe, _mult))
def _div(ivkbl, frame, interpreter):
right_obj = frame.pop()
left = frame.pop()
# Check second parameter type:
if isinstance(right_obj, int):
# Second operand was Integer
right = self._universe.new_biginteger(right_obj.get_embedded_integer())
else:
right = right_obj
# Do operation and perform conversion to Integer if required
result = left.get_embedded_biginteger() / right.get_embedded_biginteger()
if isinstance(result, long):
frame.push(self._universe.new_biginteger(result))
else:
result = left.get_embedded_biginteger() / right_obj.get_embedded_value()
if Integer.value_fits(result):
frame.push(self._universe.new_integer(result))
else:
frame.push(self._universe.new_biginteger(result))
self._install_instance_primitive(Primitive("/", self._universe, _div))
def _mod(ivkbl, frame, interpreter):
right_obj = frame.pop()
left = frame.pop()
# Check second parameter type:
if isinstance(right_obj, int):
# Second operand was Integer
right = self._universe.new_biginteger(right_obj.get_embedded_integer())
else:
right = right_obj
# Do operation:
frame.push(self._universe.new_biginteger(left.get_embedded_biginteger() % right.get_embedded_biginteger()))
frame.push(self._universe.new_biginteger(left.get_embedded_biginteger() % right_obj.get_embedded_value()))
self._install_instance_primitive(Primitive("%", self._universe, _mod))
def _and(ivkbl, frame, interpreter):
right_obj = frame.pop()
left = frame.pop()
# Check second parameter type:
if isinstance(right_obj, int):
# Second operand was Integer
right = self._universe.new_biginteger(right_obj.get_embedded_integer())
else:
right = right_obj
# Do operation:
frame.push(self._universe.new_biginteger(left.get_embedded_biginteger() & right.get_embedded_biginteger()))
frame.push(self._universe.new_biginteger(left.get_embedded_biginteger() & right_obj.get_embedded_value()))
self._install_instance_primitive(Primitive("&", self._universe, _and))
def _equals(ivkbl, frame, interpreter):
right_obj = frame.pop()
left = frame.pop()
# Check second parameter type:
if isinstance(right_obj, int):
# Second operand was Integer
right = self._universe.new_biginteger(right_obj.get_embedded_integer())
else:
right = right_obj
# Do operation:
if left.get_embedded_biginteger() == right.get_embedded_biginteger():
if left.get_embedded_biginteger() == right_obj.get_embedded_value():
frame.push(self._universe.trueObject)
else:
frame.push(self._universe.falseObject)
......@@ -145,16 +96,9 @@ class BigIntegerPrimitives(Primitives):
def _lessThan(ivkbl, frame, interpreter):
right_obj = frame.pop()
left = frame.pop()
# Check second parameter type:
if isinstance(right_obj, int):
# Second operand was Integer
right = self._universe.new_biginteger(right_obj.get_embedded_integer())
else:
right = right_obj
# Do operation:
if left.get_embedded_biginteger() < right.get_embedded_biginteger():
if left.get_embedded_biginteger() < right_obj.get_embedded_value():
frame.push(self._universe.trueObject)
else:
frame.push(self._universe.falseObject)
......
......@@ -3,10 +3,14 @@ from som.vmobjects.object import Object
class BigInteger(Object):
def __init__(self, nilObject):
super(BigInteger, self).__init__(nilObject)
self._embedded_big_integer = None
self._embedded_biginteger = None
def get_embedded_biginteger(self):
return self._embedded_big_integer
return self._embedded_biginteger
def get_embedded_value(self):
"""This Method is polymorphic with Integer"""
return self._embedded_biginteger
def set_embedded_biginteger(self, value):
self._embedded_big_integer = value
\ No newline at end of file
self._embedded_biginteger = value
......@@ -14,6 +14,10 @@ class Integer(Object):
def set_embedded_integer(self, value):
self._embedded_integer = value
def get_embedded_value(self):
"""This Method is polymorphic with BigInteger"""
return self._embedded_integer
def __str__(self):
return str(self._embedded_integer)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment