mirror of
https://github.com/janet-lang/janet
synced 2024-12-26 08:20:27 +00:00
Rename the := special form to set so it does not look like a keyword.
This commit is contained in:
parent
f7a25ecae3
commit
03dbd79165
@ -83,7 +83,7 @@ return a real number (never an integer!)
|
|||||||
Janet supports several varieties of types that can be used as labels for things in
|
Janet supports several varieties of types that can be used as labels for things in
|
||||||
your program. The most useful type for this purpose is the keyword type. A keyword
|
your program. The most useful type for this purpose is the keyword type. A keyword
|
||||||
begins with a semicolon, and then contains 0 or more alphanumeric or a few other common
|
begins with a semicolon, and then contains 0 or more alphanumeric or a few other common
|
||||||
characters. For example, `:hello`, `:my-name`, `:=`, and `:ABC123_-*&^%$` are all keywords.
|
characters. For example, `:hello`, `:my-name`, `::`, and `:ABC123_-*&^%$` are all keywords.
|
||||||
Keywords are actually just special cases of symbols, which are similar but don't start with
|
Keywords are actually just special cases of symbols, which are similar but don't start with
|
||||||
a semicolon. The difference between symbols and keywords is that keywords evaluate to themselves, while
|
a semicolon. The difference between symbols and keywords is that keywords evaluate to themselves, while
|
||||||
symbols evaluate to whatever they are bound to. To have a symbol evaluate to itself, it must be
|
symbols evaluate to whatever they are bound to. To have a symbol evaluate to itself, it must be
|
||||||
@ -247,13 +247,13 @@ symbols will raise an error.
|
|||||||
|
|
||||||
Bindings created with def have lexical scoping. Also, bindings created with def are immutable; they
|
Bindings created with def have lexical scoping. Also, bindings created with def are immutable; they
|
||||||
cannot be changed after definition. For mutable bindings, like variables in other programming
|
cannot be changed after definition. For mutable bindings, like variables in other programming
|
||||||
languages, use the `var` keyword. The assignment special form `:=` can then be used to update
|
languages, use the `var` keyword. The assignment special form `set` can then be used to update
|
||||||
a var.
|
a var.
|
||||||
|
|
||||||
```
|
```
|
||||||
(var myvar 1)
|
(var myvar 1)
|
||||||
(print myvar)
|
(print myvar)
|
||||||
(:= myvar 10)
|
(set myvar 10)
|
||||||
(print myvar)
|
(print myvar)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -18,8 +18,8 @@
|
|||||||
(if ,loaded
|
(if ,loaded
|
||||||
,state
|
,state
|
||||||
(do
|
(do
|
||||||
(:= ,loaded true)
|
(set ,loaded true)
|
||||||
(:= ,state (do ;forms)))))))
|
(set ,state (do ;forms)))))))
|
||||||
|
|
||||||
# Use tuples instead of structs to save memory
|
# Use tuples instead of structs to save memory
|
||||||
(def- HEAD 0)
|
(def- HEAD 0)
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
"Draw cells in the game of life from (x1, y1) to (x2, y2)"
|
"Draw cells in the game of life from (x1, y1) to (x2, y2)"
|
||||||
[state x1 y1 x2 y2]
|
[state x1 y1 x2 y2]
|
||||||
(def cellset @{})
|
(def cellset @{})
|
||||||
(each cell state (:= cellset@cell true))
|
(each cell state (set cellset@cell true))
|
||||||
(loop [x :range [x1 (+ 1 x2)]
|
(loop [x :range [x1 (+ 1 x2)]
|
||||||
:after (print)
|
:after (print)
|
||||||
y :range [y1 (+ 1 y2)]]
|
y :range [y1 (+ 1 y2)]]
|
||||||
@ -40,4 +40,4 @@
|
|||||||
(for i 0 20
|
(for i 0 20
|
||||||
(print "generation " i)
|
(print "generation " i)
|
||||||
(draw *state* -7 -7 7 7)
|
(draw *state* -7 -7 7 7)
|
||||||
(:= *state* (tick *state*)))
|
(set *state* (tick *state*)))
|
||||||
|
@ -9,6 +9,6 @@
|
|||||||
(def len (length list))
|
(def len (length list))
|
||||||
(for j 0 len
|
(for j 0 len
|
||||||
(def trial (get list j))
|
(def trial (get list j))
|
||||||
(if (zero? (% i trial)) (:= isprime? false)))
|
(if (zero? (% i trial)) (set isprime? false)))
|
||||||
(if isprime? (array/push list i)))
|
(if isprime? (array/push list i)))
|
||||||
list)
|
list)
|
||||||
|
@ -205,53 +205,53 @@
|
|||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>punctuation.other.janet</string>
|
<string>punctuation.other.janet</string>
|
||||||
</dict>
|
</dict>
|
||||||
<!-- string>(?<![\.:\w_\-=!@\$%^&?|\\/<>]) token match here (?![\.:\w_\-=!@\$%^&?|\\/<>])</string -->
|
<!-- string>(?<![\.:\w_\-=!@\$%^&?|\\/<>*]) token match here (?![\.:\w_\-=!@\$%^&?|\\/<>*])</string -->
|
||||||
<key>literal</key>
|
<key>literal</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>])(true|false|nil)(?![\.:\w_\-=!@\$%^&?|\\/<>])</string>
|
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>*])(true|false|nil)(?![\.:\w_\-=!@\$%^&?|\\/<>*])</string>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>constant.language.janet</string>
|
<string>constant.language.janet</string>
|
||||||
</dict>
|
</dict>
|
||||||
<key>corelib</key>
|
<key>corelib</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>])(def|do|fn|if|quasiquote|quote|splice|unquote|while|%|%=|&|&=|\*|\*=|\*doc\-width\*|\*env\*|\+|\+\+|\+=|\-|\-\-|\-=|\->|\->>|/|/=|<|<<|<<=|<=|=|==|>|>=|>>|>>=|>>>|>>>=|\^|\^=|_env|abstract\?|all|all\-symbols|and|apply|array|array/concat|array/ensure|array/insert|array/new|array/peek|array/pop|array/push|array/slice|array\?|asm|bnot|boolean\?|buffer|buffer/clear|buffer/new|buffer/popn|buffer/push\-byte|buffer/push\-integer|buffer/push\-string|buffer/slice|buffer\?|bytes\?|callable\?|case|cfunction\?|comment|comp|compile|complement|cond|coro|count|debug|debug/arg\-stack|debug/break|debug/fbreak|debug/lineage|debug/stack|debug/unbreak|debug/unfbreak|dec|deep\-not=|deep=|def\-|default|defglobal|defmacro|defmacro\-|defn|defn\-|describe|dictionary\?|disasm|distinct|doc|doc\*|doc\-format|drop\-until|drop\-while|each|empty\?|env\-lookup|error|eval|eval\-string|even\?|every\?|extreme|false\?|fiber/current|fiber/maxstack|fiber/new|fiber/setmaxstack|fiber/status|fiber\?|file/close|file/flush|file/open|file/popen|file/read|file/seek|file/write|filter|find|find\-index|first|flatten|flatten\-into|for|frequencies|function\?|gccollect|gcinterval|gcsetinterval|generate|gensym|get|getline|hash|idempotent\?|identity|if\-let|if\-not|import|import\*|inc|indexed\?|int|integer\?|interleave|interpose|invert|janet/build|janet/version|juxt|juxt\*|keep|keys|keyword\?|kvs|last|length|let|loop|macex|macex1|make\-env|map|mapcat|marshal|match|match\-1|math/acos|math/asin|math/atan|math/ceil|math/cos|math/e|math/exp|math/floor|math/inf|math/log|math/log10|math/pi|math/pow|math/random|math/seedrandom|math/sin|math/sqrt|math/tan|max|max\-order|merge|merge\-into|min|min\-order|module/find|module/native\-paths|module/paths|native|neg\?|next|nil\?|not|not=|not==|number\?|odd\?|one\?|or|order<|order<=|order>|order>=|os/clock|os/cwd|os/execute|os/exit|os/getenv|os/setenv|os/shell|os/sleep|os/time|os/which|pairs|parser/byte|parser/consume|parser/error|parser/flush|parser/new|parser/produce|parser/state|parser/status|parser/where|partial|pos\?|print|process/args|product|put|range|real|real\?|reduce|repl|require|resume|reverse|run\-context|scan\-integer|scan\-number|scan\-real|sentinel|seq|some|sort|sorted|status\-pp|stderr|stdin|stdout|string|string/ascii\-lower|string/ascii\-upper|string/bytes|string/check\-set|string/find|string/find\-all|string/from\-bytes|string/join|string/number|string/pretty|string/repeat|string/replace|string/replace\-all|string/reverse|string/slice|string/split|string\?|struct|struct\?|sum|symbol|symbol\?|table|table/getproto|table/new|table/rawget|table/setproto|table/to\-struct|table\?|take\-until|take\-while|true\?|tuple|tuple/append|tuple/prepend|tuple/slice|tuple\?|type|unless|unmarshal|update|values|varglobal|when|when\-let|with\-idemp|yield|zero\?|zipcoll|\||\|=)(?![\.:\w_\-=!@\$%^&?|\\/<>])</string>
|
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>*])(def|do|fn|if|quasiquote|quote|set|splice|unquote|var|while|%|%=|&|&=|\*|\*=|\*doc\-width\*|\*env\*|\+|\+\+|\+=|\-|\-\-|\-=|\->|\->>|/|/=|<|<<|<<=|<=|=|==|>|>=|>>|>>=|>>>|>>>=|\^|\^=|_env|abstract\?|all|all\-symbols|and|apply|array|array/concat|array/ensure|array/insert|array/new|array/peek|array/pop|array/push|array/slice|array\?|asm|bnot|boolean\?|buffer|buffer/clear|buffer/new|buffer/popn|buffer/push\-byte|buffer/push\-integer|buffer/push\-string|buffer/slice|buffer\?|bytes\?|callable\?|case|cfunction\?|comment|comp|compile|complement|cond|coro|count|debug|debug/arg\-stack|debug/break|debug/fbreak|debug/lineage|debug/stack|debug/unbreak|debug/unfbreak|dec|deep\-not=|deep=|def\-|default|defglobal|defmacro|defmacro\-|defn|defn\-|describe|dictionary\?|disasm|distinct|doc|doc\*|doc\-format|drop\-until|drop\-while|each|empty\?|env\-lookup|error|eval|eval\-string|even\?|every\?|extreme|false\?|fiber/current|fiber/maxstack|fiber/new|fiber/setmaxstack|fiber/status|fiber\?|file/close|file/flush|file/open|file/popen|file/read|file/seek|file/write|filter|find|find\-index|first|flatten|flatten\-into|for|frequencies|function\?|gccollect|gcinterval|gcsetinterval|generate|gensym|get|getline|hash|idempotent\?|identity|if\-let|if\-not|import|import\*|inc|indexed\?|int|integer\?|interleave|interpose|invert|janet/build|janet/version|juxt|juxt\*|keep|keys|keyword\?|kvs|last|length|let|loop|macex|macex1|make\-env|map|mapcat|marshal|match|match\-1|math/acos|math/asin|math/atan|math/ceil|math/cos|math/e|math/exp|math/floor|math/inf|math/log|math/log10|math/pi|math/pow|math/random|math/seedrandom|math/sin|math/sqrt|math/tan|max|max\-order|merge|merge\-into|min|min\-order|module/find|module/native\-paths|module/paths|native|neg\?|next|nil\?|not|not=|not==|number\?|odd\?|one\?|or|order<|order<=|order>|order>=|os/clock|os/cwd|os/execute|os/exit|os/getenv|os/setenv|os/shell|os/sleep|os/time|os/which|pairs|parser/byte|parser/consume|parser/error|parser/flush|parser/new|parser/produce|parser/state|parser/status|parser/where|partial|pos\?|print|process/args|product|put|range|real|real\?|reduce|repl|require|resume|reverse|run\-context|scan\-integer|scan\-number|scan\-real|sentinel|seq|some|sort|sorted|status\-pp|stderr|stdin|stdout|string|string/ascii\-lower|string/ascii\-upper|string/bytes|string/check\-set|string/find|string/find\-all|string/from\-bytes|string/join|string/number|string/pretty|string/repeat|string/replace|string/replace\-all|string/reverse|string/slice|string/split|string\?|struct|struct\?|sum|symbol|symbol\?|table|table/getproto|table/new|table/rawget|table/setproto|table/to\-struct|table\?|take\-until|take\-while|true\?|tuple|tuple/append|tuple/prepend|tuple/slice|tuple\?|type|unless|unmarshal|update|values|varglobal|when|when\-let|with\-idemp|yield|zero\?|zipcoll|\||\|=)(?![\.:\w_\-=!@\$%^&?|\\/<>*])</string>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>keyword.control.janet</string>
|
<string>keyword.control.janet</string>
|
||||||
</dict>
|
</dict>
|
||||||
<key>keysym</key>
|
<key>keysym</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>]):[\.:\w_\-=!@\$%^&?|\\/<>]*</string>
|
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>*]):[\.:\w_\-=!@\$%^&?|\\/<>*]*</string>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>constant.keyword.janet</string>
|
<string>constant.keyword.janet</string>
|
||||||
</dict>
|
</dict>
|
||||||
<key>symbol</key>
|
<key>symbol</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>])[\.a-zA-Z_\-=!@\$%^&?|\\/<>][\.:\w_\-=!@\$%^&?|\\/<>]*</string>
|
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>*])[\.a-zA-Z_\-=!@\$%^&?|\\/<>*][\.:\w_\-=!@\$%^&?|\\/<>*]*</string>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>variable.other.janet</string>
|
<string>variable.other.janet</string>
|
||||||
</dict>
|
</dict>
|
||||||
<key>hex-number</key>
|
<key>hex-number</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>])[-+]?0x([_\da-fA-F]+|[_\da-fA-F]+\.[_\da-fA-F]*|\.[_\da-fA-F]+)(&[+-]?[\da-fA-F]+)?(?![\.:\w_\-=!@\$%^&?|\\/<>])</string>
|
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>*])[-+]?0x([_\da-fA-F]+|[_\da-fA-F]+\.[_\da-fA-F]*|\.[_\da-fA-F]+)(&[+-]?[\da-fA-F]+)?(?![\.:\w_\-=!@\$%^&?|\\/<>*])</string>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>constant.numeric.hex.janet</string>
|
<string>constant.numeric.hex.janet</string>
|
||||||
</dict>
|
</dict>
|
||||||
<key>dec-number</key>
|
<key>dec-number</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>])[-+]?([_\d]+|[_\d]+\.[_\d]*|\.[_\d]+)([eE&][+-]?[\d]+)?(?![\.:\w_\-=!@\$%^&?|\\/<>])</string>
|
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>*])[-+]?([_\d]+|[_\d]+\.[_\d]*|\.[_\d]+)([eE&][+-]?[\d]+)?(?![\.:\w_\-=!@\$%^&?|\\/<>*])</string>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>constant.numeric.decimal.janet</string>
|
<string>constant.numeric.decimal.janet</string>
|
||||||
</dict>
|
</dict>
|
||||||
<key>r-number</key>
|
<key>r-number</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>])[-+]?\d\d?r([_\w]+|[_\w]+\.[_\w]*|\.[_\w]+)(&[+-]?[\w]+)?(?![\.:\w_\-=!@\$%^&?|\\/<>])</string>
|
<string>(?<![\.:\w_\-=!@\$%^&?|\\/<>*])[-+]?\d\d?r([_\w]+|[_\w]+\.[_\w]*|\.[_\w]+)(&[+-]?[\w]+)?(?![\.:\w_\-=!@\$%^&?|\\/<>*])</string>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>constant.numeric.decimal.janet</string>
|
<string>constant.numeric.decimal.janet</string>
|
||||||
</dict>
|
</dict>
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
i
|
i
|
||||||
(do
|
(do
|
||||||
(if (= t :string)
|
(if (= t :string)
|
||||||
(:= docstr ith)
|
(set docstr ith)
|
||||||
(array/push modifiers ith))
|
(array/push modifiers ith))
|
||||||
(if (< i len) (recur (+ i 1)))))))
|
(if (< i len) (recur (+ i 1)))))))
|
||||||
(def start (fstart 0))
|
(def start (fstart 0))
|
||||||
@ -37,7 +37,7 @@
|
|||||||
(while (< index arglen)
|
(while (< index arglen)
|
||||||
(buffer/push-string buf " ")
|
(buffer/push-string buf " ")
|
||||||
(string/pretty args.index 4 buf)
|
(string/pretty args.index 4 buf)
|
||||||
(:= index (+ index 1)))
|
(set index (+ index 1)))
|
||||||
(array/push modifiers (string buf ")\n\n" docstr))
|
(array/push modifiers (string buf ")\n\n" docstr))
|
||||||
# Build return value
|
# Build return value
|
||||||
~(def ,name ,;modifiers (fn ,name ,;(tuple/slice more start)))))
|
~(def ,name ,;modifiers (fn ,name ,;(tuple/slice more start)))))
|
||||||
@ -149,19 +149,19 @@
|
|||||||
# C style macros and functions for imperative sugar
|
# C style macros and functions for imperative sugar
|
||||||
(defn inc "Returns x + 1." [x] (+ x 1))
|
(defn inc "Returns x + 1." [x] (+ x 1))
|
||||||
(defn dec "Returns x - 1." [x] (- x 1))
|
(defn dec "Returns x - 1." [x] (- x 1))
|
||||||
(defmacro ++ "Increments the var x by 1." [x] ~(:= ,x (,+ ,x ,1)))
|
(defmacro ++ "Increments the var x by 1." [x] ~(set ,x (,+ ,x ,1)))
|
||||||
(defmacro -- "Decrements the var x by 1." [x] ~(:= ,x (,- ,x ,1)))
|
(defmacro -- "Decrements the var x by 1." [x] ~(set ,x (,- ,x ,1)))
|
||||||
(defmacro += "Increments the var x by n." [x n] ~(:= ,x (,+ ,x ,n)))
|
(defmacro += "Increments the var x by n." [x n] ~(set ,x (,+ ,x ,n)))
|
||||||
(defmacro -= "Decrements the vat x by n." [x n] ~(:= ,x (,- ,x ,n)))
|
(defmacro -= "Decrements the vat x by n." [x n] ~(set ,x (,- ,x ,n)))
|
||||||
(defmacro *= "Shorthand for (:= x (* x n))." [x n] ~(:= ,x (,* ,x ,n)))
|
(defmacro *= "Shorthand for (set x (* x n))." [x n] ~(set ,x (,* ,x ,n)))
|
||||||
(defmacro /= "Shorthand for (:= x (/ x n))." [x n] ~(:= ,x (,/ ,x ,n)))
|
(defmacro /= "Shorthand for (set x (/ x n))." [x n] ~(set ,x (,/ ,x ,n)))
|
||||||
(defmacro %= "Shorthand for (:= x (% x n))." [x n] ~(:= ,x (,% ,x ,n)))
|
(defmacro %= "Shorthand for (set x (% x n))." [x n] ~(set ,x (,% ,x ,n)))
|
||||||
(defmacro &= "Shorthand for (:= x (& x n))." [x n] ~(:= ,x (,& ,x ,n)))
|
(defmacro &= "Shorthand for (set x (& x n))." [x n] ~(set ,x (,& ,x ,n)))
|
||||||
(defmacro |= "Shorthand for (:= x (| x n))." [x n] ~(:= ,x (,| ,x ,n)))
|
(defmacro |= "Shorthand for (set x (| x n))." [x n] ~(set ,x (,| ,x ,n)))
|
||||||
(defmacro ^= "Shorthand for (:= x (^ x n))." [x n] ~(:= ,x (,^ ,x ,n)))
|
(defmacro ^= "Shorthand for (set x (^ x n))." [x n] ~(set ,x (,^ ,x ,n)))
|
||||||
(defmacro >>= "Shorthand for (:= x (>> x n))." [x n] ~(:= ,x (,>> ,x ,n)))
|
(defmacro >>= "Shorthand for (set x (>> x n))." [x n] ~(set ,x (,>> ,x ,n)))
|
||||||
(defmacro <<= "Shorthand for (:= x (<< x n))." [x n] ~(:= ,x (,<< ,x ,n)))
|
(defmacro <<= "Shorthand for (set x (<< x n))." [x n] ~(set ,x (,<< ,x ,n)))
|
||||||
(defmacro >>>= "Shorthand for (:= x (>>> x n))." [x n] ~(:= ,x (,>>> ,x ,n)))
|
(defmacro >>>= "Shorthand for (set x (>>> x n))." [x n] ~(set ,x (,>>> ,x ,n)))
|
||||||
|
|
||||||
(defmacro default
|
(defmacro default
|
||||||
"Define a default value for an optional argument.
|
"Define a default value for an optional argument.
|
||||||
@ -248,7 +248,7 @@
|
|||||||
(var i len)
|
(var i len)
|
||||||
(while (> i 0)
|
(while (> i 0)
|
||||||
(-- i)
|
(-- i)
|
||||||
(:= ret (if (= ret true)
|
(set ret (if (= ret true)
|
||||||
forms.i
|
forms.i
|
||||||
(tuple 'if forms.i ret))))
|
(tuple 'if forms.i ret))))
|
||||||
ret)
|
ret)
|
||||||
@ -263,7 +263,7 @@
|
|||||||
(while (> i 0)
|
(while (> i 0)
|
||||||
(-- i)
|
(-- i)
|
||||||
(def fi forms.i)
|
(def fi forms.i)
|
||||||
(:= ret (if (idempotent? fi)
|
(set ret (if (idempotent? fi)
|
||||||
(tuple 'if fi fi ret)
|
(tuple 'if fi fi ret)
|
||||||
(do
|
(do
|
||||||
(def $fi (gensym))
|
(def $fi (gensym))
|
||||||
@ -331,13 +331,13 @@
|
|||||||
(tuple 'var $iter 0)
|
(tuple 'var $iter 0)
|
||||||
(tuple 'while
|
(tuple 'while
|
||||||
(tuple/slice spreds)
|
(tuple/slice spreds)
|
||||||
(tuple := $iter (tuple + 1 $iter))
|
(tuple 'set $iter (tuple + 1 $iter))
|
||||||
sub)))
|
sub)))
|
||||||
(error (string "unexpected loop predicate: " bindings)))
|
(error (string "unexpected loop predicate: " bindings)))
|
||||||
(case verb
|
(case verb
|
||||||
:iterate (do
|
:iterate (do
|
||||||
(def $iter (gensym))
|
(def $iter (gensym))
|
||||||
(def preds @['and (tuple ':= $iter object)])
|
(def preds @['and (tuple 'set $iter object)])
|
||||||
(def subloop (doone (+ i 3) preds))
|
(def subloop (doone (+ i 3) preds))
|
||||||
(tuple 'do
|
(tuple 'do
|
||||||
(tuple 'var $iter nil)
|
(tuple 'var $iter nil)
|
||||||
@ -357,7 +357,7 @@
|
|||||||
(tuple 'while (tuple/slice preds)
|
(tuple 'while (tuple/slice preds)
|
||||||
(tuple 'def bindings $iter)
|
(tuple 'def bindings $iter)
|
||||||
subloop
|
subloop
|
||||||
(tuple ':= $iter (tuple + $iter inc)))))
|
(tuple 'set $iter (tuple + $iter inc)))))
|
||||||
:keys (do
|
:keys (do
|
||||||
(def $dict (gensym))
|
(def $dict (gensym))
|
||||||
(def $iter (gensym))
|
(def $iter (gensym))
|
||||||
@ -369,7 +369,7 @@
|
|||||||
(tuple 'while (tuple/slice preds)
|
(tuple 'while (tuple/slice preds)
|
||||||
(tuple 'def bindings $iter)
|
(tuple 'def bindings $iter)
|
||||||
subloop
|
subloop
|
||||||
(tuple ':= $iter (tuple next $dict $iter)))))
|
(tuple 'set $iter (tuple next $dict $iter)))))
|
||||||
:in (do
|
:in (do
|
||||||
(def $len (gensym))
|
(def $len (gensym))
|
||||||
(def $i (gensym))
|
(def $i (gensym))
|
||||||
@ -383,7 +383,7 @@
|
|||||||
(tuple 'while (tuple/slice preds 0)
|
(tuple 'while (tuple/slice preds 0)
|
||||||
(tuple 'def bindings (tuple get $indexed $i))
|
(tuple 'def bindings (tuple get $indexed $i))
|
||||||
subloop
|
subloop
|
||||||
(tuple ':= $i (tuple + 1 $i)))))
|
(tuple 'set $i (tuple + 1 $i)))))
|
||||||
:generate (do
|
:generate (do
|
||||||
(def $fiber (gensym))
|
(def $fiber (gensym))
|
||||||
(def $yieldval (gensym))
|
(def $yieldval (gensym))
|
||||||
@ -400,7 +400,7 @@
|
|||||||
(tuple 'while (tuple/slice preds 0)
|
(tuple 'while (tuple/slice preds 0)
|
||||||
(tuple 'def bindings $yieldval)
|
(tuple 'def bindings $yieldval)
|
||||||
subloop
|
subloop
|
||||||
(tuple := $yieldval (tuple resume $fiber)))))
|
(tuple 'set $yieldval (tuple resume $fiber)))))
|
||||||
(error (string "unexpected loop verb: " verb)))))))
|
(error (string "unexpected loop verb: " verb)))))))
|
||||||
(doone 0 nil))
|
(doone 0 nil))
|
||||||
|
|
||||||
@ -517,7 +517,7 @@
|
|||||||
(var ret args.0)
|
(var ret args.0)
|
||||||
(loop [i :range [0 len]]
|
(loop [i :range [0 len]]
|
||||||
(def v args.i)
|
(def v args.i)
|
||||||
(if (order v ret) (:= ret v)))
|
(if (order v ret) (set ret v)))
|
||||||
ret))
|
ret))
|
||||||
|
|
||||||
(defn max
|
(defn max
|
||||||
@ -566,11 +566,11 @@
|
|||||||
(def aj a.j)
|
(def aj a.j)
|
||||||
(when (by aj pivot)
|
(when (by aj pivot)
|
||||||
(def ai a.i)
|
(def ai a.i)
|
||||||
(:= a.i aj)
|
(set a.i aj)
|
||||||
(:= a.j ai)
|
(set a.j ai)
|
||||||
(++ i)))
|
(++ i)))
|
||||||
(:= a.hi a.i)
|
(set a.hi a.i)
|
||||||
(:= a.i pivot)
|
(set a.i pivot)
|
||||||
i)
|
i)
|
||||||
|
|
||||||
(defn sort-help
|
(defn sort-help
|
||||||
@ -595,7 +595,7 @@
|
|||||||
[f init ind]
|
[f init ind]
|
||||||
(var res init)
|
(var res init)
|
||||||
(loop [x :in ind]
|
(loop [x :in ind]
|
||||||
(:= res (f res x)))
|
(set res (f res x)))
|
||||||
res)
|
res)
|
||||||
|
|
||||||
(defn map
|
(defn map
|
||||||
@ -607,18 +607,18 @@
|
|||||||
(var limit (length inds.0))
|
(var limit (length 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) (:= limit l)))
|
(if (< l limit) (set limit l)))
|
||||||
(def [i1 i2 i3 i4] inds)
|
(def [i1 i2 i3 i4] inds)
|
||||||
(def res (array/new limit))
|
(def res (array/new limit))
|
||||||
(case ninds
|
(case ninds
|
||||||
1 (loop [i :range [0 limit]] (:= res.i (f i1.i)))
|
1 (loop [i :range [0 limit]] (set res.i (f i1.i)))
|
||||||
2 (loop [i :range [0 limit]] (:= res.i (f i1.i i2.i)))
|
2 (loop [i :range [0 limit]] (set res.i (f i1.i i2.i)))
|
||||||
3 (loop [i :range [0 limit]] (:= res.i (f i1.i i2.i i3.i)))
|
3 (loop [i :range [0 limit]] (set res.i (f i1.i i2.i i3.i)))
|
||||||
4 (loop [i :range [0 limit]] (:= res.i (f i1.i i2.i i3.i i4.i)))
|
4 (loop [i :range [0 limit]] (set res.i (f i1.i i2.i i3.i i4.i)))
|
||||||
(loop [i :range [0 limit]]
|
(loop [i :range [0 limit]]
|
||||||
(def args (array/new ninds))
|
(def args (array/new ninds))
|
||||||
(loop [j :range [0 ninds]] (:= args.j inds.j.i))
|
(loop [j :range [0 ninds]] (set args.j inds.j.i))
|
||||||
(:= res.i (f ;args))))
|
(set res.i (f ;args))))
|
||||||
res)
|
res)
|
||||||
|
|
||||||
(defn mapcat
|
(defn mapcat
|
||||||
@ -691,7 +691,7 @@
|
|||||||
(var going true)
|
(var going true)
|
||||||
(while (if (< i len) going)
|
(while (if (< i len) going)
|
||||||
(def item ind.i)
|
(def item ind.i)
|
||||||
(if (pred item) (:= going false) (++ i)))
|
(if (pred item) (set going false) (++ i)))
|
||||||
(if going nil i))
|
(if going nil i))
|
||||||
|
|
||||||
(defn find
|
(defn find
|
||||||
@ -784,7 +784,7 @@
|
|||||||
[ind]
|
[ind]
|
||||||
(var res true)
|
(var res true)
|
||||||
(loop [x :in ind :while res]
|
(loop [x :in ind :while res]
|
||||||
(if x nil (:= res x)))
|
(if x nil (set res x)))
|
||||||
res)
|
res)
|
||||||
|
|
||||||
(defn reverse
|
(defn reverse
|
||||||
@ -825,7 +825,7 @@ value, one key will be ignored."
|
|||||||
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 coll.a-key)
|
(def old-value coll.a-key)
|
||||||
(:= coll.a-key (a-function old-value ;args)))
|
(set coll.a-key (a-function old-value ;args)))
|
||||||
|
|
||||||
(defn merge-into
|
(defn merge-into
|
||||||
"Merges multiple tables/structs into a table. If a key appears in more than one
|
"Merges multiple tables/structs into a table. If a key appears in more than one
|
||||||
@ -834,7 +834,7 @@ value, one key will be ignored."
|
|||||||
[tab & colls]
|
[tab & colls]
|
||||||
(loop [c :in colls
|
(loop [c :in colls
|
||||||
key :keys c]
|
key :keys c]
|
||||||
(:= tab.key c.key))
|
(set tab.key c.key))
|
||||||
tab)
|
tab)
|
||||||
|
|
||||||
(defn merge
|
(defn merge
|
||||||
@ -845,7 +845,7 @@ value, one key will be ignored."
|
|||||||
(def container @{})
|
(def container @{})
|
||||||
(loop [c :in colls
|
(loop [c :in colls
|
||||||
key :keys c]
|
key :keys c]
|
||||||
(:= container.key c.key))
|
(set container.key c.key))
|
||||||
container)
|
container)
|
||||||
|
|
||||||
(defn keys
|
(defn keys
|
||||||
@ -855,7 +855,7 @@ value, one key will be ignored."
|
|||||||
(var k (next x nil))
|
(var k (next x nil))
|
||||||
(while (not= nil k)
|
(while (not= nil k)
|
||||||
(array/push arr k)
|
(array/push arr k)
|
||||||
(:= k (next x k)))
|
(set k (next x k)))
|
||||||
arr)
|
arr)
|
||||||
|
|
||||||
(defn values
|
(defn values
|
||||||
@ -865,7 +865,7 @@ value, one key will be ignored."
|
|||||||
(var k (next x nil))
|
(var k (next x nil))
|
||||||
(while (not= nil k)
|
(while (not= nil k)
|
||||||
(array/push arr x.k)
|
(array/push arr x.k)
|
||||||
(:= k (next x k)))
|
(set k (next x k)))
|
||||||
arr)
|
arr)
|
||||||
|
|
||||||
(defn pairs
|
(defn pairs
|
||||||
@ -875,7 +875,7 @@ value, one key will be ignored."
|
|||||||
(var k (next x nil))
|
(var k (next x nil))
|
||||||
(while (not= nil k)
|
(while (not= nil k)
|
||||||
(array/push arr (tuple k x.k))
|
(array/push arr (tuple k x.k))
|
||||||
(:= k (next x k)))
|
(set k (next x k)))
|
||||||
arr)
|
arr)
|
||||||
|
|
||||||
(defn frequencies
|
(defn frequencies
|
||||||
@ -885,7 +885,7 @@ value, one key will be ignored."
|
|||||||
(loop
|
(loop
|
||||||
[x :in ind]
|
[x :in ind]
|
||||||
(def n freqs.x)
|
(def n freqs.x)
|
||||||
(:= freqs.x (if n (+ 1 n) 1)))
|
(set freqs.x (if n (+ 1 n) 1)))
|
||||||
freqs)
|
freqs)
|
||||||
|
|
||||||
(defn interleave
|
(defn interleave
|
||||||
@ -906,7 +906,7 @@ value, one key will be ignored."
|
|||||||
[xs]
|
[xs]
|
||||||
(def ret @[])
|
(def ret @[])
|
||||||
(def seen @{})
|
(def seen @{})
|
||||||
(loop [x :in xs] (if seen.x nil (do (:= seen.x true) (array/push ret x))))
|
(loop [x :in xs] (if seen.x nil (do (set seen.x true) (array/push ret x))))
|
||||||
ret)
|
ret)
|
||||||
|
|
||||||
(defn flatten-into
|
(defn flatten-into
|
||||||
@ -939,7 +939,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) (:= ret.0 ind.0))
|
(if (> len 0) (set ret.0 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)
|
||||||
@ -991,7 +991,7 @@ value, one key will be ignored."
|
|||||||
$dict expr
|
$dict expr
|
||||||
~(if (dictionary? ,$dict)
|
~(if (dictionary? ,$dict)
|
||||||
,((fn aux []
|
,((fn aux []
|
||||||
(:= key (next pattern key))
|
(set key (next pattern key))
|
||||||
(if (= key nil)
|
(if (= key nil)
|
||||||
(onmatch)
|
(onmatch)
|
||||||
(match-1 (get pattern key) (tuple get $dict key) aux seen))))
|
(match-1 (get pattern key) (tuple get $dict key) aux seen))))
|
||||||
@ -1049,7 +1049,7 @@ value, one key will be ignored."
|
|||||||
(def oldcur current)
|
(def oldcur current)
|
||||||
(def spacer
|
(def spacer
|
||||||
(if (<= maxcol (+ current (length word) 1))
|
(if (<= maxcol (+ current (length word) 1))
|
||||||
(do (:= current 0) "\n ")
|
(do (set current 0) "\n ")
|
||||||
(do (++ current) " ")))
|
(do (++ current) " ")))
|
||||||
(+= current (length word))
|
(+= current (length word))
|
||||||
(if (> oldcur 0)
|
(if (> oldcur 0)
|
||||||
@ -1066,7 +1066,7 @@ value, one key will be ignored."
|
|||||||
(if (> (length word) 0) (pushword))
|
(if (> (length word) 0) (pushword))
|
||||||
(when (= b 10)
|
(when (= b 10)
|
||||||
(buffer/push-string buf "\n ")
|
(buffer/push-string buf "\n ")
|
||||||
(:= current 0)))))
|
(set current 0)))))
|
||||||
|
|
||||||
# Last word
|
# Last word
|
||||||
(pushword)
|
(pushword)
|
||||||
@ -1103,7 +1103,7 @@ value, one key will be ignored."
|
|||||||
(var key (next t nil))
|
(var key (next t nil))
|
||||||
(while (not= nil key)
|
(while (not= nil key)
|
||||||
(put newt (macex1 key) (on-value t.key))
|
(put newt (macex1 key) (on-value t.key))
|
||||||
(:= key (next t key)))
|
(set key (next t key)))
|
||||||
newt)
|
newt)
|
||||||
|
|
||||||
(defn expand-bindings [x]
|
(defn expand-bindings [x]
|
||||||
@ -1151,7 +1151,7 @@ value, one key will be ignored."
|
|||||||
(tuple t.0 (qq t.1)))
|
(tuple t.0 (qq t.1)))
|
||||||
|
|
||||||
(def specs
|
(def specs
|
||||||
{':= expanddef
|
{'set expanddef
|
||||||
'def expanddef
|
'def expanddef
|
||||||
'do expandall
|
'do expandall
|
||||||
'fn expandfn
|
'fn expandfn
|
||||||
@ -1185,14 +1185,14 @@ value, one key will be ignored."
|
|||||||
[pred xs]
|
[pred xs]
|
||||||
"Returns true if all xs are truthy, otherwise the first false or nil value."
|
"Returns true if all xs are truthy, otherwise the first false or nil value."
|
||||||
(var ret true)
|
(var ret true)
|
||||||
(loop [x :in xs :while ret] (:= ret (pred x)))
|
(loop [x :in xs :while ret] (set ret (pred x)))
|
||||||
ret)
|
ret)
|
||||||
|
|
||||||
(defn some
|
(defn some
|
||||||
[pred xs]
|
[pred xs]
|
||||||
"Returns false if all xs are false or nil, otherwise returns the first true value."
|
"Returns false if all xs are false or nil, otherwise returns the first true value."
|
||||||
(var ret nil)
|
(var ret nil)
|
||||||
(loop [x :in xs :while (not ret)] (if-let [y (pred x)] (:= ret y)))
|
(loop [x :in xs :while (not ret)] (if-let [y (pred x)] (set ret y)))
|
||||||
ret)
|
ret)
|
||||||
|
|
||||||
(defn deep-not=
|
(defn deep-not=
|
||||||
@ -1225,8 +1225,8 @@ value, one key will be ignored."
|
|||||||
(while (deep-not= current previous)
|
(while (deep-not= current previous)
|
||||||
(if (> (++ counter) 200)
|
(if (> (++ counter) 200)
|
||||||
(error "macro expansion too nested"))
|
(error "macro expansion too nested"))
|
||||||
(:= previous current)
|
(set previous current)
|
||||||
(:= current (macex1 current)))
|
(set current (macex1 current)))
|
||||||
current)
|
current)
|
||||||
|
|
||||||
###
|
###
|
||||||
@ -1274,7 +1274,7 @@ value, one key will be ignored."
|
|||||||
(if (= (type res) :function)
|
(if (= (type res) :function)
|
||||||
(res)
|
(res)
|
||||||
(do
|
(do
|
||||||
(:= good false)
|
(set good false)
|
||||||
(def {:error err :start start :end end :fiber errf} res)
|
(def {:error err :start start :end end :fiber errf} res)
|
||||||
(onstatus
|
(onstatus
|
||||||
:compile
|
:compile
|
||||||
@ -1289,7 +1289,7 @@ value, one key will be ignored."
|
|||||||
(if going (onstatus (fiber/status f) res f where))))
|
(if going (onstatus (fiber/status f) res f where))))
|
||||||
|
|
||||||
(def oldenv *env*)
|
(def oldenv *env*)
|
||||||
(:= *env* env)
|
(set *env* env)
|
||||||
|
|
||||||
# Run loop
|
# Run loop
|
||||||
(def buf @"")
|
(def buf @"")
|
||||||
@ -1299,10 +1299,10 @@ value, one key will be ignored."
|
|||||||
(var pindex 0)
|
(var pindex 0)
|
||||||
(var pstatus nil)
|
(var pstatus nil)
|
||||||
(def len (length buf))
|
(def len (length buf))
|
||||||
(if (= len 0) (:= going false))
|
(if (= len 0) (set going false))
|
||||||
(while (> len pindex)
|
(while (> len pindex)
|
||||||
(+= pindex (parser/consume p buf pindex))
|
(+= pindex (parser/consume p buf pindex))
|
||||||
(while (= (:= pstatus (parser/status p)) :full)
|
(while (= (set pstatus (parser/status p)) :full)
|
||||||
(eval1 (parser/produce p)))
|
(eval1 (parser/produce p)))
|
||||||
(when (= pstatus :error)
|
(when (= pstatus :error)
|
||||||
(onstatus :parse
|
(onstatus :parse
|
||||||
@ -1311,7 +1311,7 @@ value, one key will be ignored."
|
|||||||
nil
|
nil
|
||||||
where))))
|
where))))
|
||||||
|
|
||||||
(:= *env* oldenv)
|
(set *env* oldenv)
|
||||||
|
|
||||||
env)
|
env)
|
||||||
|
|
||||||
@ -1369,7 +1369,7 @@ value, one key will be ignored."
|
|||||||
(var state (string str))
|
(var state (string str))
|
||||||
(defn chunks [buf _]
|
(defn chunks [buf _]
|
||||||
(def ret state)
|
(def ret state)
|
||||||
(:= state nil)
|
(set state nil)
|
||||||
(when ret
|
(when ret
|
||||||
(buffer/push-string buf str)
|
(buffer/push-string buf str)
|
||||||
(buffer/push-string buf "\n")))
|
(buffer/push-string buf "\n")))
|
||||||
@ -1377,7 +1377,7 @@ value, one key will be ignored."
|
|||||||
(run-context *env* chunks
|
(run-context *env* chunks
|
||||||
(fn [sig x f source]
|
(fn [sig x f source]
|
||||||
(if (= sig :dead)
|
(if (= sig :dead)
|
||||||
(:= returnval x)
|
(set returnval x)
|
||||||
(status-pp sig x f source)))
|
(status-pp sig x f source)))
|
||||||
"eval")
|
"eval")
|
||||||
returnval)
|
returnval)
|
||||||
@ -1470,8 +1470,8 @@ value, one key will be ignored."
|
|||||||
check
|
check
|
||||||
(do
|
(do
|
||||||
(def newenv (make-env))
|
(def newenv (make-env))
|
||||||
(:= cache.path newenv)
|
(set cache.path newenv)
|
||||||
(:= loading.path true)
|
(set loading.path true)
|
||||||
(def f (find-mod path))
|
(def f (find-mod path))
|
||||||
(if f
|
(if f
|
||||||
(do
|
(do
|
||||||
@ -1490,7 +1490,7 @@ value, one key will be ignored."
|
|||||||
(if (not n)
|
(if (not n)
|
||||||
(error (string "could not open file for module " path)))
|
(error (string "could not open file for module " path)))
|
||||||
((native n) newenv)))
|
((native n) newenv)))
|
||||||
(:= loading.path false)
|
(set loading.path false)
|
||||||
newenv)))))
|
newenv)))))
|
||||||
|
|
||||||
(defn import*
|
(defn import*
|
||||||
@ -1510,7 +1510,7 @@ value, one key will be ignored."
|
|||||||
(when (not v:private)
|
(when (not v:private)
|
||||||
(def newv (table/setproto @{:private true} v))
|
(def newv (table/setproto @{:private true} v))
|
||||||
(put env (symbol prefix k) newv))
|
(put env (symbol prefix k) newv))
|
||||||
(:= k (next newenv k))))
|
(set k (next newenv k))))
|
||||||
|
|
||||||
(defmacro import
|
(defmacro import
|
||||||
"Import a module. First requires the module, and then merges its
|
"Import a module. First requires the module, and then merges its
|
||||||
@ -1547,9 +1547,9 @@ value, one key will be ignored."
|
|||||||
[env &]
|
[env &]
|
||||||
(default env *env*)
|
(default env *env*)
|
||||||
(def envs @[])
|
(def envs @[])
|
||||||
(do (var e env) (while e (array/push envs e) (:= e (table/getproto e))))
|
(do (var e env) (while e (array/push envs e) (set e (table/getproto e))))
|
||||||
(def symbol-set @{})
|
(def symbol-set @{})
|
||||||
(loop [envi :in envs
|
(loop [envi :in envs
|
||||||
k :keys envi]
|
k :keys envi]
|
||||||
(:= symbol-set.k true))
|
(set symbol-set.k true))
|
||||||
(sort (keys symbol-set)))
|
(sort (keys symbol-set)))
|
||||||
|
@ -648,16 +648,15 @@ error2:
|
|||||||
|
|
||||||
/* Keep in lexicographic order */
|
/* Keep in lexicographic order */
|
||||||
static const JanetSpecial janetc_specials[] = {
|
static const JanetSpecial janetc_specials[] = {
|
||||||
{":=", janetc_varset},
|
|
||||||
{"def", janetc_def},
|
{"def", janetc_def},
|
||||||
{"do", janetc_do},
|
{"do", janetc_do},
|
||||||
{"fn", janetc_fn},
|
{"fn", janetc_fn},
|
||||||
{"if", janetc_if},
|
{"if", janetc_if},
|
||||||
{"quasiquote", janetc_quasiquote},
|
{"quasiquote", janetc_quasiquote},
|
||||||
{"quote", janetc_quote},
|
{"quote", janetc_quote},
|
||||||
|
{"set", janetc_varset},
|
||||||
{"splice", janetc_splice},
|
{"splice", janetc_splice},
|
||||||
{"unquote", janetc_unquote},
|
{"unquote", janetc_unquote},
|
||||||
{"unquote", janetc_unquote},
|
|
||||||
{"var", janetc_var},
|
{"var", janetc_var},
|
||||||
{"while", janetc_while}
|
{"while", janetc_while}
|
||||||
};
|
};
|
||||||
|
@ -24,12 +24,12 @@
|
|||||||
(os/exit 0)
|
(os/exit 0)
|
||||||
1)
|
1)
|
||||||
"v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1)
|
"v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1)
|
||||||
"s" (fn [&] (:= *raw-stdin* true) (:= *should-repl* true) 1)
|
"s" (fn [&] (set *raw-stdin* true) (set *should-repl* true) 1)
|
||||||
"r" (fn [&] (:= *should-repl* true) 1)
|
"r" (fn [&] (set *should-repl* true) 1)
|
||||||
"p" (fn [&] (:= *exit-on-error* false) 1)
|
"p" (fn [&] (set *exit-on-error* false) 1)
|
||||||
"-" (fn [&] (:= *handleopts* false) 1)
|
"-" (fn [&] (set *handleopts* false) 1)
|
||||||
"e" (fn [i &]
|
"e" (fn [i &]
|
||||||
(:= *no-file* false)
|
(set *no-file* false)
|
||||||
(eval (get process/args (+ i 1)))
|
(eval (get process/args (+ i 1)))
|
||||||
2)})
|
2)})
|
||||||
|
|
||||||
@ -45,7 +45,7 @@
|
|||||||
(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))
|
||||||
(do
|
(do
|
||||||
(:= *no-file* false)
|
(set *no-file* false)
|
||||||
(import* _env arg :prefix "" :exit *exit-on-error*)
|
(import* _env arg :prefix "" :exit *exit-on-error*)
|
||||||
(++ i))))
|
(++ i))))
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
x)
|
x)
|
||||||
|
|
||||||
(defn start-suite [x]
|
(defn start-suite [x]
|
||||||
(:= suite-num x)
|
(set suite-num x)
|
||||||
(print "\nRunning test suite " x " tests...\n"))
|
(print "\nRunning test suite " x " tests...\n"))
|
||||||
|
|
||||||
(defn end-suite []
|
(defn end-suite []
|
||||||
|
@ -80,7 +80,7 @@
|
|||||||
|
|
||||||
# Mcarthy's 91 function
|
# Mcarthy's 91 function
|
||||||
(var f91 nil)
|
(var f91 nil)
|
||||||
(:= f91 (fn [n] (if (> n 100) (- n 10) (f91 (f91 (+ n 11))))))
|
(set f91 (fn [n] (if (> n 100) (- n 10) (f91 (f91 (+ n 11))))))
|
||||||
(assert (= 91 (f91 10)) "f91(10) = 91")
|
(assert (= 91 (f91 10)) "f91(10) = 91")
|
||||||
(assert (= 91 (f91 11)) "f91(11) = 91")
|
(assert (= 91 (f91 11)) "f91(11) = 91")
|
||||||
(assert (= 91 (f91 20)) "f91(20) = 91")
|
(assert (= 91 (f91 20)) "f91(20) = 91")
|
||||||
@ -92,7 +92,7 @@
|
|||||||
(assert (= 94 (f91 104)) "f91(104) = 94")
|
(assert (= 94 (f91 104)) "f91(104) = 94")
|
||||||
|
|
||||||
# Fibonacci
|
# Fibonacci
|
||||||
(def fib (do (var fib nil) (:= fib (fn [n] (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))))))
|
(def fib (do (var fib nil) (set fib (fn [n] (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))))))
|
||||||
(def fib2 (fn fib2 [n] (if (< n 2) n (+ (fib2 (- n 1)) (fib2 (- n 2))))))
|
(def fib2 (fn fib2 [n] (if (< n 2) n (+ (fib2 (- n 1)) (fib2 (- n 2))))))
|
||||||
|
|
||||||
(assert (= (fib 0) (fib2 0) 0) "fib(0)")
|
(assert (= (fib 0) (fib2 0) 0) "fib(0)")
|
||||||
@ -127,15 +127,15 @@
|
|||||||
(var accum 1)
|
(var accum 1)
|
||||||
(var count 0)
|
(var count 0)
|
||||||
(while (< count 16)
|
(while (< count 16)
|
||||||
(:= accum (<< accum 1))
|
(set accum (<< accum 1))
|
||||||
(:= count (+ 1 count)))
|
(set count (+ 1 count)))
|
||||||
(assert (= accum 65536) "loop in closure")))
|
(assert (= accum 65536) "loop in closure")))
|
||||||
|
|
||||||
(var accum 1)
|
(var accum 1)
|
||||||
(var count 0)
|
(var count 0)
|
||||||
(while (< count 16)
|
(while (< count 16)
|
||||||
(:= accum (<< accum 1))
|
(set accum (<< accum 1))
|
||||||
(:= count (+ 1 count)))
|
(set count (+ 1 count)))
|
||||||
(assert (= accum 65536) "loop globally")
|
(assert (= accum 65536) "loop globally")
|
||||||
|
|
||||||
(assert (= (struct 1 2 3 4 5 6 7 8) (struct 7 8 5 6 3 4 1 2)) "struct order does not matter 1")
|
(assert (= (struct 1 2 3 4 5 6 7 8) (struct 7 8 5 6 3 4 1 2)) "struct order does not matter 1")
|
||||||
@ -228,18 +228,18 @@
|
|||||||
(def xi (get xs i))
|
(def xi (get xs i))
|
||||||
(def yj (get ys j))
|
(def yj (get ys j))
|
||||||
(if (< xi yj)
|
(if (< xi yj)
|
||||||
(do (array/push ret xi) (:= i (+ i 1)))
|
(do (array/push ret xi) (set i (+ i 1)))
|
||||||
(do (array/push ret yj) (:= j (+ j 1)))))
|
(do (array/push ret yj) (set j (+ j 1)))))
|
||||||
# Push rest of xs
|
# Push rest of xs
|
||||||
(while (< i xlen)
|
(while (< i xlen)
|
||||||
(def xi (get xs i))
|
(def xi (get xs i))
|
||||||
(array/push ret xi)
|
(array/push ret xi)
|
||||||
(:= i (+ i 1)))
|
(set i (+ i 1)))
|
||||||
# Push rest of ys
|
# Push rest of ys
|
||||||
(while (< j ylen)
|
(while (< j ylen)
|
||||||
(def yj (get ys j))
|
(def yj (get ys j))
|
||||||
(array/push ret yj)
|
(array/push ret yj)
|
||||||
(:= j (+ j 1)))
|
(set j (+ j 1)))
|
||||||
ret)
|
ret)
|
||||||
|
|
||||||
(assert (apply <= (merge @[1 3 5] @[2 4 6])) "merge sort merge 1")
|
(assert (apply <= (merge @[1 3 5] @[2 4 6])) "merge sort merge 1")
|
||||||
@ -255,7 +255,7 @@
|
|||||||
(var count 0)
|
(var count 0)
|
||||||
(while (< count 128)
|
(while (< count 128)
|
||||||
(put syms (gensym) true)
|
(put syms (gensym) true)
|
||||||
(:= count (+ 1 count)))
|
(set count (+ 1 count)))
|
||||||
(assert (= (length syms) 128) "many symbols")))
|
(assert (= (length syms) 128) "many symbols")))
|
||||||
|
|
||||||
# Let
|
# Let
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
(defn myfun [x]
|
(defn myfun [x]
|
||||||
(var a 10)
|
(var a 10)
|
||||||
(:= a (do
|
(set a (do
|
||||||
(def y x)
|
(def y x)
|
||||||
(if x 8 9))))
|
(if x 8 9))))
|
||||||
|
|
||||||
@ -44,7 +44,7 @@
|
|||||||
(var good true)
|
(var good true)
|
||||||
(loop [i :range [0 n]]
|
(loop [i :range [0 n]]
|
||||||
(if (not (f))
|
(if (not (f))
|
||||||
(:= good false)))
|
(set good false)))
|
||||||
(assert good e))
|
(assert good e))
|
||||||
|
|
||||||
(assert-many (fn [] (>= 1 (math/random) 0)) 200 "(random) between 0 and 1")
|
(assert-many (fn [] (>= 1 (math/random) 0)) 200 "(random) between 0 and 1")
|
||||||
|
Loading…
Reference in New Issue
Block a user