4
This question wants you to golf batch programms. In Batch you can use
set x=averylongbatchcommand
%x% arg1
which expands to
set x=averylongbatchcommand
averylongbatchcommand arg1
Your task is it to expand these statements.
As you can also use set
for arithmetic purposes like this:
set x = 0
set /a x= %x%+1
you should ignore the statements where the pattern with set /a
is applied.
There will never be something like
set s=SET
%s% s=%s% a
where the variable is assigned using itself as command. But you have to handle things like
b:
set a =echo
%a% Hello World!
set a=goto
%a% b
Scoring
This is code-golf so the answer with the fewest bytes wins.
Testcases
set s=set
%s% e=echo
%e% I will have to use this exact thing very often because
%e% it absolutely wouldn't be shorter if i just left out these set statements
Expands to:
set s=set
set e=echo
echo I will have to use this exact thing very often because
echo it absolutely wouldn't be shorter if i just left out these set statements
set s=set
%s% i=0
set g=goto
:a
if %i%==15 (
%g% e
echo %i%
)
%s% /a i=%i%+1
%g% a
:e
%s% i=0
%g% a
Expands to:
set s=set
set i=0
set g=goto
:a
if %i%==15(
goto e
echo %i%
)
set /a i=%i%+1
goto a
e:
set i=0
goto a
set s=set
%s% s=%s% a=
Mustn't be supported.
set a=echo
%a% Hi
set a=type
%a% log.txt
Expands to:
set a=echo
echo Hi
set a=type
type log.txt
set x = 0
set /a x= %x%+1
Expands to:
set x = 0
set /a x= %x%+1
I meant you shouldn't expand statements with /a. – Roman Gräf – 2016-10-19T10:08:39.393
2Whats is the valid character set for variable names ? Can variable names have more than 1 character ? What if there is more than one
=
on aset
line ? What is the behaviour of whitespace before or after=
? In particular does space after=
become part of the assigned value ? Can the whitespace be tabs ? What is the behaviour of lines with an odd number of%
? – Ton Hospel – 2016-10-19T10:17:17.043For this question you can assume that every variable name is 1 character from a-z case-insensitive. Also whitespaces before the first
=
are ignored. But everything after the first=
including other=
will be put in the variable. – Roman Gräf – 2016-10-19T10:30:49.5531Why doesn't
i
get set to0
in the second testcase ? – Ton Hospel – 2016-10-19T10:48:20.640@TonHospel I was confused by this as well, but the line before and after that example are meant to be read as one sentence, saying that references inside lines that start with
set /a
should not be substituted. – Martin Ender – 2016-10-19T11:58:42.103@MartinEnder Even so
if %i%==15
should get substituted. Or does ignoring/a
work backward ? And I'd like the op to clarify if it's OK to expand variables inset /a
lines as long as no actual set is executed – Ton Hospel – 2016-10-19T12:12:01.013@TonHospel
%i%
doesn't get replaced because it would require iterating the substutition (in the original input the line that will ultimately seti
doesn't containset
yet). – Martin Ender – 2016-10-19T12:18:04.150@MartinEnder I Still don't understand. In the first testcase that is exactly how
e
gets set – Ton Hospel – 2016-10-19T12:34:05.980@TonHospel Oh, I did not see that. In that case, I agree that the spec is still not clear. Putting on hold until this is resolved. – Martin Ender – 2016-10-19T12:35:44.717