All this macro stuff in wonderful! I understand this is an advanced topic that challenge and amuse experts (like all of you, people!). However, my main concern is to made possible that average people may have access to advanced techniques like this one, so I have a somewhat different point of view than yours...
I slightly modified the callMacro variable and renamed it this way:set ForMacroParamsIn=for /F "tokens=1-9 delims=,;= " %%1 in
so the execution of a macro with parameters is achieved this way:%ForMacroParamsIn% ("Param1 Param2 ...") DO %macroName%
The DELIMS value set the same standard delimiters of normal Batch processing. The TOKENS value, combined with %%1 replaceable parameter, set the macro parameters to %1, %2, ... %9, the same parameters of a normal Batch function (yes, it works!).
Note that the macro definition must NOT include the DO part, just the enclosing parentheses; this means that the same macro may be executed with no parameters this way:%macroName%
If a macro is executed this way and it use %1..%9 replaceable parameters, they will be replaced by the caller program parameters in the usual way (by adding the standard CALL).
This way, a normal subroutine may be directly translated to a macro with no further modifications (just the standard macro additions, like duplicating %) provided that it comply with the macro requirements: have not GOTO's, use not %normal% expansions but only !delayed! ones, etc. I am preparing a series of advices on things like convert a ":LOOP-GOTO LOOP" cycle in a "COUNT-FOR /L !COUNT!" one and similar tasks. This mean that a (normal!) Batch user may develop a series of subroutines in the usual way and then convert they to macros using a translator program, so he/she may have access to macro benefits (faster execution speed, is there any other one?) with no need to understand the ^^^^^!%%%%!^& Batch macro language mumbo jumbo.
I am currently working in the development of SUBTOMACRO.BAT, a subroutine-to-macro conversion program that will work with these specifications:
1- A "Subroutine Library" is a LIBNAME.BAT file that contain several subroutines written under macro constraints. Each routine starts at :SubName
line and ends at EXIT /B [exitCode]
2- SUBTOMACRO.BAT program takes a Subroutine Library and create an equivalent Macro Library.
3- A "Macro Library" is a LIBNAME.macrodef.bat file that contain several SET ^"LIBNAME.SubName=(...)"
macro definition commands and a routine that allows to define just certain macros. Use it this way:
Code: Select all
setlocal DisableDelayedExpansion required in LIBNAME.macrodef files
call ReserveEnvironmentSpace #KB if you will load a large amount of macros
call LIBNAME1.macrodef load all macros from LIBNAME1
call LIBNAME2.macrodef name1 name2 load just these macros from LIBNAME2
setlocal EnableDelayedExpansion if it is required
rem Continue here with your program code
4- SUBTOMACRO.BAT is a Macro Library maintenance program. Use it this way: CALL SUBTOMACRO LIBNAME [/L:Lib[:Macro...] ...] [[-]SubName ...]
If LIBNAME.macrodef.bat file does not exist, it is created with all the subroutines of LIBNAME.bat file, or just with the SubName's given.
If LIBNAME.macrodef.bat exists, these maintenance operations are achieved on it:
- Add a new SubName definition if its macro does not exist.
- Replace an existing macro with the same SubName name definition.
- Delete an existing macro if the SubName given is preceded by dash.
When subroutines are being converted to macros, nested CALL's to another subroutines are replaced by an embedded expansion of the equivalent macro execution if such macro was previously defined; to achieve this, the CALL must be the only or last command in the line. This point means that the order of macro definitions must obey the same rules of regular programming languages (like Pascal or C) with no prototype definitions nor FORWARD references; these rules are summarized this way: to embed a nested macro execution in a new macro definition, such nested macro must be previously defined; otherwise the new macro definition will effectively CALL the nested subroutine.
Before SUBTOMACRO edit an existing Macro Library it loads all previously defined macros in that library, so dependencies to subroutines/macros of the same library are automatically resolved. However, if a subroutine call a subroutine/macro in another library, such macro must be previously defined in order to be inserted in the new definition. The /L (library load) switch define the library, or which macros of a given library, must be loaded before the editing process start. For example:CALL SUBTOMACRO LIBNAME1 /L:LIBNAME2 /L:LIBNAME3:nameX:nameY NewSub1 NewSub2