(* stripcomments.ml - Don Yang (uguu.org) Remove non-BF characters from stream. 09/02/04 *) (* Open first argument for input if available, otherwise use stdin *) let input () = if Array.length Sys.argv > 1 then open_in_bin Sys.argv.(1) else stdin;; (* Open second argument for input if available, otherwise use stdout *) let output () = if Array.length Sys.argv > 2 then open_out_bin Sys.argv.(2) else stdout;; (* Filter out non-BF characters *) let filter outfile c = match c with '[' -> output_char outfile c | ']' -> output_char outfile c | '<' -> output_char outfile c | '>' -> output_char outfile c | '-' -> output_char outfile c | '+' -> output_char outfile c | ',' -> output_char outfile c | '.' -> output_char outfile c | _ -> ();; (* Parse file character by character *) let rec parse_bf infile outfile = let eof = try (filter outfile (input_char infile); 0) with End_of_file -> ((); 1) in if eof != 0 then output_char outfile '\n' else parse_bf infile outfile;; (* Program entry point *) let infile = input() in let outfile = output() in parse_bf infile outfile; close_out outfile; close_in infile;;