mirror of
https://github.com/janet-lang/janet
synced 2024-11-28 19:19:53 +00:00
commit
3ac6b2335a
@ -438,6 +438,15 @@ static JanetSlot janetc_array(JanetFopts opts, Janet x) {
|
||||
JOP_MAKE_ARRAY);
|
||||
}
|
||||
|
||||
static JanetSlot janetc_tuple(JanetFopts opts, Janet x) {
|
||||
JanetCompiler *c = opts.compiler;
|
||||
const Janet *t = janet_unwrap_tuple(x);
|
||||
return janetc_maker(opts,
|
||||
janetc_toslots(c, t, janet_tuple_length(t)),
|
||||
JOP_MAKE_TUPLE);
|
||||
}
|
||||
|
||||
|
||||
static JanetSlot janetc_tablector(JanetFopts opts, Janet x, int op) {
|
||||
JanetCompiler *c = opts.compiler;
|
||||
return janetc_maker(opts,
|
||||
@ -547,6 +556,8 @@ JanetSlot janetc_value(JanetFopts opts, Janet x) {
|
||||
/* Empty tuple is tuple literal */
|
||||
if (janet_tuple_length(tup) == 0) {
|
||||
ret = janetc_cslot(x);
|
||||
} else if (janet_tuple_flag(tup) & JANET_TUPLE_FLAG_BRACKETCTOR) { // [] tuples are not function call
|
||||
ret = janetc_tuple(opts, x);
|
||||
} else {
|
||||
JanetSlot head = janetc_value(subopts, tup[0]);
|
||||
subopts.flags = JANET_FUNCTION | JANET_CFUNCTION;
|
||||
|
@ -440,12 +440,12 @@
|
||||
(defmacro for
|
||||
"Do a c style for loop for side effects. Returns nil."
|
||||
[binding start end & body]
|
||||
(apply loop [tuple binding :range [tuple start end]] body))
|
||||
(apply loop (tuple binding :range (tuple start end)) body))
|
||||
|
||||
(defmacro each
|
||||
"Loop over each value in ind. Returns nil."
|
||||
[binding ind & body]
|
||||
(apply loop [tuple binding :in ind] body))
|
||||
(apply loop (tuple binding :in ind) body))
|
||||
|
||||
(defmacro coro
|
||||
"A wrapper for making fibers. Same as (fiber/new (fn [&] ...body))."
|
||||
@ -778,8 +778,8 @@
|
||||
[x & forms]
|
||||
(defn fop [last n]
|
||||
(def [h t] (if (= :tuple (type n))
|
||||
[tuple (get n 0) (array/slice n 1)]
|
||||
[tuple n @[]]))
|
||||
(tuple (get n 0) (array/slice n 1))
|
||||
(tuple n @[])))
|
||||
(def parts (array/concat @[h last] t))
|
||||
(tuple/slice parts 0))
|
||||
(reduce fop x forms))
|
||||
@ -791,8 +791,8 @@
|
||||
[x & forms]
|
||||
(defn fop [last n]
|
||||
(def [h t] (if (= :tuple (type n))
|
||||
[tuple (get n 0) (array/slice n 1)]
|
||||
[tuple n @[]]))
|
||||
(tuple (get n 0) (array/slice n 1))
|
||||
(tuple n @[])))
|
||||
(def parts (array/concat @[h] t @[last]))
|
||||
(tuple/slice parts 0))
|
||||
(reduce fop x forms))
|
||||
@ -806,8 +806,8 @@
|
||||
[x & forms]
|
||||
(defn fop [last n]
|
||||
(def [h t] (if (= :tuple (type n))
|
||||
[tuple (get n 0) (array/slice n 1)]
|
||||
[tuple n @[]]))
|
||||
(tuple (get n 0) (array/slice n 1))
|
||||
(tuple n @[])))
|
||||
(def sym (gensym))
|
||||
(def parts (array/concat @[h sym] t))
|
||||
~(let [,sym ,last] (if ,sym ,(tuple/slice parts 0))))
|
||||
@ -822,8 +822,8 @@
|
||||
[x & forms]
|
||||
(defn fop [last n]
|
||||
(def [h t] (if (= :tuple (type n))
|
||||
[tuple (get n 0) (array/slice n 1)]
|
||||
[tuple n @[]]))
|
||||
(tuple (get n 0) (array/slice n 1))
|
||||
(tuple n @[])))
|
||||
(def sym (gensym))
|
||||
(def parts (array/concat @[h] t @[sym]))
|
||||
~(let [,sym ,last] (if ,sym ,(tuple/slice parts 0))))
|
||||
|
@ -402,11 +402,13 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
|
||||
goto done;
|
||||
case JANET_TUPLE:
|
||||
{
|
||||
int32_t i, count;
|
||||
int32_t i, count,flag;
|
||||
const Janet *tup = janet_unwrap_tuple(x);
|
||||
count = janet_tuple_length(tup);
|
||||
flag = janet_tuple_flag(tup);
|
||||
pushbyte(st, LB_TUPLE);
|
||||
pushint(st, count);
|
||||
pushint(st, flag);
|
||||
for (i = 0; i < count; i++)
|
||||
marshal_one(st, tup[i], flags + 1);
|
||||
/* Mark as seen AFTER marshaling */
|
||||
@ -1044,6 +1046,8 @@ static const uint8_t *unmarshal_one(
|
||||
} else if (lead == LB_TUPLE) {
|
||||
/* Tuple */
|
||||
Janet *tup = janet_tuple_begin(len);
|
||||
int32_t flag = readint(st, &data);
|
||||
janet_tuple_flag(tup)=flag;
|
||||
for (int32_t i = 0; i < len; i++) {
|
||||
data = unmarshal_one(st, data, tup + i, flags + 1);
|
||||
}
|
||||
|
@ -342,6 +342,15 @@ static Janet close_tuple(JanetParser *p, JanetParseState *state) {
|
||||
return janet_wrap_tuple(janet_tuple_end(ret));
|
||||
}
|
||||
|
||||
static Janet close_ltuple(JanetParser *p, JanetParseState *state) {
|
||||
Janet *ret = janet_tuple_begin(state->argn);
|
||||
janet_tuple_flag(ret) |= JANET_TUPLE_FLAG_BRACKETCTOR;
|
||||
for (int32_t i = state->argn - 1; i >= 0; i--)
|
||||
ret[i] = p->args[--p->argcount];
|
||||
return janet_wrap_tuple(janet_tuple_end(ret));
|
||||
}
|
||||
|
||||
|
||||
static Janet close_array(JanetParser *p, JanetParseState *state) {
|
||||
JanetArray *array = janet_array(state->argn);
|
||||
for (int32_t i = state->argn - 1; i >= 0; i--)
|
||||
@ -485,9 +494,13 @@ static int root(JanetParser *p, JanetParseState *state, uint8_t c) {
|
||||
(c == ']' && (state->flags & PFLAG_SQRBRACKETS))) {
|
||||
if (state->flags & PFLAG_ATSYM) {
|
||||
ds = close_array(p, state);
|
||||
} else {
|
||||
if (c == ']') {
|
||||
ds = close_ltuple(p, state);
|
||||
} else {
|
||||
ds = close_tuple(p, state);
|
||||
}
|
||||
}
|
||||
} else if (c == '}' && (state->flags & PFLAG_CURLYBRACKETS)) {
|
||||
if (state->argn & 1) {
|
||||
p->error = "struct and table literals expect even number of arguments";
|
||||
|
@ -31,11 +31,12 @@
|
||||
* which should be filled with Janets. The memory will not be collected until
|
||||
* janet_tuple_end is called. */
|
||||
Janet *janet_tuple_begin(int32_t length) {
|
||||
char *data = janet_gcalloc(JANET_MEMORY_TUPLE, 4 * sizeof(int32_t) + length * sizeof(Janet));
|
||||
Janet *tuple = (Janet *)(data + (4 * sizeof(int32_t)));
|
||||
char *data = janet_gcalloc(JANET_MEMORY_TUPLE, 5 * sizeof(int32_t) + length * sizeof(Janet));
|
||||
Janet *tuple = (Janet *)(data + (5 * sizeof(int32_t)));
|
||||
janet_tuple_length(tuple) = length;
|
||||
janet_tuple_sm_start(tuple) = -1;
|
||||
janet_tuple_sm_end(tuple) = -1;
|
||||
janet_tuple_flag(tuple) = 0;
|
||||
return tuple;
|
||||
}
|
||||
|
||||
|
@ -334,7 +334,7 @@ int janet_indexed_view(Janet seq, const Janet **data, int32_t *len) {
|
||||
return 1;
|
||||
} else if (janet_checktype(seq, JANET_TUPLE)) {
|
||||
*data = janet_unwrap_tuple(seq);
|
||||
*len = janet_tuple_length(janet_unwrap_struct(seq));
|
||||
*len = janet_tuple_length(janet_unwrap_tuple(seq));
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
@ -980,11 +980,15 @@ JANET_API void janet_buffer_push_u32(JanetBuffer *buffer, uint32_t x);
|
||||
JANET_API void janet_buffer_push_u64(JanetBuffer *buffer, uint64_t x);
|
||||
|
||||
/* Tuple */
|
||||
#define janet_tuple_raw(t) ((int32_t *)(t) - 4)
|
||||
|
||||
#define JANET_TUPLE_FLAG_BRACKETCTOR 1
|
||||
|
||||
#define janet_tuple_raw(t) ((int32_t *)(t) - 5)
|
||||
#define janet_tuple_length(t) (janet_tuple_raw(t)[0])
|
||||
#define janet_tuple_hash(t) ((janet_tuple_raw(t)[1]))
|
||||
#define janet_tuple_sm_start(t) ((janet_tuple_raw(t)[2]))
|
||||
#define janet_tuple_sm_end(t) ((janet_tuple_raw(t)[3]))
|
||||
#define janet_tuple_flag(t) ((janet_tuple_raw(t)[4]))
|
||||
JANET_API Janet *janet_tuple_begin(int32_t length);
|
||||
JANET_API const Janet *janet_tuple_end(Janet *tuple);
|
||||
JANET_API const Janet *janet_tuple_n(const Janet *values, int32_t n);
|
||||
|
Loading…
Reference in New Issue
Block a user