diff options
-rw-r--r-- | Brainfuck.dcl | 3 | ||||
-rw-r--r-- | Brainfuck.icl | 42 | ||||
-rw-r--r-- | ExecBrainfuck.icl | 2 |
3 files changed, 24 insertions, 23 deletions
diff --git a/Brainfuck.dcl b/Brainfuck.dcl index 672fb04..16bb99a 100644 --- a/Brainfuck.dcl +++ b/Brainfuck.dcl @@ -10,9 +10,8 @@ import StdEnv, StdList :: Tape :== [Cell] :: DataPointer :== Int -:: Output :== [Cell] :: Input :== [Cell] -run :: Program Input *env -> *env | FileSystem env +run :: Program *env -> *env | FileSystem env read :: String -> Program 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 diff --git a/ExecBrainfuck.icl b/ExecBrainfuck.icl index 84dd546..260410d 100644 --- a/ExecBrainfuck.icl +++ b/ExecBrainfuck.icl @@ -7,7 +7,7 @@ Start world # (io,world) = stdio world # (program,io) = freadline io # (ok,world) = fclose io world -# world = run (read program) [] world +# world = run (read program) world # (io,world) = stdio world # io = io <<< '\n' # (ok,world) = fclose io world |