|Newbie questions about Pliant
Pointer, Address, Link, Arrow
|Message posted by johan.boule on 2001/01/26 22:59:13
|The following pliant programm raise an exception...
gvar Int a
gvar Pointer:(Int) b
gvar Pointer:(Pointer:(Int)) c
gvar Pointer:(Pointer:(Pointer:(Int))) d
d :>> c
c :>> b
b :> a
a := 1
console a" "b" "c" "d eol
|Message posted by hubert.tonneau on 2001/01/27 00:13:28
|When you specify
d :>> c
you say to set the level 2 pointer pointed by 'd' to the same value as
the level 2 pointer 'c'.
But 'd' is not pointing anything yet, so you can crash.
Maybe you through ':>>' means do not dereference, but it simply keeps
the two last dereferencing levels.
You would need a ':>>>' instruction to get 'd' level 3 pointer to map
'c' level 2 pointer, but I did not define it because it's so rare
... and unreadable.
Anyway, if you really want 'd' level 3 pointer to point 'c' level 2 pointer,
you can still use the following:
(addressof Pointer:(Pointer Pointer:Int) d) map Address := addressof (Pointer Pointer:Int) c
If you are completely lost, let's try to make it a bit simpler through defining
a few constants that will than make the instruction shorter:
constant P1 Pointer:Int
constant P2 Pointer:P1
constant P3 Pointer:P2
Now the instruction can be:
(addressof P3 d) map Address := addressof P2 c
which says, get the address of 'd' as a level 3 pointer, and assume that it is
mapping an Address (a pointer is always an address in facts), and set this
address with the address of 'c' as a level 2 pointer.
|Message posted by maybe Johan Boulé on 2001/01/27 00:44:52
|Well, I'll have to think about that a long time before getting used to it.
Surprisingly, I feel the Pliant syntax for Pointers is even harder than the C one !
I thought that the ':>' and ':>>' metas were defined recursively until the left member's Type shall match the rigth member's one...
define p ':>' v
if "type pointed by p" = "type of v"
"address pointed by p" := addressof v
eif "p is a Pointer to another Pointer"
"address pointed by p" :> v
return # different types...
but with this implementation, there would be no diference between :> and :>>
What surprised me is that:
gvar Int i
gvar Pointer:Int j k
j :> i
k :> j
leads to different live code: (C syntax)
j = &i
k = j
But maybe it's the same live code generation,
since k = j could be implemented as k = &(*j) (pointers automatically dereferenced)?
|Message posted by hubert.tonneau on 2001/01/27 00:56:54
|Exactly: the default behaviour is automatic pointer dereferencing, and
believe me, it's the good one, but you might need a bit of time to get
used to it.
You can think about it the other way round:
In Pliant, when you use ':=' you are copying datas,
and when you use ':>' you are setting a pointer.
|Message posted by pom on 2001/01/27 02:22:08
|The fundamental difference with C is that you go from the object.
You may see a Pointer like a C reference which you may change.
gvar Pointer:Int p :> q means int &p = q, which
means you refer the int value, independently of the fact that
q shall be an int or an int &.
Hence, Pointer:Int is something like int & and
Pointer Pointer:Int something like (int &) & if such a
thing could exist in C.
I will try to make all this stuff a bit more clear for the talk at ASPIC
(where I hope you could come) and after I will try to have a "publishable"
version for Pliant Journal.