Support a 'new' Syntax (Structure pointer as return value)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Support a 'new' Syntax (Structure pointer as return value)

Post by IceSoft »

@Fred
Please implemet this new syntax for using of: structure pointer as return value:
(Currently PB returns a syntax error using this new 'keywords')

Code: Select all

Declare.*<Struct> foo()
DeclareC.*<Struct> foo()
DeclareCDLL.*<Struct> foo()
DeclareDLL.*<Struct> foo()

Procedure.*<Struct> foo()
ProcedureC.*<Struct> foo()
ProcedureCDLL.*<Struct> foo()
ProcedureDLL.*<Struct> foo()
    
ImportC
  foo.*<Struct>() As "foo@16"
EndImport
  
Import
  foo.*<Struct>() As "foo@16"
EndImport 
Here an example how it can be used:

Code: Select all

Structure cpVect
  x.d
  y.d
EndStructure

Declare.*cpVect foo()

[do something]

Procedure.*cpVect foo()
  *a.cpVect = AllocateMemory(SizeOf(cpVect))
  *a\x = 11.1
  *a\y = 22.1
  ProcedureReturn *a
EndProcedure  
  
Last edited by IceSoft on Fri Feb 08, 2013 5:20 pm, edited 1 time in total.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Support a 'new' Syntax (Structure pointer as return valu

Post by Danilo »

I use this currently:

Code: Select all

Structure Vect
  x.d
  y.d
EndStructure
Macro pVect : i : EndMacro

Declare.pVect foo()

[do something]

Procedure.pVect foo()
  *a.Vect = AllocateMemory(SizeOf(Vect))
  *a\x = 11.1
  *a\y = 22.1
  ProcedureReturn *a
EndProcedure

I like to add something to the wishlist here, because it is similar:
Please allow use of Interfaces in Declare, Prototype and Procedure return type declarations.

Code: Select all

Interface IDirectX14
  create()
EndInterface

Procedure.IDirectX14 Blah()
  ; blub
EndProcedure
It is just a pointer, so it shouldn't be a problem for Interfaces.

Currently we get the following error message: "A structure can't be used with ProcedureReturn.", which does not make sense at all.
There is no structure used in the code, and there is no ProcedureReturn! ;)

So, allowing Interfaces as return type should be easy, as it's a pointer only anyway...?

For returning real Pointers, IceSoft's example doesn't look so bad to me. Common PureBasic Syntax, everybody will understand it.
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Support a 'new' Syntax (Structure pointer as return valu

Post by IceSoft »

Danilo wrote:For returning real Pointers, IceSoft's example doesn't look so bad to me. Common PureBasic Syntax, everybody will understand it.
Thanks!
And it can be very easy internal replaced with ".i" before compiler and syntax check is running over the source file.

Here the small regular expression used internal which improve the PureBasic syntax:

Code: Select all

If CreateRegularExpression(0, "\.\*.*?[ (]")
  a.s = ReplaceRegularExpression(0, source, ".i")
  MessageRequester("Info", "Structure pointer syntax replaced here: "+a)
EndIf
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Support a 'new' Syntax (Structure pointer as return valu

Post by freak »

http://www.purebasic.fr/english/viewtop ... =3&t=44120
http://www.purebasic.fr/english/viewtop ... =3&t=52428

Please stop asking for the same thing over and over. It is getting annoying.
quidquid Latine dictum sit altum videtur
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Support a 'new' Syntax (Structure pointer as return valu

Post by IceSoft »

freak wrote:http://www.purebasic.fr/english/viewtop ... =3&t=44120
http://www.purebasic.fr/english/viewtop ... =3&t=52428

Please stop asking for the same thing over and over. It is getting annoying.
An answer like this is also annoying. So stop it please too. Thanks.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Support a 'new' Syntax (Structure pointer as return valu

Post by Fred »

It is a common rule to not open several times the same topic on a forum, or it will get messy very quickly.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Support a 'new' Syntax (Structure pointer as return valu

Post by freak »

IceSoft wrote:
freak wrote:http://www.purebasic.fr/english/viewtop ... =3&t=44120
http://www.purebasic.fr/english/viewtop ... =3&t=52428

Please stop asking for the same thing over and over. It is getting annoying.
An answer like this is also annoying. So stop it please too. Thanks.
Seriously, do you think your chances of getting things implemented are better when you annoy the hell out of the people you are asking? I don't think so.
quidquid Latine dictum sit altum videtur
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Support a 'new' Syntax (Structure pointer as return valu

Post by IceSoft »

freak wrote:
IceSoft wrote:
freak wrote:http://www.purebasic.fr/english/viewtop ... =3&t=44120
http://www.purebasic.fr/english/viewtop ... =3&t=52428

Please stop asking for the same thing over and over. It is getting annoying.
An answer like this is also annoying. So stop it please too. Thanks.
Seriously, do you think your chances of getting things implemented are better when you annoy the hell out of the people you are asking? I don't think so.
I am not sure. But anyhow I think you know about this whish and how easy the implementation is ;-)
Ok I know YOU are the master and I am only one of your smallest slave of course ;-)
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Support a 'new' Syntax (Structure pointer as return valu

Post by RichAlgeni »

This reply isn't to tell people what to do, and it's not criticize anyone's coding practices. It's just that my experience may be helpful to some.

All my procedures return an integer, positive for successful, 0 for unsuccessful, negative for an error.

Any data that is needed in both the parent and daughter procedure is instantiated (created, if you will), in the parent process. Strings are allocated, number are initialized, etc.

Never pass strings to a procedure, always pass pointers. If a structure needs to be processed in some way, initialize the structure in the calling procedure, pass the pointer, then check to see if the returned variable is a positive integer.

If an unknown amount of data will be sent back to a parent procedure, set a variable with the maximum length in the parent, allocate memory to this length, plus one byte for a terminator, then pass this length and the pointer to the procedure.

Always, always use type '.i' for integers, even if you need just one digit, it will save you in the long run.

Like I said, this is just the (maybe) benefit of my experience, but by using this practices, I have just about eliminated all IMA errors in my code, my procedures are standardized, (see functional coding), and I never have to worry about returning structures or pointers back to a calling process.
Post Reply