forked from mukeshtiwari/Idris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrainfuck.idr
81 lines (67 loc) · 2.51 KB
/
Brainfuck.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module Main
data BrainfuckCommand : Type where
GoRight : BrainfuckCommand
GoLeft : BrainfuckCommand
Increment : BrainfuckCommand
Decrement : BrainfuckCommand
Print : BrainfuckCommand
Read : BrainfuckCommand
Loop : List BrainfuckCommand -> BrainfuckCommand
data Tape : Type where
RTape : (xs : Stream Int) -> (r : Int) -> (ys : Stream Int) -> Tape
emptyTape : Tape
emptyTape = RTape (repeat 0) 0 (repeat 0)
moveRight : Tape -> Tape
moveRight (RTape xs x (y :: ys)) = RTape (x :: xs) y ys
moveLeft : Tape -> Tape
moveLeft (RTape (x :: xs) y ys) = RTape xs x (y :: ys)
tapeState : Tape -> List BrainfuckCommand -> IO Tape
tapeState x [] = return x
tapeState (RTape xs x (y :: ys)) (GoRight :: zs) =
tapeState (RTape (x :: xs) y ys) zs
tapeState (RTape (x :: xs) y ys) (GoLeft :: zs) =
tapeState (RTape xs x (y :: ys)) zs
tapeState (RTape xs r ys) (Increment :: zs) =
tapeState (RTape xs (r + 1) ys) zs
tapeState (RTape xs r ys) (Decrement :: zs) =
tapeState (RTape xs (r - 1) ys) zs
tapeState (RTape xs r ys) (Print :: zs) =
do
putChar . chr $ r
tapeState (RTape xs r ys) zs
tapeState (RTape xs r ys) (Read :: zs) =
do
c <- getChar
tapeState (RTape xs (ord c) ys) zs
tapeState (RTape xs 0 ys) (Loop ins :: zs ) =
tapeState (RTape xs 0 ys) zs
tapeState (RTape xs r ys) (Loop ins :: zs) =
do
tps <- tapeState (RTape xs r ys) ins
tapeState tps (Loop ins :: zs)
parseSource : List Char -> (List BrainfuckCommand, List Char)
parseSource [] = ([], [])
parseSource ('+' :: xs) =
let (comm, rest) = parseSource xs in (Increment :: comm, rest)
parseSource ('-' :: xs) =
let (comm, rest) = parseSource xs in (Decrement :: comm, rest)
parseSource ('>' :: xs) =
let (comm, rest) = parseSource xs in (GoRight:: comm, rest)
parseSource ('<' :: xs) =
let (comm, rest) = parseSource xs in (GoLeft :: comm, rest)
parseSource ('.' :: xs) =
let (comm, rest) = parseSource xs in (Print :: comm, rest)
parseSource (',' :: xs) =
let (comm, rest) = parseSource xs in (Read :: comm, rest)
parseSource ('[' :: xs) =
let (comm, rest) = parseSource xs
(comm', rest') = parseSource rest
in (Loop comm :: comm', rest')
parseSource (']' :: xs) = ([], xs)
parseSource (_ :: xs) = parseSource xs
parseInput : String -> List BrainfuckCommand
parseInput str = fst (parseSource (unpack str))
main : IO ()
main = do
_ <- tapeState emptyTape (parseInput "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.")
return ()