Skip to content

Commit

Permalink
Merge branch 'master' into reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed May 30, 2017
2 parents f0c76c8 + e56c62e commit 6a02897
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 286 deletions.
5 changes: 5 additions & 0 deletions batavia/builtins/divmod.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var exceptions = require('../core').exceptions
var types = require('../types')
var type_name = require('../core').type_name

function divmod(args, kwargs) {
var notAllowedTypes = [types.Bytearray, types.Bytes, types.Dict, types.FrozenSet, types.List, types.NoneType, types.NotImplementedType, types.Range, types.Set, types.Slice, types.Str, types.Tuple, types.Type]
if (arguments.length !== 2) {
throw new exceptions.BataviaError.$pyclass('Batavia calling convention not used.')
}
Expand All @@ -14,6 +16,9 @@ function divmod(args, kwargs) {
if (types.isinstance(args[0], types.Complex) || types.isinstance(args[1], types.Complex)) {
throw new exceptions.TypeError.$pyclass("can't take floor or mod of complex number.")
}
if (types.isinstance(args[0], notAllowedTypes) || types.isinstance(args[1], notAllowedTypes)) {
throw new exceptions.TypeError.$pyclass("unsupported operand type(s) for divmod(): '" + type_name(args[0]) + "' and '" + type_name(args[1]) + "'")
}

var div = Math.floor(args[0] / args[1])
var rem = args[0] % args[1]
Expand Down
15 changes: 5 additions & 10 deletions batavia/builtins/len.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var exceptions = require('../core').exceptions
var type_name = require('../core').type_name
var types = require('../types')
var callables = require('../core').callables

function len(args, kwargs) {
if (!args || args.length !== 1 || args[0] === undefined) {
Expand All @@ -9,18 +9,13 @@ function len(args, kwargs) {

var value = args[0]

if (types.isinstance(value, [types.Float, types.Int])) {
throw new exceptions.TypeError.$pyclass("object of type '" + type_name(value) + "' has no len()")
if (value.__len__) {
return callables.call_method(value, '__len__', [])
}

// if (args[0].hasOwnProperty("__len__")) {
// TODO: Fix context of python functions calling with proper vm
// throw new exceptions.NotImplementedError.$pyclass('Builtin Batavia len function is not supporting __len__ implemented.');
// return args[0].__len__.apply(vm);
// }

return new types.Int(args[0].length)
throw new exceptions.TypeError.$pyclass("object of type '" + type_name(value) + "' has no len()")
}

len.__doc__ = 'len(object)\n\nReturn the number of items of a sequence or collection.'

module.exports = len
6 changes: 4 additions & 2 deletions batavia/builtins/reversed.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
var exceptions = require('../core').exceptions
var types = require('../types')
var callables = require('../core').callables

function reversed(args, kwargs) {
var iterable = args[0]
if (args.length === 0) {
throw new exceptions.TypeError.$pyclass('reversed expected 1 arguments, got 0')
}
if (types.isinstance(iterable, [types.List, types.Tuple])) {
} else if (iterable.__reversed__) {
return callables.call_method(iterable, '__reversed__', [])
} else if (iterable.__len__ && iterable.__getitem__) {
var new_iterable = iterable.slice(0)
new_iterable.reverse()
return new types.List(new_iterable)
Expand Down
4 changes: 4 additions & 0 deletions batavia/types/Dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ Dict.prototype.toString = function() {
* Type conversions
**************************************************/

Dict.prototype.__len__ = function() {
return this.size
}

Dict.prototype.__bool__ = function() {
return this.size > 0
}
Expand Down
4 changes: 4 additions & 0 deletions batavia/types/FrozenSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ FrozenSet.prototype.toString = function() {
* Type conversions
**************************************************/

FrozenSet.prototype.__len__ = function() {
return this.data.size
}

FrozenSet.prototype.__bool__ = function() {
return this.data.__bool__()
}
Expand Down
4 changes: 4 additions & 0 deletions batavia/types/Set.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Set.prototype.toString = function() {
* Type conversions
**************************************************/

Set.prototype.__len__ = function() {
return this.data.size
}

Set.prototype.__bool__ = function() {
return this.data.__bool__()
}
Expand Down
4 changes: 4 additions & 0 deletions batavia/types/Str.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ Str.prototype.__ior__ = function(other) {
* https://docs.python.org/3.4/library/stdtypes.html#string-methods
**************************************************/

Str.prototype.__len__ = function() {
return this.length
}

Str.prototype.join = function(iter) {
var types = require('../types')

Expand Down
261 changes: 0 additions & 261 deletions tests/builtins/test_divmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,275 +14,14 @@ class BuiltinTwoargDivmodFunctionTests(BuiltinTwoargFunctionTestCase, TranspileT

not_implemented = [
'test_bool_bool',
'test_bool_bytearray',
'test_bool_bytes',
'test_bool_class',
'test_bool_dict',
'test_bool_float',
'test_bool_frozenset',
'test_bool_int',
'test_bool_list',
'test_bool_None',
'test_bool_NotImplemented',
'test_bool_range',
'test_bool_slice',
'test_bool_set',
'test_bool_str',
'test_bool_tuple',

'test_bytearray_bool',
'test_bytearray_bytearray',
'test_bytearray_bytes',
'test_bytearray_class',
'test_bytearray_dict',
'test_bytearray_float',
'test_bytearray_frozenset',
'test_bytearray_int',
'test_bytearray_list',
'test_bytearray_None',
'test_bytearray_NotImplemented',
'test_bytearray_range',
'test_bytearray_set',
'test_bytearray_slice',
'test_bytearray_str',
'test_bytearray_tuple',

'test_bytes_bool',
'test_bytes_bytearray',
'test_bytes_bytes',
'test_bytes_class',
'test_bytes_dict',
'test_bytes_float',
'test_bytes_frozenset',
'test_bytes_int',
'test_bytes_list',
'test_bytes_None',
'test_bytes_NotImplemented',
'test_bytes_range',
'test_bytes_str',
'test_bytes_slice',
'test_bytes_set',
'test_bytes_tuple',

'test_class_bool',
'test_class_bytearray',
'test_class_bytes',
'test_class_class',
'test_class_dict',
'test_class_float',
'test_class_frozenset',
'test_class_int',
'test_class_list',
'test_class_None',
'test_class_NotImplemented',
'test_class_range',
'test_class_set',
'test_class_slice',
'test_class_str',
'test_class_tuple',


'test_dict_bool',
'test_dict_bytearray',
'test_dict_bytes',
'test_dict_class',
'test_dict_dict',
'test_dict_float',
'test_dict_frozenset',
'test_dict_int',
'test_dict_list',
'test_dict_None',
'test_dict_NotImplemented',
'test_dict_range',
'test_dict_set',
'test_dict_slice',
'test_dict_str',
'test_dict_tuple',

'test_float_bool',
'test_float_bytearray',
'test_float_bytes',
'test_float_class',
'test_float_dict',
'test_float_float',
'test_float_frozenset',
'test_float_int',
'test_float_list',
'test_float_None',
'test_float_NotImplemented',
'test_float_range',
'test_float_set',
'test_float_slice',
'test_float_str',
'test_float_tuple',

'test_frozenset_bool',
'test_frozenset_bytearray',
'test_frozenset_bytes',
'test_frozenset_class',
'test_frozenset_dict',
'test_frozenset_float',
'test_frozenset_frozenset',
'test_frozenset_int',
'test_frozenset_list',
'test_frozenset_None',
'test_frozenset_NotImplemented',
'test_frozenset_range',
'test_frozenset_set',
'test_frozenset_slice',
'test_frozenset_str',
'test_frozenset_tuple',

'test_int_bool',
'test_int_bytearray',
'test_int_bytes',
'test_int_class',
'test_int_dict',
'test_int_float',
'test_int_frozenset',
'test_int_int',
'test_int_list',
'test_int_None',
'test_int_NotImplemented',
'test_int_range',
'test_int_set',
'test_int_slice',
'test_int_str',
'test_int_tuple',

'test_list_bool',
'test_list_bytearray',
'test_list_bytes',
'test_list_class',
'test_list_dict',
'test_list_float',
'test_list_frozenset',
'test_list_int',
'test_list_list',
'test_list_None',
'test_list_NotImplemented',
'test_list_range',
'test_list_set',
'test_list_slice',
'test_list_str',
'test_list_tuple',

'test_None_bool',
'test_None_bytearray',
'test_None_bytes',
'test_None_class',
'test_None_dict',
'test_None_float',
'test_None_frozenset',
'test_None_int',
'test_None_list',
'test_None_None',
'test_None_NotImplemented',
'test_None_range',
'test_None_slice',
'test_None_set',
'test_None_str',
'test_None_tuple',

'test_NotImplemented_bool',
'test_NotImplemented_bytearray',
'test_NotImplemented_bytes',
'test_NotImplemented_class',
'test_NotImplemented_dict',
'test_NotImplemented_float',
'test_NotImplemented_frozenset',
'test_NotImplemented_int',
'test_NotImplemented_list',
'test_NotImplemented_None',
'test_NotImplemented_NotImplemented',
'test_NotImplemented_range',
'test_NotImplemented_slice',
'test_NotImplemented_set',
'test_NotImplemented_str',
'test_NotImplemented_tuple',

'test_range_bool',
'test_range_bytearray',
'test_range_bytes',
'test_range_class',
'test_range_dict',
'test_range_float',
'test_range_frozenset',
'test_range_int',
'test_range_list',
'test_range_None',
'test_range_NotImplemented',
'test_range_range',
'test_range_set',
'test_range_slice',
'test_range_str',
'test_range_tuple',

'test_set_bool',
'test_set_bytearray',
'test_set_bytes',
'test_set_class',
'test_set_dict',
'test_set_float',
'test_set_frozenset',
'test_set_int',
'test_set_list',
'test_set_None',
'test_set_NotImplemented',
'test_set_range',
'test_set_set',
'test_set_slice',
'test_set_str',
'test_set_tuple',

'test_slice_bool',
'test_slice_bytearray',
'test_slice_bytes',
'test_slice_class',
'test_slice_dict',
'test_slice_float',
'test_slice_frozenset',
'test_slice_int',
'test_slice_list',
'test_slice_None',
'test_slice_NotImplemented',
'test_slice_range',
'test_slice_set',
'test_slice_slice',
'test_slice_str',
'test_slice_tuple',

'test_str_bool',
'test_str_bytearray',
'test_str_bytes',
'test_str_class',
'test_str_dict',
'test_str_float',
'test_str_frozenset',
'test_str_int',
'test_str_list',
'test_str_None',
'test_str_NotImplemented',
'test_str_range',
'test_str_set',
'test_str_slice',
'test_str_str',
'test_str_tuple',

'test_tuple_bool',
'test_tuple_bytearray',
'test_tuple_bytes',
'test_tuple_class',
'test_tuple_dict',
'test_tuple_float',
'test_tuple_frozenset',
'test_tuple_int',
'test_tuple_list',
'test_tuple_None',
'test_tuple_NotImplemented',
'test_tuple_range',
'test_tuple_set',
'test_tuple_slice',
'test_tuple_str',
'test_tuple_tuple',
]
Loading

0 comments on commit 6a02897

Please sign in to comment.