-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compiler.fs
35 lines (28 loc) · 903 Bytes
/
Compiler.fs
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
module BrainFark.Compiler
let fromString (code:string) =
let rec loop body memory =
match Memory.get memory with
| 0uy -> memory
| _ -> memory |> body |> loop body
let f, fs =
code
|> Seq.fold
(fun (f,fs) x ->
match x with
| '>' -> (f >> Memory.moveRight, fs)
| '<' -> (f >> Memory.moveLeft, fs)
| '+' -> (f >> Memory.increment, fs)
| '-' -> (f >> Memory.decrement, fs)
| '.' -> (f >> Memory.write, fs)
| ',' -> (f >> Memory.read, fs)
| '[' -> (id, f::fs)
| ']' ->
match fs with
| f'::fs' -> (f' >> loop f, fs')
| [] -> failwith "unmatched ]"
| _ -> (f,fs)
)
(id,[])
match fs with
| [] -> f
| _ -> failwith "unmatched ["