1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 00:10:27 +00:00

Improve lazy2iter and fix a typo in specials.c

This commit is contained in:
Gavlooth 2018-03-23 15:18:04 +02:00
parent 41d5b5cb90
commit 0ebc95aa2b
2 changed files with 18 additions and 20 deletions

View File

@ -109,15 +109,14 @@ body once, and then memoizes the result."
#Iterators is a conscept that looks a lot like lazy seq #Iterators is a conscept that looks a lot like lazy seq
#The following functions turn iterators to lazy seq and vice versa #The following functions turn iterators to lazy seq and vice versa
(defn- iter-self
[next]
(delay (tuple (next) (iter-self next))))
(defn iter2lazy (defn iter2lazy
"Create a lazy sequence froma an iterator" "Create a lazy sequence from an iterator"
[iter] [iter]
(def {:more more :next next} iter) (def {:more more :next next} iter)
(iter-self next)) (if (more)
(delay (tuple (next) (iter2lazy iter)))
empty-seq))
(defn lazy2iter (defn lazy2iter
"turn a lazy-seq to an iterator" "turn a lazy-seq to an iterator"
@ -138,8 +137,7 @@ body once, and then memoizes the result."
#data structures as their values are references to this #data structures as their values are references to this
#data structures. Same is true for iterators #data structures. Same is true for iterators
(defn filter2 [pred coll] (defn filter2 [pred coll] (iter2lazy (filter pred coll)))
(tail (iter2lazy (filter pred coll))))
(def arr [0 -1 -2 33 -3 0 302 -3 2 8 54 3 -2 0]) (def arr [0 -1 -2 33 -3 0 302 -3 2 8 54 3 -2 0])

View File

@ -45,7 +45,7 @@ DstSlot dstc_astquote(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv)
} }
static void destructure(DstCompiler *c, Dst left, DstSlot right, static void destructure(DstCompiler *c, Dst left, DstSlot right,
void (*leaf)(DstCompiler *c, void (*leaf)(DstCompiler *c,
DstAst *ast, DstAst *ast,
const uint8_t *sym, const uint8_t *sym,
DstSlot s, DstSlot s,
@ -72,7 +72,7 @@ static void destructure(DstCompiler *c, Dst left, DstSlot right,
localright = dstc_preread(c, ast, 0xFF, 1, right); localright = dstc_preread(c, ast, 0xFF, 1, right);
localsub = dstc_lslotn(c, 0xFF, 3); localsub = dstc_lslotn(c, 0xFF, 3);
if (i < 0x100) { if (i < 0x100) {
dstc_emit(c, ast, dstc_emit(c, ast,
(i << 24) | (i << 24) |
(localright << 16) | (localright << 16) |
(localsub << 8) | (localsub << 8) |
@ -80,7 +80,7 @@ static void destructure(DstCompiler *c, Dst left, DstSlot right,
} else { } else {
DstSlot islot = dstc_cslot(dst_wrap_integer(i)); DstSlot islot = dstc_cslot(dst_wrap_integer(i));
int32_t locali = dstc_preread(c, ast, 0xFF, 2, islot); int32_t locali = dstc_preread(c, ast, 0xFF, 2, islot);
dstc_emit(c, ast, dstc_emit(c, ast,
(locali << 24) | (locali << 24) |
(localright << 16) | (localright << 16) |
(localsub << 8) | (localsub << 8) |
@ -111,7 +111,7 @@ static void destructure(DstCompiler *c, Dst left, DstSlot right,
localright = dstc_preread(c, ast, 0xFF, 1, right); localright = dstc_preread(c, ast, 0xFF, 1, right);
localsub = dstc_lslotn(c, 0xFF, 3); localsub = dstc_lslotn(c, 0xFF, 3);
int32_t localk = dstc_preread(c, ast, 0xFF, 2, kslot); int32_t localk = dstc_preread(c, ast, 0xFF, 2, kslot);
dstc_emit(c, ast, dstc_emit(c, ast,
(localk << 24) | (localk << 24) |
(localright << 16) | (localright << 16) |
(localsub << 8) | (localsub << 8) |
@ -211,7 +211,7 @@ static DstSlot namelocal(DstCompiler *c, DstAst *ast, Dst head, int32_t flags, D
ret = localslot; ret = localslot;
} }
ret.flags |= flags; ret.flags |= flags;
dstc_nameslot(c, dst_unwrap_symbol(head), ret); dstc_nameslot(c, dst_unwrap_symbol(head), ret);
return ret; return ret;
} }
@ -276,7 +276,7 @@ static void defleaf(
tableindex = dstc_preread(c, ast, 0xFF, 1, tabslot); tableindex = dstc_preread(c, ast, 0xFF, 1, tabslot);
valsymindex = dstc_preread(c, ast, 0xFF, 2, valsym); valsymindex = dstc_preread(c, ast, 0xFF, 2, valsym);
valueindex = dstc_preread(c, ast, 0xFF, 3, s); valueindex = dstc_preread(c, ast, 0xFF, 3, s);
dstc_emit(c, ast, dstc_emit(c, ast,
(valueindex << 24) | (valueindex << 24) |
(valsymindex << 16) | (valsymindex << 16) |
(tableindex << 8) | (tableindex << 8) |
@ -352,7 +352,7 @@ DstSlot dstc_if(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
} }
/* Set target for compilation */ /* Set target for compilation */
target = (drop || tail) target = (drop || tail)
? dstc_cslot(dst_wrap_nil()) ? dstc_cslot(dst_wrap_nil())
: dstc_gettarget(opts); : dstc_gettarget(opts);
@ -366,7 +366,7 @@ DstSlot dstc_if(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
/* Condition left body */ /* Condition left body */
dstc_scope(c, 0); dstc_scope(c, 0);
left = dstc_value(bodyopts, truebody); left = dstc_value(bodyopts, truebody);
if (!drop && !tail) dstc_copy(c, ast, target, left); if (!drop && !tail) dstc_copy(c, ast, target, left);
dstc_popscope(c); dstc_popscope(c);
/* Compile jump to done */ /* Compile jump to done */
@ -377,7 +377,7 @@ DstSlot dstc_if(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
labelr = dst_v_count(c->buffer); labelr = dst_v_count(c->buffer);
dstc_scope(c, 0); dstc_scope(c, 0);
right = dstc_value(bodyopts, falsebody); right = dstc_value(bodyopts, falsebody);
if (!drop && !tail) dstc_copy(c, ast, target, right); if (!drop && !tail) dstc_copy(c, ast, target, right);
dstc_popscope(c); dstc_popscope(c);
/* Write jumps - only add jump lengths if jump actually emitted */ /* Write jumps - only add jump lengths if jump actually emitted */
@ -584,7 +584,7 @@ DstSlot dstc_fn(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
dstc_freeslot(c, s); dstc_freeslot(c, s);
if (dstc_iserr(&opts)) return dstc_cslot(dst_wrap_nil()); if (dstc_iserr(&opts)) return dstc_cslot(dst_wrap_nil());
} }
/* Build function */ /* Build function */
def = dstc_pop_funcdef(c); def = dstc_pop_funcdef(c);
def->arity = arity; def->arity = arity;
@ -603,9 +603,9 @@ DstSlot dstc_fn(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
(defindex << 16) | (defindex << 16) |
(localslot << 8) | (localslot << 8) |
DOP_CLOSURE); DOP_CLOSURE);
if (ret.index != localslot) { if (ret.index != localslot) {
dstc_emit(c, ast, dstc_emit(c, ast,
(ret.index << 16) | (ret.index << 16) |
(localslot << 8) | (localslot << 8) |
DOP_MOVE_FAR); DOP_MOVE_FAR);
@ -614,7 +614,7 @@ DstSlot dstc_fn(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
return ret; return ret;
} }
/* Keep in lexographic order */ /* Keep in lexicographic order */
static const DstSpecial dstc_specials[] = { static const DstSpecial dstc_specials[] = {
{":=", dstc_varset}, {":=", dstc_varset},
{"ast-quote", dstc_astquote}, {"ast-quote", dstc_astquote},