From 8bcb5e00193ae7304ee1409ac94893ece1701033 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 23 May 2018 23:43:48 -0400 Subject: [PATCH] Add where clause to list comprehension. --- src/compiler/boot.dst | 63 ++++++++++++++++++++++--------------------- test/suite1.dst | 2 +- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/compiler/boot.dst b/src/compiler/boot.dst index 09a3135f..b1a52f5f 100644 --- a/src/compiler/boot.dst +++ b/src/compiler/boot.dst @@ -231,38 +231,41 @@ value." (def bindings (get head1 i)) (def verb (ast.unwrap1 (get head1 (+ i 1)))) (def object (ast.unwrap1 (get head1 (+ i 2)))) - (switch verb - :range (do - (def [start end _inc] (ast.unwrap1 object)) - (def inc (if _inc _inc 1)) - (def endsym (gensym)) - (tuple 'do - (tuple 'var bindings start) - (tuple 'def endsym end) - (tuple 'while (tuple < bindings endsym) - (doone (+ i 3)) - (tuple ':= bindings (tuple + bindings inc))))) - :keys (do - (def $dict (gensym "dict")) + (if (= (ast.unwrap1 bindings) :where) + (tuple 'if verb (doone (+ i 2))) + (switch + verb + :range (do + (def [start end _inc] (ast.unwrap1 object)) + (def inc (if _inc _inc 1)) + (def endsym (gensym)) + (tuple 'do + (tuple 'var bindings start) + (tuple 'def endsym end) + (tuple 'while (tuple < bindings endsym) + (doone (+ i 3)) + (tuple ':= bindings (tuple + bindings inc))))) + :keys (do + (def $dict (gensym "dict")) + (tuple 'do + (tuple 'def $dict object) + (tuple 'var bindings (tuple next $dict nil)) + (tuple 'while (tuple not= nil bindings) + (doone (+ i 3)) + (tuple ':= bindings (tuple next $dict bindings))))) + :in (do + (def $len (gensym "len")) + (def $i (gensym "i")) + (def $indexed (gensym "indexed")) (tuple 'do - (tuple 'def $dict object) - (tuple 'var bindings (tuple next $dict nil)) - (tuple 'while (tuple not= nil bindings) + (tuple 'def $indexed object) + (tuple 'def $len (tuple length $indexed)) + (tuple 'var $i 0) + (tuple 'while (tuple < $i $len) + (tuple 'def bindings (tuple get $indexed $i)) (doone (+ i 3)) - (tuple ':= bindings (tuple next $dict bindings))))) - :in (do - (def $len (gensym "len")) - (def $i (gensym "i")) - (def $indexed (gensym "indexed")) - (tuple 'do - (tuple 'def $indexed object) - (tuple 'def $len (tuple length $indexed)) - (tuple 'var $i 0) - (tuple 'while (tuple < $i $len) - (tuple 'def bindings (tuple get $indexed $i)) - (doone (+ i 3)) - (tuple ':= $i (tuple + 1 $i))))) - (error ("unexpected loop verb: " verb)))))) + (tuple ':= $i (tuple + 1 $i))))) + (error ("unexpected loop verb: " verb))))))) (doone 0)) (defmacro for diff --git a/test/suite1.dst b/test/suite1.dst index 09596c16..2cd775f4 100644 --- a/test/suite1.dst +++ b/test/suite1.dst @@ -42,7 +42,7 @@ (defn assert-many [f n e] (var good true) - (for [i 0 n] + (loop [i :range [0 n]] (if (not (f i)) (:= good false))) (assert good e))