diff --git a/lisp/calculate_lifetimes.carp b/lisp/calculate_lifetimes.carp index f87be974..71239538 100644 --- a/lisp/calculate_lifetimes.carp +++ b/lisp/calculate_lifetimes.carp @@ -83,7 +83,9 @@ {:ast ast-final :vars '()}) - :literal (let [vars-after (cons {:name (:result-name ast) :type (:type ast)} vars) + :literal (let [vars-after (if (managed-type? (:type ast)) + (cons {:name (:result-name ast) :type (:type ast)} vars) + vars) ;;_ (println (str "vars-after literal " (:value ast) ": " vars-after)) ] {:ast ast @@ -105,12 +107,24 @@ ] final-ast) + :binop (let [left (calculate-lifetimes-internal {:ast (:a ast) :vars vars} in-ref) + vars-after-left (:vars left) + right (calculate-lifetimes-internal {:ast (:b ast) :vars vars-after-left} in-ref) + vars-after-right (:vars right) + ast0 (assoc ast :a (:ast left)) + ast1 (assoc ast0 :b (:ast right))] + {:ast ast1 + :vars vars-after-right}) + :app (let [tail (:tail ast) init-data (assoc data :pos 0) parameter-types (get-in ast '(:head :type 1)) data-after (reduce (fn (d a) (calc-lifetime-for-arg d parameter-types a)) init-data tail) vars-after (:vars data-after) - vars-after-with-ret-val (cons {:name (:result-name ast) :type (get-in ast '(:head :type 2))} vars-after) + ret-type (get-in ast '(:head :type 2)) + vars-after-with-ret-val (if (managed-type? ret-type) + (cons {:name (:result-name ast) :type ret-type} vars-after) + vars-after) ast-after (:ast data-after)] (do ;;(println (str "APP VARS AFTER\n" vars-after)) diff --git a/lisp/compiler_tests.carp b/lisp/compiler_tests.carp index 8cb13ed3..c9068b81 100644 --- a/lisp/compiler_tests.carp +++ b/lisp/compiler_tests.carp @@ -362,8 +362,8 @@ (def own-asta-10 (annotate-ast own-ast-10)) (let [free (:free own-asta-10)] (do (assert-eq '(:arrow ((:ref :string)) :string) (:type own-asta-10)) - (assert-eq 0 (count free)) (bake own-if-10) + (assert-eq 0 (count free)) (assert-eq (own-if-10 "hej") "short") (assert-eq (own-if-10 "hejsansvejsan") "long"))) @@ -376,10 +376,10 @@ (def own-asta-11 (annotate-ast own-ast-11)) (let [free (:free own-asta-11)] (do (assert-eq '(:arrow (:string) :string) (:type own-asta-11)) + (bake own-if-11) (assert-eq 1 (count free)) (assert-eq "s" (:name (first free))) (assert-eq :string (:type (first free))) - (bake own-if-11) (assert-eq (own-if-11 "hej") "short") (assert-eq (own-if-11 "hejsansvejsan") "long"))) @@ -415,3 +415,17 @@ (assert-eq 2 (count free-right)) (assert-eq (own-if-13 10) "a!") (assert-eq (own-if-13 11) "b!"))) + + + +(defn own-binop-14 () + (+ (strlen "say") (strlen "what"))) + +(def own-ast-14 (lambda-to-ast (code own-binop-14))) +(def own-con-14 (gencon own-ast-14)) +(def own-asta-14 (annotate-ast own-ast-14)) +(let [free (:free own-asta-14)] + (do (assert-eq '(:arrow () :int) (:type own-asta-14)) + (bake own-binop-14) + (assert-eq 2 (count free)) + (assert-eq (own-binop-14) 7)))