Source

ocaml-toys / brainfuck / standalone.ml

open Brainfuck

module StdIo = struct
  let putchar c = Printf.printf "%c%!" c
  let getchar () = input_char stdin
end

module StdInterpreter = Interpreter(StdIo)

let brainfuck optimize dump filename =
  let stream = Stream.of_channel (open_in filename) in
  let ast = Parser.parse stream in
  let ast' = if !optimize then Optimizer.optimize ast else ast in
  if !dump then print_endline (ParseTree.string_of_ast ast) else StdInterpreter.exec ast'

let _ =
  let dump = ref false in
  let optimize = ref false in
  let args = [
    ("-optimize", Arg.Set optimize, "optimize ast");
    ("-dump", Arg.Set dump, "dump ast, don't execute");
    ] in
  Arg.parse args (brainfuck optimize dump) "usage"