From 8ab60e475a098ab9dab8eaa6efa590dd8a5b7334 Mon Sep 17 00:00:00 2001 From: "J.-F. Cap" Date: Fri, 8 Feb 2019 01:03:21 +0100 Subject: [PATCH 1/3] typo in janet_indexed_view (no consequence but look strange) --- src/core/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/util.c b/src/core/util.c index 4264917e..fcdd6577 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -316,7 +316,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; From 5f70024f87140df997c16a8937893cdc37a17597 Mon Sep 17 00:00:00 2001 From: "J.-F. Cap" Date: Fri, 8 Feb 2019 21:49:28 +0100 Subject: [PATCH 2/3] Experimental stuffs with bracket syntax --- src/core/compile.c | 11 +++++++++++ src/core/core.janet | 20 ++++++++++---------- src/core/parse.c | 13 +++++++++++++ src/core/tuple.c | 5 +++-- src/include/janet/janet.h | 3 ++- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/core/compile.c b/src/core/compile.c index bbaae4e9..71ef6b15 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -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) == 1) { // [] tuples are not function call + ret = janetc_tuple(opts, x); } else { JanetSlot head = janetc_value(subopts, tup[0]); subopts.flags = JANET_FUNCTION | JANET_CFUNCTION; diff --git a/src/core/core.janet b/src/core/core.janet index b4a6bcd0..7105b0c1 100644 --- a/src/core/core.janet +++ b/src/core/core.janet @@ -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)))) diff --git a/src/core/parse.c b/src/core/parse.c index 09330a0a..8bf6ce0f 100644 --- a/src/core/parse.c +++ b/src/core/parse.c @@ -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)=1; + 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--) @@ -486,7 +495,11 @@ static int root(JanetParser *p, JanetParseState *state, uint8_t c) { 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) { diff --git a/src/core/tuple.c b/src/core/tuple.c index 2d7f1090..c0f7debf 100644 --- a/src/core/tuple.c +++ b/src/core/tuple.c @@ -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; } diff --git a/src/include/janet/janet.h b/src/include/janet/janet.h index e4a0f385..42cbc4e0 100644 --- a/src/include/janet/janet.h +++ b/src/include/janet/janet.h @@ -980,11 +980,12 @@ 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_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); From 5020a1bae96e52d59e86c2b2cc3e10037499002c Mon Sep 17 00:00:00 2001 From: "J.-F. Cap" Date: Sat, 9 Feb 2019 17:00:35 +0100 Subject: [PATCH 3/3] Added marshalling code to save tuple_flag --- src/core/compile.c | 2 +- src/core/marsh.c | 6 +++++- src/core/parse.c | 2 +- src/include/janet/janet.h | 3 +++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/core/compile.c b/src/core/compile.c index b3cdfdc6..97edeb88 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -556,7 +556,7 @@ 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) == 1) { // [] tuples are not function call + } 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]); diff --git a/src/core/marsh.c b/src/core/marsh.c index dab16792..89b951a9 100644 --- a/src/core/marsh.c +++ b/src/core/marsh.c @@ -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); } diff --git a/src/core/parse.c b/src/core/parse.c index 55fe526f..b968e477 100644 --- a/src/core/parse.c +++ b/src/core/parse.c @@ -344,7 +344,7 @@ static Janet close_tuple(JanetParser *p, JanetParseState *state) { static Janet close_ltuple(JanetParser *p, JanetParseState *state) { Janet *ret = janet_tuple_begin(state->argn); - janet_tuple_flag(ret)=1; + 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)); diff --git a/src/include/janet/janet.h b/src/include/janet/janet.h index 5d5a40ed..46b807ba 100644 --- a/src/include/janet/janet.h +++ b/src/include/janet/janet.h @@ -980,6 +980,9 @@ 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_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]))