Implement del

Closes #385.
This commit is contained in:
Nicolas Dandrimont 2013-12-21 23:33:44 +01:00 committed by Berker Peksag
parent 0f96c24965
commit 799c39ffad
3 changed files with 63 additions and 0 deletions

View File

@ -378,6 +378,39 @@ between the operands.
=> (infix (1 + 1))
2
del
---
.. versionadded:: 0.9.12
`del` removes an object from the current namespace.
.. code-block:: clj
=> (setv foo 42)
=> (del foo)
=> foo
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'foo' is not defined
`del` can also remove objects from a mapping, a list, ...
.. code-block:: clj
=> (setv test (list (range 10)))
=> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
=> (del (slice test 2 4)) ;; remove items from 2 to 4 excluded
=> test
[0, 1, 4, 5, 6, 7, 8, 9]
=> (setv dic {"foo" "bar"})
=> dic
{"foo": "bar"}
=> (del (get dic "foo"))
=> dic
{}
eval
----

View File

@ -1099,6 +1099,21 @@ class HyASTCompiler(object):
slice=ast.Index(value=sli.force_expr),
ctx=ast.Load())
@builds("del")
@checkargs(min=1)
def compile_del_expression(self, expr):
expr.pop(0)
ld_targets, ret = self._compile_collect(expr)
del_targets = []
for target in ld_targets:
del_targets.append(self._storeize(target, ast.Del))
return ret + ast.Delete(
lineno=expr.start_line,
col_offset=expr.start_column,
targets=del_targets)
@builds("slice")
@checkargs(min=1, max=4)
def compile_slice_expression(self, expr):

View File

@ -789,3 +789,18 @@
(assert (string? (string "a")))
(assert (string? (string 1)))
(assert (= u"unicode" (string "unicode"))))
(defn test-del []
"NATIVE: Test the behavior of del"
(setv foo 42)
(assert (= foo 42))
(del foo)
(assert (= 'good
(try
(do foo 'bad)
(except [NameError] 'good))))
(setv test (list (range 5)))
(del (get test 4))
(assert (= test [0 1 2 3]))
(del (get test 2))
(assert (= test [0 1 3])))