Variable value
Author |
Message |
Raknarg
|
Posted: Sun Apr 15, 2012 3:40 pm Post subject: Variable value |
|
|
Is there a way to have an if statement seeing if a variable has no value?
Turing: |
if has_value (x) = false then
do_something ()
end if
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
[Gandalf]
|
Posted: Sun Apr 15, 2012 7:28 pm Post subject: Re: Variable value |
|
|
As far as I remember, Turing doesn't default initialize variables. So it's more of a design issue, since trying to reference an uninitialized variable will fail at compile time. Usually you would initialize your value x to some sentinel value and check for that...
Turing: | var x : int := -1
if x = -1 then
do_something ()
end if |
The key here is that -1 has no meaning for "normal" values of x. If the use case isn't trivial, you can consider making x an object, and keep track of its initialization state in a member variable. |
|
|
|
|
|
Raknarg
|
Posted: Sun Apr 15, 2012 8:01 pm Post subject: RE:Variable value |
|
|
Thats true, but the way my program wad weird, it was hard to reference specific variables. Linked lists are weird I got around it though. |
|
|
|
|
|
Dreadnought
|
Posted: Sun Apr 15, 2012 10:24 pm Post subject: Re: Variable value |
|
|
It is possible to detect if a variable is initialized or not. Turing saves one bit representation in each type for this, we just have to find out what it is and get Turing to check it. Here's a way of doing it for the more common built-in types:
Turing: | const strUnInit := 16#80
const intUnInit := 16#80000000
const realPartUnInit := 16#80000000
const boolUnInit := 16#FF
/* The char type does not have an uninitialized value,
When declared, a char is given the value of the zero
character chr(0) or '/0'
*/
var a : char % := '\0'
var b : string
if #b = strUnInit then
put "String named 'b' is not initialized"
else
put "String named 'b' is initialized"
end if
var c : int
if #c = intUnInit then
put "Integer named 'c' is not initialized"
else
put "Integer named 'c' is initialized"
end if
var d : real
if nat4@ (addr(d )) = realPartUnInit and nat4@ (addr(d )+ (sizeof(real) div 2)) = realPartUnInit then
put "Real named 'd' is not initialized"
else
put "Real named 'd' is initialized"
end if
var f : boolean
if #f = boolUnInit then
put "Boolean named 'f' is not initialized"
else
put "Boolean named 'f' is initialized"
end if |
If you find any bugs let me know.
If you want to try stuff out yourself, here's a function that will print 32 bits.
Turing: | % Calls to printBits should be made with a # in front of the argument
% Example: printBits(#arg)
% The # is a cheat to nat
proc printBits (N : nat4)
for decreasing i : 31 .. 0
put sign (N & (1 shl i )) ..
end for
put ""
end printBits |
Hope, this helps. |
|
|
|
|
|
Dreadnought
|
Posted: Mon Apr 16, 2012 12:05 am Post subject: Re: Variable value |
|
|
I'd like to add a check for pointers (might be helpful for lists).
Turing: | const ptrUnInit := 16#FFFFFFFF
var g : ^ anyclass
if #g = ptrUnInit then
put "Pointer named 'g' is not initialized"
else
put "Pointer named 'g' is initialized"
end if |
|
|
|
|
|
|
Tony
|
Posted: Mon Apr 16, 2012 12:10 am Post subject: RE:Variable value |
|
|
pointers should be initialized (manually, if needs be) to null (which is 0). Null-Pointer Exceptions are much much much easier to figure out and solve, than random segfaults where the program just grabs a random piece of memory and runs with it as if it was a valid object, until something else breaks. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
|
|