1
0
mirror of https://github.com/janet-lang/janet synced 2025-07-06 03:52:54 +00:00

Update indentation in boot and init to be more like

most lisps.
This commit is contained in:
Calvin Rose 2018-07-01 21:12:46 -04:00
parent 79225ad3d5
commit e60c8a9b75
2 changed files with 821 additions and 819 deletions

View File

@ -15,7 +15,8 @@
"Define a function. Equivalent to (def name (fn name [args] ...))." "Define a function. Equivalent to (def name (fn name [args] ...))."
(fn defn [name & more] (fn defn [name & more]
(def len (length more)) (def len (length more))
(def fstart (fn recur [i] (def fstart
(fn recur [i]
(def {i ith} more) (def {i ith} more)
(def t (type ith)) (def t (type ith))
(def tuple? (= t :tuple)) (def tuple? (= t :tuple))
@ -29,28 +30,23 @@
(def defmacro :macro (def defmacro :macro
"Define a macro." "Define a macro."
(do
(fn defmacro [name & more] (fn defmacro [name & more]
(apply1 defn (array.concat (apply1 defn (array.concat @[name :macro] more))))
@[name :macro] more)))))
(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]
(apply1 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]
(apply1 tuple (array.concat (apply1 tuple (array.concat @['defn name :private] more)))
@['defn name :private] more)))
(defmacro def- (defmacro def-
"Define a private value that will not be exported." "Define a private value that will not be exported."
[name & more] [name & more]
(apply1 tuple (array.concat (apply1 tuple (array.concat @['def name :private] more)))
@['def name :private] more)))
# Basic predicates # Basic predicates
(defn even? [x] (== 0 (% x 2))) (defn even? [x] (== 0 (% x 2)))
@ -92,15 +88,14 @@
(defn true? [x] (= x true)) (defn true? [x] (= x true))
(defn false? [x] (= x false)) (defn false? [x] (= x false))
(defn nil? [x] (= x nil)) (defn nil? [x] (= x nil))
(def atomic? (do (def atomic?
(def non-atomic-types { (do
:array true (def non-atomic-types
{:array true
:tuple true :tuple true
:table true :table true
:struct true :struct true})
})
(fn [x] (not (get non-atomic-types (type x)))))) (fn [x] (not (get non-atomic-types (type x))))))
(defn sum [xs] (apply1 + xs)) (defn sum [xs] (apply1 + xs))
(defn product [xs] (apply1 * xs)) (defn product [xs] (apply1 * xs))
@ -141,7 +136,7 @@ Expands to (def sym (if (= nil sym) val sym))"
[condition & body] [condition & body]
(tuple 'if condition (tuple.prepend body 'do))) (tuple 'if condition (tuple.prepend body 'do)))
(defmacro when-not (defmacro unless
"Shorthand for (when (not ... " "Shorthand for (when (not ... "
[condition & body] [condition & body]
(tuple 'if condition nil (tuple.prepend body 'do))) (tuple 'if condition nil (tuple.prepend body 'do)))
@ -213,9 +208,8 @@ value."
(var i 0) (var i 0)
(var accum @['do]) (var accum @['do])
(while (< i len) (while (< i len)
(array.push accum (tuple 'def (def {i k (+ i 1) v} bindings)
(get bindings i) (array.push accum (tuple 'def k v))
(get bindings (+ 1 i))))
(+= i 2)) (+= i 2))
(array.concat accum body) (array.concat accum body)
(apply1 tuple accum)) (apply1 tuple accum))
@ -244,8 +238,7 @@ value."
:let (tuple 'let verb (doone (+ i 2))) :let (tuple 'let verb (doone (+ i 2)))
:when (tuple 'if verb (doone (+ i 2))) :when (tuple 'if verb (doone (+ i 2)))
(error ("unexpected loop predicate: " verb))) (error ("unexpected loop predicate: " verb)))
(switch (switch verb
verb
:iterate (do :iterate (do
(def preds @['and (tuple ':= bindings object)]) (def preds @['and (tuple ':= bindings object)])
(def subloop (doone (+ i 3) preds)) (def subloop (doone (+ i 3) preds))
@ -308,7 +301,9 @@ value."
evaluates to false." evaluates to false."
[& forms] [& forms]
(def len (length forms)) (def len (length forms))
(if (= len 0) true ((fn aux [i] (if (= len 0)
true
((fn aux [i]
(cond (cond
(>= (inc i) len) (get forms i) (>= (inc i) len) (get forms i)
(tuple 'if (get forms i) (aux (inc i)) false))) 0))) (tuple 'if (get forms i) (aux (inc i)) false))) 0)))
@ -318,7 +313,9 @@ evaluates to false."
evaluates to true." evaluates to true."
[& forms] [& forms]
(def len (length forms)) (def len (length forms))
(if (= len 0) false ((fn aux [i] (if (= len 0)
false
((fn aux [i]
(def fi (get forms i)) (def fi (get forms i))
(if (if
(>= (inc i) len) fi (>= (inc i) len) fi
@ -974,7 +971,8 @@ onvalue."
(def p (parser.new)) (def p (parser.new))
# Fiber stream of characters # Fiber stream of characters
(def chars (coro (def chars
(coro
(def buf @"") (def buf @"")
(var len 1) (var len 1)
(while (< 0 len) (while (< 0 len)
@ -986,7 +984,8 @@ onvalue."
0)) 0))
# Fiber stream of values # Fiber stream of values
(def vals (coro (def vals
(coro
(while going (while going
(switch (parser.status p) (switch (parser.status p)
:full (yield (parser.produce p)) :full (yield (parser.produce p))
@ -1003,14 +1002,18 @@ onvalue."
# Evaluate 1 source form # Evaluate 1 source form
(defn eval1 [source] (defn eval1 [source]
(var good true) (var good true)
(def f (fiber.new (fn [] (def f
(fiber.new
(fn []
(def res (compile source env where)) (def res (compile source env where))
(if (= (type res) :function) (if (= (type res) :function)
(res) (res)
(do (do
(:= good false) (:= good false)
(def {:error err :error-line errl :error-column errc} res) (def {:error err :error-line errl :error-column errc} res)
(onerr where "compile" (onerr
where
"compile"
(if (< 0 errl) (if (< 0 errl)
(string err " in form at line " errl ", column " errc) (string err " in form at line " errl ", column " errc)
err))))) err)))))
@ -1040,7 +1043,8 @@ onvalue."
(pp x)) (pp x))
(when f (when f
(def st (fiber.stack f)) (def st (fiber.stack f))
(loop [{ (loop
[{
:function func :function func
:tail tail :tail tail
:pc pc :pc pc
@ -1078,23 +1082,21 @@ environment is needed, use run-context."
(run-context *env* chunks (fn [x] (:= returnval x)) default-error-handler "eval") (run-context *env* chunks (fn [x] (:= returnval x)) default-error-handler "eval")
returnval) returnval)
(def module.paths @[ (def module.paths
"./?.dst" @["./?.dst"
"./?/init.dst" "./?/init.dst"
"./dst_modules/?.dst" "./dst_modules/?.dst"
"./dst_modules/?/init.dst" "./dst_modules/?/init.dst"
"/usr/local/dst/0.0.0/?.dst" "/usr/local/dst/0.0.0/?.dst"
"/usr/local/dst/0.0.0/?/init.dst" "/usr/local/dst/0.0.0/?/init.dst"])
])
(def module.native-paths @[ (def module.native-paths
"./?.so" @["./?.so"
"./?/??.so" "./?/??.so"
"./dst_modules/?.so" "./dst_modules/?.so"
"./dst_modules/?/??.so" "./dst_modules/?/??.so"
"/usr/local/dst/0.0.0/?.so" "/usr/local/dst/0.0.0/?.so"
"/usr/local/dst/0.0.0/?/??.so" "/usr/local/dst/0.0.0/?/??.so"])
])
(defn module.find (defn module.find
[path paths] [path paths]
@ -1124,7 +1126,9 @@ returned from compiling and running the file."
(defn check-native (defn check-native
[p testpath] [p testpath]
(if p p (do (if p
p
(do
(def f (file.open testpath)) (def f (file.open testpath))
(if f (do (file.close f) testpath))))) (if f (do (file.close f) testpath)))))
@ -1137,11 +1141,11 @@ returned from compiling and running the file."
(fn require [path args] (fn require [path args]
(when (get loading path) (when (get loading path)
(error (string "circular dependency: module " path " is loading"))) (error (string "circular dependency: module " path " is loading")))
(def { (def {:exit exit-on-error} (or args {}))
:exit exit-on-error
} (or args {}))
(def check (get cache path)) (def check (get cache path))
(if check check (do (if check
check
(do
(def newenv (make-env)) (def newenv (make-env))
(put cache path newenv) (put cache path newenv)
(put loading path true) (put loading path true)

View File

@ -8,10 +8,11 @@
(var *exit-on-error* :private true) (var *exit-on-error* :private true)
# Flag handlers # Flag handlers
(def handlers :private { (def handlers :private
"h" (fn [] {"h" (fn []
(print "usage: " (get args 0) " [options] scripts...") (print "usage: " (get args 0) " [options] scripts...")
(print `Options are: (print
`Options are:
-h Show this help -h Show this help
-v Print the version string -v Print the version string
-s Use raw stdin instead of getline like functionality -s Use raw stdin instead of getline like functionality
@ -29,8 +30,7 @@
"e" (fn [i] "e" (fn [i]
(:= *no-file* false) (:= *no-file* false)
(eval (get args (+ i 1))) (eval (get args (+ i 1)))
2) 2)})
})
(defn- dohandler [n i] (defn- dohandler [n i]
(def h (get handlers n)) (def h (get handlers n))
@ -56,6 +56,4 @@
(repl (fn [buf p] (repl (fn [buf p]
(def [line] (parser.where p)) (def [line] (parser.where p))
(def prompt (string "dst:" line ":" (parser.state p) "> ")) (def prompt (string "dst:" line ":" (parser.state p) "> "))
(getline prompt buf)))))) (getline prompt buf)))))))
)