how to get sub/function name and parameters

Post your Gambas programming questions here.
Post Reply
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

how to get sub/function name and parameters

Post by PJBlack »

lets say ... i have a sub/function like
Public Function test(par1 as Integer, par2 as String) As Integer
     ' something like Print FunctionName
     ' something like For Each Parameter in FunctionCall
     '                  Print ParName, ParValue
     '                End With
End
and i like to know the name of the function and the given parameters and their values ...

tried a lot but no glue ... so can anybody push me in the right direction?
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: how to get sub/function name and parameters

Post by cogier »

Can you expand your requirements, as I see it all the answers you are looking for are in the routine.
Public Function test(par1 As Integer, par2 As String) As Integer

  Print "test"
  Print "par1", Str(par1)
  Print "par2", par2

End
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: how to get sub/function name and parameters

Post by PJBlack »

but what if i want that with every function? seems much typing ...

what im thinking of is something like:
strCName = Split(System.Backtrace[0],".")[0] ' class name
strFName = Split(System.Backtrace[0],".")[1] ' function name name
For Each a??????? As ???????? (Variant?) in strCName.strFName
     Print a??????.Name ' Name of Parameter
     Print a??????.Type ' Typ of Parameter
     Print a??????.Value ' Value of Parameter
Next
hope this is more understandable :)
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: how to get sub/function name and parameters

Post by BruceSteers »

It's complicated and undocumented but it might be worth looking into gb.eval.highlight.

The IDE runs through the code very quickly and lists all symbols, function names, values etc in an enumerable collection.

How exactly it happens i do not know but code using it is in the TextHighlighter_Gambas.class file.

https://gitlab.com/gambas/gambas/-/blob ... mbas.class

something like...
 Highlight.Analyze(Text, False)
  
  iPos = 0
  For I = 0 To Highlight.Symbols.Max
   '  Read the data here...
  Next
If at first you don't succeed , try doing something differently.
BruceS
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: how to get sub/function name and parameters

Post by PJBlack »

BruceSteers wrote: Sunday 31st January 2021 2:05pmThe IDE runs through the code very quickly and lists all symbols, function names, values etc in an enumerable collection.
thats what im looking for ...

your example did not work because:

1. in
Highlight.Analyze("form1", False)
i have to manually give the name, and

2.
Highlight.Symbols
gives back a string[] with one entry and that is "form1" ... i think i knew that allready ...

thank for trying :)
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: how to get sub/function name and parameters

Post by vuott »

The function "name" can be obtained with....

Exemplum simplex:
Public Sub Main()

  Dim s As String

  For Each s In Class.Load("Main").Symbols
    Print s
  Next

End


Public Function test(par1 As Integer, par2 As String) As Integer
    
End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: how to get sub/function name and parameters

Post by Quincunxian »

I do this in a rather 'clunky' way and it takes extra effort but it works.
The following example shows a toolbox routine to get the id field from any table by providing 'a' name.
All my tables have an id field and the names are unique.

The AE ( Application Error management) module has a collection that is used like a stack.
Procedure calls are loaded as they are accessed and removed as the procedure closes.
The Finally statement removes the next procedure listed in the stack and as all procedures close,
the stack empties.

The AE module generates an error form from scratch and then provides valuable data to error tracking.
I've found over the years that the extra effort in coding is worth the benefits of being able to quickly identify the error
and to be able to see the values of the parameters passed.
It also lists the backtrace in reverse order so that it's easier to follow.

Note # The return statement HAS to be after the stack remove call as once you call a RETURN statement in
any procedure, it terminates and no other statements are processed
Public Function GetIdFromName(InTable As String, InField As String, InName As String) As Integer

  Dim $Rec As Result
  Dim $Query As String = "SELECT Id," & InField & " FROM " & InTable & " WHERE " & InField & " = '" & InName & "'"
  Dim TmpId as Integer = 0

  AE.ErrorWhere = "Public Function GetIdFromName(InTable As String, InField As String, InName As String) As Integer" & Gb.NewLine
  AE.ErrorWhere &= "Par1: " & InTable & Gb.NewLine
  AE.ErrorWhere &= "Par2: " & InField & Gb.NewLine
  AE.ErrorWhere &= "Par3: " & InName

  AE.LocationAdd(AE.ErrorWhere)

  $Rec = DB.$Con.Exec($Query)
  If (Not IsNull($Rec)) And $Rec.Available Then TmpId =  $Rec!Id
 Finally
  AE.LocationRemove
  Return TmpId
Catch

  AE.DisplayErrorData(2, Error.Text, Error.Class, Error.Where, Error.Code, Error.Backtrace)

End
This is an example of the output of a typical error.
Screenshot from 2021-02-01 06-55-40.png
Screenshot from 2021-02-01 06-55-40.png (44.52 KiB) Viewed 6257 times
[/img]
Attachments
AE.module.tar.gz
AE Module.
(2.21 KiB) Downloaded 241 times
Cheers - Quin.
I code therefore I am
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: how to get sub/function name and parameters

Post by BruceSteers »

PJBlack wrote: Sunday 31st January 2021 4:47pm
BruceSteers wrote: Sunday 31st January 2021 2:05pmThe IDE runs through the code very quickly and lists all symbols, function names, values etc in an enumerable collection.
thats what im looking for ...

your example did not work because:

1. in
Highlight.Analyze("form1", False)
i have to manually give the name, and

2.
Highlight.Symbols
gives back a string[] with one entry and that is "form1" ... i think i knew that allready ...

thank for trying :)
you have to analize the whole file text , you have essentially only analized only the word "form1" there, you'd load in the class file like...

sText=File.Load("./.src/FMain.class")
Highlight.Analyze(sText, False)

then iterate through the results somehow.
Like i say i do not know how.

Vuotts method looked pretty simple :)
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: how to get sub/function name and parameters

Post by vuott »

BruceSteers wrote: Monday 1st February 2021 3:52am Vuotts method looked pretty simple :)
...but more complexly and for more generic elements, by accessing the "CLASS_DESC_SYMBOL *table " member of " _CLASS " Structure (in the header file "gbx_class.h "):
Public Sub Main()

  Dim p As Pointer

  p = Object.Address(Me)
  
  Functio(p)

End


Private Function Functio(po As Pointer)

  Dim p1, p2 As Pointer
  Dim i As Integer

  p1 = Pointer@(po + SizeOf(gb.Pointer) * 5)
  p2 = Pointer@(p1)
  
  For i = 0 To 63
    If Byte@(p2 + i) == 0 Then
      Print
      Continue
    Endif
    Print Chr(Byte@(p2 + i));
  Next
  
End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Post Reply