basic macros (built-in macros)

Forum for discussion about the documentation project.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

basic macros (built-in macros)

Post by coderJeff »

__FB_QUERY_SYMBOL__ to be added in fbc 1.10.0 and I will be pushing changes soon.

Commit message:

Code: Select all

 fbc: add built-in macro __FB_QUERY_SYMBOL__( what, sym )

- __FB_QUERY_SYMBOL__( what, sym ) built-in macro for querying fbc symbol
  internals.  Added ./inc/fbc-int/symbol.bi (Work in progress).
- fixed: throw error in __FB_EVAL__ if parser would nest too deeply

Example:

  type T
    i as integer
  end type
  dim x as T

  print isUDT( T )       '' returns true
  print isVariable( T )  '' returns false
  print isUDT( x )       '' returns false
  print isVariable( x )  '' returns true
  '' etc

- Purpose:
    - allow writing of some compiler tests easier by exposing fbc
      internals to the test-suite
    - allow access to some fbc internals for users hacking on fbc
      and writing templating through user source code rather than
      having to hack on fbc sources directly
    - since the API in ./inc/fbc-int/symbol.bi should closely follow
      fbc internals, then __FB_QUERY_SYMBOL__ offers a way to
      incrementally document some of the compiler internals.  This
      may also allow an easier introduction to fbc sources by
      potential fbc developers.
info: previous topic about macros at basic macros in 1.08
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: basic macros (built-in macros)

Post by coderJeff »

fxm, you have been great keeping wiki up to date, thank-you.

If you do start wiki additions for this new macro when the changes are pushed, don't worry about getting too in depth just yet.

We can discuss here. The API is not firm. I hoping we will get a little feed back from users that will help shape how this is used.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: basic macros (built-in macros)

Post by fxm »

As I understand it, a list of many #define in inc/fbc-int/symbol.bi makes it easier for the user to call the new built-in macro __FB_QUERY_SYMBOL__ (which the average user probably would not call directly).
So a documentation page quoting all these #define, but however with __FB_QUERY_SYMBOL__ as the page name ?
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: basic macros (built-in macros)

Post by coderJeff »

fxm wrote: Dec 18, 2022 21:18 So a documentation page quoting all these #define, but however with __FB_QUERY_SYMBOL__ as the page name ?
Yes, correct.

The first part of the file comes from fbc sources, so won't change often.

Then after,

Code: Select all

'' Convenience MACROS
'' warning: naming is subject to change
I think we would like to get some feedback or discuss some naming of #defines.
Maybe we want to adjust some naming before we get too far here.

The goal is that we can start we something fairly small in scope, and then incrementally build on it over time.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: basic macros (built-in macros)

Post by fxm »

As soon as I could play around with __FB_QUERY_SYMBOL__ a bit, I will start the skeleton of the documentation page.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: basic macros (built-in macros)

Post by fxm »

coderJeff wrote: Dec 18, 2022 17:19 I was hoping to build official releases this month, but not sure if we will get everything stable enough.

Without waiting to play around with __FB_QUERY_SYMBOL__ a bit, I started the skeleton of the documentation page:
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: basic macros (built-in macros)

Post by fxm »

For the body of the convenient sub-macro name (after prefix 'is'), why not keep the name of the corresponding ENUM ?
  • isDataClassXXXXX( sym ) instead of isTypeOfXXXXX( sym )
    isDataTypeXXXXX( sym ) instead of isTypeXXXXX( sym )
    isSymbClassXXXXX( sym ) instead of isXXXXX( sym )
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: basic macros (built-in macros)

Post by fxm »

fxm wrote: Dec 19, 2022 13:17 Without waiting to play around with __FB_QUERY_SYMBOL__ a bit, .....
OK, I was able to play with it.
Thanks SARG.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: basic macros (built-in macros)

Post by fxm »

First point about '__FB_QUERY_SYMBOL__':
- 'isTypeOfProc( sym )' does not work with procedure
Example:

Code: Select all

#include once "fbc-int/symbol.bi"
Using FBC

Sub test()
End Sub

Print isTypeOfProc(test)      '' error 17: Syntax error in '#include once "fbc-int/symbol.bi"'

Sleep
Note: The 'Using FBC' command is not needed to call the sub-macros.

Maybe '__FB_QUERY_SYMBOL__' is not yet valid for this ?
Maybe my FreeBASIC version is not correct:
To get it, I just used the last fbc64.exe from SARG and added the symbol.bi from GitHub in inc/fbc-int/ ?
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: basic macros (built-in macros)

Post by coderJeff »

fxm wrote: Dec 23, 2022 10:07 First point about '__FB_QUERY_SYMBOL__':
- 'isTypeOfProc( sym )' does not work with procedure
Currently `__FB_QUERY_SYMBOL__` is using same parsing as `TYPEOF` to parse the symbol `SYM`.

The error is kind of same reason as error in this example:

Code: Select all

sub test()
end sub
#print typeof(test)
So right now, `__FB_QUERY_SYMBOL__` will probably fail in all the same places that `TYPEOF` would fail.

To handle procedures and maybe some other symbols, will have to add some new logic for parsing, or maybe some new constants in FB_QUERY_SYMBOL.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: basic macros (built-in macros)

Post by coderJeff »

fxm wrote: Dec 20, 2022 7:42 For the body of the convenient sub-macro name (after prefix 'is'), why not keep the name of the corresponding ENUM ?
  • isDataClassXXXXX( sym ) instead of isTypeOfXXXXX( sym )
    isDataTypeXXXXX( sym ) instead of isTypeXXXXX( sym )
    isSymbClassXXXXX( sym ) instead of isXXXXX( sym )
I am hoping that if we come up with better naming in the API user file, that the naming could feed back in to the compiler sources, and we would get better naming and therefore better implicit source documentation in the compiler sources.

We should probably look closer at some details of __FB_QUERY_SYMBOL__ before getting too attached to the naming of the convenience macros. The convenience macros are more to give an idea of we are trying to do.

My goal was to at least get the idea of it committed. For this one I thought it would be better that everyone can see what we are playing around instead of the idea sitting on my hard drive for the next 6 months.
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: basic macros (built-in macros)

Post by SARG »

If a function (not a sub) is used no compilation error but the result is wrong.

Code: Select all

#include once "fbc-int/symbol.bi"
Using FBC

function ftest() as integer
    return 1
End Function

sub stest
end sub
Print isTypeOfProc(ftest)    
'Print isTypeOfProc(stest)    
Sleep
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: basic macros (built-in macros)

Post by coderJeff »

SARG wrote: Dec 23, 2022 16:46 If a function (not a sub) is used no compilation error but the result is wrong.
Perhaps a good reason to use fxm's suggestion:
isDataClassXXXXX( sym ) instead of isTypeOfXXXXX( sym )

Currently, when used with a function, it is returning information about the return type of the function, not the function itself. Like `TypeOf` does.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: basic macros (built-in macros)

Post by fxm »

For a Sub, depending on context, I get either compile error, or '0' as result (when compiling the '*.pp' file for example), or even a crash.

'isTypeOfProc( @sym )' does not work either (always '0' as result).
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: basic macros (built-in macros)

Post by coderJeff »

I just pushed a change to fbc/master that we should find more robust.

Demo ouput for : __FB_QUERY_SYMBOL__( fbc.FB_QUERY_SYMBOL.symbclass, sym )

Code: Select all

EXAMPLES OF __FB_QUERY_SYMBOL__( fbc.FB_QUERY_SYMBOL.symbclass, sym )

[0] *invalid* :  integer ptr
[1] var       :  x
[1] var       :  y
[2] const     :  c
[2] const     :  E.elem1
[2] const     :  elem1
[3] proc      :  test
[3] proc      :  @test
[3] proc      :  T.test
[3] proc      :  ns.test
[3] proc      :  T.test
[3] proc      :  ns.ns2.test2
[6] keyword   :  integer
[7] label     :  label
[8] namespace :  ns
[8] namespace :  ns.ns2
[9] enum      :  E
[10] struct   :  U
[10] struct   :  T
[10] struct   :  ns.T1
[12] field    :  x.i
[12] field    :  y->i
[12] field    :  T.i
[12] field    :  U.a
The result of zero when getting the symbclass for "integer ptr" is correct because it is a data type expression, not an individual symbol.

Example Code for above:

Code: Select all

#include once "fbc-int/symbol.bi"

private function symbclassToStr _
	( _
		byval classid as fbc.FB_SYMBCLASS _
	) as string

	static as zstring ptr classnames _
		( _
			fbc.FB_SYMBCLASS.FB_SYMBCLASS_VAR _ 
			to fbc.FB_SYMBCLASS.FB_SYMBCLASS_NSIMPORT _
		) = _
	{ _
		@"var"      , _
		@"const"    , _
		@"proc"     , _
		@"param"    , _
		@"define"   , _
		@"keyword"  , _
		@"label"    , _
		@"namespace", _
		@"enum"     , _
		@"struct"   , _
		@"class"    , _
		@"field"    , _
		@"typedef"  , _
		@"fwdref"   , _
		@"scope"    , _
		@"reserved" , _
		@"nsimport"   _
	}

	select case classid
	case lbound(classnames) to ubound(classnames)
		return *classnames(classid)
	end select
	return "*invalid*"
end function 

#macro info( c )
	print c;
	print " <- ";
	print #c
#endmacro

#macro show_symbclass( sym )
	scope
		var classid = __FB_QUERY_SYMBOL__( fbc.FB_QUERY_SYMBOL.symbclass, sym )
		print left( "[" & classid & "] " & symbclassToStr(classid)+space(14),14) & ":  ";
		print #sym  
	end scope 
#endmacro

info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_VAR )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_CONST )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_PROC )
'info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_PARAM )
'info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_DEFINE )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_KEYWORD )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_LABEL )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_NAMESPACE )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_ENUM )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_STRUCT )
'info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_CLASS )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_FIELD )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_TYPEDEF )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_FWDREF )
'info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_SCOPE )
info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_RESERVED )
'info( fbc.FB_SYMBCLASS.FB_SYMBCLASS_NSIMPORT )

const c = 1234

type T
	i as integer
	declare sub test()
end type
dim x as T, y as T ptr

union U
	a as short
	b as long
end union

enum E
	elem1
end enum


sub test()
end sub

namespace ns
	sub test()
	end sub
	namespace ns2
		sub test2()
		end sub
	end namespace
	type T1
		x as integer
	end type
end namespace

print
print "EXAMPLES OF __FB_QUERY_SYMBOL__( fbc.FB_QUERY_SYMBOL.symbclass, sym )" 
print

show_symbclass( integer ptr )
show_symbclass( x )
show_symbclass( y )
show_symbclass( c )
show_symbclass( E.elem1 )
show_symbclass( elem1 )
show_symbclass( test ) 
show_symbclass( @test )
show_symbclass( T.test ) 
show_symbclass( ns.test ) 
show_symbclass( T.test ) 
show_symbclass( ns.ns2.test2 )
show_symbclass( integer )
label:
show_symbclass( label )
show_symbclass( ns )
show_symbclass( ns.ns2 )
show_symbclass( E )
show_symbclass( U )
show_symbclass( T )
show_symbclass( ns.T1 )
show_symbclass( x.i )
show_symbclass( y->i )
show_symbclass( T.i )
show_symbclass( U.a )

sleep
Post Reply