def create_new_environment(initialized_args: Dict[str, RObj], env: Environment): new_env: Environment = Environment(env) for name, val in initialized_args.items(): val_val = val.evaluate(new_env) new_env.add(name, val_val) return new_env
def arrange_params_with_function_args(params, func, env: Environment): res = func_args_arrange(params, func.input_args) new_env: Environment = Environment(env) for name, val in res.items(): new_env.add(name, val) return new_env
from R.Environment import Environment import R.RObj import R.BuiltIn as builtin from R.Function import FunctionObj, CallObj, Atomic, Arg import R.AtomicObjs as objs import R.Types as types import R.LanguageObjs as language this_env: Environment = Environment(builtin.built_in_env) this_env.set_global(this_env) print('123') items1 = [ language.AssignObj.create(objs.SymbolObj('h'), Atomic.create(1, types.IntegerType())), Atomic.create(2, types.IntegerType()), Atomic.create(5, types.IntegerType()) ] items2 = [ Atomic.create(False, types.LogicalType()), language.AssignObj.create(objs.SymbolObj('f'), Atomic.create(1, types.IntegerType())), Atomic.create(5, types.IntegerType()), language.AssignObj.create(objs.SymbolObj('h'), Atomic.create(1, types.IntegerType())) ] symbol_c = objs.SymbolObj('c') c_call1 = CallObj(symbol_c, items1)
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
from typing import List from abc import abstractmethod import R.Types as types from R.Environment import Environment from R.Function import FunctionObj, RObj, Arg, DotsObj, Param from R.AtomicObjs import ListItem, ListObj, VectorObj, VectorItem, Atomic, EmptyParamObj, NULLObj import R.RuntimeErrors as errors built_in_env: Environment = Environment(None) class BuiltInFun(FunctionObj): def __init__(self, name, input_args): super(BuiltInFun, self).__init__(input_args, None) self._type = types.BuiltInType() self.name = name @staticmethod @abstractmethod def create(*args): pass @abstractmethod def compute(self, params: List[Param], env: Environment): pass def cast_vector_items(items, python_type, r_type): ret = [VectorItem(item.name, Atomic(python_type(item.value.value), r_type())) for item in items]