1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09: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] ...))."
(fn defn [name & more]
(def len (length more))
(def fstart (fn recur [i]
(def fstart
(fn recur [i]
(def {i ith} more)
(def t (type ith))
(def tuple? (= t :tuple))
@ -29,28 +30,23 @@
(def defmacro :macro
"Define a macro."
(do
(fn defmacro [name & more]
(apply1 defn (array.concat
@[name :macro] more)))))
(apply1 defn (array.concat @[name :macro] more))))
(defmacro defmacro-
"Define a private macro that will not be exported."
[name & more]
(apply1 tuple (array.concat
@['defmacro name :private] more)))
(apply1 tuple (array.concat @['defmacro name :private] more)))
(defmacro defn-
"Define a private function that will not be exported."
[name & more]
(apply1 tuple (array.concat
@['defn name :private] more)))
(apply1 tuple (array.concat @['defn name :private] more)))
(defmacro def-
"Define a private value that will not be exported."
[name & more]
(apply1 tuple (array.concat
@['def name :private] more)))
(apply1 tuple (array.concat @['def name :private] more)))
# Basic predicates
(defn even? [x] (== 0 (% x 2)))
@ -92,15 +88,14 @@
(defn true? [x] (= x true))
(defn false? [x] (= x false))
(defn nil? [x] (= x nil))
(def atomic? (do
(def non-atomic-types {
:array true
(def atomic?
(do
(def non-atomic-types
{:array true
:tuple true
:table true
:struct true
})
:struct true})
(fn [x] (not (get non-atomic-types (type x))))))
(defn sum [xs] (apply1 + xs))
(defn product [xs] (apply1 * xs))
@ -123,7 +118,7 @@
(defmacro default
"Define a default value for an optional argument.
Expands to (def sym (if (= nil sym) val sym))"
Expands to (def sym (if (= nil sym) val sym))"
[sym val]
(tuple 'def sym (tuple 'if (tuple = nil sym) val sym)))
@ -141,16 +136,16 @@ Expands to (def sym (if (= nil sym) val sym))"
[condition & body]
(tuple 'if condition (tuple.prepend body 'do)))
(defmacro when-not
(defmacro unless
"Shorthand for (when (not ... "
[condition & body]
(tuple 'if condition nil (tuple.prepend body 'do)))
(defmacro cond
"Evaluates conditions sequentially until the first true condition
is found, and then executes the corresponding body. If there are an
odd number of forms, the last expression is executed if no forms
are matched. If there are no matches, return nil."
"Evaluates conditions sequentially until the first true condition
is found, and then executes the corresponding body. If there are an
odd number of forms, the last expression is executed if no forms
are matched. If there are no matches, return nil."
[& pairs]
(defn aux [i]
(def restlen (- (length pairs) i))
@ -177,16 +172,16 @@ are matched. If there are no matches, return nil."
(defn apply
"Evaluate to (f ...args), where the final value of args must be an array or
tuple and will be spliced into the function call. For example, (apply + 1 2 @[3 4])
evaluates to 10."
tuple and will be spliced into the function call. For example, (apply + 1 2 @[3 4])
evaluates to 10."
[f & args]
(def last (- (length args) 1))
(apply1 f (array.concat (array.slice args 0 -2) (get args last))))
(defmacro switch
"Select the body that equals the dispatch value. When pairs
has an odd number of arguments, the last is the default expression.
If no match is found, returns nil"
"Select the body that equals the dispatch value. When pairs
has an odd number of arguments, the last is the default expression.
If no match is found, returns nil"
[dispatch & pairs]
(def atm (atomic? dispatch))
(def sym (if atm dispatch (gensym)))
@ -204,18 +199,17 @@ If no match is found, returns nil"
(aux 0))))
(defmacro let
"Create a scope and bind values to symbols. Each pair in bindings is
assigned as if with def, and the body of the let form returns the last
value."
"Create a scope and bind values to symbols. Each pair in bindings is
assigned as if with def, and the body of the let form returns the last
value."
[bindings & body]
(if (odd? (length bindings)) (error "expected even number of bindings to let"))
(def len (length bindings))
(var i 0)
(var accum @['do])
(while (< i len)
(array.push accum (tuple 'def
(get bindings i)
(get bindings (+ 1 i))))
(def {i k (+ i 1) v} bindings)
(array.push accum (tuple 'def k v))
(+= i 2))
(array.concat accum body)
(apply1 tuple accum))
@ -244,8 +238,7 @@ value."
:let (tuple 'let verb (doone (+ i 2)))
:when (tuple 'if verb (doone (+ i 2)))
(error ("unexpected loop predicate: " verb)))
(switch
verb
(switch verb
:iterate (do
(def preds @['and (tuple ':= bindings object)])
(def subloop (doone (+ i 3) preds))
@ -305,20 +298,24 @@ value."
(defmacro and
"Evaluates to the last argument if all preceding elements are true, otherwise
evaluates to false."
evaluates to false."
[& forms]
(def len (length forms))
(if (= len 0) true ((fn aux [i]
(if (= len 0)
true
((fn aux [i]
(cond
(>= (inc i) len) (get forms i)
(tuple 'if (get forms i) (aux (inc i)) false))) 0)))
(defmacro or
"Evaluates to the last argument if all preceding elements are false, otherwise
evaluates to true."
evaluates to true."
[& forms]
(def len (length forms))
(if (= len 0) false ((fn aux [i]
(if (= len 0)
false
((fn aux [i]
(def fi (get forms i))
(if
(>= (inc i) len) fi
@ -336,7 +333,7 @@ evaluates to true."
(tuple fiber.new (apply tuple 'fn [] body)))
(defmacro if-let
"Takes the first one or two forms in a vector and if both are true binds
"Takes the first one or two forms in a vector and if both are true binds
all the forms with let and evaluates the first expression else
evaluates the second"
[bindings tru fal]
@ -367,13 +364,13 @@ evaluates to true."
(aux 0))
(defmacro when-let
"Takes the first one or two forms in vector and if true binds
"Takes the first one or two forms in vector and if true binds
all the forms with let and evaluates the body"
[bindings & body]
(tuple 'if-let bindings (tuple.prepend body 'do)))
(defn comp
"Takes multiple functions and returns a function that is the composition
"Takes multiple functions and returns a function that is the composition
of those functions."
[& functions]
(switch (length functions)
@ -398,7 +395,7 @@ evaluates to true."
(defn extreme
"Returns the most extreme value in args based on the orderer order.
Returns nil if args is empty."
Returns nil if args is empty."
[order args]
(def len (length args))
(when (pos? len)
@ -459,7 +456,7 @@ Returns nil if args is empty."
(defn reduce
"Reduce, also know as fold-left in many languages, transforms
an indexed type (array, tuple) with a function to produce a value."
an indexed type (array, tuple) with a function to produce a value."
[f init ind]
(var res init)
(loop [x :in ind]
@ -468,7 +465,7 @@ an indexed type (array, tuple) with a function to produce a value."
(defn map
"Map a function over every element in an array or tuple and return
the same type as the input sequence."
the same type as the input sequence."
[f & inds]
(def ninds (length inds))
(if (= 0 ninds) (error "expected at least 1 indexed collection"))
@ -491,7 +488,7 @@ the same type as the input sequence."
(defn each
"Map a function over every element in an array or tuple but do not
return a new indexed type."
return a new indexed type."
[f & inds]
(def ninds (length inds))
(if (= 0 ninds) (error "expected at least 1 indexed collection"))
@ -512,8 +509,8 @@ return a new indexed type."
(defn mapcat
"Map a function over every element in an array or tuple and
use array to concatenate the results. Returns the same
type as the input sequence."
use array to concatenate the results. Returns the same
type as the input sequence."
[f ind t]
(def res @[])
(loop [x :in ind]
@ -524,7 +521,7 @@ type as the input sequence."
(defn filter
"Given a predicate, take only elements from an array or tuple for
which (pred element) is truthy. Returns the same type as the input sequence."
which (pred element) is truthy. Returns the same type as the input sequence."
[pred ind t]
(def res @[])
(loop [item :in ind]
@ -561,7 +558,7 @@ which (pred element) is truthy. Returns the same type as the input sequence."
(defn take-until
"Given a predicate, take only elements from an indexed type that satisfy
the predicate, and abort on first failure. Returns a new tuple."
the predicate, and abort on first failure. Returns a new tuple."
[pred ind]
(def i (find-index pred ind))
(if i
@ -575,7 +572,7 @@ the predicate, and abort on first failure. Returns a new tuple."
(defn drop-until
"Given a predicate, remove elements from an indexed type that satisfy
the predicate, and abort on first failure. Returns a new tuple."
the predicate, and abort on first failure. Returns a new tuple."
[pred ind]
(def i (find-index pred ind))
(tuple.slice ind i -1))
@ -603,8 +600,8 @@ the predicate, and abort on first failure. Returns a new tuple."
(defmacro ->
"Threading macro. Inserts x as the second value in the first form
in form, and inserts the modified firsts form into the second form
in the same manner, and so on. Useful for expressing pipelines of data."
in form, and inserts the modified firsts form into the second form
in the same manner, and so on. Useful for expressing pipelines of data."
[x & forms]
(defn fop [last n]
(def [h t] (if (= :tuple (type n))
@ -616,8 +613,8 @@ in the same manner, and so on. Useful for expressing pipelines of data."
(defmacro ->>
"Threading macro. Inserts x as the last value in the first form
in form, and inserts the modified firsts form into the second form
in the same manner, and so on. Useful for expressing pipelines of data."
in form, and inserts the modified firsts form into the second form
in the same manner, and so on. Useful for expressing pipelines of data."
[x & forms]
(defn fop [last n]
(def [h t] (if (= :tuple (type n))
@ -667,7 +664,7 @@ in the same manner, and so on. Useful for expressing pipelines of data."
:array array.reverse) t))
(defn zipcoll
"Creates an table or tuple from two arrays/tuples. If a third argument of
"Creates an table or tuple from two arrays/tuples. If a third argument of
:struct is given result is struct else is table."
[keys vals t]
(def res @{})
@ -681,7 +678,7 @@ in the same manner, and so on. Useful for expressing pipelines of data."
res))
(defn update
"Accepts a key argument and passes its' associated value to a function.
"Accepts a key argument and passes its' associated value to a function.
The key then, is associated to the function's return value"
[coll a-key a-function & args]
(def old-value (get coll a-key))
@ -747,7 +744,7 @@ in the same manner, and so on. Useful for expressing pipelines of data."
(defn pp
"Pretty print a value. Displays values inside collections, and is safe
to call on any table. Does not print table prototype information."
to call on any table. Does not print table prototype information."
[x]
(def buf @"")
@ -911,7 +908,7 @@ to call on any table. Does not print table prototype information."
(defn deep-not= [x y]
"Like not=, but mutable types (arrays, tables, buffers) are considered
equal if they have identical structure. Much slower than not=."
equal if they have identical structure. Much slower than not=."
(def tx (type x))
(or
(not= tx (type y))
@ -925,7 +922,7 @@ equal if they have identical structure. Much slower than not=."
(defn deep= [x y]
"Like =, but mutable types (arrays, tables, buffers) are considered
equal if they have identical structure. Much slower than =."
equal if they have identical structure. Much slower than =."
(not (deep-not= x y)))
(defn macroexpand
@ -954,17 +951,17 @@ equal if they have identical structure. Much slower than =."
newenv)
(defn run-context
"Run a context. This evaluates expressions of dst in an environment,
and is encapsulates the parsing, compilation, and evaluation of dst.
env is the environment to evaluate the code in, chunks is a function
that returns strings or buffers of source code (from a repl, file,
network connection, etc. onvalue and onerr are callbacks that are
invoked when a result is returned and when an error is produced,
respectively.
"Run a context. This evaluates expressions of dst in an environment,
and is encapsulates the parsing, compilation, and evaluation of dst.
env is the environment to evaluate the code in, chunks is a function
that returns strings or buffers of source code (from a repl, file,
network connection, etc. onvalue and onerr are callbacks that are
invoked when a result is returned and when an error is produced,
respectively.
This function can be used to implement a repl very easily, simply
pass a function that reads line from stdin to chunks, and print to
onvalue."
This function can be used to implement a repl very easily, simply
pass a function that reads line from stdin to chunks, and print to
onvalue."
[env chunks onvalue onerr where]
# Are we done yet?
@ -974,7 +971,8 @@ onvalue."
(def p (parser.new))
# Fiber stream of characters
(def chars (coro
(def chars
(coro
(def buf @"")
(var len 1)
(while (< 0 len)
@ -986,7 +984,8 @@ onvalue."
0))
# Fiber stream of values
(def vals (coro
(def vals
(coro
(while going
(switch (parser.status p)
:full (yield (parser.produce p))
@ -1003,14 +1002,18 @@ onvalue."
# Evaluate 1 source form
(defn eval1 [source]
(var good true)
(def f (fiber.new (fn []
(def f
(fiber.new
(fn []
(def res (compile source env where))
(if (= (type res) :function)
(res)
(do
(:= good false)
(def {:error err :error-line errl :error-column errc} res)
(onerr where "compile"
(onerr
where
"compile"
(if (< 0 errl)
(string err " in form at line " errl ", column " errc)
err)))))
@ -1040,7 +1043,8 @@ onvalue."
(pp x))
(when f
(def st (fiber.stack f))
(loop [{
(loop
[{
:function func
:tail tail
:pc pc
@ -1066,7 +1070,7 @@ onvalue."
(defn eval
"Evaluates a string in the current environment. If more control over the
environment is needed, use run-context."
environment is needed, use run-context."
[str]
(var state (string str))
(defn chunks [buf]
@ -1078,23 +1082,21 @@ environment is needed, use run-context."
(run-context *env* chunks (fn [x] (:= returnval x)) default-error-handler "eval")
returnval)
(def module.paths @[
"./?.dst"
(def module.paths
@["./?.dst"
"./?/init.dst"
"./dst_modules/?.dst"
"./dst_modules/?/init.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 @[
"./?.so"
(def module.native-paths
@["./?.so"
"./?/??.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"])
(defn module.find
[path paths]
@ -1110,8 +1112,8 @@ environment is needed, use run-context."
(def require
"Require a module with the given name. Will search all of the paths in
module.paths, then the path as a raw file path. Returns the new environment
returned from compiling and running the file."
module.paths, then the path as a raw file path. Returns the new environment
returned from compiling and running the file."
(do
(defn check-mod
@ -1124,7 +1126,9 @@ returned from compiling and running the file."
(defn check-native
[p testpath]
(if p p (do
(if p
p
(do
(def f (file.open testpath))
(if f (do (file.close f) testpath)))))
@ -1137,11 +1141,11 @@ returned from compiling and running the file."
(fn require [path args]
(when (get loading path)
(error (string "circular dependency: module " path " is loading")))
(def {
:exit exit-on-error
} (or args {}))
(def {:exit exit-on-error} (or args {}))
(def check (get cache path))
(if check check (do
(if check
check
(do
(def newenv (make-env))
(put cache path newenv)
(put loading path true)
@ -1184,9 +1188,9 @@ returned from compiling and running the file."
(defmacro import [path & args]
"Import a module. First requires the module, and then merges its
symbols into the current environment, prepending a given prefix as needed.
(use the :as or :prefix option to set a prefix). If no prefix is provided,
use the name of the module as a prefix."
symbols into the current environment, prepending a given prefix as needed.
(use the :as or :prefix option to set a prefix). If no prefix is provided,
use the name of the module as a prefix."
(def argm (map (fn [x]
(if (and (symbol? x) (= (get x 0) 58))
x
@ -1196,7 +1200,7 @@ use the name of the module as a prefix."
(defn repl [getchunk onvalue onerr]
"Run a repl. The first parameter is an optional function to call to
get a chunk of source code. Should return nil for end of file."
get a chunk of source code. Should return nil for end of file."
(def newenv (make-env))
(default getchunk (fn [buf]
(file.read stdin :line buf)))

View File

@ -1,17 +1,18 @@
# Copyright 2017-2018 (C) Calvin Rose
(do
(var *should-repl* :private false)
(var *no-file* :private true)
(var *raw-stdin* :private false)
(var *handleopts* :private true)
(var *exit-on-error* :private true)
(var *should-repl* :private false)
(var *no-file* :private true)
(var *raw-stdin* :private false)
(var *handleopts* :private true)
(var *exit-on-error* :private true)
# Flag handlers
(def handlers :private {
"h" (fn []
# Flag handlers
(def handlers :private
{"h" (fn []
(print "usage: " (get args 0) " [options] scripts...")
(print `Options are:
(print
`Options are:
-h Show this help
-v Print the version string
-s Use raw stdin instead of getline like functionality
@ -29,17 +30,16 @@
"e" (fn [i]
(:= *no-file* false)
(eval (get args (+ i 1)))
2)
})
2)})
(defn- dohandler [n i]
(defn- dohandler [n i]
(def h (get handlers n))
(if h (h i) (print "unknown flag -" n)))
# Process arguments
(var i 1)
(def lenargs (length args))
(while (< i lenargs)
# Process arguments
(var i 1)
(def lenargs (length args))
(while (< i lenargs)
(def arg (get args i))
(if (and *handleopts* (= "-" (string.slice arg 0 1)))
(+= i (dohandler (string.slice arg 1 2) i))
@ -48,7 +48,7 @@
(import* _env arg :exit *exit-on-error*)
(++ i))))
(when (or *should-repl* *no-file*)
(when (or *should-repl* *no-file*)
(if *raw-stdin*
(repl nil identity)
(do
@ -56,6 +56,4 @@
(repl (fn [buf p]
(def [line] (parser.where p))
(def prompt (string "dst:" line ":" (parser.state p) "> "))
(getline prompt buf))))))
)
(getline prompt buf)))))))