1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-23 15:00:27 +00:00

Replace varset! with algol style :=

This commit is contained in:
Calvin Rose 2018-03-16 18:15:34 -04:00
parent f5213e4064
commit 4a76f2ae32
6 changed files with 27 additions and 22 deletions

View File

@ -18,6 +18,10 @@ Implemented in mostly standard C99, dst runs on Windows, Linux and macOS.
The few features that are not standard C (dynamic library loading, compiler specific optimizations), The few features that are not standard C (dynamic library loading, compiler specific optimizations),
are fairly straight forward. Dst can be easily ported to new platforms. are fairly straight forward. Dst can be easily ported to new platforms.
There is not much in the way of documentation yet because it is still a "personal project" and
I don't want to freeze features prematurely. You can look in the examples directory, the test directory,
or the file `src/compiler/boot.dst` to get a sense of what dst code looks like.
## Features ## Features
* First class closures * First class closures

View File

@ -11,10 +11,11 @@
state state
(do (do
(def n (f)) (def n (f))
(varset! state n) (:= state n)
(varset! loaded true) (:= loaded true)
n)))) n))))
# This creates one more closure than necessary but oh well
(defmacro delay (defmacro delay
"Macro for lazy evaluation" "Macro for lazy evaluation"
[& forms] (tuple mem0 (apply1 tuple (array-concat ['fn []] forms)))) [& forms] (tuple mem0 (apply1 tuple (array-concat ['fn []] forms))))

View File

@ -139,7 +139,7 @@ If no match is found, returns nil"
:more (fn [] (< i len)) :more (fn [] (< i len))
:next (fn [] :next (fn []
(def ret (get x i)) (def ret (get x i))
(varset! i (+ i 1)) (:= i (+ i 1))
ret) ret)
}) })
(def iters { (def iters {
@ -156,7 +156,7 @@ If no match is found, returns nil"
:more (fn [] (< i top)) :more (fn [] (< i top))
:next (fn [] :next (fn []
(def ret i) (def ret i)
(varset! i (+ i 1)) (:= i (+ i 1))
ret) ret)
}) })
@ -199,9 +199,9 @@ If no match is found, returns nil"
(do (do
(def n (next)) (def n (next))
(if (pred n) n (nextgood))) (if (pred n) n (nextgood)))
(varset! alive false)))) (:= alive false))))
(defn nnext [] (def ret temp) (varset! temp (nextgood)) ret) (defn nnext [] (def ret temp) (:= temp (nextgood)) ret)
(defn nmore [] (when isnew (varset! isnew false) (nnext)) alive) (defn nmore [] (when isnew (:= isnew false) (nnext)) alive)
{:more nmore :next nnext}) {:more nmore :next nnext})
(defmacro let [bindings & body] (defmacro let [bindings & body]
@ -213,7 +213,7 @@ If no match is found, returns nil"
(array-push accum (tuple 'def (array-push accum (tuple 'def
(get head i) (get head i)
(get head (+ 1 i)))) (get head (+ 1 i))))
(varset! i (+ i 2))) (:= i (+ i 2)))
(array-push accum (tuple-prepend body 'do)) (array-push accum (tuple-prepend body 'do))
(apply1 tuple accum)) (apply1 tuple accum))
@ -223,7 +223,7 @@ If no match is found, returns nil"
:more (fn [] lastkey) :more (fn [] lastkey)
:next (fn [] :next (fn []
(def ret (tuple lastkey (get x lastkey))) (def ret (tuple lastkey (get x lastkey)))
(varset! lastkey (next x lastkey)) (:= lastkey (next x lastkey))
ret) ret)
}) })
@ -233,7 +233,7 @@ If no match is found, returns nil"
:more (fn [] lastkey) :more (fn [] lastkey)
:next (fn [] :next (fn []
(def ret lastkey) (def ret lastkey)
(varset! lastkey (next x lastkey)) (:= lastkey (next x lastkey))
ret) ret)
}) })
@ -243,7 +243,7 @@ If no match is found, returns nil"
:more (fn [] lastkey) :more (fn [] lastkey)
:next (fn [] :next (fn []
(def ret (get x lastkey)) (def ret (get x lastkey))
(varset! lastkey (next x lastkey)) (:= lastkey (next x lastkey))
ret) ret)
}) })
@ -260,7 +260,7 @@ If no match is found, returns nil"
(tuple 'def endsym end) (tuple 'def endsym end)
(tuple 'while (tuple '< sym endsym) (tuple 'while (tuple '< sym endsym)
(tuple-prepend body 'do) (tuple-prepend body 'do)
(tuple 'varset! sym (tuple '+ sym inc))))) (tuple ':= sym (tuple '+ sym inc)))))
(defmacro -> (defmacro ->
[x & forms] [x & forms]
@ -311,7 +311,7 @@ If no match is found, returns nil"
(pp seen buf k) (pp seen buf k)
(buffer-push-string buf " ") (buffer-push-string buf " ")
(pp seen buf v) (pp seen buf v)
(varset! k (next a k)) (:= k (next a k))
(when k (buffer-push-string buf " "))) (when k (buffer-push-string buf " ")))
(buffer-push-string buf end))) (buffer-push-string buf end)))
buf) buf)
@ -369,7 +369,7 @@ onvalue."
(while (< 0 len) (while (< 0 len)
(buffer-clear buf) (buffer-clear buf)
(chunks buf) (chunks buf)
(varset! len (length buf)) (:= len (length buf))
(for [i 0 len] (for [i 0 len]
(yield (get buf i)))) (yield (get buf i))))
0))) 0)))
@ -385,17 +385,17 @@ onvalue."
(select (fiber-status chars) (select (fiber-status chars)
:new (parser-byte p (resume chars)) :new (parser-byte p (resume chars))
:pending (parser-byte p (resume chars)) :pending (parser-byte p (resume chars))
(varset! going false)))) (:= going false))))
(when (not= :root (parser-status p)) (when (not= :root (parser-status p))
(onerr "parse" "unexpected end of source")) (onerr "parse" "unexpected end of source"))
nil))) nil)))
(defn more [] (if temp true (defn more [] (if temp true
(do (do
(varset! temp true) (:= temp true)
(varset! tempval (resume f)) (:= tempval (resume f))
going))) going)))
(defn next [] (if temp (defn next [] (if temp
(do (varset! temp nil) tempval) (do (:= temp nil) tempval)
(resume f))) (resume f)))
{:more more :next next}) {:more more :next next})
(fn [env chunks onvalue onerr] (fn [env chunks onvalue onerr]

View File

@ -615,6 +615,7 @@ DstSlot dstc_fn(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
/* Keep in lexographic order */ /* Keep in lexographic order */
static const DstSpecial dstc_specials[] = { static const DstSpecial dstc_specials[] = {
{":=", dstc_varset},
{"ast-quote", dstc_astquote}, {"ast-quote", dstc_astquote},
{"def", dstc_def}, {"def", dstc_def},
{"do", dstc_do}, {"do", dstc_do},
@ -622,7 +623,6 @@ static const DstSpecial dstc_specials[] = {
{"if", dstc_if}, {"if", dstc_if},
{"quote", dstc_quote}, {"quote", dstc_quote},
{"var", dstc_var}, {"var", dstc_var},
{"varset!", dstc_varset},
{"while", dstc_while} {"while", dstc_while}
}; };

View File

@ -136,7 +136,7 @@ int dst_core_struct(DstArgs args) {
int dst_core_fiber(DstArgs args) { int dst_core_fiber(DstArgs args) {
DstFiber *fiber; DstFiber *fiber;
if (args.n < 1) return dst_throw(args, "expected at least one argument"); if (args.n < 1) return dst_throw(args, "expected at least 1 argument");
if (!dst_checktype(args.v[0], DST_FUNCTION)) if (!dst_checktype(args.v[0], DST_FUNCTION))
return dst_throw(args, "expected a function"); return dst_throw(args, "expected a function");
fiber = dst_fiber(dst_unwrap_function(args.v[0]), 64); fiber = dst_fiber(dst_unwrap_function(args.v[0]), 64);

View File

@ -13,7 +13,7 @@
(print " -r Enter the repl after running all scripts") (print " -r Enter the repl after running all scripts")
(exit 0)) (exit 0))
"v" (fn [] (print VERSION) (exit 0)) "v" (fn [] (print VERSION) (exit 0))
"r" (fn [] (varset! should-repl true)) "r" (fn [] (:= should-repl true))
}) })
(defn dohandler [n] (defn dohandler [n]
@ -27,7 +27,7 @@
(if (= "-" (string-slice arg 0 1)) (if (= "-" (string-slice arg 0 1))
(dohandler (string-slice arg 1 2)) (dohandler (string-slice arg 1 2))
(do (do
(varset! no-file false) (:= no-file false)
(import arg)))) (import arg))))
(when (or should-repl no-file) (when (or should-repl no-file)