1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-22 06:20:27 +00:00

Make negative indexing work like python.

This would break a lot of code :|
This commit is contained in:
Calvin Rose 2023-07-14 17:18:20 -05:00
parent 989f0726e3
commit 1808c923bf
9 changed files with 23 additions and 33 deletions

View File

@ -700,7 +700,7 @@
4 (let [[f g h i] functions] (fn [& x] (f (g (h (i ;x))))))
(let [[f g h i] functions]
(comp (fn [x] (f (g (h (i x)))))
;(tuple/slice functions 4 -1)))))
;(tuple/slice functions 4)))))
(defn identity
"A function that returns its argument."
@ -1208,7 +1208,7 @@
(assert (symbol? alias) "alias must be a symbol")
(assert (and (> (length alias) 2) (= 42 (first alias) (last alias))) "name must have leading and trailing '*' characters")
(def prefix (dyn :defdyn-prefix))
(def kw (keyword prefix (slice alias 1 -2)))
(def kw (keyword prefix (slice alias 1 -1)))
~(def ,alias :dyn ,;more ,kw))
(defn has-key?
@ -1821,7 +1821,7 @@
# Partition body into sections.
(def oddlen (odd? (length cases)))
(def else (if oddlen (last cases)))
(def patterns (partition 2 (if oddlen (slice cases 0 -2) cases)))
(def patterns (partition 2 (if oddlen (slice cases 0 -1) cases)))
# Keep an array for accumulating the compilation output
(def x-sym (if (idempotent? x) x (gensym)))
@ -2052,7 +2052,7 @@
(tuple/slice
(array/concat
@[(in t 0) (expand-bindings bound)]
(tuple/slice t 2 -2)
(tuple/slice t 2 -1)
@[(recur last)])))
(defn expandall [t]
@ -2227,7 +2227,7 @@
[name & body]
(def expansion (apply defn name body))
(def fbody (last expansion))
(def modifiers (tuple/slice expansion 2 -2))
(def modifiers (tuple/slice expansion 2 -1))
(def metadata @{})
(each m modifiers
(cond
@ -2917,7 +2917,7 @@
(def buf @"")
(with-dyns [*err* buf *err-color* false]
(bad-parse x y))
(set exit-error (string/slice buf 0 -2)))
(set exit-error (string/slice buf 0 -1)))
(defn bc [&opt x y z a b]
(when exit
(bad-compile x y z a b)
@ -2926,7 +2926,7 @@
(def buf @"")
(with-dyns [*err* buf *err-color* false]
(bad-compile x nil z a b))
(set exit-error (string/slice buf 0 -2))
(set exit-error (string/slice buf 0 -1))
(set exit-fiber y))
(unless f
(error (string "could not find file " path)))
@ -3789,7 +3789,7 @@
"Generate bindings for native functions in a convenient manner."
[name ret-type & body]
(def real-ret-type (eval ret-type))
(def meta (slice body 0 -2))
(def meta (slice body 0 -1))
(def arg-pairs (partition 2 (last body)))
(def formal-args (map 0 arg-pairs))
(def type-args (map 1 arg-pairs))

View File

@ -297,7 +297,7 @@ JANET_CORE_FN(cfun_array_remove,
int32_t at = janet_getinteger(argv, 1);
int32_t n = 1;
if (at < 0) {
at = array->count + at + 1;
at = array->count + at;
}
if (at < 0 || at > array->count)
janet_panicf("removal index %d out of range [0,%d]", at, array->count);

View File

@ -334,20 +334,11 @@ size_t janet_getsize(const Janet *argv, int32_t n) {
}
int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t length, const char *which) {
int32_t raw = janet_getinteger(argv, n);
int32_t not_raw = raw;
if (not_raw < 0) not_raw += length + 1;
if (not_raw < 0 || not_raw > length)
janet_panicf("%s index %d out of range [%d,%d]", which, raw, -length - 1, length);
return not_raw;
}
int32_t janet_getargindex(const Janet *argv, int32_t n, int32_t length, const char *which) {
int32_t raw = janet_getinteger(argv, n);
int32_t not_raw = raw;
if (not_raw < 0) not_raw += length;
if (not_raw < 0 || not_raw > length)
janet_panicf("%s index %d out of range [%d,%d)", which, raw, -length, length);
janet_panicf("%s index %d out of range [%d,%d]", which, raw, -length, length);
return not_raw;
}

View File

@ -1997,7 +1997,6 @@ JANET_API JanetDictView janet_getdictionary(const Janet *argv, int32_t n);
JANET_API void *janet_getabstract(const Janet *argv, int32_t n, const JanetAbstractType *at);
JANET_API JanetRange janet_getslice(int32_t argc, const Janet *argv);
JANET_API int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t length, const char *which);
JANET_API int32_t janet_getargindex(const Janet *argv, int32_t n, int32_t length, const char *which);
JANET_API uint64_t janet_getflags(const Janet *argv, int32_t n, const char *flags);
/* Optionals */

View File

@ -44,7 +44,7 @@
(number? x) (string x)
(string? x) (string/slice x
(length "test/suite-")
(- (inc (length ".janet"))))
(- (length ".janet")))
(string x)))
(set start-time (os/clock))
(eprint "Starting suite " suite-name "..."))

View File

@ -37,14 +37,14 @@
(assert (array= @[:one :two :three :four :five]
@[:one :two :three :four :five]) "array comparison 3")
(assert (array= (array/slice @[1 2 3] 0 2) @[1 2]) "array/slice 1")
(assert (array= (array/slice @[0 7 3 9 1 4] 2 -2) @[3 9 1]) "array/slice 2")
(assert (array= (array/slice @[0 7 3 9 1 4] 2 -1) @[3 9 1]) "array/slice 2")
# Array remove
# 687a3c9
(assert (deep= (array/remove @[1 2 3 4 5] 2) @[1 2 4 5]) "array/remove 1")
(assert (deep= (array/remove @[1 2 3 4 5] 2 2) @[1 2 5]) "array/remove 2")
(assert (deep= (array/remove @[1 2 3 4 5] 2 200) @[1 2]) "array/remove 3")
(assert (deep= (array/remove @[1 2 3 4 5] -3 200) @[1 2 3]) "array/remove 4")
(assert (deep= (array/remove @[1 2 3 4 5] -2 200) @[1 2 3]) "array/remove 4")
# array/peek

View File

@ -96,13 +96,13 @@
# Regression #301
# a3d4ecddb
(def b (buffer/new-filled 128 0x78))
(assert (= 38 (length (buffer/blit @"" b -1 90))) "buffer/blit 1")
(assert (= 38 (length (buffer/blit @"" b 0 90))) "buffer/blit 1")
(def a @"abcdefghijklm")
(assert (deep= @"abcde" (buffer/blit @"" a -1 0 5)) "buffer/blit 2")
(assert (deep= @"bcde" (buffer/blit @"" a -1 1 5)) "buffer/blit 3")
(assert (deep= @"cde" (buffer/blit @"" a -1 2 5)) "buffer/blit 4")
(assert (deep= @"de" (buffer/blit @"" a -1 3 5)) "buffer/blit 5")
(assert (deep= @"abcde" (buffer/blit @"" a 0 0 5)) "buffer/blit 2")
(assert (deep= @"bcde" (buffer/blit @"" a 0 1 5)) "buffer/blit 3")
(assert (deep= @"cde" (buffer/blit @"" a 0 2 5)) "buffer/blit 4")
(assert (deep= @"de" (buffer/blit @"" a 0 3 5)) "buffer/blit 5")
# buffer/push-at
# c55d93512

View File

@ -69,7 +69,7 @@
(def first-nl (= (chr "\n") (first str)))
(def last-nl (= (chr "\n") (last str)))
(string/slice str (if first-nl 1 0) (if last-nl -2)))
(string/slice str (if first-nl 1 0) (if last-nl -1)))
(defn reindent-reference
"Same as reindent but use parser functionality. Useful for

View File

@ -32,10 +32,10 @@
# Buffer self blitting, check for use after free
# bbcfaf128
(def buf1 @"1234567890")
(buffer/blit buf1 buf1 -1)
(buffer/blit buf1 buf1 -1)
(buffer/blit buf1 buf1 -1)
(buffer/blit buf1 buf1 -1)
(buffer/blit buf1 buf1 (length buf1))
(buffer/blit buf1 buf1 (length buf1))
(buffer/blit buf1 buf1 (length buf1))
(buffer/blit buf1 buf1 (length buf1))
(assert (= (string buf1) (string/repeat "1234567890" 16))
"buffer blit against self")