From a58c813ddaba5b059080c99beb964849d832f3f6 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Mon, 18 Mar 2013 19:47:48 -0400 Subject: [PATCH] adding in a slice operator --- hy/compiler.py | 22 ++++++++++++++++++++++ tests/native_tests/language.hy | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/hy/compiler.py b/hy/compiler.py index 19b1d59..bad6434 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -247,6 +247,28 @@ class HyASTCompiler(object): slice=ast.Index(value=sli), ctx=ast.Load()) + @builds("slice") + def compile_slice_expression(self, expr): + expr.pop(0) # index + val = self.compile(expr.pop(0)) # target + + low = None + if expr != []: + low = self.compile(expr.pop(0)) + + high = None + if expr != []: + high = self.compile(expr.pop(0)) + + return ast.Subscript( + lineno=expr.start_line, + col_offset=expr.start_column, + value=val, + slice=ast.Slice(lower=low, + upper=high, + step=None), + ctx=ast.Load()) + @builds("assoc") def compile_assoc_expression(self, expr): expr.pop(0) # assoc diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index b532353..3a4ddcf 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -176,3 +176,10 @@ "NATIVE: test firsty things" (assert (= (first [1 2 3 4 5]) 1)) (assert (= (car [1 2 3 4 5]) 1))) + + +(defn test-slice [] + "NATIVE: test slice" + (assert (= (slice [1 2 3 4 5] 1) [2 3 4 5])) + (assert (= (slice [1 2 3 4 5] 1 3) [2 3])) + (assert (= (slice [1 2 3 4 5]) [1 2 3 4 5])))