Ejemplo n.º 1
0
    def create(input_args: List, body: RObj):
        args = []
        for arg in input_args:
            if isinstance(arg, RObj.get_r_obj('SymbolObj')):
                a = Arg(arg.name, None)
            elif isinstance(arg, RObj.get_r_obj('AssignObj')):
                if not isinstance(arg.item, RObj.get_r_obj('SymbolObj')):
                    raise errors.R_RuntimeError(
                        'invalid assigment of function')
                else:
                    a = Arg(arg.item.name, arg.value)
            args.append(a)

        return FunctionObj(args, body)
Ejemplo n.º 2
0
    def show_self(self):
        val_arg = self.argument.show_self()
        val_iter = self.iter_item.show_self()
        val_body = self.body.show_self()

        ret = 'for (' + val_arg + ' in ' + val_iter + ')' +\
              (val_body if isinstance(self.body, RObj.get_r_obj('SuiteObj')) else ' {}'.format(val_body))
        return ret
Ejemplo n.º 3
0
 def show_self(self, *args, **kwargs):
     val_arg = self.argument.show_self()
     val_body = self.body.show_self()
     val_alterbody = self.alterbody.show_self(
     ) if self.alterbody is not None else None
     res = 'if ('+val_arg+')' + (val_body if isinstance(self.body, RObj.get_r_obj('SuiteObj')) else ' {}'.format(val_body)) + \
           ('' if val_alterbody is None else ('\nelse ' (val_alterbody if isinstance(self.alterbody, RObj.get_r_obj('SuiteObj')) else ' {}'.format(val_alterbody))))
     return res
Ejemplo n.º 4
0
 def show_self(self):
     args = []
     for arg in self.input_args:
         arg_val = arg[0] + ((' = {}'.format(arg[1].show_self()))
                             if arg[1] is not None else '')
         args.append(arg_val)
     body_val = self.body.show_self()
     args_val = ', '.join(args)
     ret = 'function (' + args_val + ')' + \
           (body_val if isinstance(self.body, RObj.get_r_obj('SuiteObj'))
            else ' {}'.format(body_val))
     return ret
Ejemplo n.º 5
0
 def show_self_for_print(self, *args, **kwargs):
     args = []
     for arg in self.input_args:
         arg_val = arg.name + ((' = {}'.format(arg.value.show_self()))
                               if arg.value is not None else '')
         args.append(arg_val)
     body_val = self.body.show_self()
     args_val = ', '.join(args)
     ret = 'function (' + args_val + ')' + \
           (body_val if isinstance(self.body, RObj.get_r_obj('SuiteObj'))
            else ' {}'.format(body_val)) + '\n<bytecode: {}>'.format(id(self))
     return ret
Ejemplo n.º 6
0
 def get_default_class(self):
     if self.get_type().name == 'character':
         ret = 'character'
     elif self.get_type().name == 'double':
         ret = 'numeric'
     elif self.get_type().name == 'integer':
         ret = 'integer'
     elif self.get_type().name == 'logical':
         ret = 'logical'
     else:
         raise Exception('vector was initialized with improper type - {}'.format(self.get_type().name))
     return RObj.get_r_obj('Atomic')(ret, types.CharacterType())
Ejemplo n.º 7
0
    def compute(self, env: Environment):
        if self.base_obj.get_type().name == 'symbol':
            fun: FunctionObj = env.find_function(self.base_obj.name)
        else:
            fun: FunctionObj = self.base_obj.evaluate(env)
            if not isinstance(fun, FunctionObj):
                raise errors.ApplyToNonFunction()

        args = []

        assg_obj = RObj.get_r_obj('AssignObj')

        for item in self.items:
            # item_val = item.evaluate(env)
            if isinstance(item, DotsObj):
                args.extend(item.items)
            elif isinstance(item, Atomic):
                n = VectorObj.create([VectorItem(None, item)])
                args.append(Param(None, n))
            elif isinstance(item, assg_obj):
                if item.mode == 'plain':
                    if item.item.get_type().name == 'symbol':
                        name = item.item.name
                    elif isinstance(item.item, Atomic):
                        if item.item.get_type().name == 'character':
                            name = item.item[0]
                        else:
                            raise errors.InvalidLeftHandAssignment()
                    else:
                        raise errors.InvalidLeftHandAssignment()
                    arg = Param(name, item.value.evaluate(env))
                    args.append(arg)
                else:
                    arg = Param(None, item.evaluate(env))
                    args.append(arg)
            else:
                arg = Param(None, item.evaluate(env))
                args.append(arg)

        try:
            ret = fun.compute(args, env)
        except errors.R_RuntimeError as e:
            r = RError(self, e.message)
            if not self.exception_occurred(r):
                raise r
        return ret
Ejemplo n.º 8
0
 def create(items: List[VectorItem]):
     if len(items) == 0:
         return RObj.get_r_obj('NULLObj')()
     return VectorObj(items, type(items[0][1].type)())
Ejemplo n.º 9
0
 def get_default_class(self):
     return RObj.get_r_obj('Atomic')('function', types.CharacterType())
Ejemplo n.º 10
0
    def show_self(self):
        val_body = self.body.show_self()

        ret = 'repeat' + (val_body if isinstance(
            self.body, RObj.get_r_obj('SuiteObj')) else ' {}'.format(val_body))
        return ret
Ejemplo n.º 11
0
 def get_default_class(self):
     val_op = '='
     return RObj.get_r_obj('Atomic')(val_op, types.CharacterType())
Ejemplo n.º 12
0
    def evaluate(self, env: Environment):
        if self.as_from_lang:
            try:
                return self.base_obj.compute(env)
            except errors.R_RuntimeError as e:
                r = RError.create_simple(self, e.message)
                if not self.exception_occurred(r):
                    raise r

        if self.base_obj.get_type().name == 'symbol':
            fun: FunctionObj = env.find_function(self.base_obj.name)
            if not isinstance(fun, FunctionObj):
                raise RError.create_simple(self,
                                           errors.ApplyToNonFunction().message)
        else:
            fun: FunctionObj = self.base_obj.evaluate(env)
            if not isinstance(fun, FunctionObj):
                raise errors.ApplyToNonFunction()

        args = []

        assg_obj = RObj.get_r_obj('AssignObj')

        for item in self.items:
            if isinstance(item, DotsObj):
                args.extend(item.items)
            elif isinstance(item, Atomic):
                n = VectorObj.create([VectorItem(None, item)])
                args.append(Param(None, n))
            elif isinstance(item, assg_obj):
                if item.item.get_type().name == 'symbol':
                    name = item.item.name
                elif isinstance(item.item, Atomic):
                    if item.item.get_type().name == 'character':
                        name = item.item[0]
                    else:
                        raise RError.create_simple(
                            self,
                            errors.InvalidLeftHandAssignment().message)
                else:
                    raise RError.create_simple(
                        self,
                        errors.InvalidLeftHandAssignment().message)
                arg = Param(name, item.value.evaluate(env))
                args.append(arg)
            else:
                arg = Param(None, item.evaluate(env))
                args.append(arg)
            #     if item.mode == 'plain':
            #         if item.item.get_type().name == 'symbol':
            #             name = item.item.name
            #         elif isinstance(item.item, Atomic):
            #             if item.item.get_type().name == 'character':
            #                 name = item.item[0]
            #             else:
            #                 raise errors.InvalidLeftHandAssignment()
            #         else:
            #             raise errors.InvalidLeftHandAssignment()
            #         arg = Param(name, item.value.evaluate(env))
            #         args.append(arg)
            #     else:
            #         arg = Param(None, item.evaluate(env))
            #         args.append(arg)
            # else:
            #     arg = Param(None, item.evaluate(env))
            #     args.append(arg)

        try:
            res = func_args_arrange(args, fun.input_args)
            new_env: Environment = Environment(env)
            for name, val in res.items():
                new_env.add(name, val)
            ret = fun.compute(new_env)
            return ret
        except errors.R_RuntimeError as e:
            r = RError.create_simple(self, e.message)
            if not self.exception_occurred(r):
                raise r
Ejemplo n.º 13
0
def func_args_arrange(fun_params: List[Tuple],
                      fun_args: List[Tuple]) -> Dict[str, RObj]:
    args_res = {arg[0]: arg[1] for arg in fun_args}

    assign_type = RObj.get_r_obj('AssignObj')

    named_params = dict()

    unset_params = []

    params = list(enumerate(fun_params))

    for index, param in params:
        if isinstance(param, assign_type):
            if param.mode == 'plain':
                item = param.item
                if item.get_type().name == 'symbol':
                    named_params[item.name] = (index, param)
                elif isinstance(item, Atomic):
                    if item.type.name == 'character':
                        named_params[item.value] = (index, param)
                    else:
                        raise errors.InvalidLeftHandAssignment()
                else:
                    raise errors.InvalidLeftHandAssignment()
        else:
            unset_params.append((index, param))

    dots_items = []

    has_dots_obj = len(list(filter(lambda arg: arg[0] == '...',
                                   fun_args))) != 0

    for name, obj in named_params.items():
        if name not in args_res:
            if not has_dots_obj:
                raise errors.UnusedArguments(obj[1])
            else:
                dots_items.append(obj)
        else:
            args_res[name] = obj[1]

    unused_count = len(unset_params)

    i = 0

    res = args_res.copy()

    for arg_name, val in args_res.items():
        if val is None:
            if arg_name == '...':
                arr = [*unset_params[i:], *dots_items]
                itms = [i[1] for i in sorted(arr, key=lambda o: o[0])]
                res[arg_name] = DotsObj.create(itms)
                i = unused_count
            else:
                if i == unused_count:
                    res[arg_name] = EmptyParamObj(arg_name)
                else:
                    param = unset_params[i]
                    i = i + 1
                    res[arg_name] = param[1][1]

    return res
Ejemplo n.º 14
0
 def evaluate(self, env: Environment):
     if self.mode == 'super':
         env = env.global_env
         if self.item.get_type().name == 'symbol':
             val_value: RObj = self.value.evaluate(env)
             name = self.item.name
             env.add(name, val_value)
             return val_value
         elif isinstance(self.item, RObj.get_r_obj('Atomic')):
             if self.item.get_type().name == "character":
                 name = self.item.value
                 val_value: RObj = self.value.evaluate(env)
                 env.add(name, val_value)
                 return val_value
             else:
                 raise errors.InvalidLeftHandAssignment()
         else:
             val_value: RObj = self.value.evaluate(env)
             ret = self.item.set_value(val_value, env)
             return ret
         # if env == env.global_env:
         #     if self.item.get_type().name == 'symbol':
         #         val_value: RObj = self.init_if_atomic(self.value.evaluate(env))
         #         name = self.item.name
         #         env.add(name, val_value)
         #         return val_value
         #     elif isinstance(self.item, RObj.get_r_obj('Atomic')):
         #         if self.item.get_type().name == "character":
         #             name = self.item.value
         #             val_value: RObj = self.init_if_atomic(self.value.evaluate(env))
         #             env.add(name, val_value)
         #             return val_value
         #         else:
         #             raise errors.InvalidLeftHandAssignment()
         #     else:
         #         val_value: RObj = self.init_if_atomic(self.value.evaluate(env))
         #         ret = self.item.set_value(val_value, env)
         #         return ret
         # else:
         #     if self.item.get_type().name == 'symbol':
         #         val_value: RObj = self.init_if_atomic(self.value.evaluate(env))
         #         name = self.item.name
         #         env.add(name, val_value)
         #         env.global_env.add(name, val_value)
         #         return val_value
         #     elif isinstance(self.item, RObj.get_r_obj('Atomic')):
         #         if self.item.get_type().name == "character":
         #             name = self.item.value
         #             val_value: RObj = self.init_if_atomic(self.value.evaluate(env))
         #             env.add(name, val_value)
         #             env.global_env.add(name, val_value)
         #             return val_value
         #         else:
         #             raise errors.InvalidLeftHandAssignment()
         #     else:
         #         raise errors.InvalidLeftHandAssignment()
     else:
         if self.item.get_type().name == 'symbol':
             val_value: RObj = self.value.evaluate(env)
             name = self.item.name
             env.add(name, val_value)
             return val_value
         elif isinstance(self.item, RObj.get_r_obj('Atomic')):
             if self.item.get_type().name == "character":
                 name = self.item.value
                 val_value: RObj = self.value.evaluate(env)
                 env.add(name, val_value)
                 return val_value
             else:
                 raise errors.InvalidLeftHandAssignment()
         else:
             val_value: RObj = self.value.evaluate(env)
             ret = self.item.set_value(val_value, env)
             return ret