-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReader.fs
49 lines (44 loc) · 1.35 KB
/
Reader.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Brainfuck.Reader
open System.IO
open Brainfuck.Primitives
let read (reader: TextReader) =
let mutable line = 1
let mutable col = 1
let locations = ResizeArray()
[ while reader.Peek() <> -1 do
match char (reader.Read()) with
| '>' ->
yield IncrementPointer
col <- col + 1
| '<' ->
yield DecrementPointer
col <- col + 1
| '+' ->
yield IncrementCell
col <- col + 1
| '-' ->
yield DecrementCell
col <- col + 1
| ',' ->
yield GetChar
col <- col + 1
| '.' ->
yield PutChar
col <- col + 1
| '[' ->
locations.Add struct (line, col)
yield JumpIfEqual
col <- col + 1
| ']' ->
if Seq.isEmpty locations then
raise (SyntaxError("Unexpected ']'", line, col))
locations.RemoveAt(locations.Count - 1)
yield JumpIfNotEqual
col <- col + 1
| '\n' ->
line <- line + 1
col <- 1
| _ -> col <- col + 1
if not (Seq.isEmpty locations) then
let struct (line, col) = locations[0]
raise (SyntaxError("Unmatched '['", line, col)) ]