Beispiel #1
0
 def assign(self, lhs, rhs, evaluation):
     if lhs.get_head_name() == 'List':
         if not (rhs.get_head_name() == 'List') or len(lhs.leaves) != len(rhs.leaves):
             evaluation.message(self.get_name(), 'shape', lhs, rhs)
             return False
         else:
             result = True
             for left, right in zip(lhs.leaves, rhs.leaves):
                 if not self.assign(left, right, evaluation):
                     result = False
             return result
     elif lhs.get_head_name() == 'Part':
         if len(lhs.leaves) < 1:
             evaluation.message(self.get_name(), 'setp', lhs)
             return False
         symbol = lhs.leaves[0]
         name = symbol.get_name()
         if not name:
             evaluation.message(self.get_name(), 'setps', symbol)
             return False
         if 'Protected' in evaluation.definitions.get_attributes(name):
             evaluation.message(self.get_name(), 'wrsym', name)
             return False
         rule = evaluation.definitions.get_ownvalue(name)
         if rule is None:
             evaluation.message(self.get_name(), 'noval', symbol)
             return False
         indices = lhs.leaves[1:]
         result = walk_parts([rule.replace], indices, evaluation, rhs)
         if result:
             evaluation.definitions.set_ownvalue(name, result)
         else:
             return False
     else:
         return self.assign_elementary(lhs, rhs, evaluation)
Beispiel #2
0
 def apply_normal(self, dims, default, data, evaluation):
     """System`Normal[System`SparseArray[System`Automatic, dims_List, default_, data_List]]"""
     its = [Expression("List", n) for n in dims.leaves]
     table = Expression("Table", default, *its)
     table = table.evaluate(evaluation)
     # Now, apply the rules...
     for item in data.leaves:
         pos, val = item.leaves
         if pos.has_form("List", None):
             table = walk_parts([table], pos.leaves, evaluation, val)
     return table
Beispiel #3
0
    def apply(self, list, i, evaluation):
        "Part[list_, i___]"

        if list == SymbolFailed:
            return
        indices = i.get_sequence()
        # How to deal with ByteArrays
        if list.get_head_name() == "System`ByteArray":
            list = list.evaluate(evaluation)
            if len(indices) > 1:
                print("Part::partd1: Depth of object ByteArray[<3>] " +
                      "is not sufficient for the given part specification.")
                return
            idx = indices[0]
            if idx.get_head_name() == "System`Integer":
                idx = idx.get_int_value()
                if idx == 0:
                    return Symbol("System`ByteArray")
                data = list._leaves[0].value
                lendata = len(data)
                if idx < 0:
                    idx = data - idx
                    if idx < 0:
                        evaluation.message("Part", "partw", i, list)
                        return
                else:
                    idx = idx - 1
                    if idx > lendata:
                        evaluation.message("Part", "partw", i, list)
                        return
                return Integer(data[idx])
            if idx == Symbol("System`All"):
                return list
            # TODO: handling ranges and lists...
            evaluation.message("Part", "notimplemented")
            return

        # Otherwise...
        result = walk_parts([list], indices, evaluation)
        if result:
            return result
Beispiel #4
0
    def assign(self, lhs, rhs, evaluation):
        if lhs.get_head_name() == 'System`List':
            if (not (rhs.get_head_name() == 'System`List')
                    or len(lhs.leaves) != len(rhs.leaves)):  # nopep8

                evaluation.message(self.get_name(), 'shape', lhs, rhs)
                return False
            else:
                result = True
                for left, right in zip(lhs.leaves, rhs.leaves):
                    if not self.assign(left, right, evaluation):
                        result = False
                return result
        elif lhs.get_head_name() == 'System`Part':
            if len(lhs.leaves) < 1:
                evaluation.message(self.get_name(), 'setp', lhs)
                return False
            symbol = lhs.leaves[0]
            name = symbol.get_name()
            if not name:
                evaluation.message(self.get_name(), 'setps', symbol)
                return False
            if 'System`Protected' in evaluation.definitions.get_attributes(
                    name):
                evaluation.message(self.get_name(), 'wrsym', symbol)
                return False
            rule = evaluation.definitions.get_ownvalue(name)
            if rule is None:
                evaluation.message(self.get_name(), 'noval', symbol)
                return False
            indices = lhs.leaves[1:]
            result = walk_parts([rule.replace], indices, evaluation, rhs)
            if result:
                evaluation.definitions.set_ownvalue(name, result)
            else:
                return False
        else:
            return self.assign_elementary(lhs, rhs, evaluation)