Macro is not executed line by line.
Posted: Fri Feb 28, 2020 9:44 pm
To act as normal code, macro content should be replaced and executed line by line.
This code show how macro works :
If macro is executed line per line non-infinite recursive macro can be used
With some tricks to use nested macro, line per line executions also allow to edit macro content form another macro. For example to create compiler variables. If I remove all tricks and workaround to be more human understandable is can looks like this :
If that kind of code work i believe PureBasic preprocessor can be Turing complete.
This code show how macro works :
Code: Select all
Macro CEI
CompilerElseIf #True
EndMacro
Macro TestCompilerElseIf
CompilerIf #False
; Nothing
CEI ; In macro CEI is replaced before "execute" CompilerIf. So firsh #FOO is choosed
#FOO = "FOO defined in macro"
CompilerElse
#FOO = "FOO defined in normal code"
CompilerEndIf
EndMacro
; The same bloc of code as the one inside macro "TestCompilerElseIf"
CompilerIf #False
; Nothing
CEI ; Outside macro CompilerIf is executed Before (because it's on another line). So the macro is never replaced.
#FOO = "FOO defined in macro"
CompilerElse
#FOO = "FOO defined in normal code"
CompilerEndIf
TestCompilerElseIf ; Constant already declared with a different value: #FOO
Code: Select all
Macro DQ
"
EndMacro
Macro Recursive(DEEP)
CompilerWarning DQ#DEEP passes restantes"
CompilerSelect DEEP
CompilerCase 1
CompilerWarning "Dernière passe"
CompilerCase 2
Recursive(1)
CompilerEndSelect
EndMacro
Recursive(2)
Code: Select all
EnableExplicit ; This is concept code. It does NOT works without lot patchs and some preprocessor changes.
Macro SetMacroVar(MacroVar, Value)
UndefineMacro MacroVar
Macro MacroVar
Value
EndMacro
EndMacro
Macro AddMacroVar(MacroVar, Value)
SetMacro(MacroVar, (MacroVar) + (Value))
EndMacro
Macro Normalize(MacroVar, Value)
CompilerSelect Value
CompilerCase 0
SetMacroVar(MacroVar, 0)
CompilerCase 1
SetMacroVar(MacroVar, 1)
CompilerCase 2
SetMacroVar(MacroVar, 2)
CompilerCase 3
SetMacroVar(MacroVar, 3)
; And so on
CompilerEndSelect
EndMacro
SetMacroVar(FOO, 1)
AddMacroVar(FOO, 2)
CompilerWarning "FOO" ; Display "3"