-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hs
53 lines (43 loc) · 1.83 KB
/
main.hs
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
-- BF interpreter
import Data.Char -- ord chr
main = interpretBF (parseStringToBfTerms inputBfProg1) ([],[])
inputBfProg1 = ",.++.>,..."
inputBfProg2 = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.------.--------.-------------------------------------------------------------------.-----------------------."
data BfTerm =
Increment |
Decrement |
Forward |
Back |
Read |
Print
deriving(Show, Eq)
parseStringToBfTerms = map parseCharToBfTerm
parseCharToBfTerm c
| c=='+' = Increment
| c=='-' = Decrement
| c=='.' = Print
| c=='>' = Forward
| c=='<' = Back
| c==',' = Read
--data World' a = YYY ([a],[a])
type World = ([Int], [Int]) -- Tape --type synonym
---showWorld (x, y) = map chr (x++y) --lol, impl trait Show for World?
---g = ([72,101,108],[108,111,33]) :: World
---test_showWorld g = do
--- putStrLn $ showWorld g
interpretBF :: [BfTerm] -> World -> IO ()
interpretBF (x:xs) world = do
newWorld <- applyBfTermToWorld x world
interpretBF xs newWorld
interpretBF [] world = return ()
applyBfTermToWorld :: BfTerm -> World -> IO World
applyBfTermToWorld term ([], (x:xs)) = applyBfTermToWorld term ([0], (x:xs))
applyBfTermToWorld term (zs, []) = applyBfTermToWorld term (zs, [0])
applyBfTermToWorld term (zs, (x:xs))
| term==Increment = do return (zs, succ x :xs)
| term==Decrement = do return (zs, pred x :xs)
| term==Forward = do return (zs++[x], xs)
| term==Back = do return (init zs, last zs :x:xs)
| term==Print = do putChar $ chr x ; return (zs, (x:xs))
| term==Read = do x <- getChar; return (zs, (ord x:xs))
| otherwise = return (zs, (x:xs)) -- do nothing