mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-26 05:07:41 +00:00 
			
		
		
		
	Fix some code style, add tuple/type function.
We need to be able to detect tuple type from janet code, otherwise tuples will contain hidden state. The tuple/type function is able to detect the flags in the tuple so the programmer can access them if needed.
This commit is contained in:
		| @@ -446,7 +446,6 @@ static JanetSlot janetc_tuple(JanetFopts opts, Janet x) { | |||||||
|             JOP_MAKE_TUPLE); |             JOP_MAKE_TUPLE); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| static JanetSlot janetc_tablector(JanetFopts opts, Janet x, int op) { | static JanetSlot janetc_tablector(JanetFopts opts, Janet x, int op) { | ||||||
|     JanetCompiler *c = opts.compiler; |     JanetCompiler *c = opts.compiler; | ||||||
|     return janetc_maker(opts, |     return janetc_maker(opts, | ||||||
| @@ -556,7 +555,7 @@ JanetSlot janetc_value(JanetFopts opts, Janet x) { | |||||||
|                     /* Empty tuple is tuple literal */ |                     /* Empty tuple is tuple literal */ | ||||||
|                     if (janet_tuple_length(tup) == 0) { |                     if (janet_tuple_length(tup) == 0) { | ||||||
|                         ret = janetc_cslot(x); |                         ret = janetc_cslot(x); | ||||||
| 		    } else if (janet_tuple_flag(tup) & JANET_TUPLE_FLAG_BRACKETCTOR) { // [] 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); |                         ret = janetc_tuple(opts, x); | ||||||
|                     } else { |                     } else { | ||||||
|                         JanetSlot head = janetc_value(subopts, tup[0]); |                         JanetSlot head = janetc_value(subopts, tup[0]); | ||||||
|   | |||||||
| @@ -402,7 +402,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) { | |||||||
|             goto done; |             goto done; | ||||||
|         case JANET_TUPLE: |         case JANET_TUPLE: | ||||||
|             { |             { | ||||||
| 	      int32_t i, count,flag; |                 int32_t i, count, flag; | ||||||
|                 const Janet *tup = janet_unwrap_tuple(x); |                 const Janet *tup = janet_unwrap_tuple(x); | ||||||
|                 count = janet_tuple_length(tup); |                 count = janet_tuple_length(tup); | ||||||
|                 flag = janet_tuple_flag(tup); |                 flag = janet_tuple_flag(tup); | ||||||
| @@ -1047,7 +1047,7 @@ static const uint8_t *unmarshal_one( | |||||||
|                     /* Tuple */ |                     /* Tuple */ | ||||||
|                     Janet *tup = janet_tuple_begin(len); |                     Janet *tup = janet_tuple_begin(len); | ||||||
|                     int32_t flag = readint(st, &data); |                     int32_t flag = readint(st, &data); | ||||||
| 		    janet_tuple_flag(tup)=flag; |                     janet_tuple_flag(tup) = flag; | ||||||
|                     for (int32_t i = 0; i < len; i++) { |                     for (int32_t i = 0; i < len; i++) { | ||||||
|                         data = unmarshal_one(st, data, tup + i, flags + 1); |                         data = unmarshal_one(st, data, tup + i, flags + 1); | ||||||
|                     } |                     } | ||||||
|   | |||||||
| @@ -335,22 +335,14 @@ static int comment(JanetParser *p, JanetParseState *state, uint8_t c) { | |||||||
|     return 1; |     return 1; | ||||||
| } | } | ||||||
|  |  | ||||||
| static Janet close_tuple(JanetParser *p, JanetParseState *state) { | static Janet close_tuple(JanetParser *p, JanetParseState *state, int32_t flag) { | ||||||
|     Janet *ret = janet_tuple_begin(state->argn); |     Janet *ret = janet_tuple_begin(state->argn); | ||||||
|  |     janet_tuple_flag(ret) = flag; | ||||||
|     for (int32_t i = state->argn - 1; i >= 0; i--) |     for (int32_t i = state->argn - 1; i >= 0; i--) | ||||||
|         ret[i] = p->args[--p->argcount]; |         ret[i] = p->args[--p->argcount]; | ||||||
|     return janet_wrap_tuple(janet_tuple_end(ret)); |     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) { | static Janet close_array(JanetParser *p, JanetParseState *state) { | ||||||
|     JanetArray *array = janet_array(state->argn); |     JanetArray *array = janet_array(state->argn); | ||||||
|     for (int32_t i = state->argn - 1; i >= 0; i--) |     for (int32_t i = state->argn - 1; i >= 0; i--) | ||||||
| @@ -495,11 +487,7 @@ static int root(JanetParser *p, JanetParseState *state, uint8_t c) { | |||||||
|                     if (state->flags & PFLAG_ATSYM) { |                     if (state->flags & PFLAG_ATSYM) { | ||||||
|                         ds = close_array(p, state); |                         ds = close_array(p, state); | ||||||
|                     } else { |                     } else { | ||||||
| 		      if (c == ']') { |                         ds = close_tuple(p, state, c == ']' ? JANET_TUPLE_FLAG_BRACKETCTOR : 0); | ||||||
| 			ds = close_ltuple(p, state); |  | ||||||
| 		      } else { |  | ||||||
|                         ds = close_tuple(p, state); |  | ||||||
| 		      } |  | ||||||
|                     } |                     } | ||||||
|                 } else if (c == '}' && (state->flags & PFLAG_CURLYBRACKETS)) { |                 } else if (c == '}' && (state->flags & PFLAG_CURLYBRACKETS)) { | ||||||
|                     if (state->argn & 1) { |                     if (state->argn & 1) { | ||||||
|   | |||||||
| @@ -332,16 +332,19 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { | |||||||
|         case JANET_ARRAY: |         case JANET_ARRAY: | ||||||
|         case JANET_TUPLE: |         case JANET_TUPLE: | ||||||
|             { |             { | ||||||
|  |                 int32_t i, len; | ||||||
|  |                 const Janet *arr; | ||||||
|                 int isarray = janet_checktype(x, JANET_ARRAY); |                 int isarray = janet_checktype(x, JANET_ARRAY); | ||||||
|                 janet_buffer_push_cstring(S->buffer, isarray ? "@[" : "("); |                 janet_indexed_view(x, &arr, &len); | ||||||
|  |                 int hasbrackets = !isarray && (janet_tuple_flag(arr) & JANET_TUPLE_FLAG_BRACKETCTOR); | ||||||
|  |                 const char *startstr = isarray ? "@[" : hasbrackets ? "[" : "("; | ||||||
|  |                 const char endchar = isarray ? ']' : hasbrackets ? ']' : ')'; | ||||||
|  |                 janet_buffer_push_cstring(S->buffer, startstr); | ||||||
|                 S->depth--; |                 S->depth--; | ||||||
|                 S->indent += 2; |                 S->indent += 2; | ||||||
|                 if (S->depth == 0) { |                 if (S->depth == 0) { | ||||||
|                     janet_buffer_push_cstring(S->buffer, "..."); |                     janet_buffer_push_cstring(S->buffer, "..."); | ||||||
|                 } else { |                 } else { | ||||||
|                     int32_t i, len; |  | ||||||
|                     const Janet *arr; |  | ||||||
|                     janet_indexed_view(x, &arr, &len); |  | ||||||
|                     if (!isarray && len >= 5) |                     if (!isarray && len >= 5) | ||||||
|                         janet_buffer_push_u8(S->buffer, ' '); |                         janet_buffer_push_u8(S->buffer, ' '); | ||||||
|                     if (is_dict_value && len >= 5) print_newline(S, 0); |                     if (is_dict_value && len >= 5) print_newline(S, 0); | ||||||
| @@ -352,7 +355,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { | |||||||
|                 } |                 } | ||||||
|                 S->indent -= 2; |                 S->indent -= 2; | ||||||
|                 S->depth++; |                 S->depth++; | ||||||
|                 janet_buffer_push_u8(S->buffer, isarray ? ']' : ')'); |                 janet_buffer_push_u8(S->buffer, endchar); | ||||||
|                 break; |                 break; | ||||||
|             } |             } | ||||||
|         case JANET_STRUCT: |         case JANET_STRUCT: | ||||||
|   | |||||||
| @@ -120,6 +120,16 @@ static Janet cfun_tuple_append(int32_t argc, Janet *argv) { | |||||||
|     return janet_wrap_tuple(janet_tuple_end(n)); |     return janet_wrap_tuple(janet_tuple_end(n)); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | static Janet cfun_tuple_type(int32_t argc, Janet *argv) { | ||||||
|  |     janet_fixarity(argc, 1); | ||||||
|  |     const Janet *tup = janet_gettuple(argv, 0); | ||||||
|  |     if (janet_tuple_flag(tup) & JANET_TUPLE_FLAG_BRACKETCTOR) { | ||||||
|  |         return janet_ckeywordv("brackets"); | ||||||
|  |     } else { | ||||||
|  |         return janet_ckeywordv("parens"); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| static const JanetReg tuple_cfuns[] = { | static const JanetReg tuple_cfuns[] = { | ||||||
|     { |     { | ||||||
|         "tuple/slice", cfun_tuple_slice, |         "tuple/slice", cfun_tuple_slice, | ||||||
| @@ -142,6 +152,15 @@ static const JanetReg tuple_cfuns[] = { | |||||||
|                 "returns a new tuple. Items are prepended such that the " |                 "returns a new tuple. Items are prepended such that the " | ||||||
|                 "last element in items is the first element in the new tuple.") |                 "last element in items is the first element in the new tuple.") | ||||||
|     }, |     }, | ||||||
|  |     { | ||||||
|  |         "tuple/type", cfun_tuple_type, | ||||||
|  |         JDOC("(tuple/type tup)\n\n" | ||||||
|  |                 "Checks how the tuple was constructed. Will return the keyword " | ||||||
|  |                 ":brackets if the tuple was parsed with brackets, and :parens " | ||||||
|  |                 "otherwise. The two types of tuples will behave the same most of " | ||||||
|  |                 "the time, but will print differently and be treated differently by " | ||||||
|  |                 "the compiler.") | ||||||
|  |     }, | ||||||
|     {NULL, NULL, NULL} |     {NULL, NULL, NULL} | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -351,4 +351,12 @@ | |||||||
| (def t (put @{} :hi 1)) | (def t (put @{} :hi 1)) | ||||||
| (assert (deep= t @{:hi 1}) "regression #24") | (assert (deep= t @{:hi 1}) "regression #24") | ||||||
|  |  | ||||||
|  | # Tuple types | ||||||
|  |  | ||||||
|  | (assert (= (tuple/type '(1 2 3)) :parens) "normal tuple") | ||||||
|  | (assert (= (tuple/type [1 2 3]) :parens) "normal tuple 1") | ||||||
|  | (assert (= (tuple/type '[1 2 3]) :brackets) "bracketed tuple 2") | ||||||
|  | (assert (= (tuple/type (-> '(1 2 3) marshal unmarshal)) :parens) "normal tuple marshalled/unmarshalled") | ||||||
|  | (assert (= (tuple/type (-> '[1 2 3] marshal unmarshal)) :brackets) "normal tuple marshalled/unmarshalled") | ||||||
|  |  | ||||||
| (end-suite) | (end-suite) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Calvin Rose
					Calvin Rose