Author Topic: Pointer syntax...  (Read 7821 times)

0 Members and 1 Guest are viewing this topic.

Aurel

  • Guest
Pointer syntax...
« 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

Charles Pegge

  • Guest
Re: Pointer syntax...
« Reply #1 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)
« Last Edit: April 05, 2011, 08:53:45 AM by Charles Pegge »

Aurel

  • Guest
Re: Pointer syntax...
« Reply #2 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
« Last Edit: April 16, 2011, 09:32:07 AM by Aurel »

Charles Pegge

  • Guest
Re: Pointer syntax...
« Reply #3 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

Aurel

  • Guest
Re: Pointer syntax...
« Reply #4 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. ;)
« Last Edit: April 16, 2011, 01:15:51 PM by Aurel »

kryton9

  • Guest
Re: Pointer syntax...
« Reply #5 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
« Last Edit: June 11, 2011, 05:50:13 AM by o2admin »

o2admin

  • Administrator
  • *****
  • Posts: 21
  • OxygenBasic
    • Oxygen Basic
Re: Pointer syntax...
« Reply #6 on: June 11, 2011, 05:54:40 AM »
Thanks Kent,

I've switched on the o2 syntax :)

Charles

JRS

  • Guest
Re: Pointer syntax...
« Reply #7 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.

kryton9

  • Guest
Re: Pointer syntax...
« Reply #8 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.  
« Last Edit: June 11, 2011, 10:26:58 PM by kryton9 »

Aurel

  • Guest
Re: Pointer syntax...
« Reply #9 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

kryton9

  • Guest
Re: Pointer syntax...
« Reply #10 on: June 11, 2011, 04:19:50 PM »
That is nice Aurel, another way to do it that I didn't know about.

Charles Pegge

  • Guest
Re: Pointer syntax...
« Reply #11 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
« Last Edit: June 11, 2011, 10:00:10 PM by Charles Pegge »

kryton9

  • Guest
Re: Pointer syntax...
« Reply #12 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!!!
« Last Edit: June 11, 2011, 10:33:46 PM by kryton9 »

Charles Pegge

  • Guest
Re: Pointer syntax...
« Reply #13 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
« Last Edit: June 12, 2011, 05:46:57 AM by Charles Pegge »

Aurel

  • Guest
Re: Pointer syntax...
« Reply #14 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