-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.fs
39 lines (32 loc) · 1 KB
/
Parser.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
module Parser
open System.IO
let parse (tokens: char []) =
let parseField = function
| 'G' -> Grass
| 'M' -> Mountain
| 'S' | 'W' -> Water
| 'T' -> Tree
| 'V' -> RoadV
| 'H' -> RoadH
| token -> sprintf "Invalid field token %c" token |> failwith
let parseEntity = function
| 'T' | 't' -> Some Tank
| 'I' | 'i' -> Some Infantry
| 'J' | 'j' -> Some Jeep
| 'B' | 'b' -> Some Base
| '0' -> None
| token -> sprintf "Invalid entity token %c" token |> failwith
let team =
if System.Char.IsUpper tokens.[1] then
Friendly
else
Enemy
match (parseField tokens.[0], parseEntity tokens.[1]) with
| field, Some entity -> OccupiedKind (field, entity, team)
| field, None -> EmptyKind field
let parseFile fileName : SquareKind [,] =
let contents = File.ReadAllLines fileName
contents
|> Seq.map (Seq.chunkBySize 2)
|> array2D
|> Array2D.map parse