aboutsummaryrefslogtreecommitdiff
path: root/Brainfuck.icl
diff options
context:
space:
mode:
authorCamil Staps2015-05-22 16:59:09 +0200
committerCamil Staps2015-05-22 16:59:09 +0200
commit00ab2235f1621de99779ee94e99781b27d5a2f57 (patch)
treee480cb74c7cb93f1a0855d2b29f586b95fec65a3 /Brainfuck.icl
parentInitial commit (diff)
stdin input
Diffstat (limited to 'Brainfuck.icl')
-rw-r--r--Brainfuck.icl42
1 files changed, 22 insertions, 20 deletions
diff --git a/Brainfuck.icl b/Brainfuck.icl
index 197a54b..b692744 100644
--- a/Brainfuck.icl
+++ b/Brainfuck.icl
@@ -13,27 +13,32 @@ instance == Command where
(==) IFNZ IFNZ = True
(==) _ _ = False
-run :: Program Input *env -> *env | FileSystem env
-run p i world = run` p 0 0 [0,0..] i world
+run :: Program *env -> *env | FileSystem env
+run p world = run` p 0 0 [0,0..] world
where
- run` :: Program InstrPointer DataPointer Tape Input *env -> *env | FileSystem env
- run` p iptr dptr t i world
+ run` :: Program InstrPointer DataPointer Tape *env -> *env | FileSystem env
+ run` p iptr dptr t world
| iptr >= length p = world
- | p!!iptr == INC || p!!iptr == DEC = run` p (iptr + 1) dptr (apply (p!!iptr) dptr t) i world
- | p!!iptr == INCP = run` p (iptr + 1) (dptr + 1) t i world
- | p!!iptr == DECP = run` p (iptr + 1) (dptr - 1) t i world
- | p!!iptr == IN = run` p (iptr + 1) dptr t` i` world
- | p!!iptr == IFZ && t!!dptr <> 0 = run` p (iptr + 1) dptr t i world
- | p!!iptr == IFZ = run` p (forward p iptr) dptr t i world
- | p!!iptr == IFNZ && t!!dptr == 0 = run` p (iptr + 1) dptr t i world
- | p!!iptr == IFNZ = run` p (backward p iptr) dptr t i world
- // must be OUT now
+ | p!!iptr == INC || p!!iptr == DEC = run` p (iptr + 1) dptr (apply (p!!iptr) dptr t) world
+ | p!!iptr == INCP = run` p (iptr + 1) (dptr + 1) t world
+ | p!!iptr == DECP = run` p (iptr + 1) (dptr - 1) t world
+ | p!!iptr == IFZ && t!!dptr <> 0 = run` p (iptr + 1) dptr t world
+ | p!!iptr == IFZ = run` p (forward p iptr) dptr t world
+ | p!!iptr == IFNZ && t!!dptr == 0 = run` p (iptr + 1) dptr t world
+ | p!!iptr == IFNZ = run` p (backward p iptr) dptr t world
+ // must be IN / OUT now
# (io,world) = stdio world
- //# io = io <<< toChar (t!!dptr)
+ | p!!iptr == IN
+ # (ok,c,io) = freadc io
+ | not ok = abort "Couldn't read input"
+ # (ok,world) = fclose io world
+ | not ok = abort "Couldn't close stdio"
+ | otherwise = run` p (iptr + 1) dptr (update t dptr c) world
+ // must be OUT now
# io = io <<< toChar (t!!dptr)
# (ok,world) = fclose io world
| not ok = abort "Couldn't close stdio"
- | otherwise = run` p (iptr + 1) dptr t i world
+ | otherwise = run` p (iptr + 1) dptr t world
where
cast :: Cell -> Cell
cast n
@@ -45,11 +50,8 @@ where
apply INC dptr t = (take dptr t) ++ [cast (t!!dptr + 1)] ++ (drop (dptr + 1) t)
apply DEC dptr t = (take dptr t) ++ [cast (t!!dptr - 1)] ++ (drop (dptr + 1) t)
- (t`,i`) = input t dptr i
- input :: Tape DataPointer Input -> (Tape, Input)
- input _ _ [] = abort "No input"
- input t dptr [i:is]
- | otherwise = ((take dptr t) ++ [i] ++ (drop (dptr + 1) t), is)
+ update :: Tape DataPointer Char -> Tape
+ update t ptr c = (take ptr t) ++ [fromChar c : drop (ptr + 1) t]
forward` :: Program InstrPointer Int -> InstrPointer
forward` _ ptr 0 = ptr