示例#1
0
    def handle_in_check(self,lhs,rhs,active_event):
        '''
        Call has form:
            lhs in rhs
        
        rhs can have three basic types: it can be a list, a map, or a
        string.  That means that it can either be a WaldoMapVariable,
        a WaldoListVariable, a WaldoStringVariable, or a Python
        string.

        Instead of using static type inference at compile time to
        determine, for sake of development, just doing dynamic check
        to determine which type it is and do the in processing here.

        FIXME: it is faster to do the static checks with type
        inference, etc. at compile time rather than at run time.
        '''
        lhs_val = self.get_val_if_waldo(lhs,active_event)

        # handles Python string case
        if util.is_string(rhs):
            return lhs_val in rhs
        
        elif is_non_ext_text_var(rhs):
            return lhs_val in rhs.get_val(active_event)

        elif is_non_ext_map_var(rhs):
            return rhs.get_val(active_event).contains_key_called(active_event,lhs_val)
        
        elif is_non_ext_list_var(rhs):
            return rhs.get_val(active_event).contains_val_called(active_event,lhs_val)


        util.logger_assert(
            'Error when calling in: unknown right hand side of expression')
示例#2
0
    def get_for_iter(self,to_iter_over,active_event):
        '''
        When call for loop on Waldo variables, need to get item to
        iterate over
        '''

        if (isinstance(to_iter_over,dict) or
            isinstance(to_iter_over,list) or
            util.is_string(to_iter_over)):
            return iter(to_iter_over)

        if is_non_ext_text_var(to_iter_over):
            return iter(to_iter_over.get_val(active_event))

        if is_non_ext_map_var(to_iter_over):
            return iter(to_iter_over.get_val(active_event).get_keys(active_event))

        if is_non_ext_list_var(to_iter_over):
            # FIXME: This is an inefficient way of reading all values
            # over list.
            to_return = []
            internal_val = to_iter_over.get_val(active_event)
            for i in range(0, internal_val.get_len(active_event)):
                to_append = internal_val.get_val_on_key(active_event,i)

                # The reason that we do this here is that in a for
                # loop, the operation we perform in the compiled code
                # immediately following the actual for header is
                # assign to another Waldo variable the variable being
                # held by to_append.  (We do this through a set_val
                # call.  set_val must take in InternalMaps or
                # InternalLists.  Therefore, get_val on the
                # WaldoList/WaldoMap first.  Note this is unnecessary
                # for all other for iterations because none of the
                # others possibly return a WaldoObject to iterate over.
                if (is_non_ext_list_var(to_append) or
                    is_non_ext_map_var(to_append)):
                    to_append = to_append.get_val(active_event)
                
                to_return.append(to_append)

            return iter(to_return)

        util.logger_assert(
            'Calling get_for_iter on an object that does not support iteration')
示例#3
0
    def to_text(self,what_to_call_to_text_on,active_event):
        '''
        @returns {Python String}
        '''
        if not isinstance(what_to_call_to_text_on,WaldoLockedObj):
            return str(what_to_call_to_text_on)

        # strings for waldo variable value types
        if (is_non_ext_text_var(what_to_call_to_text_on) or
            is_non_ext_num_var(what_to_call_to_text_on) or
            is_non_ext_true_false_var(what_to_call_to_text_on)):
            return str(what_to_call_to_text_on.get_val(active_event))

        # strings for reference types
        # lists
        if is_non_ext_list_var(what_to_call_to_text_on):
            to_return_arg = ''
            waldo_internal_list = what_to_call_to_text_on.get_val(active_event)
            # get each element separately from the list and call
            # to_text on it.
            for i in range(0,waldo_internal_list.get_len(active_event)):
                list_val = waldo_internal_list.get_val_on_key(active_event,i)
                to_return_arg += self.to_text(list_val,active_event) + ', '
            
            return '[%s]' % to_return_arg

        # maps
        if is_non_ext_map_var(what_to_call_to_text_on):
            to_return_arg = ''
            waldo_internal_map = what_to_call_to_text_on.get_val(active_event)
            # get each element separately from the list and call
            # to_text on it.
            waldo_key_list = waldo_internal_map.get_keys(active_event)
            waldo_internal_key_list = waldo_key_list.get_val(active_event)
            
            for i in range(0,waldo_internal_key_list.get_len(active_event)):
                key_val = waldo_internal_key_list.get_val_on_key(active_event,i)

                # get item from map
                item_val = waldo_internal_map.get_val_on_key(active_event,key_val)

                
                to_return_arg += (
                    self.to_text(key_val,active_event) + ': ' +
                    self.to_text(item_val,active_event) + ', ')

            
            return '{%s}' % to_return_arg
示例#4
0
    def handle_len(self,what_calling_len_on, active_event):
        '''
        Can support python lists, dicts, strings, or waldo lists, waldo maps,
        waldo texts.
        
        @returns {int}
        '''
        if (isinstance(what_calling_len_on, dict) or
            isinstance(what_calling_len_on, list) or
            util.is_string(what_calling_len_on)):
            return len(what_calling_len_on)

        if is_non_ext_text_var(what_calling_len_on):
            return len(what_calling_len_on.get_val(active_event))


        if (is_non_ext_list_var(what_calling_len_on) or
            is_non_ext_map_var(what_calling_len_on)):
            return what_calling_len_on.get_val(active_event).get_len(active_event)

        util.logger_assert(
            'Calling len on an object that does not support the function')