1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 07:50:27 +00:00

Rename the := special form to set so it does not look like a keyword.

This commit is contained in:
Calvin Rose 2018-12-16 21:57:32 -05:00
parent f7a25ecae3
commit 03dbd79165
11 changed files with 106 additions and 107 deletions

View File

@ -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
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
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
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
@ -247,13 +247,13 @@ symbols will raise an error.
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
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.
```
(var myvar 1)
(print myvar)
(:= myvar 10)
(set myvar 10)
(print myvar)
```

View File

@ -18,8 +18,8 @@
(if ,loaded
,state
(do
(:= ,loaded true)
(:= ,state (do ;forms)))))))
(set ,loaded true)
(set ,state (do ;forms)))))))
# Use tuples instead of structs to save memory
(def- HEAD 0)

View File

@ -24,7 +24,7 @@
"Draw cells in the game of life from (x1, y1) to (x2, y2)"
[state x1 y1 x2 y2]
(def cellset @{})
(each cell state (:= cellset@cell true))
(each cell state (set cellset@cell true))
(loop [x :range [x1 (+ 1 x2)]
:after (print)
y :range [y1 (+ 1 y2)]]
@ -40,4 +40,4 @@
(for i 0 20
(print "generation " i)
(draw *state* -7 -7 7 7)
(:= *state* (tick *state*)))
(set *state* (tick *state*)))

View File

@ -9,6 +9,6 @@
(def len (length list))
(for j 0 len
(def trial (get list j))
(if (zero? (% i trial)) (:= isprime? false)))
(if (zero? (% i trial)) (set isprime? false)))
(if isprime? (array/push list i)))
list)

View File

@ -205,53 +205,53 @@
<key>name</key>
<string>punctuation.other.janet</string>
</dict>
<!-- string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;]) token match here (?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])</string -->
<!-- string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*]) token match here (?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])</string -->
<key>literal</key>
<dict>
<key>match</key>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])(true|false|nil)(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])</string>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])(true|false|nil)(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])</string>
<key>name</key>
<string>constant.language.janet</string>
</dict>
<key>corelib</key>
<dict>
<key>match</key>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])(def|do|fn|if|quasiquote|quote|splice|unquote|while|%|%=|&amp;|&amp;=|\*|\*=|\*doc\-width\*|\*env\*|\+|\+\+|\+=|\-|\-\-|\-=|\-&gt;|\-&gt;&gt;|&#47;|&#47;=|&lt;|&lt;&lt;|&lt;&lt;=|&lt;=|=|==|&gt;|&gt;=|&gt;&gt;|&gt;&gt;=|&gt;&gt;&gt;|&gt;&gt;&gt;=|\^|\^=|_env|abstract\?|all|all\-symbols|and|apply|array|array&#47;concat|array&#47;ensure|array&#47;insert|array&#47;new|array&#47;peek|array&#47;pop|array&#47;push|array&#47;slice|array\?|asm|bnot|boolean\?|buffer|buffer&#47;clear|buffer&#47;new|buffer&#47;popn|buffer&#47;push\-byte|buffer&#47;push\-integer|buffer&#47;push\-string|buffer&#47;slice|buffer\?|bytes\?|callable\?|case|cfunction\?|comment|comp|compile|complement|cond|coro|count|debug|debug&#47;arg\-stack|debug&#47;break|debug&#47;fbreak|debug&#47;lineage|debug&#47;stack|debug&#47;unbreak|debug&#47;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&#47;current|fiber&#47;maxstack|fiber&#47;new|fiber&#47;setmaxstack|fiber&#47;status|fiber\?|file&#47;close|file&#47;flush|file&#47;open|file&#47;popen|file&#47;read|file&#47;seek|file&#47;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&#47;build|janet&#47;version|juxt|juxt\*|keep|keys|keyword\?|kvs|last|length|let|loop|macex|macex1|make\-env|map|mapcat|marshal|match|match\-1|math&#47;acos|math&#47;asin|math&#47;atan|math&#47;ceil|math&#47;cos|math&#47;e|math&#47;exp|math&#47;floor|math&#47;inf|math&#47;log|math&#47;log10|math&#47;pi|math&#47;pow|math&#47;random|math&#47;seedrandom|math&#47;sin|math&#47;sqrt|math&#47;tan|max|max\-order|merge|merge\-into|min|min\-order|module&#47;find|module&#47;native\-paths|module&#47;paths|native|neg\?|next|nil\?|not|not=|not==|number\?|odd\?|one\?|or|order&lt;|order&lt;=|order&gt;|order&gt;=|os&#47;clock|os&#47;cwd|os&#47;execute|os&#47;exit|os&#47;getenv|os&#47;setenv|os&#47;shell|os&#47;sleep|os&#47;time|os&#47;which|pairs|parser&#47;byte|parser&#47;consume|parser&#47;error|parser&#47;flush|parser&#47;new|parser&#47;produce|parser&#47;state|parser&#47;status|parser&#47;where|partial|pos\?|print|process&#47;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&#47;ascii\-lower|string&#47;ascii\-upper|string&#47;bytes|string&#47;check\-set|string&#47;find|string&#47;find\-all|string&#47;from\-bytes|string&#47;join|string&#47;number|string&#47;pretty|string&#47;repeat|string&#47;replace|string&#47;replace\-all|string&#47;reverse|string&#47;slice|string&#47;split|string\?|struct|struct\?|sum|symbol|symbol\?|table|table&#47;getproto|table&#47;new|table&#47;rawget|table&#47;setproto|table&#47;to\-struct|table\?|take\-until|take\-while|true\?|tuple|tuple&#47;append|tuple&#47;prepend|tuple&#47;slice|tuple\?|type|unless|unmarshal|update|values|varglobal|when|when\-let|with\-idemp|yield|zero\?|zipcoll|\||\|=)(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])</string>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])(def|do|fn|if|quasiquote|quote|set|splice|unquote|var|while|%|%=|&amp;|&amp;=|\*|\*=|\*doc\-width\*|\*env\*|\+|\+\+|\+=|\-|\-\-|\-=|\-&gt;|\-&gt;&gt;|&#47;|&#47;=|&lt;|&lt;&lt;|&lt;&lt;=|&lt;=|=|==|&gt;|&gt;=|&gt;&gt;|&gt;&gt;=|&gt;&gt;&gt;|&gt;&gt;&gt;=|\^|\^=|_env|abstract\?|all|all\-symbols|and|apply|array|array&#47;concat|array&#47;ensure|array&#47;insert|array&#47;new|array&#47;peek|array&#47;pop|array&#47;push|array&#47;slice|array\?|asm|bnot|boolean\?|buffer|buffer&#47;clear|buffer&#47;new|buffer&#47;popn|buffer&#47;push\-byte|buffer&#47;push\-integer|buffer&#47;push\-string|buffer&#47;slice|buffer\?|bytes\?|callable\?|case|cfunction\?|comment|comp|compile|complement|cond|coro|count|debug|debug&#47;arg\-stack|debug&#47;break|debug&#47;fbreak|debug&#47;lineage|debug&#47;stack|debug&#47;unbreak|debug&#47;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&#47;current|fiber&#47;maxstack|fiber&#47;new|fiber&#47;setmaxstack|fiber&#47;status|fiber\?|file&#47;close|file&#47;flush|file&#47;open|file&#47;popen|file&#47;read|file&#47;seek|file&#47;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&#47;build|janet&#47;version|juxt|juxt\*|keep|keys|keyword\?|kvs|last|length|let|loop|macex|macex1|make\-env|map|mapcat|marshal|match|match\-1|math&#47;acos|math&#47;asin|math&#47;atan|math&#47;ceil|math&#47;cos|math&#47;e|math&#47;exp|math&#47;floor|math&#47;inf|math&#47;log|math&#47;log10|math&#47;pi|math&#47;pow|math&#47;random|math&#47;seedrandom|math&#47;sin|math&#47;sqrt|math&#47;tan|max|max\-order|merge|merge\-into|min|min\-order|module&#47;find|module&#47;native\-paths|module&#47;paths|native|neg\?|next|nil\?|not|not=|not==|number\?|odd\?|one\?|or|order&lt;|order&lt;=|order&gt;|order&gt;=|os&#47;clock|os&#47;cwd|os&#47;execute|os&#47;exit|os&#47;getenv|os&#47;setenv|os&#47;shell|os&#47;sleep|os&#47;time|os&#47;which|pairs|parser&#47;byte|parser&#47;consume|parser&#47;error|parser&#47;flush|parser&#47;new|parser&#47;produce|parser&#47;state|parser&#47;status|parser&#47;where|partial|pos\?|print|process&#47;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&#47;ascii\-lower|string&#47;ascii\-upper|string&#47;bytes|string&#47;check\-set|string&#47;find|string&#47;find\-all|string&#47;from\-bytes|string&#47;join|string&#47;number|string&#47;pretty|string&#47;repeat|string&#47;replace|string&#47;replace\-all|string&#47;reverse|string&#47;slice|string&#47;split|string\?|struct|struct\?|sum|symbol|symbol\?|table|table&#47;getproto|table&#47;new|table&#47;rawget|table&#47;setproto|table&#47;to\-struct|table\?|take\-until|take\-while|true\?|tuple|tuple&#47;append|tuple&#47;prepend|tuple&#47;slice|tuple\?|type|unless|unmarshal|update|values|varglobal|when|when\-let|with\-idemp|yield|zero\?|zipcoll|\||\|=)(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])</string>
<key>name</key>
<string>keyword.control.janet</string>
</dict>
<key>keysym</key>
<dict>
<key>match</key>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;]):[\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;]*</string>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*]):[\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*]*</string>
<key>name</key>
<string>constant.keyword.janet</string>
</dict>
<key>symbol</key>
<dict>
<key>match</key>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])[\.a-zA-Z_\-=!@\$%^&amp;?|\\/&lt;&gt;][\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;]*</string>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])[\.a-zA-Z_\-=!@\$%^&amp;?|\\/&lt;&gt;*][\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*]*</string>
<key>name</key>
<string>variable.other.janet</string>
</dict>
<key>hex-number</key>
<dict>
<key>match</key>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])[-+]?0x([_\da-fA-F]+|[_\da-fA-F]+\.[_\da-fA-F]*|\.[_\da-fA-F]+)(&amp;[+-]?[\da-fA-F]+)?(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])</string>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])[-+]?0x([_\da-fA-F]+|[_\da-fA-F]+\.[_\da-fA-F]*|\.[_\da-fA-F]+)(&amp;[+-]?[\da-fA-F]+)?(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])</string>
<key>name</key>
<string>constant.numeric.hex.janet</string>
</dict>
<key>dec-number</key>
<dict>
<key>match</key>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])[-+]?([_\d]+|[_\d]+\.[_\d]*|\.[_\d]+)([eE&amp;][+-]?[\d]+)?(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])</string>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])[-+]?([_\d]+|[_\d]+\.[_\d]*|\.[_\d]+)([eE&amp;][+-]?[\d]+)?(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])</string>
<key>name</key>
<string>constant.numeric.decimal.janet</string>
</dict>
<key>r-number</key>
<dict>
<key>match</key>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])[-+]?\d\d?r([_\w]+|[_\w]+\.[_\w]*|\.[_\w]+)(&amp;[+-]?[\w]+)?(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;])</string>
<string>(?&lt;![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])[-+]?\d\d?r([_\w]+|[_\w]+\.[_\w]*|\.[_\w]+)(&amp;[+-]?[\w]+)?(?![\.:\w_\-=!@\$%^&amp;?|\\/&lt;&gt;*])</string>
<key>name</key>
<string>constant.numeric.decimal.janet</string>
</dict>

View File

@ -25,7 +25,7 @@
i
(do
(if (= t :string)
(:= docstr ith)
(set docstr ith)
(array/push modifiers ith))
(if (< i len) (recur (+ i 1)))))))
(def start (fstart 0))
@ -37,7 +37,7 @@
(while (< index arglen)
(buffer/push-string buf " ")
(string/pretty args.index 4 buf)
(:= index (+ index 1)))
(set index (+ index 1)))
(array/push modifiers (string buf ")\n\n" docstr))
# Build return value
~(def ,name ,;modifiers (fn ,name ,;(tuple/slice more start)))))
@ -149,19 +149,19 @@
# C style macros and functions for imperative sugar
(defn inc "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 -- "Decrements the var x by 1." [x] ~(:= ,x (,- ,x ,1)))
(defmacro += "Increments the var x by n." [x n] ~(:= ,x (,+ ,x ,n)))
(defmacro -= "Decrements the vat x by n." [x n] ~(:= ,x (,- ,x ,n)))
(defmacro *= "Shorthand for (:= x (* x n))." [x n] ~(:= ,x (,* ,x ,n)))
(defmacro /= "Shorthand for (:= x (/ x n))." [x n] ~(:= ,x (,/ ,x ,n)))
(defmacro %= "Shorthand for (:= x (% x n))." [x n] ~(:= ,x (,% ,x ,n)))
(defmacro &= "Shorthand for (:= x (& x n))." [x n] ~(:= ,x (,& ,x ,n)))
(defmacro |= "Shorthand for (:= x (| x n))." [x n] ~(:= ,x (,| ,x ,n)))
(defmacro ^= "Shorthand for (:= x (^ x n))." [x n] ~(:= ,x (,^ ,x ,n)))
(defmacro >>= "Shorthand for (:= x (>> x n))." [x n] ~(:= ,x (,>> ,x ,n)))
(defmacro <<= "Shorthand for (:= x (<< x n))." [x n] ~(:= ,x (,<< ,x ,n)))
(defmacro >>>= "Shorthand for (:= x (>>> x n))." [x n] ~(:= ,x (,>>> ,x ,n)))
(defmacro ++ "Increments the var x by 1." [x] ~(set ,x (,+ ,x ,1)))
(defmacro -- "Decrements the var x by 1." [x] ~(set ,x (,- ,x ,1)))
(defmacro += "Increments the var x by n." [x n] ~(set ,x (,+ ,x ,n)))
(defmacro -= "Decrements the vat x by n." [x n] ~(set ,x (,- ,x ,n)))
(defmacro *= "Shorthand for (set x (* x n))." [x n] ~(set ,x (,* ,x ,n)))
(defmacro /= "Shorthand for (set x (/ x n))." [x n] ~(set ,x (,/ ,x ,n)))
(defmacro %= "Shorthand for (set x (% x n))." [x n] ~(set ,x (,% ,x ,n)))
(defmacro &= "Shorthand for (set x (& x n))." [x n] ~(set ,x (,& ,x ,n)))
(defmacro |= "Shorthand for (set x (| x n))." [x n] ~(set ,x (,| ,x ,n)))
(defmacro ^= "Shorthand for (set x (^ x n))." [x n] ~(set ,x (,^ ,x ,n)))
(defmacro >>= "Shorthand for (set x (>> x n))." [x n] ~(set ,x (,>> ,x ,n)))
(defmacro <<= "Shorthand for (set x (<< x n))." [x n] ~(set ,x (,<< ,x ,n)))
(defmacro >>>= "Shorthand for (set x (>>> x n))." [x n] ~(set ,x (,>>> ,x ,n)))
(defmacro default
"Define a default value for an optional argument.
@ -248,7 +248,7 @@
(var i len)
(while (> i 0)
(-- i)
(:= ret (if (= ret true)
(set ret (if (= ret true)
forms.i
(tuple 'if forms.i ret))))
ret)
@ -263,7 +263,7 @@
(while (> i 0)
(-- i)
(def fi forms.i)
(:= ret (if (idempotent? fi)
(set ret (if (idempotent? fi)
(tuple 'if fi fi ret)
(do
(def $fi (gensym))
@ -331,13 +331,13 @@
(tuple 'var $iter 0)
(tuple 'while
(tuple/slice spreds)
(tuple := $iter (tuple + 1 $iter))
(tuple 'set $iter (tuple + 1 $iter))
sub)))
(error (string "unexpected loop predicate: " bindings)))
(case verb
:iterate (do
(def $iter (gensym))
(def preds @['and (tuple ':= $iter object)])
(def preds @['and (tuple 'set $iter object)])
(def subloop (doone (+ i 3) preds))
(tuple 'do
(tuple 'var $iter nil)
@ -357,7 +357,7 @@
(tuple 'while (tuple/slice preds)
(tuple 'def bindings $iter)
subloop
(tuple ':= $iter (tuple + $iter inc)))))
(tuple 'set $iter (tuple + $iter inc)))))
:keys (do
(def $dict (gensym))
(def $iter (gensym))
@ -369,7 +369,7 @@
(tuple 'while (tuple/slice preds)
(tuple 'def bindings $iter)
subloop
(tuple ':= $iter (tuple next $dict $iter)))))
(tuple 'set $iter (tuple next $dict $iter)))))
:in (do
(def $len (gensym))
(def $i (gensym))
@ -383,7 +383,7 @@
(tuple 'while (tuple/slice preds 0)
(tuple 'def bindings (tuple get $indexed $i))
subloop
(tuple ':= $i (tuple + 1 $i)))))
(tuple 'set $i (tuple + 1 $i)))))
:generate (do
(def $fiber (gensym))
(def $yieldval (gensym))
@ -400,7 +400,7 @@
(tuple 'while (tuple/slice preds 0)
(tuple 'def bindings $yieldval)
subloop
(tuple := $yieldval (tuple resume $fiber)))))
(tuple 'set $yieldval (tuple resume $fiber)))))
(error (string "unexpected loop verb: " verb)))))))
(doone 0 nil))
@ -517,7 +517,7 @@
(var ret args.0)
(loop [i :range [0 len]]
(def v args.i)
(if (order v ret) (:= ret v)))
(if (order v ret) (set ret v)))
ret))
(defn max
@ -566,11 +566,11 @@
(def aj a.j)
(when (by aj pivot)
(def ai a.i)
(:= a.i aj)
(:= a.j ai)
(set a.i aj)
(set a.j ai)
(++ i)))
(:= a.hi a.i)
(:= a.i pivot)
(set a.hi a.i)
(set a.i pivot)
i)
(defn sort-help
@ -595,7 +595,7 @@
[f init ind]
(var res init)
(loop [x :in ind]
(:= res (f res x)))
(set res (f res x)))
res)
(defn map
@ -607,18 +607,18 @@
(var limit (length inds.0))
(loop [i :range [0 ninds]]
(def l (length inds.i))
(if (< l limit) (:= limit l)))
(if (< l limit) (set limit l)))
(def [i1 i2 i3 i4] inds)
(def res (array/new limit))
(case ninds
1 (loop [i :range [0 limit]] (:= res.i (f i1.i)))
2 (loop [i :range [0 limit]] (:= res.i (f i1.i i2.i)))
3 (loop [i :range [0 limit]] (:= 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)))
1 (loop [i :range [0 limit]] (set res.i (f i1.i)))
2 (loop [i :range [0 limit]] (set res.i (f i1.i i2.i)))
3 (loop [i :range [0 limit]] (set res.i (f i1.i i2.i i3.i)))
4 (loop [i :range [0 limit]] (set res.i (f i1.i i2.i i3.i i4.i)))
(loop [i :range [0 limit]]
(def args (array/new ninds))
(loop [j :range [0 ninds]] (:= args.j inds.j.i))
(:= res.i (f ;args))))
(loop [j :range [0 ninds]] (set args.j inds.j.i))
(set res.i (f ;args))))
res)
(defn mapcat
@ -691,7 +691,7 @@
(var going true)
(while (if (< i len) going)
(def item ind.i)
(if (pred item) (:= going false) (++ i)))
(if (pred item) (set going false) (++ i)))
(if going nil i))
(defn find
@ -784,7 +784,7 @@
[ind]
(var res true)
(loop [x :in ind :while res]
(if x nil (:= res x)))
(if x nil (set res x)))
res)
(defn reverse
@ -825,7 +825,7 @@ value, one key will be ignored."
The key then, is associated to the function's return value"
[coll a-key a-function & args]
(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
"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]
(loop [c :in colls
key :keys c]
(:= tab.key c.key))
(set tab.key c.key))
tab)
(defn merge
@ -845,7 +845,7 @@ value, one key will be ignored."
(def container @{})
(loop [c :in colls
key :keys c]
(:= container.key c.key))
(set container.key c.key))
container)
(defn keys
@ -855,7 +855,7 @@ value, one key will be ignored."
(var k (next x nil))
(while (not= nil k)
(array/push arr k)
(:= k (next x k)))
(set k (next x k)))
arr)
(defn values
@ -865,7 +865,7 @@ value, one key will be ignored."
(var k (next x nil))
(while (not= nil k)
(array/push arr x.k)
(:= k (next x k)))
(set k (next x k)))
arr)
(defn pairs
@ -875,7 +875,7 @@ value, one key will be ignored."
(var k (next x nil))
(while (not= nil k)
(array/push arr (tuple k x.k))
(:= k (next x k)))
(set k (next x k)))
arr)
(defn frequencies
@ -885,7 +885,7 @@ value, one key will be ignored."
(loop
[x :in ind]
(def n freqs.x)
(:= freqs.x (if n (+ 1 n) 1)))
(set freqs.x (if n (+ 1 n) 1)))
freqs)
(defn interleave
@ -906,7 +906,7 @@ value, one key will be ignored."
[xs]
(def ret @[])
(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)
(defn flatten-into
@ -939,7 +939,7 @@ value, one key will be ignored."
[sep ind]
(def len (length ind))
(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)
(while (< i len)
(array/push ret sep ind.i)
@ -991,7 +991,7 @@ value, one key will be ignored."
$dict expr
~(if (dictionary? ,$dict)
,((fn aux []
(:= key (next pattern key))
(set key (next pattern key))
(if (= key nil)
(onmatch)
(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 spacer
(if (<= maxcol (+ current (length word) 1))
(do (:= current 0) "\n ")
(do (set current 0) "\n ")
(do (++ current) " ")))
(+= current (length word))
(if (> oldcur 0)
@ -1066,7 +1066,7 @@ value, one key will be ignored."
(if (> (length word) 0) (pushword))
(when (= b 10)
(buffer/push-string buf "\n ")
(:= current 0)))))
(set current 0)))))
# Last word
(pushword)
@ -1103,7 +1103,7 @@ value, one key will be ignored."
(var key (next t nil))
(while (not= nil key)
(put newt (macex1 key) (on-value t.key))
(:= key (next t key)))
(set key (next t key)))
newt)
(defn expand-bindings [x]
@ -1151,7 +1151,7 @@ value, one key will be ignored."
(tuple t.0 (qq t.1)))
(def specs
{':= expanddef
{'set expanddef
'def expanddef
'do expandall
'fn expandfn
@ -1185,14 +1185,14 @@ value, one key will be ignored."
[pred xs]
"Returns true if all xs are truthy, otherwise the first false or nil value."
(var ret true)
(loop [x :in xs :while ret] (:= ret (pred x)))
(loop [x :in xs :while ret] (set ret (pred x)))
ret)
(defn some
[pred xs]
"Returns false if all xs are false or nil, otherwise returns the first true value."
(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)
(defn deep-not=
@ -1225,8 +1225,8 @@ value, one key will be ignored."
(while (deep-not= current previous)
(if (> (++ counter) 200)
(error "macro expansion too nested"))
(:= previous current)
(:= current (macex1 current)))
(set previous current)
(set current (macex1 current)))
current)
###
@ -1274,7 +1274,7 @@ value, one key will be ignored."
(if (= (type res) :function)
(res)
(do
(:= good false)
(set good false)
(def {:error err :start start :end end :fiber errf} res)
(onstatus
:compile
@ -1289,7 +1289,7 @@ value, one key will be ignored."
(if going (onstatus (fiber/status f) res f where))))
(def oldenv *env*)
(:= *env* env)
(set *env* env)
# Run loop
(def buf @"")
@ -1299,10 +1299,10 @@ value, one key will be ignored."
(var pindex 0)
(var pstatus nil)
(def len (length buf))
(if (= len 0) (:= going false))
(if (= len 0) (set going false))
(while (> len pindex)
(+= pindex (parser/consume p buf pindex))
(while (= (:= pstatus (parser/status p)) :full)
(while (= (set pstatus (parser/status p)) :full)
(eval1 (parser/produce p)))
(when (= pstatus :error)
(onstatus :parse
@ -1311,7 +1311,7 @@ value, one key will be ignored."
nil
where))))
(:= *env* oldenv)
(set *env* oldenv)
env)
@ -1369,7 +1369,7 @@ value, one key will be ignored."
(var state (string str))
(defn chunks [buf _]
(def ret state)
(:= state nil)
(set state nil)
(when ret
(buffer/push-string buf str)
(buffer/push-string buf "\n")))
@ -1377,7 +1377,7 @@ value, one key will be ignored."
(run-context *env* chunks
(fn [sig x f source]
(if (= sig :dead)
(:= returnval x)
(set returnval x)
(status-pp sig x f source)))
"eval")
returnval)
@ -1470,8 +1470,8 @@ value, one key will be ignored."
check
(do
(def newenv (make-env))
(:= cache.path newenv)
(:= loading.path true)
(set cache.path newenv)
(set loading.path true)
(def f (find-mod path))
(if f
(do
@ -1490,7 +1490,7 @@ value, one key will be ignored."
(if (not n)
(error (string "could not open file for module " path)))
((native n) newenv)))
(:= loading.path false)
(set loading.path false)
newenv)))))
(defn import*
@ -1510,7 +1510,7 @@ value, one key will be ignored."
(when (not v:private)
(def newv (table/setproto @{:private true} v))
(put env (symbol prefix k) newv))
(:= k (next newenv k))))
(set k (next newenv k))))
(defmacro import
"Import a module. First requires the module, and then merges its
@ -1547,9 +1547,9 @@ value, one key will be ignored."
[env &]
(default env *env*)
(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 @{})
(loop [envi :in envs
k :keys envi]
(:= symbol-set.k true))
(set symbol-set.k true))
(sort (keys symbol-set)))

View File

@ -648,16 +648,15 @@ error2:
/* Keep in lexicographic order */
static const JanetSpecial janetc_specials[] = {
{":=", janetc_varset},
{"def", janetc_def},
{"do", janetc_do},
{"fn", janetc_fn},
{"if", janetc_if},
{"quasiquote", janetc_quasiquote},
{"quote", janetc_quote},
{"set", janetc_varset},
{"splice", janetc_splice},
{"unquote", janetc_unquote},
{"unquote", janetc_unquote},
{"var", janetc_var},
{"while", janetc_while}
};

View File

@ -24,12 +24,12 @@
(os/exit 0)
1)
"v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1)
"s" (fn [&] (:= *raw-stdin* true) (:= *should-repl* true) 1)
"r" (fn [&] (:= *should-repl* true) 1)
"p" (fn [&] (:= *exit-on-error* false) 1)
"-" (fn [&] (:= *handleopts* false) 1)
"s" (fn [&] (set *raw-stdin* true) (set *should-repl* true) 1)
"r" (fn [&] (set *should-repl* true) 1)
"p" (fn [&] (set *exit-on-error* false) 1)
"-" (fn [&] (set *handleopts* false) 1)
"e" (fn [i &]
(:= *no-file* false)
(set *no-file* false)
(eval (get process/args (+ i 1)))
2)})
@ -45,7 +45,7 @@
(if (and *handleopts* (= "-" (string/slice arg 0 1)))
(+= i (dohandler (string/slice arg 1 2) i))
(do
(:= *no-file* false)
(set *no-file* false)
(import* _env arg :prefix "" :exit *exit-on-error*)
(++ i))))

View File

@ -13,7 +13,7 @@
x)
(defn start-suite [x]
(:= suite-num x)
(set suite-num x)
(print "\nRunning test suite " x " tests...\n"))
(defn end-suite []

View File

@ -80,7 +80,7 @@
# Mcarthy's 91 function
(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 11)) "f91(11) = 91")
(assert (= 91 (f91 20)) "f91(20) = 91")
@ -92,7 +92,7 @@
(assert (= 94 (f91 104)) "f91(104) = 94")
# 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))))))
(assert (= (fib 0) (fib2 0) 0) "fib(0)")
@ -127,15 +127,15 @@
(var accum 1)
(var count 0)
(while (< count 16)
(:= accum (<< accum 1))
(:= count (+ 1 count)))
(set accum (<< accum 1))
(set count (+ 1 count)))
(assert (= accum 65536) "loop in closure")))
(var accum 1)
(var count 0)
(while (< count 16)
(:= accum (<< accum 1))
(:= count (+ 1 count)))
(set accum (<< accum 1))
(set count (+ 1 count)))
(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")
@ -228,18 +228,18 @@
(def xi (get xs i))
(def yj (get ys j))
(if (< xi yj)
(do (array/push ret xi) (:= i (+ i 1)))
(do (array/push ret yj) (:= j (+ j 1)))))
(do (array/push ret xi) (set i (+ i 1)))
(do (array/push ret yj) (set j (+ j 1)))))
# Push rest of xs
(while (< i xlen)
(def xi (get xs i))
(array/push ret xi)
(:= i (+ i 1)))
(set i (+ i 1)))
# Push rest of ys
(while (< j ylen)
(def yj (get ys j))
(array/push ret yj)
(:= j (+ j 1)))
(set j (+ j 1)))
ret)
(assert (apply <= (merge @[1 3 5] @[2 4 6])) "merge sort merge 1")
@ -255,7 +255,7 @@
(var count 0)
(while (< count 128)
(put syms (gensym) true)
(:= count (+ 1 count)))
(set count (+ 1 count)))
(assert (= (length syms) 128) "many symbols")))
# Let

View File

@ -33,7 +33,7 @@
(defn myfun [x]
(var a 10)
(:= a (do
(set a (do
(def y x)
(if x 8 9))))
@ -44,7 +44,7 @@
(var good true)
(loop [i :range [0 n]]
(if (not (f))
(:= good false)))
(set good false)))
(assert good e))
(assert-many (fn [] (>= 1 (math/random) 0)) 200 "(random) between 0 and 1")