Skip to content

Commit

Permalink
Added multiple operators for Boolean.
Browse files Browse the repository at this point in the history
  • Loading branch information
prachi2396 authored and freakboy3742 committed Apr 11, 2016
1 parent 060885f commit f498abf
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 141 deletions.
97 changes: 86 additions & 11 deletions python/common/org/python/types/Bool.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,86 @@ public org.python.types.Str __format__(org.python.Object format_string) {
__doc__ = ""
)
public org.python.Object __lt__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__lt__() has not been implemented.");
if (other instanceof org.python.types.Int) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) < ((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) < (((org.python.types.Bool) other).value ? 1 : 0));
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1.0 : 0.0) < (((org.python.types.Float) other).value));
}
throw new org.python.exceptions.TypeError("unorderable types: bool() < " + other.typeName() + "()");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __le__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__le__() has not been implemented.");
if (other instanceof org.python.types.Int) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) <= ((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) <= (((org.python.types.Bool) other).value ? 1 : 0));
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1.0 : 0.0) <= (((org.python.types.Float) other).value));
}
throw new org.python.exceptions.TypeError("unorderable types: bool() <= " + other.typeName() + "()");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __eq__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__eq__() has not been implemented.");

if (other instanceof org.python.types.Int) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) == ((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) == (((org.python.types.Bool) other).value ? 1 : 0));
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1.0 : 0.0) == (((org.python.types.Float) other).value));
}
return new org.python.types.Bool(false);
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __ne__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__ne__() has not been implemented.");
if (other instanceof org.python.types.Int) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) != ((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) != (((org.python.types.Bool) other).value ? 1 : 0));
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1.0 : 0.0) != (((org.python.types.Float) other).value));
}
return new org.python.types.Bool(true);
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __gt__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__gt__() has not been implemented.");
if (other instanceof org.python.types.Int) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) > ((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) > (((org.python.types.Bool) other).value ? 1 : 0));
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1.0 : 0.0) > (((org.python.types.Float) other).value));
}

throw new org.python.exceptions.TypeError("unorderable types: bool() > " + other.typeName() + "()");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __ge__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__ge__() has not been implemented.");
if (other instanceof org.python.types.Int) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) >= ((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) >= (((org.python.types.Bool) other).value ? 1 : 0));
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1.0 : 0.0) >= (((org.python.types.Float) other).value));
}
throw new org.python.exceptions.TypeError("unorderable types: bool() >= " + other.typeName() + "()");
}

@org.python.Method(
Expand Down Expand Up @@ -141,14 +185,36 @@ public org.python.Object __add__(org.python.Object other) {
__doc__ = ""
)
public org.python.Object __sub__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__sub__() has not been implemented.");
if (other instanceof org.python.types.Int) {
return new org.python.types.Int( (((org.python.types.Bool) this).value ? 1 : 0) - ((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Int( (((org.python.types.Bool) this).value ? 1 : 0) - (((org.python.types.Bool) other).value ? 1 : 0));
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Float( (((org.python.types.Bool) this).value ? 1.0 : 0.0) - (((org.python.types.Float) other).value));
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for -: 'bool' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __mul__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__mul__() has not been implemented.");
if (other instanceof org.python.types.Int) {
return new org.python.types.Int( (((org.python.types.Bool) this).value ? 1 : 0)*((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Int( (((org.python.types.Bool) this).value ? 1 : 0)*(((org.python.types.Bool) other).value ? 1 : 0));
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Float( (((org.python.types.Bool) this).value ? 1.0 : 0.0)*(((org.python.types.Float) other).value));
} else if (other instanceof org.python.types.Str) {
if(((org.python.types.Bool) this).value){
return new org.python.types.Str(((org.python.types.Str) other).value);
}
else{
return new org.python.types.Str("");

}
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for *: 'bool' and '" + other.typeName() + "'");
}

@org.python.Method(
Expand Down Expand Up @@ -245,21 +311,30 @@ public org.python.Object __rshift__(org.python.Object other) {
__doc__ = ""
)
public org.python.Object __and__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__and__() has not been implemented.");
if (other instanceof org.python.types.Bool) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) & (((org.python.types.Bool) other).value ? 1 : 0));
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for &: 'bool' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __xor__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__xor__() has not been implemented.");
if (other instanceof org.python.types.Bool) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) ^ (((org.python.types.Bool) other).value ? 1 : 0));
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for ^: 'bool' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __or__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("bool.__or__() has not been implemented.");
if (other instanceof org.python.types.Bool) {
return new org.python.types.Bool( (((org.python.types.Bool) this).value ? 1 : 0) | (((org.python.types.Bool) other).value ? 1 : 0));
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for |: 'bool' and '" + other.typeName() + "'");
}

@org.python.Method(
Expand Down
Loading

0 comments on commit f498abf

Please sign in to comment.