Last active
July 21, 2017 17:32
-
-
Save luizperes/ea80b7e3f2b5dda887ced3429de9dea5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
type State = { | |
Tape : int[] | |
Index : int | |
} | |
let print_tape state : State = | |
printfn "%A" state.Tape | |
state | |
let shift_stmt v state: State = | |
{ state with Index = (state.Index + v) } | |
let increment_stmt v state : State = | |
let new_value = (Array.get state.Tape state.Index) + v | |
Array.set state.Tape state.Index new_value | |
state | |
let while_stmt state : State = | |
state | |
let stop_rec_stmt state : State = | |
state | |
let print_stmt state: State = | |
printf "%c" (char (Array.get state.Tape state.Index)) | |
state | |
let read_stmt state : State = | |
let c = (Console.Readline()) | |
state | |
let cond code : State = | |
let explode (s:string) = | |
[for c in s -> c] | |
code | |
|> explode | |
|> List.fold | |
(fun acc c -> | |
match c with | |
| '>' -> shift_stmt 1 acc | |
| '<' -> shift_stmt -1 acc | |
| '+' -> increment_stmt 1 acc | |
| '-' -> increment_stmt -1 acc | |
| '[' -> while_stmt acc | |
| ']' -> stop_rec_stmt acc | |
| '.' -> print_stmt acc | |
| ',' -> read_stmt acc | |
| '#' -> print_tape acc | |
| _ -> acc | |
) { Tape = Array.create 100 0; Index = 0 } | |
[<EntryPoint>] | |
let main argv = | |
match argv with | |
| [| first |] -> ignore (cond first) | |
| _ -> failwith "Must have only one arg" | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment