Repository navigation
Fb Eval
Home › Intrinsic Defines › FB_EVAL
Intrinsic define (macro) performed by the compiler.
__FB_EVAL__( arg )arg
Evaluates the argument (constant-expression) at compile time.
When the argument evaluation produces a string, __FB_EVAL__ returns a string formatted with a preprocessor operator:
- a Non-Escaped String Literal (of form: $"text"),
- or an Escaped String Literal (of form: !"text") if needed.
For other datatypes produced, simple Literals (of Integers, Floating Points, Booleans) without prefix/suffix are returned.
__FB_EVAL__ macro is useful where there is any of the following:
- non functional expression (i.e. side-effects), - needs to be evaluated (i.e. simplified, respecting the operator precedence) before passing it on, - used in a place where the compiler would not allow an expression.
- New to FreeBASIC
#print 1 + 2
#print __FB_EVAL__( 1 + 2 )
#print 4 * Atn(1)
#print __FB_EVAL__( 4 * Atn(1) )
/' Compiler output:
1 + 2
3
4 * Atn(1)
3.141592653589793
'/' In this example, the three '__FB_EVAL__' are absolutely mandatory in this 'assign()' macro.
' Even for '__FB_QUOTE__( __FB_EVAL__( expr ) )', because for the case of expr = cos(1/x),
' 'cos(1/x)' must be properly evaluated before be quoted (after the previous 'assign("x", x+1)'),
' otherwise in that case 'cos(1/x+1)' is taken into account (giving 'cos(2)') instead of 'cos(1/(x+1))' (giving 'cos (1/2)')
' because the operator precedence is not applied by '__FB_QUOTE__'.
#macro assign( sym, expr )
__FB_UNQUOTE__( __FB_EVAL__( "#undef " + sym ) )
__FB_UNQUOTE__( __FB_EVAL__( "#define " + sym + " " + __FB_QUOTE__( __FB_EVAL__( expr ) ) ) )
#endmacro
#define x
assign( "x", 1 )
Print x
assign( "x", x+1 )
Print x
assign( "x", Cos(1/x) )
Print x
assign( "x", "hello" )
Print x
assign( "x", x+x )
Print x
/' Output:
1
2
0.877582...
hello
hellohello
'/See also __FB_ARG_EXTRACT__ example.
Category: Intrinsic Defines
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing