Anonymous user
Execute Brain****/F Sharp: Difference between revisions
no edit summary
m (add horizontal scrolling) |
No edit summary |
||
Line 2:
<br clear=all/>
<div style='width: full; overflow: scroll'><lang fsharp>open System
let
let pgm = Seq.toArray pgmStr // Turn program into an array
let ptr = ref 0 // Pointer into input
let ip = ref 0 // Instruction Pointer
let input = ref (Array.create 100 0)
let InputNumber() =
Line 13:
let mutable num = 0
printfn "Enter a valid number"
fValid <-
while not fValid do
printfn "Invalid input. Please enter a valid number"
fValid <-
num
let InputAscii() =
printfn "Enter an ascii character"
let chOut = (Console.ReadKey().KeyChar)
printfn ""
int chOut
let AdvanceIp() = ip := !ip + 1 // Advance IP
Line 23 ⟶ 29:
let SkipBrackets fForward =
if (!input).[!ptr] <> 0 then // If we have a 0 input
AdvanceIp() // Then just advance to next instruction
else // otherwise
Line 36 ⟶ 42:
| _ -> ignore(0) // Ignore anything else
AdvanceIp() // When we're on the matching bracket, skip to the next instruction
let MovePtr fRight =
if fRight then
ptr := !ptr + 1
if !ptr >= (!input).Length then
Array.Resize(input, (!input).Length + 20)
else
ptr := !ptr - 1
if !ptr < 0 then
let newInput = Array.create ((!input).Length + 20) 0
Array.ConstrainedCopy(!input, 0, newInput, 20, (!input).Length)
ptr := 19
let interpretCmd() =
match pgm.[!ip] with
| '>' ->
| '<' ->
| '+' -> (!input).[!ptr] <- (!input).[!ptr] + 1; AdvanceIp()
| '-' -> (!input).[!ptr] <- (!input).[!ptr] - 1; AdvanceIp()
| '.' -> printf "%c" (char (!input).[!ptr]); AdvanceIp()
| ',' -> (!input).[!ptr] <- InputNumber(); AdvanceIp()
| '~' -> (!input).[!ptr] <- InputAscii(); AdvanceIp()
| '[' -> SkipBrackets true
| ']' -> SkipBrackets false
| _ ->
while !ip >= 0 && !ip < pgm.Length do
|