Revision history for KeyPgPpmacro


Revision [24730]

Last edited on 2020-11-20 04:07:53 by fxm [added warning versus operator precedence]
Additions:
WARNING: In the macro body, it may be mandatory to have to surround by parentheses any used parameter if it is inside an expression with one operator at least, in order to not undergo an unwanted precedence change of operators (if passing as argument an expression with also operators).


Revision [24413]

Edited on 2020-09-14 02:31:34 by fxm [macros with parameters can now be invoked without using parentheses around the arguments using '#macro name ? (arg)' definition syntax]
Additions:
Note: Unlike the function-like ##[[KeyPgPpdefine|#define]]## declaration, spaces can be put between the macro name and the opening parenthesis for any declaration syntax of macro.


Revision [24411]

Edited on 2020-09-13 02:40:59 by fxm [macros with parameters can now be invoked without using parentheses around the arguments using '#macro name ? (arg)' definition syntax]
Additions:
**Note:** Beware of the possibility of triggering so a conflict with expressions containing the name of the macro as one of their terms.


Revision [24410]

Edited on 2020-09-12 16:49:20 by fxm [macros with parameters can now be invoked without using parentheses around the arguments using '#macro name ? (arg)' definition syntax]
Additions:
If using the optional question mark (?) after the ##//identifier//## in the definition syntax, macros with parameters can be invoked without using parentheses around the arguments.
Deletions:
If using ##**?**## (after ##//identifier//##) in the definition syntax, macros with parameters can be invoked without using parentheses around the arguments.


Revision [24409]

Edited on 2020-09-12 16:39:08 by fxm [macros with parameters can now be invoked without using parentheses around the arguments using '#macro name ? (arg)' definition syntax]
Additions:
**#macro** //identifier// [?] ( [ //parameters// ] )
**#macro** //identifier// [?] ( [ //parameters//, ] //[[KeyPgDots|variadic_parameter...]]// )
If using ##**?**## (after ##//identifier//##) in the definition syntax, macros with parameters can be invoked without using parentheses around the arguments.
Deletions:
**#macro** //identifier//( [ //parameters// ] )
**#macro** //identifier//( [ //parameters//, ] //[[KeyPgDots|variadic_parameter...]]// )


Revision [23959]

Edited on 2020-05-09 09:27:23 by fxm [Added 2 examples]
Additions:
#macro test1( arg1, arg2... )
test1( "1", "2" )
test1( "3" )
test1( 5, 6 )
test1( 7 )
#macro test2( arg1, arg2... )
test2( 5 )
test2( 5,6, 7, , 9, 10, ,,13, 14 )
Deletions:
#macro test1(arg1, arg2...)
test1("1", "2")
test1("3")
test1(5, 6)
test1(7)
#macro test2(arg1, arg2...)
test2(5)
test2(5,6, 7, , 9, 10, ,,13, 14)


Revision [23958]

Edited on 2020-05-09 09:19:35 by fxm [Added 2 examples]
Additions:
{{fbdoc item="filename" value="examples/manual/prepro/macro.bas"}}%%(freebasic)
' macro as an expression value
a + b
Print Print1( "Hello ", "World!" )
/' Output :
Hello World!
'/
%%
{{fbdoc item="filename" value="examples/manual/prepro/macro2.bas"}}%%(freebasic)
' macro as multiple statements
Print a;
Print " ";
Print b;
Print "!"
/' Output :
Hello World!
'/
%%
{{fbdoc item="filename" value="examples/manual/prepro/macro3.bas"}}%%(freebasic)
' macro with a variadic parameter
#macro test1(arg1, arg2...)
Print arg1
#if #arg2 = ""
Print "2nd argument not passed"
#else
Print arg2
#endif
test1("1", "2")
Print "-----------------------"
test1("3")
Print "-----------------------"
test1(5, 6)
Print "-----------------------"
test1(7)
/' Output :
1
2
-----------------------
3
2nd argument not passed
-----------------------
5
6
-----------------------
7
2nd argument not passed
'/
%%
{{fbdoc item="filename" value="examples/manual/prepro/macro4.bas"}}%%(freebasic)
' macro with a variadic parameter which can contain several sub-parameters:
' To distinguish between the different arguments passed by variadic_parameter,
' you can first convert variadic_parameter to a string using the Operator # (Preprocessor Stringize),
' then differentiate in this string (#variadic_parameter) each passed argument by locating the separators (usually a comma).
#macro test2(arg1, arg2...)
Print "'" & Trim(#arg1) & "'"
Scope
Dim As String s = Trim(#arg2)
If s <> "" Then
Do
Dim As Integer k = Instr(1, s, ",")
If k = 0 Then
Print "'" & s & "'"
Exit Do
End If
Print "'" & Left(s, k - 1) & "'"
s = Trim(Mid(s, k+1))
Loop
End If
End Scope
test2(5)
Print "----"
test2(5,6, 7, , 9, 10, ,,13, 14)
/' Output :
'5'
----
'5'
'6'
'7'
''
'9'
'10'
''
''
'13'
'14'
'/
%%
Deletions:
{{fbdoc item="filename" value="examples/manual/prepro/macro.bas"}}%%(freebasic)
'' macro as an expression value
a + b
print Print1( "Hello", "World" )
'' Output :
'' Hello World!
%%
{{fbdoc item="filename" value="examples/manual/prepro/macro2.bas"}}%%(freebasic)
'' macro as multiple statements
print a;
print " ";
print b;
print "!"
'' Output :
'' Hello World!
%%


Revision [20459]

Edited on 2016-02-10 16:06:58 by DkLwikki [Update link format]
Additions:
**#macro** //identifier//( [ //parameters//, ] //[[KeyPgDots|variadic_parameter...]]// )
##**#macro**## is the multi-line version of ##[[KeyPgPpdefine|#define]]##.
- ##[[KeyPgPpdefine|#define]]##
- ##[[KeyPgPpifdef|#ifdef]]##
- ##[[KeyPgPpundef|#undef]]##
Deletions:
**#macro** //identifier//( [ //parameters//, ] //[[KeyPgDots variadic_parameter...]]// )
##**#macro**## is the multi-line version of ##[[KeyPgPpdefine #define]]##.
- ##[[KeyPgPpdefine #define]]##
- ##[[KeyPgPpifdef #ifdef]]##
- ##[[KeyPgPpundef #undef]]##


Revision [15308]

Edited on 2011-10-01 09:44:01 by DkLwikki [Point to #define instead of duplicating]
Additions:
**#macro** //identifier//( [ //parameters// ] )
//body//
**#macro** //identifier//( [ //parameters//, ] //[[KeyPgDots variadic_parameter...]]// )
//body//
##**#macro**## is the multi-line version of ##[[KeyPgPpdefine #define]]##.
print a;
print " ";
print b;
print "!"
Deletions:
**#macro** //macro_name//( [ //parameters// ] )
//macro body//
**#macro** //macro_name//( [ //parameters//, ] //variadic_parameter//[[KeyPgDots ...]] )
//macro body//
##**#macro**## defines a function like macro where ##//macro body//## may span multiple lines.
Parameters supplied to the function like macro are substituted where they occur in the ##//macro body//##. The entire ##//macro body//## is substituted where ever ##//macro_name//## appears in the source code. The number of parameters supplied to a macro must match the number of parameters in its ##**#macro**## definition.
Using ##[[KeyPgDots ...]]## (an ellipsis) behind the last parameter of a macro allows to create a variadic macro, see [[KeyPgPpdefine #define]].
The ##[[KeyPgPpifdef #ifdef]]## and ##[[KeyPgPpifndef #ifndef]]## preprocessor conditionals can test if ##//macro_name//## exists or does not exist.
The ##[[KeyPgPpundef #undef]]## preprocessor directive will undefine a macro so that it may be redefined with another definition.
Macro substitution can be checked by using the -g -r switches. The compiler doesn't erase the intermediate .asm file where the code actually sent to the compiler can be seen close to its translation into assembler.
print a;
print " ";
print b;
print "!"
- ##[[KeyPgPpif #if]]##
- ##[[KeyPgPpelse #else]]##
- ##[[KeyPgPpelseif #elseif]]##
- ##[[KeyPgPpendif #endif]]##
- ##[[KeyPgPpifndef #ifndef]]##
- ##[[KeyPgDefined defined]]##


Revision [14863]

Edited on 2010-10-12 08:37:36 by DkLwikki [Added variadic macros]
Additions:
**#macro** //macro_name//( [ //parameters// ] )

**#macro** //macro_name//( [ //parameters//, ] //variadic_parameter//[[KeyPgDots ...]] )

Using ##[[KeyPgDots ...]]## (an ellipsis) behind the last parameter of a macro allows to create a variadic macro, see [[KeyPgPpdefine #define]].
Deletions:
**#macro** //macro_name//( [ //param1// [, //param_list//] ] )


Revision [14424]

Edited on 2009-08-29 21:56:29 by CountingPine [Remove CamelCase links in "New to FreeBASIC"]
Additions:
- New to ""FreeBASIC""
Deletions:
- New to FreeBASIC


Revision [11644]

The oldest known version of this page was created on 2007-11-10 09:14:45 by JeffMarshall [Remove CamelCase links in "New to FreeBASIC"]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode