Esempio n. 1
0
    def __setitem__(self, key, value):
        key = _colonify(key)
        if not _listp(value):
            raise TypeError('Values must be lists')
        head, tail, _ = _copy_list(value)

        if self.consistent:
            try:
                prev, cell = self._cell(key)
            except KeyError:
                pass
            else:
                cont = _cdr(cell)
                while cont and not _keywordp(_car(cont)):
                    cont = _cdr(cont)
                if head:
                    _setcdr(cell, head)
                    _setcdr(tail, cont)
                else:
                    _setcdr(cell, cont)
                return

        if head:
            _setcdr(tail, self.place)
            self._bind(cons(key, head))
        else:
            self._bind(cons(key, self.place))
Esempio n. 2
0
def test_cons_ctr():
    a = e.intern('a')
    b = e.intern('b')

    assert repr(e.cons(a, b)) == '(a . b)'
    assert repr(e.cons(a)) == '(a)'
    assert repr(e.cons()) == 'nil'
Esempio n. 3
0
def _copy_list(cell, endp=lambda c: not c):
    head, tail = None, None
    while cell and not endp(cell):
        if head is None:
            head = cons(_car(cell))
            tail = head
        else:
            new_tail = cons(_car(cell))
            _setcdr(tail, new_tail)
            tail = new_tail
        cell = _cdr(cell)
    return (head or _nil), tail, cell
Esempio n. 4
0
 def insert(self, index, value):
     """Insert an element at a given index."""
     if index == 0 and self.place:  # Insert before current head
         _push_head(self.place, value)
     elif index == 0 and not self.place:  # Empty, create new head
         self._bind(cons(value))
     else:
         prev = self._cell(index - 1)
         cell = _cdr(prev)
         if cell:  # Insert before interior cell
             _push_head(cell, value)
         else:  # Insert after final cell
             _setcdr(prev, cons(value))
Esempio n. 5
0
    def __setitem__(self, key, value):
        # If the underlying plist must remain consistent, check to see if the
        # key already exists. If it does, change it.
        if self.consistent:
            try:
                _, cell = self._cell(key)
                _setcar(_cdr(cell), value)
                return
            except KeyError:
                pass

        if self.colonify:
            key = _colonify(key)
        else:
            key = EmacsObject(key, prefer_symbol=True)

        # Push to the head of the list
        head = cons(value, self.place)
        head = cons(key, head)
        self._bind(head)
Esempio n. 6
0
def test_length():
    a = e.intern('a')
    b = e.intern('b')
    c = e.intern('c')

    assert len(e.list([a, b, c])) == 3
    assert len(e.vector([a, b])) == 2
    assert len(e.str('alpha')) == 5
    assert len(e.intern('nil')) == 0

    with pytest.raises(TypeError):
        len(e.cons(a, b))
Esempio n. 7
0
def _push_head(cell, value):
    tail = cons(_car(cell), _cdr(cell))
    _setcar(cell, value)
    _setcdr(cell, tail)