Advertisement
SlimeAlikoum

brainfuck interpreter

Dec 12th, 2023 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.71 KB | None | 0 0
  1. let mystere x =
  2.     let (%) x y = if x < 0 then y-1 else x mod y in
  3.     let ($) x y = if x = y then 1 else -1 in
  4.     let t = Array.make 30000 0 in
  5.     let f = open_in x in
  6.     let rec aux i l =
  7.         try let c = input_char f in match c with
  8.         | '>' | '<' -> aux ((i+(c$'>'))%30000) l
  9.         | '[' -> aux i ((pos_in f)::l)
  10.         | ']' -> let h::q = l in
  11.             if t.(i) = 0 then aux i q
  12.             else (seek_in f h; aux i l)
  13.         |_->(match c with
  14.         | '+' | '-' -> t.(i) <- (t.(i)+(c$'+'))%256
  15.         | '.' -> print_char (Char.chr t.(i)); flush stdout
  16.         | ',' -> t.(i) <- Char.code (input_char stdin)
  17.         |_->()); aux i l
  18.         with _ -> close_in f
  19.     in aux 0 [] ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement