Example #1
0
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')
Example #2
0
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')
Example #3
0
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')
Example #4
0
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)
Example #5
0
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)
Example #6
0
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)
Example #7
0
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))
Example #8
0
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')
Example #9
0
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')
Example #10
0
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')
Example #11
0
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')
Example #12
0
def func_int_input(msg :objs.AILObject):
    try:
        i = int(input(str(msg)))

        return i
    except ValueError as e:
        return AILRuntimeError(str(e), 'ValueError')
Example #13
0
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)
Example #14
0
def func_int(obj):
    v = objs.unpack_ailobj(obj)

    try:
        return int(v)
    except Exception as e:
        return AILRuntimeError(str(e), 'PythonError')
Example #15
0
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')
Example #16
0
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')
Example #17
0
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))
Example #18
0
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)
Example #19
0
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)
Example #20
0
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)
Example #21
0
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')
Example #22
0
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)
Example #23
0
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)
Example #24
0
File: array.py Project: laomobk/AIL
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
Example #25
0
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')
Example #26
0
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])
Example #27
0
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
Example #28
0
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
Example #29
0
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')
Example #30
0
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)