-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrainfuck.ml
46 lines (40 loc) · 1.58 KB
/
brainfuck.ml
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
(* a quick Brainf*ck interpreter *)
let code, codelen, jump =
let b = Buffer.create 1024 in
let jumps = Hashtbl.create 16 in
let add x y = Hashtbl.add jumps x y in
let rec load stack i = match input_char stdin with
| '>'|'<'|'+'|'-'|'.'|','|'['|']' as c ->
Buffer.add_char b c;
let stack = match c, stack with
| '[', _ -> i :: stack
| ']', [] -> Format.eprintf "unmatched ']'@."; exit 1
| ']', b :: stack -> add b (i + 1); add i b; stack
| _ -> stack in
load stack (i + 1)
| _ -> load stack i
| exception End_of_file ->
if stack <> [] then (Format.eprintf "unmatched '['@."; exit 1) in
load [] 0;
let code = Buffer.contents b in
let len = String.length code in
let jump = Array.make len 0 in
Hashtbl.iter (fun x y -> jump.(x) <- y) jumps;
code, len, jump
let zero = Char.chr 0
let mem = Bytes.make 30_000 zero
let get i = Bytes.get_uint8 mem i
let set i n = Bytes.set_uint8 mem i n
let rec exec pc ptr = if pc < codelen then match code.[pc] with
| '>' -> exec (pc + 1) (ptr + 1)
| '<' -> exec (pc + 1) (ptr - 1)
| '+' -> set ptr (succ (get ptr) land 255); exec (pc + 1) ptr
| '-' -> set ptr (pred (get ptr)); exec (pc + 1) ptr
| '.' -> let c = Bytes.get mem ptr in output_char stdout c;
if c = '\n' then flush stdout;
exec (pc + 1) ptr
| ',' -> Bytes.set mem ptr (input_char stdin); exec (pc + 1) ptr
| '[' -> exec (if get ptr = 0 then jump.(pc) else pc + 1) ptr
| ']' -> exec (if get ptr <> 0 then jump.(pc) else pc + 1) ptr
| _ -> assert false
let () = exec 0 0; flush stdout