mirror of
https://github.com/janet-lang/janet
synced 2024-11-28 19:19:53 +00:00
Update indentation in boot and init to be more like
most lisps.
This commit is contained in:
parent
79225ad3d5
commit
e60c8a9b75
@ -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))
|
||||||
|
|
||||||
@ -123,7 +118,7 @@
|
|||||||
|
|
||||||
(defmacro default
|
(defmacro default
|
||||||
"Define a default value for an optional argument.
|
"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]
|
[sym val]
|
||||||
(tuple 'def sym (tuple 'if (tuple = nil sym) val sym)))
|
(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]
|
[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)))
|
||||||
|
|
||||||
(defmacro cond
|
(defmacro cond
|
||||||
"Evaluates conditions sequentially until the first true condition
|
"Evaluates conditions sequentially until the first true condition
|
||||||
is found, and then executes the corresponding body. If there are an
|
is found, and then executes the corresponding body. If there are an
|
||||||
odd number of forms, the last expression is executed if no forms
|
odd number of forms, the last expression is executed if no forms
|
||||||
are matched. If there are no matches, return nil."
|
are matched. If there are no matches, return nil."
|
||||||
[& pairs]
|
[& pairs]
|
||||||
(defn aux [i]
|
(defn aux [i]
|
||||||
(def restlen (- (length pairs) i))
|
(def restlen (- (length pairs) i))
|
||||||
@ -177,16 +172,16 @@ are matched. If there are no matches, return nil."
|
|||||||
|
|
||||||
(defn apply
|
(defn apply
|
||||||
"Evaluate to (f ...args), where the final value of args must be an array or
|
"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])
|
tuple and will be spliced into the function call. For example, (apply + 1 2 @[3 4])
|
||||||
evaluates to 10."
|
evaluates to 10."
|
||||||
[f & args]
|
[f & args]
|
||||||
(def last (- (length args) 1))
|
(def last (- (length args) 1))
|
||||||
(apply1 f (array.concat (array.slice args 0 -2) (get args last))))
|
(apply1 f (array.concat (array.slice args 0 -2) (get args last))))
|
||||||
|
|
||||||
(defmacro switch
|
(defmacro switch
|
||||||
"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.
|
||||||
If no match is found, returns nil"
|
If no match is found, returns nil"
|
||||||
[dispatch & pairs]
|
[dispatch & pairs]
|
||||||
(def atm (atomic? dispatch))
|
(def atm (atomic? dispatch))
|
||||||
(def sym (if atm dispatch (gensym)))
|
(def sym (if atm dispatch (gensym)))
|
||||||
@ -204,18 +199,17 @@ If no match is found, returns nil"
|
|||||||
(aux 0))))
|
(aux 0))))
|
||||||
|
|
||||||
(defmacro let
|
(defmacro let
|
||||||
"Create a scope and bind values to symbols. Each pair in bindings is
|
"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
|
assigned as if with def, and the body of the let form returns the last
|
||||||
value."
|
value."
|
||||||
[bindings & body]
|
[bindings & body]
|
||||||
(if (odd? (length bindings)) (error "expected even number of bindings to let"))
|
(if (odd? (length bindings)) (error "expected even number of bindings to let"))
|
||||||
(def len (length bindings))
|
(def len (length bindings))
|
||||||
(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))
|
||||||
@ -305,20 +298,24 @@ value."
|
|||||||
|
|
||||||
(defmacro and
|
(defmacro and
|
||||||
"Evaluates to the last argument if all preceding elements are true, otherwise
|
"Evaluates to the last argument if all preceding elements are true, otherwise
|
||||||
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)))
|
||||||
|
|
||||||
(defmacro or
|
(defmacro or
|
||||||
"Evaluates to the last argument if all preceding elements are false, otherwise
|
"Evaluates to the last argument if all preceding elements are false, otherwise
|
||||||
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
|
||||||
@ -336,7 +333,7 @@ evaluates to true."
|
|||||||
(tuple fiber.new (apply tuple 'fn [] body)))
|
(tuple fiber.new (apply tuple 'fn [] body)))
|
||||||
|
|
||||||
(defmacro if-let
|
(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
|
all the forms with let and evaluates the first expression else
|
||||||
evaluates the second"
|
evaluates the second"
|
||||||
[bindings tru fal]
|
[bindings tru fal]
|
||||||
@ -367,13 +364,13 @@ evaluates to true."
|
|||||||
(aux 0))
|
(aux 0))
|
||||||
|
|
||||||
(defmacro when-let
|
(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"
|
all the forms with let and evaluates the body"
|
||||||
[bindings & body]
|
[bindings & body]
|
||||||
(tuple 'if-let bindings (tuple.prepend body 'do)))
|
(tuple 'if-let bindings (tuple.prepend body 'do)))
|
||||||
|
|
||||||
(defn comp
|
(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."
|
of those functions."
|
||||||
[& functions]
|
[& functions]
|
||||||
(switch (length functions)
|
(switch (length functions)
|
||||||
@ -398,7 +395,7 @@ evaluates to true."
|
|||||||
|
|
||||||
(defn extreme
|
(defn extreme
|
||||||
"Returns the most extreme value in args based on the orderer order.
|
"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]
|
[order args]
|
||||||
(def len (length args))
|
(def len (length args))
|
||||||
(when (pos? len)
|
(when (pos? len)
|
||||||
@ -459,7 +456,7 @@ Returns nil if args is empty."
|
|||||||
|
|
||||||
(defn reduce
|
(defn reduce
|
||||||
"Reduce, also know as fold-left in many languages, transforms
|
"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]
|
[f init ind]
|
||||||
(var res init)
|
(var res init)
|
||||||
(loop [x :in ind]
|
(loop [x :in ind]
|
||||||
@ -468,7 +465,7 @@ an indexed type (array, tuple) with a function to produce a value."
|
|||||||
|
|
||||||
(defn map
|
(defn map
|
||||||
"Map a function over every element in an array or tuple and return
|
"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]
|
[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"))
|
||||||
@ -491,7 +488,7 @@ the same type as the input sequence."
|
|||||||
|
|
||||||
(defn each
|
(defn each
|
||||||
"Map a function over every element in an array or tuple but do not
|
"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]
|
[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"))
|
||||||
@ -512,8 +509,8 @@ return a new indexed type."
|
|||||||
|
|
||||||
(defn mapcat
|
(defn mapcat
|
||||||
"Map a function over every element in an array or tuple and
|
"Map a function over every element in an array or tuple and
|
||||||
use array to concatenate the results. Returns the same
|
use array to concatenate the results. Returns the same
|
||||||
type as the input sequence."
|
type as the input sequence."
|
||||||
[f ind t]
|
[f ind t]
|
||||||
(def res @[])
|
(def res @[])
|
||||||
(loop [x :in ind]
|
(loop [x :in ind]
|
||||||
@ -524,7 +521,7 @@ type as the input sequence."
|
|||||||
|
|
||||||
(defn filter
|
(defn filter
|
||||||
"Given a predicate, take only elements from an array or tuple for
|
"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]
|
[pred ind t]
|
||||||
(def res @[])
|
(def res @[])
|
||||||
(loop [item :in ind]
|
(loop [item :in ind]
|
||||||
@ -561,7 +558,7 @@ which (pred element) is truthy. Returns the same type as the input sequence."
|
|||||||
|
|
||||||
(defn take-until
|
(defn take-until
|
||||||
"Given a predicate, take only elements from an indexed type that satisfy
|
"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]
|
[pred ind]
|
||||||
(def i (find-index pred ind))
|
(def i (find-index pred ind))
|
||||||
(if i
|
(if i
|
||||||
@ -575,7 +572,7 @@ the predicate, and abort on first failure. Returns a new tuple."
|
|||||||
|
|
||||||
(defn drop-until
|
(defn drop-until
|
||||||
"Given a predicate, remove elements from an indexed type that satisfy
|
"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]
|
[pred ind]
|
||||||
(def i (find-index pred ind))
|
(def i (find-index pred ind))
|
||||||
(tuple.slice ind i -1))
|
(tuple.slice ind i -1))
|
||||||
@ -603,8 +600,8 @@ the predicate, and abort on first failure. Returns a new tuple."
|
|||||||
|
|
||||||
(defmacro ->
|
(defmacro ->
|
||||||
"Threading macro. Inserts x as the second value in the first form
|
"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 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 the same manner, and so on. Useful for expressing pipelines of data."
|
||||||
[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))
|
||||||
@ -616,8 +613,8 @@ in the same manner, and so on. Useful for expressing pipelines of data."
|
|||||||
|
|
||||||
(defmacro ->>
|
(defmacro ->>
|
||||||
"Threading macro. Inserts x as the last value in the first form
|
"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 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 the same manner, and so on. Useful for expressing pipelines of data."
|
||||||
[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))
|
||||||
@ -667,7 +664,7 @@ in the same manner, and so on. Useful for expressing pipelines of data."
|
|||||||
:array array.reverse) t))
|
:array array.reverse) t))
|
||||||
|
|
||||||
(defn zipcoll
|
(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."
|
:struct is given result is struct else is table."
|
||||||
[keys vals t]
|
[keys vals t]
|
||||||
(def res @{})
|
(def res @{})
|
||||||
@ -681,7 +678,7 @@ in the same manner, and so on. Useful for expressing pipelines of data."
|
|||||||
res))
|
res))
|
||||||
|
|
||||||
(defn update
|
(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"
|
The key then, is associated to the function's return value"
|
||||||
[coll a-key a-function & args]
|
[coll a-key a-function & args]
|
||||||
(def old-value (get coll a-key))
|
(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
|
(defn pp
|
||||||
"Pretty print a value. Displays values inside collections, and is safe
|
"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]
|
[x]
|
||||||
|
|
||||||
(def buf @"")
|
(def buf @"")
|
||||||
@ -911,7 +908,7 @@ to call on any table. Does not print table prototype information."
|
|||||||
|
|
||||||
(defn deep-not= [x y]
|
(defn deep-not= [x y]
|
||||||
"Like not=, but mutable types (arrays, tables, buffers) are considered
|
"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))
|
(def tx (type x))
|
||||||
(or
|
(or
|
||||||
(not= tx (type y))
|
(not= tx (type y))
|
||||||
@ -925,7 +922,7 @@ equal if they have identical structure. Much slower than not=."
|
|||||||
|
|
||||||
(defn deep= [x y]
|
(defn deep= [x y]
|
||||||
"Like =, but mutable types (arrays, tables, buffers) are considered
|
"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)))
|
(not (deep-not= x y)))
|
||||||
|
|
||||||
(defn macroexpand
|
(defn macroexpand
|
||||||
@ -954,17 +951,17 @@ equal if they have identical structure. Much slower than =."
|
|||||||
newenv)
|
newenv)
|
||||||
|
|
||||||
(defn run-context
|
(defn run-context
|
||||||
"Run a context. This evaluates expressions of dst in an environment,
|
"Run a context. This evaluates expressions of dst in an environment,
|
||||||
and is encapsulates the parsing, compilation, and evaluation of dst.
|
and is encapsulates the parsing, compilation, and evaluation of dst.
|
||||||
env is the environment to evaluate the code in, chunks is a function
|
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,
|
that returns strings or buffers of source code (from a repl, file,
|
||||||
network connection, etc. onvalue and onerr are callbacks that are
|
network connection, etc. onvalue and onerr are callbacks that are
|
||||||
invoked when a result is returned and when an error is produced,
|
invoked when a result is returned and when an error is produced,
|
||||||
respectively.
|
respectively.
|
||||||
|
|
||||||
This function can be used to implement a repl very easily, simply
|
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
|
pass a function that reads line from stdin to chunks, and print to
|
||||||
onvalue."
|
onvalue."
|
||||||
[env chunks onvalue onerr where]
|
[env chunks onvalue onerr where]
|
||||||
|
|
||||||
# Are we done yet?
|
# Are we done yet?
|
||||||
@ -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
|
||||||
@ -1066,7 +1070,7 @@ onvalue."
|
|||||||
|
|
||||||
(defn eval
|
(defn eval
|
||||||
"Evaluates a string in the current environment. If more control over the
|
"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]
|
[str]
|
||||||
(var state (string str))
|
(var state (string str))
|
||||||
(defn chunks [buf]
|
(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")
|
(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]
|
||||||
@ -1110,8 +1112,8 @@ environment is needed, use run-context."
|
|||||||
|
|
||||||
(def require
|
(def require
|
||||||
"Require a module with the given name. Will search all of the paths in
|
"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
|
module.paths, then the path as a raw file path. Returns the new environment
|
||||||
returned from compiling and running the file."
|
returned from compiling and running the file."
|
||||||
(do
|
(do
|
||||||
|
|
||||||
(defn check-mod
|
(defn check-mod
|
||||||
@ -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)
|
||||||
@ -1184,9 +1188,9 @@ returned from compiling and running the file."
|
|||||||
|
|
||||||
(defmacro import [path & args]
|
(defmacro import [path & args]
|
||||||
"Import a module. First requires the module, and then merges its
|
"Import a module. First requires the module, and then merges its
|
||||||
symbols into the current environment, prepending a given prefix as needed.
|
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 :as or :prefix option to set a prefix). If no prefix is provided,
|
||||||
use the name of the module as a prefix."
|
use the name of the module as a prefix."
|
||||||
(def argm (map (fn [x]
|
(def argm (map (fn [x]
|
||||||
(if (and (symbol? x) (= (get x 0) 58))
|
(if (and (symbol? x) (= (get x 0) 58))
|
||||||
x
|
x
|
||||||
@ -1196,7 +1200,7 @@ use the name of the module as a prefix."
|
|||||||
|
|
||||||
(defn repl [getchunk onvalue onerr]
|
(defn repl [getchunk onvalue onerr]
|
||||||
"Run a repl. The first parameter is an optional function to call to
|
"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))
|
(def newenv (make-env))
|
||||||
(default getchunk (fn [buf]
|
(default getchunk (fn [buf]
|
||||||
(file.read stdin :line buf)))
|
(file.read stdin :line buf)))
|
||||||
|
@ -1,17 +1,18 @@
|
|||||||
# Copyright 2017-2018 (C) Calvin Rose
|
# Copyright 2017-2018 (C) Calvin Rose
|
||||||
(do
|
(do
|
||||||
|
|
||||||
(var *should-repl* :private false)
|
(var *should-repl* :private false)
|
||||||
(var *no-file* :private true)
|
(var *no-file* :private true)
|
||||||
(var *raw-stdin* :private false)
|
(var *raw-stdin* :private false)
|
||||||
(var *handleopts* :private true)
|
(var *handleopts* :private true)
|
||||||
(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,17 +30,16 @@
|
|||||||
"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))
|
||||||
(if h (h i) (print "unknown flag -" n)))
|
(if h (h i) (print "unknown flag -" n)))
|
||||||
|
|
||||||
# Process arguments
|
# Process arguments
|
||||||
(var i 1)
|
(var i 1)
|
||||||
(def lenargs (length args))
|
(def lenargs (length args))
|
||||||
(while (< i lenargs)
|
(while (< i lenargs)
|
||||||
(def arg (get args i))
|
(def arg (get args i))
|
||||||
(if (and *handleopts* (= "-" (string.slice arg 0 1)))
|
(if (and *handleopts* (= "-" (string.slice arg 0 1)))
|
||||||
(+= i (dohandler (string.slice arg 1 2) i))
|
(+= i (dohandler (string.slice arg 1 2) i))
|
||||||
@ -48,7 +48,7 @@
|
|||||||
(import* _env arg :exit *exit-on-error*)
|
(import* _env arg :exit *exit-on-error*)
|
||||||
(++ i))))
|
(++ i))))
|
||||||
|
|
||||||
(when (or *should-repl* *no-file*)
|
(when (or *should-repl* *no-file*)
|
||||||
(if *raw-stdin*
|
(if *raw-stdin*
|
||||||
(repl nil identity)
|
(repl nil identity)
|
||||||
(do
|
(do
|
||||||
@ -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)))))))
|
||||||
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user