Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.71 KB | None | 0 0
  1. let flip (a:char) : char =
  2.     match a with
  3.     | '0'  -> '1'
  4.     | '1' -> '0'
  5.     | _ -> raise (System.ArgumentException("shit is fucked"))
  6. let findClosing (arr: char array, start:int) : int =
  7.     let mutable openingCount = 0
  8.     let mutable closingIndex = 0
  9.     for i = start to arr.Length - 1 do
  10.         match arr.[i] with
  11.         | '[' -> openingCount <- openingCount + 1
  12.         | ']' when openingCount = 0 -> match closingIndex with
  13.                                        | 0 -> closingIndex <- i
  14.                                        | _ -> ()
  15.         | ']' when openingCount > 0 -> openingCount <- openingCount - 1
  16.         | _ -> ()
  17.     closingIndex
  18. let findOpening (arr: char array, start:int) : int =
  19.     let mutable endingCount = 0
  20.     let mutable openingIndex = 0
  21.     for i = start downto 0 do
  22.         match arr.[i] with
  23.         | ']' -> endingCount <- endingCount + 1
  24.         | '[' when endingCount = 0 -> match openingIndex with
  25.                                       | 0 -> openingIndex <- i
  26.                                       | _ -> ()
  27.         | '[' when endingCount > 0 -> endingCount <- endingCount - 1
  28.         | _ -> ()
  29.     openingIndex
  30. let rec executeInstructions (instructions: char array, tape: char array, instructionsLoc: int, tapeLoc: int) : string =
  31.     match instructionsLoc with
  32.     | i when i = instructions.Length -> new System.String(tape)
  33.     | _ -> match instructions.[instructionsLoc] with
  34.            | '<' -> match tapeLoc with
  35.                     | 0 -> new System.String(tape)
  36.                     | _ -> executeInstructions(instructions, tape, instructionsLoc + 1, tapeLoc - 1)
  37.            | '>' -> match tapeLoc with
  38.                     | i when i = tape.Length - 1 -> new System.String(tape)
  39.                     | _ -> executeInstructions(instructions, tape, instructionsLoc + 1, tapeLoc + 1)
  40.            | '*' -> tape.[tapeLoc] <- flip tape.[tapeLoc]
  41.                     executeInstructions(instructions, tape, instructionsLoc + 1, tapeLoc)
  42.            | '[' -> match tape.[tapeLoc] with
  43.                     | '0' -> executeInstructions(instructions, tape, findClosing(instructions, instructionsLoc + 1), tapeLoc)
  44.                     | _ -> executeInstructions(instructions, tape, instructionsLoc + 1, tapeLoc)
  45.            | ']' -> match tape.[tapeLoc] with
  46.                     | '1' -> executeInstructions(instructions, tape, findOpening(instructions, instructionsLoc - 1), tapeLoc)
  47.                     | _ -> executeInstructions(instructions, tape, instructionsLoc + 1, tapeLoc)
  48.            | _ -> executeInstructions(instructions, tape, instructionsLoc + 1, tapeLoc)
  49. let interpreter (code: string) (tape: string) =
  50. executeInstructions(code.ToCharArray(), tape.ToCharArray(), 0, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement