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

Update apply to be variadic, and keep non variadic form as apply1

This commit is contained in:
Calvin Rose 2018-03-16 15:45:24 -04:00
parent 72d8e74a71
commit 110c780747
4 changed files with 31 additions and 20 deletions

View File

@ -4,12 +4,10 @@
(def fibasm (asm '{ (def fibasm (asm '{
arity 1 arity 1
bytecode [ bytecode [
(ldi 1 0x2) # $1 = 2 (ldi 1 0x2) # $1 = 2
(lt 1 0 1) # $1 = $0 < $1 (lt 1 0 1) # $1 = $0 < $1
(jmpn 1 :recursive) # if (not $1) goto :recursive (jmpi 1 :done) # if ($1) goto :done
(ret 0) # return $0 (lds 1) # $1 = self
:recursive
(lds 1) # $1 = self
(addim 0 0 -0x1) # $0 = $0 - 1 (addim 0 0 -0x1) # $0 = $0 - 1
(push 0) # push($0) (push 0) # push($0)
(call 2 1) # $2 = call($1) (call 2 1) # $2 = call($1)
@ -17,6 +15,7 @@
(push 0) # push($0) (push 0) # push($0)
(call 0 1) # $0 = call($1) (call 0 1) # $0 = call($1)
(addi 0 0 2) # $0 = $0 + $2 (integers) (addi 0 0 2) # $0 = $0 + $2 (integers)
:done
(ret 0) # return $0 (ret 0) # return $0
] ]
})) }))

View File

@ -17,7 +17,7 @@
(defmacro delay (defmacro delay
"Macro for lazy evaluation" "Macro for lazy evaluation"
[& forms] (tuple mem0 (apply tuple (array-concat ['fn []] forms)))) [& forms] (tuple mem0 (apply1 tuple (array-concat ['fn []] forms))))
# Use tuples instead of structs to save memory # Use tuples instead of structs to save memory
(def HEAD :private 0) (def HEAD :private 0)
@ -56,7 +56,7 @@
"Return a sequence of integers [start, end)." "Return a sequence of integers [start, end)."
[start end] [start end]
(if (< start end) (if (< start end)
(cons start (range (+ 1 start) end)) (cons start (range2 (+ 1 start) end))
empty-seq)) empty-seq))
(defn range (defn range
@ -85,7 +85,7 @@
[n s] [n s]
(delay (delay
(def x (s)) (def x (s))
(if (s) (if (zero? n) s (drop (- n 1) (tail s))) empty-seq)) (if (s) (if (zero? n) s (drop (- n 1) (tail s))) empty-seq)))
(defn take (defn take
"Returns at most the first n values of s." "Returns at most the first n values of s."
@ -97,5 +97,9 @@
(defn take-while (defn take-while
"Returns a sequence of values until the predicate is false." "Returns a sequence of values until the predicate is false."
[pred s] [pred s]
(delay (if (s) ))) (delay
(def x (s))
(when x
(def thehead (get HEAD x))
(if thehead (tuple thehead (take-while pred (get TAIL x)))))))

View File

@ -13,7 +13,7 @@
(def start (fstart 0)) (def start (fstart 0))
(def fnbody (tuple-prepend (tuple-prepend (tuple-slice more start) name) 'fn)) (def fnbody (tuple-prepend (tuple-prepend (tuple-slice more start) name) 'fn))
(def formargs (array-concat ['def name] (array-slice more 0 start) [fnbody])) (def formargs (array-concat ['def name] (array-slice more 0 start) [fnbody]))
(apply tuple formargs))) (apply1 tuple formargs)))
(def defmacro :macro (def defmacro :macro
"Define a macro." "Define a macro."
@ -21,18 +21,18 @@
(def defn* (get (get _env 'defn) 'value)) (def defn* (get (get _env 'defn) 'value))
(fn [name & more] (fn [name & more]
(def args (array-concat [] name :macro more)) (def args (array-concat [] name :macro more))
(apply defn* args)))) (apply1 defn* args))))
(defmacro defmacro- (defmacro defmacro-
"Define a private macro that will not be exported." "Define a private macro that will not be exported."
[name & more] [name & more]
(apply tuple (array-concat (apply1 tuple (array-concat
['defmacro name :private] more))) ['defmacro name :private] more)))
(defmacro defn- (defmacro defn-
"Define a private function that will not be exported." "Define a private function that will not be exported."
[name & more] [name & more]
(apply tuple (array-concat (apply1 tuple (array-concat
['defn name :private] more))) ['defn name :private] more)))
(defn even? [x] (== 0 (% x 2))) (defn even? [x] (== 0 (% x 2)))
@ -91,6 +91,11 @@ are matched. If there are no matches, return nil."
[sym] [sym]
(tuple doc* '_env sym)) (tuple doc* '_env sym))
(def apply
(fn [f & args]
(def last (- (length args) 1))
(apply1 f (array-concat (array-slice args 0 -2) (get args last)))))
(defmacro select (defmacro select
"Select the body that equals the dispatch value. When pairs "Select the body that equals the dispatch value. When pairs
has an odd number of arguments, the last is the default expression. has an odd number of arguments, the last is the default expression.
@ -210,7 +215,7 @@ If no match is found, returns nil"
(get head (+ 1 i)))) (get head (+ 1 i))))
(varset! i (+ i 2))) (varset! i (+ i 2)))
(array-push accum (tuple-prepend body 'do)) (array-push accum (tuple-prepend body 'do))
(apply tuple accum)) (apply1 tuple accum))
(defn pairs [x] (defn pairs [x]
(var lastkey (next x nil)) (var lastkey (next x nil))
@ -265,7 +270,7 @@ If no match is found, returns nil"
[(get n 0) (array-slice n 1)] [(get n 0) (array-slice n 1)]
[n []])) [n []]))
(def parts (array-concat [h last] t)) (def parts (array-concat [h last] t))
(apply tuple parts)) (apply1 tuple parts))
(reduce fop x forms)) (reduce fop x forms))
(defmacro ->> (defmacro ->>
@ -276,7 +281,7 @@ If no match is found, returns nil"
[(get n 0) (array-slice n 1)] [(get n 0) (array-slice n 1)]
[n []])) [n []]))
(def parts (array-concat [h] t [last])) (def parts (array-concat [h] t [last]))
(apply tuple parts)) (apply1 tuple parts))
(reduce fop x forms)) (reduce fop x forms))
# Start pretty printer # Start pretty printer
@ -380,7 +385,10 @@ onvalue."
(select (fiber-status chars) (select (fiber-status chars)
:new (parser-byte p (resume chars)) :new (parser-byte p (resume chars))
:pending (parser-byte p (resume chars)) :pending (parser-byte p (resume chars))
(varset! going false))))))) (varset! going false))))
(when (not= :root (parser-status p))
(onerr "parse" "unexpected end of source"))
nil)))
(defn more [] (if temp true (defn more [] (if temp true
(do (do
(varset! temp true) (varset! temp true)
@ -427,13 +435,13 @@ onvalue."
(def newenv (require path)) (def newenv (require path))
(def { (def {
:prefix prefix :prefix prefix
} (apply table args)) } (apply1 table args))
(foreach (pairs newenv) (fn [[k v]] (foreach (pairs newenv) (fn [[k v]]
(when (not (get v :private)) (when (not (get v :private))
(put env (symbol (if prefix prefix "") k) v))))) (put env (symbol (if prefix prefix "") k) v)))))
(defmacro import [path & args] (defmacro import [path & args]
(apply tuple (array-concat [import* '_env path] args))) (apply1 tuple (array-concat [import* '_env path] args)))
(defn repl [getchunk] (defn repl [getchunk]
(def newenv (make-env)) (def newenv (make-env))

View File

@ -89,7 +89,7 @@ DstTable *dst_stl_env() {
dst_env_cfuns(env, cfuns); dst_env_cfuns(env, cfuns);
dst_env_def(env, "error", dst_wrap_function(dst_quick_asm(1, 0, 1, error_asm, sizeof(error_asm)))); dst_env_def(env, "error", dst_wrap_function(dst_quick_asm(1, 0, 1, error_asm, sizeof(error_asm))));
dst_env_def(env, "apply", dst_wrap_function(dst_quick_asm(2, 0, 2, apply_asm, sizeof(apply_asm)))); dst_env_def(env, "apply1", dst_wrap_function(dst_quick_asm(2, 0, 2, apply_asm, sizeof(apply_asm))));
dst_env_def(env, "yield", dst_wrap_function(dst_quick_asm(1, 0, 2, yield_asm, sizeof(yield_asm)))); dst_env_def(env, "yield", dst_wrap_function(dst_quick_asm(1, 0, 2, yield_asm, sizeof(yield_asm))));
dst_env_def(env, "resume", dst_wrap_function(dst_quick_asm(2, 0, 2, resume_asm, sizeof(resume_asm)))); dst_env_def(env, "resume", dst_wrap_function(dst_quick_asm(2, 0, 2, resume_asm, sizeof(resume_asm))));