Path: blob/main/python/pylang/src/baselib/internal.py
1398 views
# vim:fileencoding=utf-81# License: BSD23# globals: exports, console, ρσ_iterator_symbol, ρσ_kwargs_symbol, ρσ_arraylike, ρσ_list_contains, ρσ_list_constructor, ρσ_str, ρσ_int, ρσ_float45def ρσ_eslice(arr, step, start, end):6if jstype(arr) is 'string' or v'arr instanceof String':7is_string = True8arr = arr.split('')910if step < 0:11step = -step12arr = arr.slice().reverse()13if jstype(start) is not "undefined": start = arr.length - start - 114if jstype(end) is not "undefined": end = arr.length - end - 115if jstype(start) is "undefined": start = 016if jstype(end) is "undefined": end = arr.length1718arr = arr.slice(start, end).filter(def(e, i): return i % step is 0;)19if is_string:20arr = arr.join('')21return arr2223def ρσ_delslice(arr, step, start, end):24if jstype(arr) is 'string' or v'arr instanceof String':25is_string = True26arr = arr.split('')27if step < 0:28if jstype(start) is "undefined": start = arr.length29if jstype(end) is "undefined": end = 030start, end, step = end, start, -step31if jstype(start) is "undefined": start = 032if jstype(end) is "undefined": end = arr.length3334if step is 1:35arr.splice(start, end - start)36else:37if end > start:38indices = v'[]'39for v'var i = start; i < end; i += step':40indices.push(i)41for v'var i = indices.length - 1; i >= 0; i--':42arr.splice(indices[i], 1)4344if is_string:45arr = arr.join('')46return arr4748def ρσ_flatten(arr):49ans = []50for v'var i=0; i < arr.length; i++':51value = arr[i] # noqa:undef52if Array.isArray(value):53ans = ans.concat(ρσ_flatten(value))54else:55ans.push(value)56return ans5758def ρσ_unpack_asarray(num, iterable):59if ρσ_arraylike(iterable):60return iterable61ans = v'[]'62if jstype(iterable[ρσ_iterator_symbol]) is 'function':63iterator = iterable.keys() if jstype(Map) is 'function' and v'iterable instanceof Map' else iterable[ρσ_iterator_symbol]()64result = iterator.next()65while not result.done and ans.length < num:66ans.push(result.value)67result = iterator.next()68return ans6970def ρσ_extends(child, parent):71child.prototype = Object.create(parent.prototype)72child.prototype.constructor = child7374ρσ_in = (def ():75if jstype(Map) is 'function' and jstype(Set) is 'function':76return def(val, arr):77if jstype(arr) is 'string':78return arr.indexOf(val) is not -179if jstype(arr.__contains__) is 'function':80return arr.__contains__(val)81if v'arr instanceof Map || arr instanceof Set':82return arr.has(val)83if ρσ_arraylike(arr):84return ρσ_list_contains.call(arr, val)85return Object.prototype.hasOwnProperty.call(arr, val)86return def(val, arr):87if jstype(arr) is 'string':88return arr.indexOf(val) is not -189if jstype(arr.__contains__) is 'function':90return arr.__contains__(val)91if ρσ_arraylike(arr):92return ρσ_list_contains.call(arr, val)93return Object.prototype.hasOwnProperty.call(arr, val)94)()9596def ρσ_Iterable(iterable):97# Once ES6 is mature, change AST_ForIn to use the iterator protocol and get98# rid of this function entirely99if ρσ_arraylike(iterable):100return iterable101if jstype(iterable[ρσ_iterator_symbol]) is 'function':102iterator = iterable.keys() if jstype(Map) is 'function' and v'iterable instanceof Map' else iterable[ρσ_iterator_symbol]()103ans = []104result = iterator.next()105while not result.done:106ans.push(result.value)107result = iterator.next()108return ans109# so we can use 'for ... in' syntax with objects, as we would with dicts in python110return Object.keys(iterable)111112ρσ_desugar_kwargs = (def ():113if jstype(Object.assign) is 'function':114return def():115ans = Object.create(None)116ans[ρσ_kwargs_symbol] = True117for v'var i = 0; i < arguments.length; i++':118Object.assign(ans, arguments[i])119return ans120return def():121ans = Object.create(None)122ans[ρσ_kwargs_symbol] = True123for v'var i = 0; i < arguments.length; i++':124keys = Object.keys(arguments[i])125for v'var j = 0; j < keys.length; j++':126ans[keys[j]] = arguments[i][keys[j]]127return ans128)()129130def ρσ_interpolate_kwargs(f, supplied_args):131if not f.__argnames__:132return f.apply(this, supplied_args)133has_prop = Object.prototype.hasOwnProperty134kwobj = supplied_args.pop()135if f.__handles_kwarg_interpolation__:136args = Array(Math.max(supplied_args.length, f.__argnames__.length) + 1)137args[-1] = kwobj138for v'var i = 0; i < args.length - 1; i++':139if i < f.__argnames__.length:140prop = f.__argnames__[i]141if has_prop.call(kwobj, prop):142args[i] = kwobj[prop]143v'delete kwobj[prop]'144elif i < supplied_args.length:145args[i] = supplied_args[i]146else:147args[i] = supplied_args[i]148return f.apply(this, args)149150for v'var i = 0; i < f.__argnames__.length; i++':151prop = f.__argnames__[i]152if has_prop.call(kwobj, prop):153supplied_args[i] = kwobj[prop]154return f.apply(this, supplied_args)155156def ρσ_interpolate_kwargs_constructor(apply, f, supplied_args):157if apply:158f.apply(this, supplied_args)159else:160ρσ_interpolate_kwargs.call(this, f, supplied_args)161return this162163def ρσ_getitem(obj, key):164if obj.__getitem__:165return obj.__getitem__(key)166if jstype(key) is 'number' and key < 0:167key += obj.length168return obj[key]169170def ρσ_setitem(obj, key, val):171if obj.__setitem__:172obj.__setitem__(key, val)173else:174if jstype(key) is 'number' and key < 0:175key += obj.length176obj[key] = val177178def ρσ_delitem(obj, key):179if obj.__delitem__:180obj.__delitem__(key)181elif jstype(obj.splice) is 'function':182obj.splice(key, 1)183else:184if jstype(key) is 'number' and key < 0:185key += obj.length186v'delete obj[key]'187188def ρσ_bound_index(idx, arr):189if jstype(idx) is 'number' and idx < 0:190idx += arr.length191return idx192193def ρσ_splice(arr, val, start, end):194start = start or 0195if start < 0:196start += arr.length197if end is undefined:198end = arr.length199if end < 0:200end += arr.length201Array.prototype.splice.apply(arr, v'[start, end - start].concat(val)')202203ρσ_exists = {204'n': def(expr):205return expr is not undefined and expr is not None206,'d': def(expr):207if expr is undefined or expr is None:208return Object.create(None)209return expr210,'c': def(expr):211if jstype(expr) is 'function':212return expr213return def():214return undefined215,'g': def(expr):216if expr is undefined or expr is None or jstype(expr.__getitem__) is not 'function':217return {'__getitem__': def(): return undefined;}218,'e': def(expr, alt):219return alt if expr is undefined or expr is None else expr220}221222def ρσ_mixin():223# Implement a depth-first left-to-right method resolution order This is not224# the same as python's MRO, but I really dont feel like implementing the C3225# linearization right now, if that is even possible with prototypical226# inheritance.227seen = Object.create(None)228# Ensure the following special properties are never copied229seen.__argnames__ = seen.__handles_kwarg_interpolation__ = seen.__init__ = seen.__annotations__ = seen.__doc__ = seen.__bind_methods__ = seen.__bases__ = seen.constructor = seen.__class__ = True230resolved_props = {}231p = target = arguments[0].prototype232while p and p is not Object.prototype:233props = Object.getOwnPropertyNames(p)234for v'var i = 0; i < props.length; i++':235seen[props[i]] = True236p = Object.getPrototypeOf(p)237for v'var c = 1; c < arguments.length; c++':238p = arguments[c].prototype239while p and p is not Object.prototype:240props = Object.getOwnPropertyNames(p)241for v'var i = 0; i < props.length; i++':242name = props[i]243if seen[name]:244continue245seen[name] = True246resolved_props[name] = Object.getOwnPropertyDescriptor(p, name)247p = Object.getPrototypeOf(p)248Object.defineProperties(target, resolved_props)249250def ρσ_instanceof():251obj = arguments[0]252bases = ''253if obj and obj.constructor and obj.constructor.prototype:254bases = obj.constructor.prototype.__bases__ or ''255for v'var i = 1; i < arguments.length; i++':256q = arguments[i]257if v'obj instanceof q':258return True259if (q is Array or q is ρσ_list_constructor) and Array.isArray(obj):260return True261if q is ρσ_str and (jstype(obj) is 'string' or v'obj instanceof String'):262return True263if q is ρσ_int and (jstype(obj) is 'number' and Number.isInteger(obj)):264return True265if q is ρσ_float and (jstype(obj) is 'number' and not Number.isInteger(obj)):266return True267if bases.length > 1:268for v'var c = 1; c < bases.length; c++':269cls = bases[c]270while cls:271if q is cls:272return True273p = Object.getPrototypeOf(cls.prototype)274if not p:275break276cls = p.constructor277return False278279280