Skip to content

Commit

Permalink
Explicitly move in set for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Oct 1, 2023
1 parent 1973bf3 commit 5464ed6
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 71 deletions.
2 changes: 1 addition & 1 deletion lib/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ sexp_cond:
{ Lambda ($loc, params, body) }

%inline sexp_field_set:
| Set; Ampersand; var = sexp_expr; value = sexp_expr
| Set; Ampersand; var = sexp_expr; Exclamation; value = sexp_expr
{ Set ($loc, ($loc(var), var), value) }

%inline sexp_field_get:
Expand Down
6 changes: 5 additions & 1 deletion lib/syntax_errors.messages
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ prog: Lbrack False Rpar

prog: Lpar Set Ampersand False Wildcard

Expecting '!' to indicate move of to-set expression

prog: Lpar Set Ampersand False Exclamation Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Lpar Set Ampersand False False Wildcard
prog: Lpar Set Ampersand False Exclamation False Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down
8 changes: 4 additions & 4 deletions std/hashtbl.smu
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@
(prelude/iter-range 0 size
(fn [i] (__unsafe_ptr_set &(array/data data) i !#empty)))
(def old-data& !tbl.data)
(set &tbl.data data)
(set &tbl.nitems 0)
(set &tbl.data !data)
(set &tbl.nitems !0)
(iter-data-move &old-data (fn [key! value!] (insert &tbl !key !value))))

(defn insert [tbl& key! value!]
(if (>. (load-factor tbl) load-limit)
(grow &tbl))
(let [idx (probe-linear tbl key true)]
(set &tbl.data.[idx] (#item {:key :value}))
(set &tbl.nitems (+ 1 tbl.nitems)))))
(set &tbl.data.[idx] !(#item {:key :value}))
(set &tbl.nitems !(+ 1 tbl.nitems)))))

(defn find [tbl key]
(let [idx (probe-linear tbl key false)]
Expand Down
2 changes: 1 addition & 1 deletion test/functions.t/poly_fn_ret_fn.smu
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(defn black-box [f g]
(if once
(do (set &once false) (copy f))
(do (set &once !false) (copy f))
(copy g)))

(def foo (fn [a] (print (fmt-str a " foo"))))
Expand Down
8 changes: 4 additions & 4 deletions test/functions.t/tailrec_mutable.smu
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

(defn mut-bref [i rf&]
(if (> i 0)
(set &(.a rf) true)
(set &(.a rf) !true)
(mut-bref (+ i 1) &rf)))

(defn dontmut-bref [i rf&]
(if (> i 0)
(set &(.a rf) false)
(set &(.a rf) !false)
-- introduce a new object, so the old one won't get mutated
(do
(def rf2& {:a true})
Expand All @@ -30,7 +30,7 @@

(defn mod-rec [r& i]
(if (= i 2)
(set &(.a r) i)
(set &(.a r) !i)
(mod-rec &r (+ i 1))))

(let [ar& {:a 20}]
Expand All @@ -52,7 +52,7 @@
-- int
(defn change-int [i& j]
(if (= j 100)
(set &i j)
(set &i !j)
(change-int &i (+ j 1))))

(let [i& 0]
Expand Down
8 changes: 4 additions & 4 deletions test/misc.t/array_push.smu
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
(def nested& [[0 1] [2 3]])
(def a [4 5])
(array/push &nested !(copy a))
(set & nested.[1] (copy a))
(set &nested.[1] (copy a))
(set & nested.[1] !(copy a))
(set &nested.[1] !(copy a))
(array/push &nested ![4 5])
(set &nested.[1] [4 5])
(set &nested.[1] [4 5])
(set &nested.[1] ![4 5])
(set &nested.[1] ![4 5])
4 changes: 2 additions & 2 deletions test/misc.t/closure_monomorph.smu
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
-- TODO cannot capture [cmp] here
(if (< (cmp arr.[j] pivot) 0)
(do
(set &i (+ i 1))
(set &i !(+ i 1))
(array/swap-items &arr i j)))))
(def i (+ i 1))
(array/swap-items &arr i hi)
Expand Down Expand Up @@ -43,7 +43,7 @@
-- TODO cannot capture [cmp] here
(if (< (cmp arr.[j] pivot) 0)
(do
(set &i (+ i 1))
(set &i !(+ i 1))
(array/swap-items &arr i j)))))
(def i (+ i 1))
(array/swap-items &arr i hi)
Expand Down
2 changes: 1 addition & 1 deletion test/misc.t/m2.smu
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
(rec
(defn pop [arr&]
(print "pop")
(set &cnt (+ cnt 1))
(set &cnt !(+ cnt 1))
(array/drop-back &arr)
(pop-all &arr))
(defn pop-all [arr&]
Expand Down
3 changes: 1 addition & 2 deletions test/misc.t/partials.smu
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
(defn set-moved ()
(def a& {:a [10] :b [20]})
(ignore {a.a})
(set &a.a [20])
)
(set &a.a ![20]))

(set-moved)
4 changes: 2 additions & 2 deletions test/misc.t/upward_mut.smu
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(defn counter []
(def cnt& 0)
(fn [] (set &cnt (+ cnt 1))
(fn [] (set &cnt !(+ cnt 1))
cnt))

(def c (counter))
Expand All @@ -13,7 +13,7 @@

(defn counter []
(def cnt& {:c 0})
(fn [] (set &cnt {:c (+ (.c cnt) 1)})
(fn [] (set &cnt !{:c (+ (.c cnt) 1)})
cnt))

(def c (counter))
Expand Down
8 changes: 4 additions & 4 deletions test/mutable_value_semantics.t/array_copies.smu
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
(def d b)

-- (set &(array-get a 0) 12)
(set &a.[0] 12)
(set &a.[0] !12)
(print-0th a)
-- (set &(array-get c 0) 15)
(set &c.[0] 15)
(set &c.[0] !15)
(print-0th a)
(print-0th b)
(print-0th c)
Expand All @@ -25,10 +25,10 @@
(def d b)

-- (set &(array-get a 0) 12)
(set &a.[0] 12)
(set &a.[0] !12)
(print-0th a)
-- (set &(array-get c 0) 15)
(set &c.[0] 15)
(set &c.[0] !15)
(print-0th a)
(print-0th b)
(print-0th c)
Expand Down
4 changes: 2 additions & 2 deletions test/mutable_value_semantics.t/array_in_record_copies.smu
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(def a& {:a [10]})
(def b (copy a))
(set &a.a.[0] 12)
(set &a.a.[0] !12)

(defn print-thing [a]
(print (fmt-str (.a a).[0])))
Expand All @@ -15,7 +15,7 @@
(defn in-fun []
(def a& {:a [10]})
(def b (copy a))
(set &a.a.[0] 12)
(set &a.a.[0] !12)

(print-thing a)
(print-thing b))
Expand Down
4 changes: 2 additions & 2 deletions test/mutable_value_semantics.t/const_let.smu
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(def v& [0])
(def const (copy v.[0]))
(set &v.[0] 1)
(set &v.[0] !1)
(print (fmt-str v.[0]))
(print (fmt-str const))

(defn in-fun []
(def v& [0])
(def const (copy v.[0]))
(set &v.[0] 1)
(set &v.[0] !1)
(print (fmt-str v.[0]))
(print (fmt-str const)))

Expand Down
2 changes: 1 addition & 1 deletion test/mutable_value_semantics.t/hidden_match_reference.smu
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
((#some i)
-- set op. Depending on whether i behaves as a reference or value, return 1 or 2
(let [i (copy i)]
(set &op (#some 2))
(set &op !(#some 2))
i))
(#none -1)))

Expand Down
6 changes: 3 additions & 3 deletions test/mutable_value_semantics.t/member_refcounts.smu
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
(type r {:a (array int)})
(def a& [10])
(def r {:a (copy a)})
(set &a.[0] 20)
(set &a.[0] !20)
(print (fmt-str (.a r).[0]))

(def r [(copy a)])
(set &a.[0] 30)
(set &a.[0] !30)
(print (fmt-str r.[0].[0]))

(def r (#some (copy a)))
(set & a.[0] 40)
(set & a.[0] !40)
(match r
((#some a) (print (fmt-str a.[0])))
(#none (print "none")))
2 changes: 1 addition & 1 deletion test/mutable_value_semantics.t/modify_in_fn.smu
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(type f {:a& int})
(defn modify [r&]
(set &(.a r) 30))
(set &(.a r) !30))

(defn mod2 [a&]
(array/push &a !20))
Expand Down
4 changes: 2 additions & 2 deletions test/mutable_value_semantics.t/mut_alias.smu
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(def f {:a 0})
(def fst& (copy f))
(def snd (copy fst))
(set &(.a fst) 1)
(set &(.a fst) !1)
(print (fmt-str (.a fst)))
(print (fmt-str (.a f)))
(print (fmt-str (.a snd)))
Expand All @@ -12,7 +12,7 @@
(def f {:a 0})
(def fst& (copy f))
(def snd (copy fst))
(set &(.a fst) 1)
(set &(.a fst) !1)
(print (fmt-str (.a fst)))
(print (fmt-str (.a f)))
(print (fmt-str (.a snd))))
Expand Down
2 changes: 1 addition & 1 deletion test/mutable_value_semantics.t/nested_array.smu
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
(def a& [[10] [20]])
(def b (copy a))

(set &a.[0].[0] 15)
(set &a.[0].[0] !15)
(prnt a)
(prnt b)
2 changes: 1 addition & 1 deletion test/mutable_value_semantics.t/ref_to_const.smu
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
(defn infunc []
(def base-length 2.0)
(def length& !base-length)
(set &length (+. length 3.0)))
(set &length !(+. length 3.0)))

(ignore infunc)
8 changes: 4 additions & 4 deletions test/mutable_value_semantics.t/simple_set.smu
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(def b& 10)
(set &b 14)
(set &b !14)
(print (fmt-str b))

(defn hmm []
(def b& 10)
(set &b 15)
(set &b !15)
b)
(print (fmt-str (hmm)))

Expand All @@ -13,5 +13,5 @@
(ignore b)

(def c [30])
(set &a.[0] (copy c))
(set &a.[0] [10])
(set &a.[0] !(copy c))
(set &a.[0] ![10])
2 changes: 1 addition & 1 deletion test/mutable_value_semantics.t/unneeded_mut.smu
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(ignore a))

(defn do_mut [a& b]
(set &a (copy b)))
(set &a !(copy b)))

(def b& 0)
(ignore b)
Expand Down
2 changes: 1 addition & 1 deletion test/records.t/nested_prealloc.smu
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
(if (= i 10)
()
(do
(set &(.wrapped test) {:dat (+ (.dat (.wrapped test)) 1) :b 0 :c 0})
(set &(.wrapped test) !{:dat (+ (.dat (.wrapped test)) 1) :b 0 :c 0})
(vector_loop (+ i 1)))))
(vector_loop 0)
(.wrapped test))
Expand Down
Loading

0 comments on commit 5464ed6

Please sign in to comment.