Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!eu.feeder.erje.net!news.ecp.fr!aioe.org!.POSTED!not-for-mail From: "Rod Pemberton" Newsgroups: comp.lang.forth Subject: Re: Brainfuck in ANS Forth Date: Thu, 01 May 2014 16:08:22 -0400 Organization: Aioe.org NNTP Server Lines: 117 Message-ID: References: NNTP-Posting-Host: CNsg4fVcCsvs3UaOgZtQCw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.16 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.forth:30045 On Thu, 01 May 2014 11:31:38 -0400, Mark Wills wrote: > Just for fun... > >> Increment the pointer. > < Decrement the pointer. > + Increment the byte at the pointer. > - Decrement the byte at the pointer. > . Output the byte at the pointer. > , Input a byte and store it in the byte at the pointer. > [ Jump forward past the matching ] if the byte at the pointer is zero. > ] Jump backward to the matching [ unless the byte at the pointer is > zero. > > > > create array > 8000 chars allot The array is usually 32KB. > variable pointer > array pointer ! > > : reset ( -- ) > \ zero bf memory and reset pointer > array 8000 0 fill > array pointer ! ; 0 fill. Good. Officially, BF doesn't need that. But, I found that some BF programs wont' work without it, and all seem to work with it. > : peek ( -- ) > pointer @ c@ ; > : poke ( -- ) > pointer @ c! ; ... > : > ( -- ) > \ increment the pointer > pointer @ 1+ pointer ! ; How common is +! ? It seems I picked it up from fig-Forth and one critical word needs it. > : < ( -- ) > \ decrement the pointer > pointer @ 1- pointer ! ; > ... > : + ( -- ) > \ increment the byte at the pointer > peek 1+ poke ; > : - ( -- ) > \ decrement the byte at the pointer > peek 1- poke ; If you're using peek and poke, why not words for 'pointer @' and 'pointer !' ... > : . ( -- ) > \ output the byte at the pointer > peek . ; . or EMIT ? Shouldn't it be EMIT ? It's a value for a character. > : , ( -- ) > \ input a byte and store it in the byte at the pointer > pad 3 expect pad 3 number drop poke ; Um, did ANS Forth remove KEY ? > : [ \ ( -- ) > \ jump forward past the matching ] if the byte at the pointer is zero > [compile] begin compile peek [compile] while ; immediate > : ] ( -- ) > \ jump backward to the matching [ unless the byte at the pointer is > zero > [compile] repeat ; immediate ... > : hw > + + + + + + + + [ > + + + + [ > + + > + + + > + + + > + < < < < - ] > > + > > + > - > > + [ < ] < - ] > > . > - - - . + + + + + + + . . + + + . > > > > . < - . < . + + + . - - - - - - . - - - - - - - - . > > + . > + + . ; > Ha! Each one has a space after them for Forth space parsing. And, it's a colon-def. Awkward. > Test: > > hw > 72 101 108 108 111 32 87 111 114 108 100 33 10 > > Note: BrainFuck can't emit characters, only numbers. So the Hello World! > example above (taken from WikiPedia) outputs the ASCII *values* of Hello > World! complete with a carriage return. > > http://en.wikipedia.org/wiki/Brainfuck No. You've misunderstood something. It's supposed to emit ASCII text "Hello World!" with linefeed (ASCII LF 0x0A). The . in BF emits the ASCII character for the value. If you look at the C equivalent for . and , on the Wikipedia page, they use getchar() and putchar() in C which fetches a char and emits a char, respectively. Rod Pemberton