Oxygen Basic

Programming => Bugs & Feature Requests => Topic started by: Aurel on April 05, 2011, 08:03:24 AM

Title: Pointer syntax...
Post by: Aurel on April 05, 2011, 08:03:24 AM
I have small request if you dont mind.
Charles i know that FB is tricky in some things but sintax of pointer referencing is
little bit weird.
dim a as long
& p = & a
why is needed again to type & sign as pointed again.
I know that you have reason for this but looks quite weird, it looks like
pointer p is pointed to pointer a.
I hope that you understand me and that i dont need something unusual.

all best
Aurel
Title: Re: Pointer syntax...
Post by: Charles Pegge on April 05, 2011, 08:31:29 AM
Hi Aurel,

The principle is that once an indirect variable is provided with an address, it is treated just like a normal variable. This is exactly how byref parameters work within functions for most basics.

normal variables have a fixed address calculated ar compile time. but these pointered variables have an address which can be altered while the program is running. I've extended the meaning of "&" so it can be used on the left hand side for an address assignment.

I know this is a novelty :) but it keeps the syntax clean further down the line by avoiding explicit pointer expressions.

Charles

PS: the '&' operator also works with function addresses so you can declare a function then assign an address to it later. (loading DLLs at run time - often known as late binding)
Title: Re: Pointer syntax...
Post by: Aurel on April 16, 2011, 09:29:24 AM
Weird to me , i think that more logical will be:
Code: [Select]
Pointer p1,p2
Long a,b,c,d
a=5
b=2
c=a+b
&p1 = c 'reference pointer
print @p1    'dereference pointer
d=a-b
&p2 = d
print @p2

Just a idea ,nothing else...
If i ever add pointer in ABasic i will use something similiar
Title: Re: Pointer syntax...
Post by: Charles Pegge on April 16, 2011, 12:17:23 PM
Hi Aurel,

Avoiding explicit pointers is a core feature of Oxygen. This applies uniformly to objects, functions and variables. Also to COM objects. Once an indirect variable is given an address, no further pointer notation is required.

However, for the purposes of reading C code directly I'm considering whether to create a "#Cpointer" directive. But I would prefer to avoid it.

Charles
Title: Re: Pointer syntax...
Post by: Aurel on April 16, 2011, 01:14:08 PM
Charles...
Dont take my ideas to serius ,it seems that im to used to EBasic syntax  ::)
I like the way you developing Oxygen Basic. ;)
Title: Re: Pointer syntax...
Post by: kryton9 on June 11, 2011, 02:50:48 AM
I was playing around with pointers in oxygen tonight and thought this might be helpful to others.
Code: OxygenBasic
  1. dim pa as long ptr
  2. dim a, ap  as long
  3.  
  4. a=42
  5.  
  6. &pa= &a 'pa becomes a reference of a
  7.  
  8. print "address of a: " + str(@a) + "    pa: " + str(@pa)
  9. print "value of a: " + str(a) + "    pa: " + str(pa)
  10.  
  11. print "a + 10 = " + str(a + 10) + "    pa + 10 = " + str(pa + 10)
  12.  
  13. print "*&pa " + str(*&pa) + "     is the same as @pa " + str(@pa)
  14.  
  15. ap = &a 'ap = the address of a"
  16. print "address of a stored in ap " + str(ap)
  17.  
  18. ap = @a 'another way to say ap = the address of a
  19. print "address of a stored in ap " + str(ap)
  20.  
  21. print "the value at ap " + str(*ap)
  22.  
  23. 'so when you see &a or @a you read that as: The address of a
  24. 'when you see *ap it is the value at ap
  25.  
  26. 'when you use references, &pa = &a, then pa refers directly to the value of a
  27. 'again this can be written as &pa = @a also
Title: Re: Pointer syntax...
Post by: o2admin on June 11, 2011, 05:54:40 AM
Thanks Kent,

I've switched on the o2 syntax :)

Charles
Title: Re: Pointer syntax...
Post by: JRS on June 11, 2011, 07:17:08 AM
Quote
I've switched on the o2 syntax

That is good news Charles. I'm glad you nailed it.
Title: Re: Pointer syntax...
Post by: kryton9 on June 11, 2011, 03:06:11 PM
John and Charles, wow thanks so much the new highlighter is AWESOME!!

Peter, yes pointer use in Oxygen is very easy. I just wanted to show that if
you wanted for whatever reason to do all of those different things how they would be coded as a reference.

The easiest is how Charles has in his VarPointer.o2bas example code.

Code: OxygenBasic
  1. dim pa as long ptr
  2. dim a as long
  3.  
  4. a=42
  5.  
  6. &pa= @a 'pa becomes a reference of a
  7.  
  8. print pa
  9.  
Title: Re: Pointer syntax...
Post by: Aurel on June 11, 2011, 03:22:40 PM
Maby for you...heh
I like this way:
Code: [Select]
sys pa
int a

a=42

&pa= a 'pa becomes a reference of a

print pa
Title: Re: Pointer syntax...
Post by: kryton9 on June 11, 2011, 04:19:50 PM
That is nice Aurel, another way to do it that I didn't know about.
Title: Re: Pointer syntax...
Post by: Charles Pegge on June 11, 2011, 09:37:30 PM
Well almost:

pa needs to be defined as a pointer

sys * pa

Code: OxygenBasic
  1. sys *pa
  2. sys a
  3. a=42
  4. @pa=@a
  5. print "Assigned address " @pa "  value " pa
  6.  

'@' is safer to use than '&' because of the &h.. hexadecimal number ambiguity.

But PowerBasic users may get confused by '@' because they use it to mean '*' :)

Charles
Title: Re: Pointer syntax...
Post by: kryton9 on June 11, 2011, 10:30:09 PM
So the @ is the reference operator?

By the way guys... to get syntax highlighting to work on your code. In your code block instead of (code)  change it to (code=o2).  Use [] instead of (), I had to use () to get it to post correctly.
I really like this new highlighting system.  Thanks alot!!!
Title: Re: Pointer syntax...
Post by: Charles Pegge on June 12, 2011, 05:36:45 AM
All parameters passed byref in a Basic function are pointered variables. But most programmers are not aware of it. Oxygen uses exactly the same mechanism to handle other pointered variables.

When passing addresses of variables to assembler, you will need to
assign the address to a direct variable first.


p=@v
mov ecx,p


You can inspect code generated by the compiler in Scite like this:

###
code window..
###

Press ctrl-F7 and see the results in the lower panel

Code: OxygenBasic
  1. ###
  2. sys v=42
  3. sys *p
  4. @p=@v
  5. ###
  6.  
  7. print p
  8.  


Code: [Select]
>"C:\cevp\projects\opcode\OxygenBasic\gxo2" " -a -c -m C:\cevp\projects\opcode\OxygenBasic\t.o2bas"
                                '  ' 7
 C7 83 00 10 00 00 2A 00 00 00  '  mov [ebx+4096]  ,42
                                '  ' 8
                                '  ' 9
 8D BB 00 10 00 00              '  lea edi,[ebx+4096]
 8B C7                          '  mov eax,edi
 8D BB 04 10 00 00              '  lea edi,[ebx+4100]
 89 07                          '  mov [edi] ,eax
                                '  ' 10
      


Okay

>Exit code: 0

Charles
Title: Re: Pointer syntax...
Post by: Aurel on June 12, 2011, 12:50:36 PM
Hmm with integer work but why next code not work?
Code: [Select]
sys ps
string s

s="It's a string"

&ps = s

print ps
Title: Re: Pointer syntax...
Post by: Charles Pegge on June 12, 2011, 01:23:42 PM
You have to be careful with string pointering because they are memory managed by the system but you can do it like this:

Code: OxygenBasic
  1. string * ps
  2. string s
  3.  
  4. s="It's a string"
  5.  
  6. @ps = @s
  7.  
  8. print ps
  9.  
  10.  

Whenever 's' changes you will see it in ps. You can use this principle for any type: integer float string ..

Charles
Title: Re: Pointer syntax...
Post by: Aurel on June 14, 2011, 02:31:17 AM
Charles if i understand properly what pointer is -> integer which hold adress
of variable - is that right?
or im completely wrong ?
Title: Re: Pointer syntax...
Post by: Charles Pegge on June 14, 2011, 03:54:09 AM

Hi Aurel,

You can use any 4 byte integer to be hold the address of another variable.

But to make this value useful the compiler needs to know what type it is intended to be.

To extend the above example:

Code: OxygenBasic
  1.  
  2. sys p 'can hold any address
  3. string s
  4. string *ps 'string byref / string pointer
  5.  
  6. s="Hello!"
  7. p=@s
  8. @ps=p
  9.  
  10. print ps 'Hello!'
  11. print p 'string address
  12.  
  13.  

Charles
Title: Re: Pointer syntax...
Post by: Aurel on June 14, 2011, 04:14:25 AM
Thanks Charles ...so im right ;D
I see now how things work,thanks again on explanation... ;)
Title: Re: Pointer syntax...
Post by: kryton9 on June 14, 2012, 04:30:54 PM
Confused again, as both of these work, probably because Oxygen is so smart. Which is correct however?

Code: OxygenBasic
  1. method Get() as cVec2 ptr
  2.       cVec2 temp
  3.       temp.x = x
  4.       temp.y = y
  5.       return temp
  6. end method

Code: OxygenBasic
  1. method Get() as cVec2 ptr
  2.       cVec2 temp
  3.       temp.x = x
  4.       temp.y = y
  5.       return @temp
  6. end method
Title: Re: Pointer syntax...
Post by: Charles Pegge on June 14, 2012, 09:58:00 PM
Hi Kent,

Yes both of these return a vec2 pointer. Oxygen figures out that it cannot return classes or higher types directly, only pointers.

But you have to be careful. Your temp is a local variable and therefore goes out of scope once the function ends. The solution is to create a dynamic temp.

method Get() as cVec2  
      new cvec2 temp
      temp.x = x  
      temp.y = y  
      return temp  
end method

With one further piece of syntax, you can use this method as a class factory.

the word let is for creating tynamic variables and objects, initialised with a pointer:

let w=v.copy

Here is an example, of an object cloning itself. We have made all the pointers invisible :)

Code: OxygenBasic
  1. class cvec2
  2.  
  3. float x,y
  4.  
  5. method constructor()
  6. end method
  7.  
  8. method destructor()
  9. end method
  10.  
  11. method Copy() as cVec2  
  12.       new cvec2 temp
  13.       temp.x = x  
  14.       temp.y = y  
  15.       return temp  
  16. end method
  17.  
  18. end class
  19.  
  20. new cvec2 v
  21. v<=1.5,2.25
  22.  
  23. let w=v.copy
  24.  
  25. print w.x "," w.y
  26.  
  27. del v
  28. del w
  29.  

Charles
Title: Re: Pointer syntax...
Post by: kryton9 on June 14, 2012, 10:11:55 PM
You are confusing me Charles. Too much too absorb. Cool, but I need to learn a step at a time :(

1. First, which of the ones that I have is correct without oxygen magic?
2. Why does it work now if temp is a local variable as it is now?
3. "Your temp is a local variable and therefore goes out of scope once the function ends. " But many times local variables are used in a method and returned. This is confusing.
4. In your example when is temp deleted?
Title: Re: Pointer syntax...
Post by: Charles Pegge on June 14, 2012, 10:56:21 PM
Both of your examples work but they are unsafe, since the memory they point to is disallocated stack space.

You can safely return local primitives by-value but you cannot safely return any local variables by-reference, that is to say, by pointer.

When local integers and floats go out of scope, they are not specifically deleted, more abandoned and left to perish in reusable stack space. Local strings suffer a similar fate. They live in heap space, which is specifically disallocated by the garbage collector at the end of the procedure. But when a local string is returned, it's name is removed from the garbage collector's 'death' list, and it is spared the fate of the other local strings.

In my example, temp lives in heap space, and is not on the garbage collector's list. So it remains there until you delete it.

Charles

Title: Re: Pointer syntax...
Post by: kryton9 on June 14, 2012, 11:02:50 PM
Thanks for the info Charles. All of this helps as I come back to the forums and reread posts to refresh my memory.