Setting variables by reference, using @ works now.

This commit is contained in:
Erik Svedäng 2018-02-03 07:50:34 +01:00
parent 06248af2f8
commit c90357ac6d
4 changed files with 28 additions and 18 deletions

View File

@ -0,0 +1,25 @@
(defn changing-target-of-ref []
(let [s1 @"hello"
s2 @"goodbye"
r &s1]
(do (set! r &s2)
(IO.println r))))
(defn set-derefed []
(let [s @"hello"
r &s]
(do (set! @r @"new")
(IO.println &s))))
(defn f [r]
(set! @r 100))
(defn set-in-parameter []
(let [x 1]
(do (f &x)
(IO.println &(str x)))))
(defn-do main []
(changing-target-of-ref)
(set-derefed)
(set-in-parameter))

View File

@ -8,20 +8,3 @@
;; (Bucket.init 0 [])) ;; (Bucket.init 0 []))
;; (defn f [] ;; (defn f []
;; (the (Bucket Float Bool) (empty))) ;; run checks in the concretizer to fix this ;; (the (Bucket Float Bool) (empty))) ;; run checks in the concretizer to fix this
(defn changing-target-of-ref []
(let [s1 @"hello"
s2 @"goodbye"
r &s1]
(do (set! r &s2)
(IO.println r))))
(defn set-derefed []
(let [s @"hello"
r &s]
(do (set! @r @"new")
(IO.println &s))))
(defn main []
(set-derefed))

View File

@ -13,6 +13,7 @@ carp ./examples/updating.carp -x;
carp ./examples/sorting.carp -x; carp ./examples/sorting.carp -x;
carp ./examples/globals.carp -x --log-memory; carp ./examples/globals.carp -x --log-memory;
carp ./examples/generic_structs.carp -x; carp ./examples/generic_structs.carp -x;
carp ./examples/setting_variables.carp -x;
# Actual tests (using the test suite) # Actual tests (using the test suite)
carp ./test/memory.carp -x --log-memory; carp ./test/memory.carp -x --log-memory;

View File

@ -487,7 +487,8 @@ manageMemory typeEnv globalEnv root =
let varInfo = info variable let varInfo = info variable
correctVariable = case variable of correctVariable = case variable of
XObj (Lst (XObj Ref _ _ : x : _)) _ _ -> x -- Peek inside the ref to get the actual variable to set --XObj (Lst (XObj Ref _ _ : x : _)) _ _ -> x -- Peek inside the ref to get the actual variable to set
(XObj (Lst (XObj (Sym (SymPath _ "copy") _) _ _ : symObj@(XObj (Sym sym _) _ _) : _)) _ _) -> symObj
x -> x x -> x
MemState managed deps <- get MemState managed deps <- get