Problem with variables?

November 23, 2010 at 16:19:06
Specs: Windows 7
Hey guys, I've been trying to make a BrainF*** (See http://en.wikipedia.org/wiki/Brainf... Interpreter, but for some reason it's not working... Here is a test I made:

@ECHO off
TITLE Brainf*** Interpreter
FOR /L %%a IN (0,1,256) DO (
SET number=%%a
SET cell%number%=0
)
PAUSE>nul
ECHO %cell1%
ECHO %cell2%
ECHO %cell10%
ECHO %cell50%
ECHO %cell100%
ECHO %cell200%
ECHO %cell256%
PAUSE>nul
ECHO.
ECHO %cell257%
PAUSE>nul

After the for loop, it's supposed to run a couple of tests. It is supposed to display the values for numerous cells, but for some reason, It is displaying "ECHO is off." As for the cell257, that is supposed to display an error, but it also displays "ECHO is off." Does anyone know why? Thanks!


See More: Problem with variables?

Report •


#1
November 23, 2010 at 16:42:08
nvm, I figured it out, but does anyone know how to call a variable from within a variable like this: (%outer(%inner%)%)?

Report •

#2
November 23, 2010 at 19:55:48
Enable delayed expansion, then use "!Var1%Var2%!"
====
SETLOCAL EnableDelayed Expansion

ECHO !Var1%Var2%!

Can I put a question here?


Report •

#3
November 23, 2010 at 20:01:30
!out%in%!


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
November 24, 2010 at 10:46:09
Yes! Thank you so much! But now I'm having trouble figuring out how to work the "[" "]" commands into the interpreter using Batch :(....

Report •

#5
November 24, 2010 at 12:32:33
Please be more specific. Despite what you may think we aren't psychic. What are the "[" "]" commands?

Can I put a question here?


Report •

#6
November 24, 2010 at 21:03:53
Yes, I know that you aren't psychic, but I specifically gave a link to the wikipedia article of Brainf*** in my first post so you could understand what I'm talking about without me having to explain it all. (no anger intended). Please check the article for more info about the 8 commands of Brainf***, and if I finish this project, I'll gladly send a download link to anyone who helps me! :)

Report •

#7
November 24, 2010 at 22:09:55
I'm glad it ain't just me who lost the plot.


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
November 24, 2010 at 23:25:35
what do you mean?

Report •

#9
November 25, 2010 at 00:19:30
After poking around that wiki link for 15 minutes I don't know what the [ issue is.


=====================================
Life is too important to be taken seriously.

M2


Report •

#10
November 25, 2010 at 11:06:29
oh whoops, i must've given you the wrong link, here is the real one: http://en.wikipedia.org/wiki/Brainf---

Report •

#11
November 25, 2010 at 11:07:17
Dammit, it keeps on censoring the f*** part, just search brain f u c k on wikipedia. sorry :)

Report •

#12
November 25, 2010 at 12:55:08
I read over it, but I still don't understand your question...

What exactly are you trying to do?

Can I put a question here?


Report •

#13
November 25, 2010 at 13:41:49
You input a brainf*** code, and it interprets it and runs it. Unforunately, so far you can only run one command at a time, which is a great limitation on the functionality of BF, but I'll sort that out later. Apparently, my friend, the guy who came up with the idea, says that the "[" "]" commands are grouping/looping commands, although I'm still trying to understand it completely. My friend does not know any batch, or any programming language at all, so I thought I'd take up a little challenge. Unfortunately, I am an amateur programmer, and can barely make a program without someone's help. I have no idea how to incorporate grouping commands into a Batch BF Interpreter. If anyone is willing to help me with the interpreter itself at all, I would be very grateful! Plus, I think it is fun to make a BF interpretr, because BF is a really cool esoteric PL!

Also, sorry for that long, pointless explanation, but I thought you may want to know what I was doing in the first place.


Report •

#14
November 26, 2010 at 19:21:59
ditto for me


=====================================
Life is too important to be taken seriously.

M2


Report •

#15
November 26, 2010 at 23:53:32
what do you mean?

Report •

#16
November 27, 2010 at 00:30:10
a response to #12... got crossed up


=====================================
Life is too important to be taken seriously.

M2


Report •

#17
November 27, 2010 at 13:40:41
Mechanix2Go & Matt123: Confusion
http://en.wikipedia.org/wiki/Brainf...
"[" and "]" element loops. "[" jumps to the right of the matching "]" if the current value is zero.

Also: I'm better at getting past the censor than you. :P

EDIT:
Bloblblobl:
Personally, I'd pre-parse the input string, and build a list of "]" and their corresponding "[". That said, batch is not a good language to do this; bf depends on "<" and ">". Neither are handled well by CMD. Feel like posting what you got?

EDIT 2:
Second thought: Bf specifies a block of memory of 30,000 byte-elements. This shouldn't be an issue pre-Vista, but both Vista and Win7 limit the environment block to 5,120 characters. (Actually, exceeding the environment limit of XP doesn't seem to be a problem; presumably CMD has its own environment stack, independent of the system maintained block.)

How To Ask Questions The Smart Way


Report •

#18
November 28, 2010 at 08:33:59
Hi R2,

I read the ] usage on the wiki article.

I'm still waiting to find out what the question is.


=====================================
Life is too important to be taken seriously.

M2


Report •

#19
November 28, 2010 at 11:48:54
Ok, here's what I got so far, but just let me tell you I am not very good at batch:

FOR /L %%a IN (0,1,255) DO (
SET cell%%a=0
ECHO Current Cell: Cell%%a
)
PAUSE>nul
SET current=0

:command
SET /P command=">>> "
IF %command%==+ GOTO plus
IF %command%==- GOTO minus
IF %command%==> GOTO next
IF %command%==< GOTO prev
IF %command%==. GOTO output
IF %command%==, GOTO input
IF %command%==[ GOTO begin
IF %command%==] GOTO end
CLS
GOTO command

:plus
SET current+=1
IF %current% GTR 255 GOTO toohigh

:minus
SET current-=1
IF %current% LSS 0 GOTO toolow


Report •

#20
November 28, 2010 at 12:26:08
Dunno what this is about:

SET /P command=">>> "

If you want to manipulate arithmetic values you need:

SET /a current+=1


=====================================
Life is too important to be taken seriously.

M2


Report •

#21
December 1, 2010 at 18:26:06
Well, your first problem is your input; you're entering 1 command character at a time, so it'd be fundamentally impossible to run to the matching brace. You need to enter / read the entire command buffer at one time, before any real work gets done.

EDIT: Consider the following program. It's less than ideal, but probably the best you'll get with CMD. If anyone wants to work out when it'll break, feel free.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET _GT=^>
SET _LT=^<
SET cmd=
FOR /F "tokens=*" %%a IN (in.txt) DO (SET _line="%%a" &CALL :BuildCmd)
ECHO !cmd!
GOTO :EOF

:BuildCmd
SET _cnt=-1
REM Note: You must test for '>' and '<' using !_GT! and !_LT!, not "^>" and "^<"
:BuildCmdLoop
SET /A _cnt += 1
SET chr=!_line:~%_cnt%,1!
IF "!chr!"=="!_GT!" SET cmd=!cmd!^>
IF "!chr!"=="!_LT!" SET cmd=!cmd!^<
IF "!chr!"=="+" SET cmd=!cmd!+
IF "!chr!"=="-" SET cmd=!cmd!-
IF "!chr!"=="." SET cmd=!cmd!.
IF "!chr!"=="," SET cmd=!cmd!,
IF "!chr!"=="[" SET cmd=!cmd![
IF "!chr!"=="]" SET cmd=!cmd!]
IF "!chr!"=="" GOTO :EOF
GOTO BuildCmdLoop

How To Ask Questions The Smart Way


Report •

#22
December 1, 2010 at 21:26:00
Thanks! I knew that the problem was with the input, I just didn't know how to make batch break apart strings and read commands seperately. So this will break down the string it gets? It doesn't look as complicated as I thought it would...

Report •

#23
December 2, 2010 at 08:16:18
Making a BF interpreter requires the following steps:
A) Reading program
    - CMD consideration: '<' and '>' characters
B) Executing program
    - Consideration: Nested loops
C) Getting input
D) Displaying output
    - CMD: Probably would have to set up an array to map numbers to characters
        * Most characters should be possible, excluding Ctrl-Z and '!', 
          and the other control-type characters.

How To Ask Questions The Smart Way


Report •

#24
December 2, 2010 at 15:46:41
So what you submitted would be the "Reading the program" part? Or just part of it?

Report •

#25
December 2, 2010 at 16:02:13
Most of it; the loop handling bit of part B might require some logic either in part A or between A and B.

How To Ask Questions The Smart Way


Report •

#26
December 2, 2010 at 16:24:39
Thanks! I'll start working on it some more this weekend. I now know who to turn to if I have problems :D

Report •

Ask Question