1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-30 17:13:15 +00:00

Remove indexing with numeric constants from janet.

This commit is contained in:
Calvin Rose 2018-12-28 23:44:39 -05:00
parent 5c84f0f5d9
commit 2bcedd5920
4 changed files with 32 additions and 35 deletions

View File

@ -88,7 +88,7 @@
(defn string? "Check if x is a string." [x] (= (type x) :string)) (defn string? "Check if x is a string." [x] (= (type x) :string))
(defn symbol? "Check if x is a symbol." [x] (= (type x) :symbol)) (defn symbol? "Check if x is a symbol." [x] (= (type x) :symbol))
(defn keyword? "Check if x is a keyword style symbol." [x] (defn keyword? "Check if x is a keyword style symbol." [x]
(if (not= (type x) :symbol) nil (= 58 x.0))) (if (not= (type x) :symbol) nil (= 58 (get x 0))))
(defn buffer? "Check if x is a buffer." [x] (= (type x) :buffer)) (defn buffer? "Check if x is a buffer." [x] (= (type x) :buffer))
(defn function? "Check if x is a function (not a cfunction)." [x] (defn function? "Check if x is a function (not a cfunction)." [x]
(= (type x) :function)) (= (type x) :function))
@ -494,7 +494,7 @@
[& functions] [& functions]
(case (length functions) (case (length functions)
0 nil 0 nil
1 functions.0 1 (get functions 0)
2 (let [[f g] functions] (fn [x] (f (g x)))) 2 (let [[f g] functions] (fn [x] (f (g x))))
3 (let [[f g h] functions] (fn [x] (f (g (h x))))) 3 (let [[f g h] functions] (fn [x] (f (g (h x)))))
4 (let [[f g h i] functions] (fn [x] (f (g (h (i x)))))) 4 (let [[f g h i] functions] (fn [x] (f (g (h (i x))))))
@ -519,7 +519,7 @@
[order args] [order args]
(def len (length args)) (def len (length args))
(when (pos? len) (when (pos? len)
(var ret args.0) (var [ret] args)
(loop [i :range [0 len]] (loop [i :range [0 len]]
(def v args.i) (def v args.i)
(if (order v ret) (set ret v))) (if (order v ret) (set ret v)))
@ -546,7 +546,7 @@
(defn first (defn first
"Get the first element from an indexed data structure." "Get the first element from an indexed data structure."
[xs] [xs]
xs.0) (get xs 0))
(defn last (defn last
"Get the last element from an indexed data structure." "Get the last element from an indexed data structure."
@ -609,7 +609,7 @@
[f & inds] [f & inds]
(def ninds (length inds)) (def ninds (length inds))
(if (= 0 ninds) (error "expected at least 1 indexed collection")) (if (= 0 ninds) (error "expected at least 1 indexed collection"))
(var limit (length inds.0)) (var limit (length (get inds 0)))
(loop [i :range [0 ninds]] (loop [i :range [0 ninds]]
(def l (length inds.i)) (def l (length inds.i))
(if (< l limit) (set limit l))) (if (< l limit) (set limit l)))
@ -758,7 +758,7 @@
[x & forms] [x & forms]
(defn fop [last n] (defn fop [last n]
(def [h t] (if (= :tuple (type n)) (def [h t] (if (= :tuple (type n))
[tuple n.0 (array/slice n 1)] [tuple (get n 0) (array/slice n 1)]
[tuple n @[]])) [tuple n @[]]))
(def parts (array/concat @[h last] t)) (def parts (array/concat @[h last] t))
(tuple/slice parts 0)) (tuple/slice parts 0))
@ -771,7 +771,7 @@
[x & forms] [x & forms]
(defn fop [last n] (defn fop [last n]
(def [h t] (if (= :tuple (type n)) (def [h t] (if (= :tuple (type n))
[tuple n.0 (array/slice n 1)] [tuple (get n 0) (array/slice n 1)]
[tuple n @[]])) [tuple n @[]]))
(def parts (array/concat @[h] t @[last])) (def parts (array/concat @[h] t @[last]))
(tuple/slice parts 0)) (tuple/slice parts 0))
@ -786,7 +786,7 @@
[x & forms] [x & forms]
(defn fop [last n] (defn fop [last n]
(def [h t] (if (= :tuple (type n)) (def [h t] (if (= :tuple (type n))
[tuple n.0 (array/slice n 1)] [tuple (get n 0) (array/slice n 1)]
[tuple n @[]])) [tuple n @[]]))
(def sym (gensym)) (def sym (gensym))
(def parts (array/concat @[h sym] t)) (def parts (array/concat @[h sym] t))
@ -802,7 +802,7 @@
[x & forms] [x & forms]
(defn fop [last n] (defn fop [last n]
(def [h t] (if (= :tuple (type n)) (def [h t] (if (= :tuple (type n))
[tuple n.0 (array/slice n 1)] [tuple (get n 0) (array/slice n 1)]
[tuple n @[]])) [tuple n @[]]))
(def sym (gensym)) (def sym (gensym))
(def parts (array/concat @[h] t @[sym])) (def parts (array/concat @[h] t @[sym]))
@ -1034,7 +1034,7 @@ value, one key will be ignored."
[sep ind] [sep ind]
(def len (length ind)) (def len (length ind))
(def ret (array/new (- (* 2 len) 1))) (def ret (array/new (- (* 2 len) 1)))
(if (> len 0) (set ret.0 ind.0)) (if (> len 0) (put ret 0 (get ind 0)))
(var i 1) (var i 1)
(while (< i len) (while (< i len)
(array/push ret sep ind.i) (array/push ret sep ind.i)
@ -1062,7 +1062,9 @@ value, one key will be ignored."
~(if (= nil (def ,pattern ,expr)) ,sentinel ,(onmatch)))) ~(if (= nil (def ,pattern ,expr)) ,sentinel ,(onmatch))))
(tuple? pattern) (tuple? pattern)
(match-1 pattern.0 expr (fn [] (match-1
(get pattern 0) expr
(fn []
~(if (and ,;(tuple/slice pattern 1)) ,(onmatch) ,sentinel)) seen) ~(if (and ,;(tuple/slice pattern 1)) ,(onmatch) ,sentinel)) seen)
(array? pattern) (array? pattern)
@ -1221,39 +1223,40 @@ value, one key will be ignored."
(defn expanddef [t] (defn expanddef [t]
(def last (get t (- (length t) 1))) (def last (get t (- (length t) 1)))
(def bound t.1) (def bound (get t 1))
(tuple/slice (tuple/slice
(array/concat (array/concat
@[t.0 (expand-bindings bound)] @[(get t 0) (expand-bindings bound)]
(tuple/slice t 2 -2) (tuple/slice t 2 -2)
@[(macex1 last)]))) @[(macex1 last)])))
(defn expandall [t] (defn expandall [t]
(def args (map macex1 (tuple/slice t 1))) (def args (map macex1 (tuple/slice t 1)))
(tuple t.0 ;args)) (tuple (get t 0) ;args))
(defn expandfn [t] (defn expandfn [t]
(if (symbol? t.1) (def t1 (get t 1))
(if (symbol? t1)
(do (do
(def args (map macex1 (tuple/slice t 3))) (def args (map macex1 (tuple/slice t 3)))
(tuple 'fn t.1 t.2 ;args)) (tuple 'fn t1 (get t 2) ;args))
(do (do
(def args (map macex1 (tuple/slice t 2))) (def args (map macex1 (tuple/slice t 2)))
(tuple 'fn t.1 ;args)))) (tuple 'fn t1 ;args))))
(defn expandqq [t] (defn expandqq [t]
(defn qq [x] (defn qq [x]
(case (type x) (case (type x)
:tuple (do :tuple (do
(def x0 x.0) (def x0 (get x 0))
(if (or (= 'unquote x0) (= 'unquote-splicing x0)) (if (or (= 'unquote x0) (= 'unquote-splicing x0))
(tuple x0 (macex1 x.1)) (tuple x0 (macex1 (get x 1)))
(tuple/slice (map qq x)))) (tuple/slice (map qq x))))
:array (map qq x) :array (map qq x)
:table (table (map qq (kvs x))) :table (table (map qq (kvs x)))
:struct (struct (map qq (kvs x))) :struct (struct (map qq (kvs x)))
x)) x))
(tuple t.0 (qq t.1))) (tuple (get t 0) (qq (get t 1))))
(def specs (def specs
{'set expanddef {'set expanddef
@ -1267,7 +1270,7 @@ value, one key will be ignored."
'while expandall}) 'while expandall})
(defn dotup [t] (defn dotup [t]
(def h t.0) (def h (get t 0))
(def s specs.h) (def s specs.h)
(def entry (or *env*.h {})) (def entry (or *env*.h {}))
(def m entry:value) (def m entry:value)

View File

@ -30,13 +30,7 @@ static JanetSlot multisym_parse_part(JanetCompiler *c, const uint8_t *sympart, i
if (sympart[0] == ':') { if (sympart[0] == ':') {
return janetc_cslot(janet_symbolv(sympart, len)); return janetc_cslot(janet_symbolv(sympart, len));
} else { } else {
int err = 0;
double num = janet_scan_number(sympart + 1, len - 1, &err);
if (err) {
return janetc_resolve(c, janet_symbol(sympart + 1, len - 1)); return janetc_resolve(c, janet_symbol(sympart + 1, len - 1));
} else {
return janetc_cslot(janet_wrap_number(num));
}
} }
} }

View File

@ -11,7 +11,7 @@
# Flag handlers # Flag handlers
(def handlers :private (def handlers :private
{"h" (fn [&] {"h" (fn [&]
(print "usage: " process/args.0 " [options] scripts...") (print "usage: " (get process/args 0) " [options] scripts...")
(print (print
`Options are: `Options are:
-h Show this help -h Show this help

View File

@ -214,11 +214,11 @@
# Closure in while loop # Closure in while loop
(def closures (seq [i :range [0 5]] (fn [] i))) (def closures (seq [i :range [0 5]] (fn [] i)))
(assert (= 0 (closures.0)) "closure in loop 0") (assert (= 0 ((get closures 0))) "closure in loop 0")
(assert (= 1 (closures.1)) "closure in loop 1") (assert (= 1 ((get closures 1))) "closure in loop 1")
(assert (= 2 (closures.2)) "closure in loop 2") (assert (= 2 ((get closures 2))) "closure in loop 2")
(assert (= 3 (closures.3)) "closure in loop 3") (assert (= 3 ((get closures 3))) "closure in loop 3")
(assert (= 4 (closures.4)) "closure in loop 4") (assert (= 4 ((get closures 4))) "closure in loop 4")
# More numerical tests # More numerical tests
(assert (== 1 1.0) "numerical equal 1") (assert (== 1 1.0) "numerical equal 1")