1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-25 22:53:16 +00:00

Merge branch 'master' into net

This commit is contained in:
Calvin Rose 2020-02-09 20:00:58 -06:00
commit 8ae6ae65a1
2 changed files with 34 additions and 6 deletions

View File

@ -1583,14 +1583,16 @@
ret)
(defn all
"Returns true if all xs are truthy, otherwise the first false or nil value."
"Returns true if all xs are truthy, otherwise the resulty of first
falsey predicate value, (pred x)."
[pred xs]
(var ret true)
(loop [x :in xs :while ret] (set ret (pred x)))
ret)
(defn some
"Returns nil if all xs are false or nil, otherwise returns the first true value."
"Returns nil if all xs are false or nil, otherwise returns the result of the
first truthy predicate, (pred x)."
[pred xs]
(var ret nil)
(loop [x :in xs :while (not ret)] (if-let [y (pred x)] (set ret y)))

View File

@ -28,6 +28,11 @@
#include "vector.h"
#endif
static int arity1or2(JanetFopts opts, JanetSlot *args) {
(void) opts;
int32_t arity = janet_v_count(args);
return arity == 1 || arity == 2;
}
static int fixarity1(JanetFopts opts, JanetSlot *args) {
(void) opts;
return janet_v_count(args) == 1;
@ -63,6 +68,27 @@ static JanetSlot genericSSI(JanetFopts opts, int op, JanetSlot s, int32_t imm) {
return target;
}
/* Emit an insruction that implements a form by itself. */
static JanetSlot opfunction(
JanetFopts opts,
JanetSlot *args,
int op,
Janet defaultArg2) {
JanetCompiler *c = opts.compiler;
int32_t len;
len = janet_v_count(args);
JanetSlot t;
if (len == 1) {
t = janetc_gettarget(opts);
janetc_emit_sss(c, op, t, args[0], janetc_cslot(defaultArg2), 1);
return t;
} else if (len == 2) {
t = janetc_gettarget(opts);
janetc_emit_sss(c, op, t, args[0], args[1], 1);
}
return t;
}
/* Emit a series of instructions instead of a function call to a math op */
static JanetSlot opreduce(
JanetFopts opts,
@ -113,7 +139,7 @@ static JanetSlot do_get(JanetFopts opts, JanetSlot *args) {
return opreduce(opts, args, JOP_GET, janet_wrap_nil());
}
static JanetSlot do_next(JanetFopts opts, JanetSlot *args) {
return opreduce(opts, args, JOP_NEXT, janet_wrap_nil());
return opfunction(opts, args, JOP_NEXT, janet_wrap_nil());
}
static JanetSlot do_modulo(JanetFopts opts, JanetSlot *args) {
return opreduce(opts, args, JOP_MODULO, janet_wrap_nil());
@ -143,7 +169,7 @@ static JanetSlot do_yield(JanetFopts opts, JanetSlot *args) {
}
}
static JanetSlot do_resume(JanetFopts opts, JanetSlot *args) {
return opreduce(opts, args, JOP_RESUME, janet_wrap_nil());
return opfunction(opts, args, JOP_RESUME, janet_wrap_nil());
}
static JanetSlot do_apply(JanetFopts opts, JanetSlot *args) {
/* Push phase */
@ -270,7 +296,7 @@ static const JanetFunOptimizer optimizers[] = {
{fixarity1, do_error},
{minarity2, do_apply},
{maxarity1, do_yield},
{fixarity2, do_resume},
{arity1or2, do_resume},
{fixarity2, do_in},
{fixarity3, do_put},
{fixarity1, do_length},
@ -293,7 +319,7 @@ static const JanetFunOptimizer optimizers[] = {
{NULL, do_neq},
{fixarity2, do_propagate},
{fixarity2, do_get},
{fixarity2, do_next},
{arity1or2, do_next},
{fixarity2, do_modulo},
{fixarity2, do_remainder},
};