ref -> box; fix box representations; add ds modifying functions to base

This commit is contained in:
Scott Richmond 2024-06-04 16:57:32 -04:00
parent 6cc7f045a2
commit fa5e298d94
3 changed files with 44 additions and 17 deletions

View File

@ -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 @{}]

View File

@ -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)

View File

@ -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