From fa5e298d94806491b22493f50ba0e1ce4212193f Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Tue, 4 Jun 2024 16:57:32 -0400 Subject: [PATCH] ref -> box; fix box representations; add ds modifying functions to base --- janet/base.janet | 51 +++++++++++++++++++++++++++++++---------- janet/interpreter.janet | 8 +++---- janet/scanner.janet | 2 +- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/janet/base.janet b/janet/base.janet index 98cc9e5..22e7472 100644 --- a/janet/base.janet +++ b/janet/base.janet @@ -68,7 +68,7 @@ :list (string "[" (stringify x) "]") :dict (string "#{" (stringify x) "}") :set (string "${" (stringify x) "}") - :ref (string "ref " (x :name) "{" (x :value) "}") + :ref (string "box " (x :name) " [ " (stringify x) " ]") :pkg (show-pkg x) (stringify x))) @@ -97,14 +97,25 @@ (print (string/join patterns " ")) (print doc)) +(defn- conj!-set [sett value] + (set (sett value) true) + sett) + (defn- conj-set [sett value] (def new (merge sett)) - (set (new value) true) - new) + (conj!-set new value)) + +(defn- conj!-list [list value] + (array/push list value)) (defn- conj-list [list value] (def new (array/slice list)) - (array/push new value)) + (conj!-list new value)) + +(defn conj! [x value] + (case (ludus/type x) + :list (conj!-list x value) + :set (conj!-set x value))) (defn conj [x value] (case (ludus/type x) @@ -112,14 +123,26 @@ :set (conj-set x value) (error (string "cannot conj onto " (show x))))) +(defn disj! [sett value] + (set (sett value) nil) + sett) + (defn disj [sett value] (def new (merge sett)) (set (new value) nil) new) +(defn assoc! [dict key value] + (set (dict key) value) + dict) + (defn assoc [dict key value] (merge dict {key value})) +(defn dissoc! [dict key] + (set (dict key) nil) + dict) + (defn dissoc [dict key] (def new (merge dict)) (set (new key) nil) @@ -147,15 +170,15 @@ (pp x) x) -(defn concat [x y] +(defn concat [x y & zs] (case (ludus/type x) - :string (string x y) - :list (array/concat @[] x y) - :set (merge x y))) + :string (string x y ;zs) + :list (array/concat @[] x y ;zs) + :set (merge x y ;zs))) -(defn deref [x] (get x :value)) +(defn unbox [x] (get x :^value)) -(defn set! [x] (set (x :value) x)) +(defn store! [x] (set (x :^value) x)) (def ctx { "print!" print! @@ -182,10 +205,14 @@ "doc!" doc! "concat" concat "conj" conj + "conj!" conj! "disj" disj + "disj!" disj! "push" array/push "assoc" assoc + "assoc!" assoc! "dissoc" dissoc + "dissoc!" dissoc! "get" ludus/get "nth" ludus/get "first" first @@ -205,8 +232,8 @@ "ceil" math/ceil "round" math/round "range" range - "deref" deref - "set!" set! + "unbox" unbox + "store!" store! }) (def base (let [b @{}] diff --git a/janet/interpreter.janet b/janet/interpreter.janet index 0e3d61a..3e94d15 100644 --- a/janet/interpreter.janet +++ b/janet/interpreter.janet @@ -340,8 +340,9 @@ (defn- ref [ast ctx] (def {:data value-ast :name name} ast) (def value (interpret value-ast ctx)) - (set (ctx name) @{:^type :ref :^value value :^name name}) - value) + (def box @{:^type :ref :^value value :name name}) + (set (ctx name) box) + box) (defn- repeatt [ast ctx] (def [times-ast body] (ast :data)) @@ -633,8 +634,7 @@ (do (set source ` -fn myadd (x, y) -> add (x, y) -myadd (1, _) (2) +box foo = :bar `) (def result (run)) (b/show result) diff --git a/janet/scanner.janet b/janet/scanner.janet index 5eaf85d..a324fbf 100644 --- a/janet/scanner.janet +++ b/janet/scanner.janet @@ -2,6 +2,7 @@ "List of Ludus reserved words." ## see ludus-spec repo for more info {"as" :as ## impl + "box" :ref "do" :do ## impl "else" :else ## impl "false" :false ## impl -> literal word @@ -16,7 +17,6 @@ "panic!" :panic ## impl (should _not_ be a function) "pkg" :pkg "recur" :recur ## impl - "ref" :ref ## impl "then" :then ## impl "true" :true ## impl -> literal word "use" :use ## wip