Skip to content

Instantly share code, notes, and snippets.

@lmdexpr
Created April 1, 2015 14:55
Show Gist options
  • Save lmdexpr/684bf13cba0238e07163 to your computer and use it in GitHub Desktop.
Save lmdexpr/684bf13cba0238e07163 to your computer and use it in GitHub Desktop.
-- Original Brain F*ck code
++++++++[>+++++++++<-]>.<+++++[>++++++<-]>-.<++[>+++<-]>+..+++.<++++++[>----<-]>.<++++++[>++++<-]>.+++.------.--------.
-- Solis_mb-lang code
llllllllmSllllllllloibSsolllllmSlllllloibSisollmSllloibSlsslllsollllllmSiiiioibSsollllllmSlllloibSslllsiiiiiisiiiiiiiis
let (>>) f g = f |> ignore |> g
let eval program =
let mem = Array.make 65536 0
and stk = Stack.create ()
and ptr = ref 0
and op = String.get program
in let rec rewind_to_end pc =
match op pc with
| 'm' -> rewind_to_end (pc + 1) |> rewind_to_end
| 'b' -> pc + 1
| _ -> rewind_to_end @@ pc + 1
and eval_op pc =
match op pc with
| 'm' when mem.(!ptr) = 0 -> rewind_to_end @@ pc + 1
| 'b' -> Stack.top stk
| op -> ((match op with
| 'm' -> Stack.push pc stk
| 'S' -> ptr := !ptr + 1
| 'o' -> ptr := !ptr - 1
| 'l' -> mem.(!ptr) <- mem.(!ptr) + 1
| 'i' -> mem.(!ptr) <- mem.(!ptr) - 1
| 's' -> print_char @@ Char.chr mem.(!ptr)
| '_' -> mem.(!ptr) <- Char.code @@ input_char stdin
| ' ' -> ()
| _ -> failwith @@ "SyntaxError: " ^ Char.escaped op
);
pc + 1)
in let rec eval_bf = function
| pc when pc >= String.length program -> ()
| pc -> pc |> eval_op |> eval_bf
in eval_bf 0
let () = print_string "input code:" >> read_line |> eval >> print_newline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment