https://wiki.haskell.org/index.php?title=Short_examples/BF_interpreter&feed=atom&action=history
Short examples/BF interpreter - Revision history
2024-03-28T13:37:35Z
Revision history for this page on the wiki
MediaWiki 1.35.5
https://wiki.haskell.org/index.php?title=Short_examples/BF_interpreter&diff=64344&oldid=prev
Atravers: Added "Code" category
2021-04-26T01:12:42Z
<p>Added "Code" category</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 01:12, 26 April 2021</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>This is [[User:SamB|SamB]]'s <del class="diffchange diffchange-inline">Brainf*ck</del> interpreter.</div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>This is [[User:SamB|SamB]]'s <ins class="diffchange diffchange-inline">BF</ins> interpreter.</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><haskell></div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><haskell></div></td>
</tr>
<tr>
<td colspan="2" class="diff-lineno">Line 50:</td>
<td colspan="2" class="diff-lineno">Line 50:</td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (either print (`evalStateT` tape)) (parse parseInstrs name source)</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (either print (`evalStateT` tape)) (parse parseInstrs name source)</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></haskell></div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></haskell></div></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Code]]</div></td>
</tr>
</table>
Atravers
https://wiki.haskell.org/index.php?title=Short_examples/BF_interpreter&diff=12726&oldid=prev
SamB: Copy over [http://www.haskell.org/hawiki/ShortExamples_2fBFInterpreter?action=recall&date=1133302643]
2007-05-01T17:54:23Z
<p>Copy over [http://www.haskell.org/hawiki/ShortExamples_2fBFInterpreter?action=recall&date=1133302643]</p>
<p><b>New page</b></p><div>This is [[User:SamB|SamB]]'s Brainf*ck interpreter.<br />
<br />
<haskell><br />
import Text.ParserCombinators.Parsec<br />
import Control.Monad (sequence_)<br />
import Control.Monad.State<br />
import Foreign<br />
import System (getArgs)<br />
import Prelude hiding (read)<br />
<br />
type BF a b = StateT (Ptr a) IO b<br />
read :: (Storable a, Integral a) => BF a a<br />
write :: (Storable a, Integral a) => a -> BF a ()<br />
read = do p <- get; liftIO (peek p)<br />
write x = do p <- get; liftIO (poke p x)<br />
<br />
loop :: (Storable a, Integral a) => BF a b -> BF a ()<br />
loop body = do x <- read;<br />
when (x /= 0) (body >> loop body)<br />
<br />
putc, getc, prev, next, decr, incr<br />
:: (Storable a, Integral a) => BF a ()<br />
<br />
putc = read >>= (liftIO . putChar . toEnum . fromEnum)<br />
getc = liftM (toEnum . fromEnum) (liftIO getChar) >>= write<br />
<br />
prev = do p <- get; put (advancePtr p (-1))<br />
next = do p <- get; put (advancePtr p 1)<br />
decr = do x <- read; write (x-1)<br />
incr = do x <- read; write (x+1)<br />
<br />
parseInstrs :: (Storable a, Integral a) => Parser (BF a ())<br />
parseInstr :: (Storable a, Integral a) => Parser (BF a ())<br />
<br />
parseInstr = liftM loop (between (char '[') (char ']') parseInstrs)<br />
<|> (char '<' >> return prev)<br />
<|> (char '>' >> return next)<br />
<|> (char '-' >> return decr)<br />
<|> (char '+' >> return incr)<br />
<|> (char '.' >> return putc)<br />
<|> (char ',' >> return getc)<br />
<|> (noneOf "]" >> return (return ()))<br />
<br />
parseInstrs = liftM sequence_ (many parseInstr)<br />
<br />
<br />
main = do [name] <- getArgs<br />
source <- readFile name<br />
tape <- (mallocBytes 10000 :: IO (Ptr Word8))<br />
(either print (`evalStateT` tape)) (parse parseInstrs name source)<br />
</haskell></div>
SamB