-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.fs
35 lines (28 loc) · 985 Bytes
/
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
module Parser
open System
type Op = Sum | Int | Diff
type Expr =
| Data of string
| Set of Op * Expr list
let (|Op|_|) = function
| "SUM" -> Some(Sum)
| "INT" -> Some(Int)
| "DIF" -> Some(Diff)
| _ -> None
let rec (|Expression|_|) = function
| "[" :: Op op :: Args(args, tail) -> Some(Set(op, args), tail)
| _ -> None
and (|Args|_|) = function
| "]" :: tail -> Some([], tail)
| Expression(e, Args(args, tail)) -> Some(e :: args, tail)
| f :: Args(args, tail) -> Some(Data(f) :: args, tail)
| _ -> None
let loadFile name =
let lines = List.ofSeq (IO.File.ReadLines(__SOURCE_DIRECTORY__ + "/" + name))
Set.ofList (List.map Int32.Parse lines)
let rec eval = function
| Data f -> loadFile f
| Set(Sum, args) -> Set.unionMany (List.map eval args)
| Set(Int, args) -> Set.intersectMany (List.map eval args)
| Set(Diff, h::t) -> Set.difference (eval h) (Set.unionMany (List.map eval t))
| Set(_) -> Set.empty