1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +00:00

Add where clause to list comprehension.

This commit is contained in:
Calvin Rose 2018-05-23 23:43:48 -04:00
parent b09bf72490
commit 8bcb5e0019
2 changed files with 34 additions and 31 deletions

View File

@ -231,38 +231,41 @@ value."
(def bindings (get head1 i)) (def bindings (get head1 i))
(def verb (ast.unwrap1 (get head1 (+ i 1)))) (def verb (ast.unwrap1 (get head1 (+ i 1))))
(def object (ast.unwrap1 (get head1 (+ i 2)))) (def object (ast.unwrap1 (get head1 (+ i 2))))
(switch verb (if (= (ast.unwrap1 bindings) :where)
:range (do (tuple 'if verb (doone (+ i 2)))
(def [start end _inc] (ast.unwrap1 object)) (switch
(def inc (if _inc _inc 1)) verb
(def endsym (gensym)) :range (do
(tuple 'do (def [start end _inc] (ast.unwrap1 object))
(tuple 'var bindings start) (def inc (if _inc _inc 1))
(tuple 'def endsym end) (def endsym (gensym))
(tuple 'while (tuple < bindings endsym) (tuple 'do
(doone (+ i 3)) (tuple 'var bindings start)
(tuple ':= bindings (tuple + bindings inc))))) (tuple 'def endsym end)
:keys (do (tuple 'while (tuple < bindings endsym)
(def $dict (gensym "dict")) (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 'do
(tuple 'def $dict object) (tuple 'def $indexed object)
(tuple 'var bindings (tuple next $dict nil)) (tuple 'def $len (tuple length $indexed))
(tuple 'while (tuple not= nil bindings) (tuple 'var $i 0)
(tuple 'while (tuple < $i $len)
(tuple 'def bindings (tuple get $indexed $i))
(doone (+ i 3)) (doone (+ i 3))
(tuple ':= bindings (tuple next $dict bindings))))) (tuple ':= $i (tuple + 1 $i)))))
:in (do (error ("unexpected loop verb: " verb)))))))
(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))))))
(doone 0)) (doone 0))
(defmacro for (defmacro for

View File

@ -42,7 +42,7 @@
(defn assert-many [f n e] (defn assert-many [f n e]
(var good true) (var good true)
(for [i 0 n] (loop [i :range [0 n]]
(if (not (f i)) (if (not (f i))
(:= good false))) (:= good false)))
(assert good e)) (assert good e))