def _peek(this, size): this = convert_to_pyobj(this) size = unpack_ailobj(size) if type(size) != int: return AILRuntimeError('iobuffer.peek() needs an integer.') try: return this.__this___buffer.peek(size) except Exception as e: return AILRuntimeError(str(e), 'PythonError')
def _sys_exit(_, code): if not compare_type(code, INTEGER_TYPE): return AILRuntimeError('exit() needs an integer.') c = unpack_ailobj(code) import sys try: sys.exit(c) except Exception as e: return AILRuntimeError(str(e), 'PythonError')
def structobj_setattr(self, name :str, value): pthis = hasattr(self, '_pthis_') # check _pthis_ attr if name in self.protected and not pthis: return AILRuntimeError('Cannot modify a protected attribute.', 'AttributeError') if name in self.members and (pthis or not _is_reserved_name(name)): self.members[name] = _check_bound(self, value) else: return AILRuntimeError('struct \'%s\' object has no attribute \'%s\'' % (self['__name__'], name), 'AttributeError')
def float_add(self :obj.AILObject, other :obj.AILObject) -> obj.AILObject: if type(other['__value__']) not in (int , float): # do not have __value__ property return AILRuntimeError('Not support \'+\' with type %s' % other['__class__'].name, 'TypeError') sv = self['__value__'] so = other['__value__'] try: res = sv + so except Exception as e: return AILRuntimeError(str(e), 'PythonRuntimeError') return obj.ObjectCreater.new_object(FLOAT_TYPE, res)
def float_mod(self :obj.AILObject, other :obj.AILObject) -> obj.AILObject: if other['__value__'] is None: # do not have __value__ property return AILRuntimeError('Not support \'+\' with type %s' % str(other), 'TypeError') sv = self['__value__'] so = other['__value__'] try: res = sv % so except Exception as e: return AILRuntimeError(str(e), 'PythonRuntimeError') return obj.ObjectCreater.new_object(FLOAT_TYPE, res)
def str_muit(self, times: obj.AILObject) -> obj.AILObject: if type(times) != obj.AILObject: return AILRuntimeError('Cannot operate with Python object', 'TypeError') if times['__class__'] != integer.INTEGER_TYPE: return AILRuntimeError( 'Not support \'*\' with type %s' % times['__class__'], 'TypeError') t = times['__value__'] rs = self['__value__'] * t return obj.ObjectCreater.new_object(STRING_TYPE, rs)
def throw_error(msg, etype=None): now_f = MAIN_INTERPRETER_STATE.frame_stack[-1] where = now_f.code.name msg = unpack_ailobj(msg) if type is not None: etype = unpack_ailobj(etype) if not (type(msg) == type(etype) == str): return AILRuntimeError('throw() needs 1 or 2 string as arguments.') erro = AILRuntimeError(msg, etype, now_f) MAIN_INTERPRETER_STATE.err_stack.append(make_err_struct_object( erro, where))
def _write(this, string): this = convert_to_pyobj(this) string = unpack_ailobj(string) buf = this.__this___buffer if type(string) != str: return AILRuntimeError('iobuffer.write() needs a string.') try: if not buf.writable(): return AILRuntimeError('not writeable', 'OSError') return buf.write(string) except Exception as e: return AILRuntimeError(str(e), 'PythonError')
def _read(this, size): this = convert_to_pyobj(this) size = unpack_ailobj(size) buf = this.__this___buffer if type(size) != int: return AILRuntimeError('iobuffer.read() needs an integer.') try: if not buf.readable(): return AILRuntimeError('not readable', 'OSError') return buf.read(size) except Exception as e: return AILRuntimeError(str(e), 'PythonError')
def obj_getattr(aobj, name): if not _is_reserved_attr_name(name) and name in aobj.properties: return aobj[name] return AILRuntimeError( '\'%s\' object has no attribute \'%s\'' % (aobj['__class__'].name, name), 'AttributeError')
def _close(this): this = convert_to_pyobj(this) try: this.__this___buffer.close() this.__this_closed = True except Exception as e: return AILRuntimeError(str(e), 'PythonError')
def func_int_input(msg :objs.AILObject): try: i = int(input(str(msg))) return i except ValueError as e: return AILRuntimeError(str(e), 'ValueError')
def int_muit(self: obj.AILObject, other: obj.AILObject) -> obj.AILObject: if other['__value__'] is None: # do not have __value__ property return AILRuntimeError('Not support \'*\' with type %s' % str(other), 'TypeError') sv = self['__value__'] so = other['__value__'] try: res = sv * so except Exception as e: return AILRuntimeError(str(e), 'PythonRuntimeError') if res in range(*POOL_RANGE): return INTEGER_POOL[int(abs(POOL_RANGE[0]) + res)] return obj.ObjectCreater.new_object(INTEGER_TYPE, res)
def func_int(obj): v = objs.unpack_ailobj(obj) try: return int(v) except Exception as e: return AILRuntimeError(str(e), 'PythonError')
def convert_to_integer(pyint :int): from objects import integer, string try: if pyint['__class__'] in ( integer.INTEGER_TYPE, FLOAT_TYPE, string.STRING_TYPE): pyint['__value__'] = float(pyint['__value__']) return pyint elif type(pyint) in (int, float, str): return obj.ObjectCreater.new_object(integer.INTEGER_TYPE, float(pyint)) except ValueError as e: return AILRuntimeError(str(e), 'ValueError') return AILRuntimeError('argument must be a string or a number', 'TypeError')
def float_init(self :obj.AILObject, value :obj.AILObject): if type(value) in (float, int): self['__value__'] = value elif obj.compare_type(value, FLOAT_TYPE): self['__value__'] = value['__value__'] else: return AILRuntimeError('invalid number type \'%s\'' % type(value), 'TypeError')
def str_eq(self, ostr: obj.AILObject) -> obj.AILObject: if type(ostr) != obj.AILObject: return AILRuntimeError('Cannot operate with Python object', 'TypeError') if ostr['__class__'] != STRING_TYPE: return AILRuntimeError( 'Not support \'==\' with type %s' % ostr['__class__'], 'TypeError') ss = self['__value__'] os = ostr['__value__'] if len(ss) != len(os): return obj.ObjectCreater.new_object(bool.BOOL_TYPE, 0) else: s = sum([a == b for a, b in zip(ss, os)]) return obj.ObjectCreater.new_object(bool.BOOL_TYPE, s == len(os))
def _open(fp, mode): fp = unpack_ailobj(fp) mode = unpack_ailobj(mode) if not (type(fp) == type(mode) == str): return AILRuntimeError('open() needs 2 string as argument') try: iobuf = open(fp, mode) except FileNotFoundError: return null except OSError as e: return AILRuntimeError(str(e), 'OSError') except Exception as e: return AILRuntimeError(str(e), 'RuntimeError') return _convert_to_iobuffer(iobuf)
def int_add(self: obj.AILObject, other: obj.AILObject) -> obj.AILObject: if other['__class__'] not in ( INTEGER_TYPE, afloat.FLOAT_TYPE): # do not have __value__ property return AILRuntimeError( 'Not support \'+\' with type %s' % other['__class__'].name, 'TypeError') sv = self['__value__'] so = other['__value__'] try: res = sv + so except Exception as e: return AILRuntimeError(str(e), 'PythonRuntimeError') if res in range(*POOL_RANGE): return INTEGER_POOL[int(abs(POOL_RANGE[0]) + res)] return obj.ObjectCreater.new_object(INTEGER_TYPE, res)
def int_div(self: obj.AILObject, other: obj.AILObject) -> obj.AILObject: if other['__value__'] is None: # do not have __value__ property return AILRuntimeError('Not support \'/\' with type %s' % str(other), 'TypeError') if other['__value__'] == 0: return AILRuntimeError('0 cannot be used as a divisor', 'ZeroDivisonError') sv = self['__value__'] so = other['__value__'] try: res = sv / so except Exception as e: return AILRuntimeError(str(e), 'PythonRuntimeError') return obj.ObjectCreater.new_object(afloat.FLOAT_TYPE, res)
def pyfunc_func_call(self: obj.AILObject, *args) -> obj.AILObject: fobj = self['__pyfunction__'] try: rtn = fobj(*args) if rtn is None: return obj.null return obj.ObjectCreater.new_object(objects.wrapper.WRAPPER_TYPE, rtn) except Exception as e: return AILRuntimeError(str(e), 'PythonError')
def func_hex(x): if isinstance(x, objs.AILObject) and \ x['__class__'] == aint.INTEGER_TYPE: v = x['__value__'] elif isinstance(x, int): v = x else: return AILRuntimeError('hex() needs an integer', 'TypeError') return hex(v)
def func_ord(x): if isinstance(x, objs.AILObject) and \ x['__class__'] == astr.STRING_TYPE: v = x['__value__'] elif isinstance(x, int): v = x else: return AILRuntimeError('ord() needs a string', 'TypeError') return ord(v)
def _check_index(self, index): if isinstance(index, objs.AILObject) and \ index['__class__'] == integer.INTEGER_TYPE: i = index['__value__'] elif isinstance(index, int): i = index else: return AILRuntimeError('array subscript index must be integer.', 'TypeError') l = self['__value__'] if i >= len(l): return AILRuntimeError( 'index out of range (len %s, index %s)' % (len(l), str(i)), 'IndexError') return i
def struct_getattr(self, name :str): pthis = hasattr(self, '_pthis_') if name in self.members and ( pthis or not _is_reserved_name(name)): return self.members[name] return AILRuntimeError('struct \'%s\' has no attribute \'%s\'' % (self['__name__'], name), 'AttributeError')
def str_getitem(self, index: int): if isinstance(index, obj.AILObject) and \ index['__class__'] == integer.INTEGER_TYPE: i = index['__value__'] elif isinstance(index, int): i = index else: return AILRuntimeError('array subscript index must be integer.', 'TypeError') l = self['__value__'] if i >= len(l): return AILRuntimeError( 'index out of range (len %s, index %s)' % (len(l), str(i)), 'IndexError') return obj.convert_to_ail_object(l[i])
def func_show_struct(sobj): if not (objs.compare_type(sobj, struct.STRUCT_OBJ_TYPE) or objs.compare_type(sobj, struct.STRUCT_TYPE)): return AILRuntimeError('show_struct needs a struct type or object', 'TypeError') ln1 = str(sobj) + '\n' memb = sobj.members.items() meml = '\n'.join(['\t%s : %s' % (k, v) for k, v in memb if k[:2] != '__']) block = '{\n%s\n}' % meml return ln1 + block
def obj_setattr(aobj, name, value): from objects import string if obj.compare_type(name, string.STRING_TYPE): name = string.convert_to_string(name) if not _is_reserved_attr_name(name): return AILRuntimeError( '\'%s\' object has no attribute \'%s\'' % (aobj['__class__'].name, name), 'AttributeError') aobj.properties[name] = value
def int_init(self: obj.AILObject, value: obj.AILObject): if isinstance(value, int): self['__value__'] = value elif isinstance(value, float): o = obj.ObjectCreater.new_object(afloat.FLOAT_TYPE, value) self.reference = o.reference self.properties = o.properties elif obj.compare_type(value, INTEGER_TYPE): self['__value__'] = value['__value__'] else: return AILRuntimeError('invalid number type \'%s\'' % type(value), 'TypeError')
def new_struct(struct_type, default_list=None): # return a struct object if default_list is not None and \ not objs.compare_type(default_list, array.ARRAY_TYPE): return AILRuntimeError('member initialize need an array') elif default_list is not None: default_list = default_list['__value__'] if objs.compare_type(struct_type, struct.STRUCT_OBJ_TYPE): return AILRuntimeError('new() needs a struct type, not a struct object', 'TypeError') if not objs.compare_type(struct_type, struct.STRUCT_TYPE): return AILRuntimeError('new() need a struct type') m = struct_type.members.keys() if default_list is not None: if len(default_list) < len(m): return AILRuntimeError( 'struct \'%s\' initialize missing %d required argument(s) : %s' % (struct_type['__name__'], len(m), '(%s)' % (', '.join(m))), 'TypeError') elif len(default_list) > len(m): return AILRuntimeError( 'struct \'%s\' initialize takes %d argument(s) but %d were given' % (struct_type['__name__'], len(m), len(default_list)), 'TypeError') if default_list is not None: md = {k:v for k, v in zip(m, default_list)} else: md = {k:null.null for k in m} n = struct_type['__name__'] pl = struct_type.protected return objs.ObjectCreater.new_object( struct.STRUCT_OBJ_TYPE, n, md, struct_type, pl)