Page 1 of 1

Batch file macro and SNAKE.BAT

Posted: 31 Dec 2019 02:19
by Meerkat
I am new to batch macros and researching topics here in DosTips. I am aware of Macros with parameters appended and it's awesome, but I am specifically trying to implement macros that have no parameters, just manipulate variables and/or echo something, etc. Shown below is my test:

Code: Select all

@echo off
setlocal disabledelayedexpansion
(set \n=^^^
%== this creates escaped line feed for macro ==%
)

set macro0=set "x=1"^
	^& set "y=2"

set macro1=set "x=1" %\n%
	set "y=2"

set macro2=%\n%
	set "x=1"%\n%
	set "y=2"

set macro3=(%\n%
	set "x=1"  %\n%
	set "y=2"  %\n%
)

setlocal enabledelayedexpansion

set x=&set y=
%macro0%
echo(macro0 results: x="%x%";y="%y%"

set x=&set y=
%macro1%
echo(macro1 results: x="%x%";y="%y%"

set x=&set y=
%macro2%
echo(macro2 results: x="%x%";y="%y%"

set x=&set y=
%macro3%
echo(macro3 results: x="%x%";y="%y%"
Command Prompt wrote:macro0 results: x="1";y="2"
macro1 results: x="1";y=""
macro2 results: x="";y=""
macro3 results: x="1";y="2"
macro0 is the earliest I found here in the forum, and works well. macro1 and macro2 are pitfalls: I think only the same line as "set macro=" is truly executed. My solution is to surround the macro with parentheses, as with macro3.

My question is regarding the macros in Dave Benham's SNAKE.BAT. Why does this work well, while macro1 and macro2 do not? Sample snippet from SNAKE.BAT (arrows added by me):

Code: Select all

:: draw
:::  draws the board
set draw=%\n%          <------ 
set screen=%\n%        <------
for /l %%Y in (0,1,!height!) do set screen=!screen!!line%%Y!!LF!%\n%
...
Thanks and happy new year everyone!

Meerkat

Re: Batch file macro and SNAKE.BAT

Posted: 31 Dec 2019 03:45
by jeb
Hi Meerkat,

the parenthesis are necessary in macros, when you want to use the %\n% for multiple lines.
That's because a raw line feed outside parenthesis stops the parser and the remaining stuff will be dropped.

You can't compare it to the code from Snake.bat, as the code you shown is part of a function.
The %\n% will stop the parser for that line, but that doesn't hurt, as there is no more code on that line.
It's simply another way of undefine/empty variables. I would use set "draw=" instead.

Code: Select all

set draw=%\n%
set screen=%\n%

Re: Batch file macro and SNAKE.BAT

Posted: 09 Jan 2020 11:09
by Meerkat
jeb wrote:
31 Dec 2019 03:45
the parenthesis are necessary in macros, when you want to use the %\n% for multiple lines.
That's because a raw line feed outside parenthesis stops the parser and the remaining stuff will be dropped.
From the quote, I just found out the answer to my question now (~10 days since asking :oops:). Upon scanning lines of code of SNAKE.BAT, I found out that when macros should be executed outside a parenthesis block, it is simply surrounded by parentheses in the code for them to work. For example, getKey is defined like this:

Code: Select all

set getKey=%\n%
for %%# in (1 2) do if %%#==2 (%\n%
  set key=%\n%
  set inKey=%\n%
  ...
Shown below is snippet of SNAKE.BAT outside a parenthesis block. Arrowed is the %getkey% "call".

Code: Select all

...
<nul set /p "=%~1 "
call :purge
:getStringLoop
(%getKey% !upper! 0 1 2 3 4 5 6 7 8 9 " " _ - {Enter} !BS!)    <------------
if defined key (
  if !key! equ {Enter} (
    echo(
    exit /b
  )
...
For the opposite, when macro "calls" in code have no surrounding parentheses, I found them inside FOR loops in which the DO clause is a parenthesis block, hence the macros work.

Thanks a lot!

Meerkat