-
Notifications
You must be signed in to change notification settings - Fork 38
/
Exercise.hs
55 lines (37 loc) · 1.46 KB
/
Exercise.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
54
55
module Y2018.M10.D25.Exercise where
{--
Today's Haskell problem is the same as yesterday's Haskell problem, except that
we are taking on the looping constructs now and building a compiler instead
of an interpreter for our BF* opcodes, which, as you recall are:
.
+
-
<
>
[
]
--}
import Control.Monad.State
import Y2018.M10.D24.Exercise
type CompiledCode m = BFState -> m BFState
-- the compiler takes a BFProgram and returns a lambda that represent the
-- program-fragment, returning the rest of the program to be compiled
compiler :: Monad m => BFProgram -> (BFProgram, [CompiledCode m])
compiler programListing = undefined
-- It may be good to break down the problem of the compiler, having it call
-- a helper function, compileOp, for each op-code to compile
type BF_OP_CODE = Char
compileOp :: Monad m => BF_OP_CODE -> CompiledCode m
compileOp opcode = undefined
-- Also, how do you manage compiling a block of code inside [ ]-loops?
compileBlock :: Monad m => BFProgram -> (BFProgram, CompiledCode m)
compileBlock programListing = undefined
-- note that compileBlock treats the block of code in the [ ]-loop atomically.
-- So, once you have a BF* program compiled, run it with some initial state
runBFProgram :: Monad m => [CompiledCode m] -> StateT BFState m ()
runBFProgram opcodes = undefined
-- run the below BF programs against start. What do you get?
bfprog1, bfprog2 :: BFProgram
bfprog1 = "[.>]"
bfprog2 = "[[-]>]"
-- WHAT? A LOOP WITHIN A LOOP? OH! THE HORROR!