BUG FIX: Must handle deletion of temporary values used during initialization of global variables.

This commit is contained in:
Erik Svedäng 2018-02-06 08:21:50 +01:00
parent e8ac5c86c6
commit d23a211985
7 changed files with 38 additions and 8 deletions

View File

@ -29,4 +29,11 @@
(IO.println &(str &x))
x))
;; TODO: Should be a macro and provide the location of the assertion.
;; Also its output looks weird because of lack of 'prn'.
(defn assert-eq [expected got]
(if (= expected got)
()
(println* "ASSERTION FAILED! Expected " expected " but got " got)))
)

View File

@ -6,7 +6,6 @@
* 1.0 - The completed version of the language with all planned features and extra nice ergonomics.
## Critical Bugs
* [0.3] Must handle deletion of temporary values used during initialization of global variables.
* [0.3] References must keep track of their origin and prevent usage of them if the origin has been given away.
## Big Language Features

View File

@ -29,3 +29,10 @@
;; r &s]
;; (do (String.delete s)
;; (IO.println r))))
(use Array)
(def g (copy (nth &[@"A" @"B" @"C"] 1)))
(defn main []
(IO.println &g))

View File

@ -16,14 +16,9 @@ carp ./examples/generic_structs.carp -x;
carp ./examples/setting_variables.carp -x;
# Actual tests (using the test suite)
carp ./test/memory.carp -x --log-memory;
echo
for f in ./test/*.carp; do
if [ $f != "./test/memory.carp" ]; then
echo $f
carp -x $f
fi
echo $f
carp -x --log-memory $f
echo
done

View File

@ -460,6 +460,16 @@ manageMemory typeEnv globalEnv root =
do okBody <- visitedBody
return (XObj (Lst [defn, nameSymbol, args, okBody]) i t)
[def@(XObj Def _ _), nameSymbol@(XObj (Sym _ _) _ _), expr] ->
do visitedExpr <- visit expr
result <- unmanage expr
return $
case result of
Left e -> Left e
Right () ->
do okExpr <- visitedExpr
return (XObj (Lst [def, nameSymbol, okExpr]) i t)
[letExpr@(XObj Let _ _), XObj (Arr bindings) bindi bindt, body] ->
let Just letReturnType = t
in case letReturnType of

View File

@ -169,6 +169,7 @@ toC toCMode root = emitterSrc (execState (visit startingIndent root) (EmitterSta
let innerIndent = indent + indentAmount
ret <- visit innerIndent expr
appendToSrc (addIndent innerIndent ++ pathToC path ++ " = " ++ ret ++ ";\n")
delete innerIndent i
appendToSrc (addIndent indent ++ "}\n")
return ""

11
test/init_global.carp Normal file
View File

@ -0,0 +1,11 @@
;; This test is special because it requires it's own run.
;; It initializes a global with a something that needs to allocate memory and call functions,
;; then checks that the memory balance is correct after the init step.
(load "Debug.carp")
(def g (copy (Array.nth &[@"A" @"B" @"C"] 1)))
;; The one allocation left after 'carp_init_globals' should be 'g' itself:
(defn main []
(Debug.assert-eq 1l (Debug.memory-balance)))