From 4a76f2ae32836415b23feb8f5b1e80bf4cec86e0 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 16 Mar 2018 18:15:34 -0400 Subject: [PATCH] Replace varset! with algol style := --- README.md | 4 ++++ examples/lazyseqs.dst | 5 +++-- src/compiler/boot.dst | 32 ++++++++++++++++---------------- src/compiler/specials.c | 2 +- src/core/corelib.c | 2 +- src/mainclient/init.dst | 4 ++-- 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 0be7f9cb..98d874f6 100644 --- a/README.md +++ b/README.md @@ -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), 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 * First class closures diff --git a/examples/lazyseqs.dst b/examples/lazyseqs.dst index 5d7052ee..026ec48b 100644 --- a/examples/lazyseqs.dst +++ b/examples/lazyseqs.dst @@ -11,10 +11,11 @@ state (do (def n (f)) - (varset! state n) - (varset! loaded true) + (:= state n) + (:= loaded true) n)))) +# This creates one more closure than necessary but oh well (defmacro delay "Macro for lazy evaluation" [& forms] (tuple mem0 (apply1 tuple (array-concat ['fn []] forms)))) diff --git a/src/compiler/boot.dst b/src/compiler/boot.dst index 298273d0..8b3bec09 100644 --- a/src/compiler/boot.dst +++ b/src/compiler/boot.dst @@ -139,7 +139,7 @@ If no match is found, returns nil" :more (fn [] (< i len)) :next (fn [] (def ret (get x i)) - (varset! i (+ i 1)) + (:= i (+ i 1)) ret) }) (def iters { @@ -156,7 +156,7 @@ If no match is found, returns nil" :more (fn [] (< i top)) :next (fn [] (def ret i) - (varset! i (+ i 1)) + (:= i (+ i 1)) ret) }) @@ -199,9 +199,9 @@ If no match is found, returns nil" (do (def n (next)) (if (pred n) n (nextgood))) - (varset! alive false)))) - (defn nnext [] (def ret temp) (varset! temp (nextgood)) ret) - (defn nmore [] (when isnew (varset! isnew false) (nnext)) alive) + (:= alive false)))) + (defn nnext [] (def ret temp) (:= temp (nextgood)) ret) + (defn nmore [] (when isnew (:= isnew false) (nnext)) alive) {:more nmore :next nnext}) (defmacro let [bindings & body] @@ -213,7 +213,7 @@ If no match is found, returns nil" (array-push accum (tuple 'def (get head i) (get head (+ 1 i)))) - (varset! i (+ i 2))) + (:= i (+ i 2))) (array-push accum (tuple-prepend body 'do)) (apply1 tuple accum)) @@ -223,7 +223,7 @@ If no match is found, returns nil" :more (fn [] lastkey) :next (fn [] (def ret (tuple lastkey (get x lastkey))) - (varset! lastkey (next x lastkey)) + (:= lastkey (next x lastkey)) ret) }) @@ -233,7 +233,7 @@ If no match is found, returns nil" :more (fn [] lastkey) :next (fn [] (def ret lastkey) - (varset! lastkey (next x lastkey)) + (:= lastkey (next x lastkey)) ret) }) @@ -243,7 +243,7 @@ If no match is found, returns nil" :more (fn [] lastkey) :next (fn [] (def ret (get x lastkey)) - (varset! lastkey (next x lastkey)) + (:= lastkey (next x lastkey)) ret) }) @@ -260,7 +260,7 @@ If no match is found, returns nil" (tuple 'def endsym end) (tuple 'while (tuple '< sym endsym) (tuple-prepend body 'do) - (tuple 'varset! sym (tuple '+ sym inc))))) + (tuple ':= sym (tuple '+ sym inc))))) (defmacro -> [x & forms] @@ -311,7 +311,7 @@ If no match is found, returns nil" (pp seen buf k) (buffer-push-string buf " ") (pp seen buf v) - (varset! k (next a k)) + (:= k (next a k)) (when k (buffer-push-string buf " "))) (buffer-push-string buf end))) buf) @@ -369,7 +369,7 @@ onvalue." (while (< 0 len) (buffer-clear buf) (chunks buf) - (varset! len (length buf)) + (:= len (length buf)) (for [i 0 len] (yield (get buf i)))) 0))) @@ -385,17 +385,17 @@ onvalue." (select (fiber-status chars) :new (parser-byte p (resume chars)) :pending (parser-byte p (resume chars)) - (varset! going false)))) + (:= going false)))) (when (not= :root (parser-status p)) (onerr "parse" "unexpected end of source")) nil))) (defn more [] (if temp true (do - (varset! temp true) - (varset! tempval (resume f)) + (:= temp true) + (:= tempval (resume f)) going))) (defn next [] (if temp - (do (varset! temp nil) tempval) + (do (:= temp nil) tempval) (resume f))) {:more more :next next}) (fn [env chunks onvalue onerr] diff --git a/src/compiler/specials.c b/src/compiler/specials.c index 55c19eaa..f65955ed 100644 --- a/src/compiler/specials.c +++ b/src/compiler/specials.c @@ -615,6 +615,7 @@ DstSlot dstc_fn(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) { /* Keep in lexographic order */ static const DstSpecial dstc_specials[] = { + {":=", dstc_varset}, {"ast-quote", dstc_astquote}, {"def", dstc_def}, {"do", dstc_do}, @@ -622,7 +623,6 @@ static const DstSpecial dstc_specials[] = { {"if", dstc_if}, {"quote", dstc_quote}, {"var", dstc_var}, - {"varset!", dstc_varset}, {"while", dstc_while} }; diff --git a/src/core/corelib.c b/src/core/corelib.c index 688bbf3d..190de25c 100644 --- a/src/core/corelib.c +++ b/src/core/corelib.c @@ -136,7 +136,7 @@ int dst_core_struct(DstArgs args) { int dst_core_fiber(DstArgs args) { 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)) return dst_throw(args, "expected a function"); fiber = dst_fiber(dst_unwrap_function(args.v[0]), 64); diff --git a/src/mainclient/init.dst b/src/mainclient/init.dst index d43957a3..34c3782a 100644 --- a/src/mainclient/init.dst +++ b/src/mainclient/init.dst @@ -13,7 +13,7 @@ (print " -r Enter the repl after running all scripts") (exit 0)) "v" (fn [] (print VERSION) (exit 0)) - "r" (fn [] (varset! should-repl true)) + "r" (fn [] (:= should-repl true)) }) (defn dohandler [n] @@ -27,7 +27,7 @@ (if (= "-" (string-slice arg 0 1)) (dohandler (string-slice arg 1 2)) (do - (varset! no-file false) + (:= no-file false) (import arg)))) (when (or should-repl no-file)