Page 1 of 1

TypeOf for Procedures

Posted: Tue Oct 28, 2014 10:09 am
by Shield
Hello :)

I think it might be useful (well it would certainly be useful for me at the moment :P) to be able
to determine the return type of a / the current procedure.

Code: Select all

TypeOf(MyProcedure())
#PB_Compiler_ProcedureType
Any opinions on that topic are welcome,

thanks. :)

Re: TypeOf for Procedures

Posted: Tue Oct 28, 2014 9:35 pm
by Zebuddi123
Hi Shield In what way would it be useful as you say currently it would be for you? can you give us an example so i can see if i think that would be useful for me :).

When i use say droopy libs and sometimes i forget the return type (maybe it some code ive come back too etc ) I use this tool, a quick listview that shows returnable procedures and type in the current file.

pbide tool:
ProgramParameter(0) = "%FILE"
ProgramParameter(1) = "%TEMPFILE"

shortcut: Left-Ctrl+\

Zebuddi. :)

Code: Select all

EnableExplicit
Global Window_0,  ListIcon_0, Filename$=ProgramParameter(0) , TmpFilename$=ProgramParameter(1)
Define  NewList entries.s(), event.i

Procedure DoTypes(Filename$, List entries.s())
	Protected regex_proctype, *mem, buffer.i, FileString$, spos.i, epos.i, procname.s, type.s, retstring.s, nbr.i, i.i
	regex_proctype.i = CreateRegularExpression(#PB_Any, "(ProcedureC\.|Procedure\.|ProcedureCDLL\.|ProcedureDLL\.)[scbwliqfdau]\s.+?\(.*\)")	
	Dim pt$(0)
	If ReadFile(0,Filename$)
		*mem=AllocateMemory(Lof(0))
		If *mem
			buffer=ReadData(0, *mem, Lof(0))
			FileString$=PeekS(*mem)
			nbr=ExtractRegularExpression(regex_proctype, FileString$, pt$())
			If nbr>0
				For i=0 To nbr-1
					If FindString(pt$(i), Chr(46))
						spos=FindString(pt$(i), Chr(46)) ; find the point
						epos=FindString(pt$(i), Chr(40) , spos+1) ; find the brace
						type=Mid(pt$(i), spos+1, 1)				  ; this is the return type
						procname=Mid(pt$(i), spos+3)			  ; this is the procedure name
						If type
							Select type
								Case "s"
									type="String"
								Case "c"
									type="Character "
								Case "b"
									type="Byte"
								Case "w"
									type="Word"
								Case "l"
									type="Long"
								Case "i"
									type="Integer"
								Case "q"
									type="Quad"
								Case "f"
									type="Float"
								Case "d"
									type="Double"
								Case "a"
									type="Ascii"
									Case "u"
									type="Unicode"
							EndSelect
							AddElement(entries())
							entries()=procname+Chr(10)+type
						EndIf
					EndIf
				Next
				SortList(entries(), #PB_Sort_Ascending)
			EndIf
		EndIf
		CloseFile(0)
		FreeRegularExpression(regex_proctype)
		FreeArray(pt$())
		FreeMemory(*mem)
		If nbr=0
			MessageRequester("Error", "No Procedure/s With Return Types Found")
			End
		EndIf
	EndIf
EndProcedure

Procedure OpenWindow_0(x = 0, y = 0, width = 560, height = 310)	
	Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Procedure Return Types",  #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
	CreateStatusBar(0, WindowID(Window_0))
	AddStatusBarField(50)
	StatusBarText(0, 0, "Entries:")
	AddStatusBarField(510)
	ListIcon_0 = ListIconGadget(#PB_Any, 0, 7, 560, 280, "Nbr", 50, #PB_ListIcon_GridLines)
	AddGadgetColumn(ListIcon_0, 1, "Procedure", 410)
	AddGadgetColumn(ListIcon_0, 2, "Ret Type", 100)
EndProcedure

Procedure Window_0_Events(event, List entries.s())
	Select event
		Case #PB_Event_CloseWindow
			FreeList(entries())
			ProcedureReturn #False
			
		Case #PB_Event_Menu
			Select EventMenu()
			EndSelect
			
		Case #PB_Event_Gadget
			Select EventGadget()
			EndSelect
	EndSelect
	ProcedureReturn #True
EndProcedure

Procedure Main(List entries.s())
	OpenWindow_0()
	StickyWindow(Window_0, 1)
	ForEach entries()
		AddGadgetItem(ListIcon_0, -1, Str(ListIndex(entries())+1)+Chr(10)+entries())
	Next	
	StatusBarText(0, 1 ,Str(ListSize(entries()))) 
EndProcedure

If Filename$
	DoTypes(Filename$, entries())
	Main(entries())
ElseIf TmpFilename$
	DoTypes(TmpFilename$, entries())
	Main(entries())
Else
	End
EndIf

Repeat
	event = WaitWindowEvent()
Until Window_0_Events(event,entries()) = #False

End

Re: TypeOf for Procedures

Posted: Tue Oct 28, 2014 9:48 pm
by Shield
Well, in my particular case I tried to apply some macro magic and wanted to generate a temporary variable holding a result
that needs to be returned later. Unfortunately, I can't do that easily because I don't know the return type. Basically, I have
to specify the type twice.

Re: TypeOf for Procedures

Posted: Tue Oct 28, 2014 10:34 pm
by idle
would this help shield?

Code: Select all

Structure PB_Any
  type.i
  StructureUnion
    a.a
    b.b
    c.c
    u.u
    w.w
    l.l
    i.i
    f.f
    d.d
    q.q
    *ptr
  EndStructureUnion
  s.s
EndStructure

Macro New_PB_Any() 
  AllocateMemory(SizeOf(pb_any))
EndMacro   

Macro PB_Any_EQ(ResultAny,ValAny) 
   Select ValAny\type 
    Case #PB_String 
      ResultAny\s = ValAny\s 
    Default  
    ResultAny\q = ValAny\q
  EndSelect  
  ResultAny\type = ValAny\type 
EndMacro   

Macro Free_PB_Any(pAny) 
  Select pAny\type 
    Case #PB_String 
      pany\s=""
  EndSelect 
  FreeMemory(pany)
EndMacro   

Procedure foo(*val.PB_Any)
  Protected *p.pb_any = New_PB_Any()
  PB_Any_EQ(*p,*val)
  ProcedureReturn *p 
EndProcedure 

Global *pr.pb_any,in.pb_any  

in\type = #PB_String 
in\s = "hello" 

*pr = foo(@in) 
If *pr\type = #PB_String 
  Debug *pr\s 
EndIf   
Free_PB_Any(*pr)

in\type = #PB_Float 
in\f = 1.235 
*pr = foo(@in) 
If *pr\type = #PB_Float 
  Debug *pr\f
EndIf   
Free_PB_Any(*pr)   


Re: TypeOf for Procedures

Posted: Tue Oct 28, 2014 11:28 pm
by Shield
No. :) It's not a runtime problem.

What I tried to do is a ProcedureReturn macro that automatically does something (freeing memory) before returning.
That memory block would be invalid after freeing, which is why I need to create a copy first.

If I had a way to determine the return type of a procedure, the compiler could do this automatically.


Edit: Sorry for the lack of demonstration code here, I forgot to bring it back home.

Re: TypeOf for Procedures

Posted: Wed Oct 29, 2014 12:18 am
by idle
Yes I agree it would be useful to be able to determine the return type of a procedure
can you post an example of what your trying to achieve?