2015-04-20 06:40:45 +03:00
|
|
|
module Node
|
|
|
|
|
|
|
|
open Types
|
|
|
|
|
|
|
|
let TRUE = Bool(true)
|
|
|
|
let SomeTRUE = Some(TRUE)
|
|
|
|
let FALSE = Bool(false)
|
|
|
|
let SomeFALSE = Some(FALSE)
|
|
|
|
let NIL = Nil
|
|
|
|
let SomeNIL = Some(NIL)
|
|
|
|
let ZERO = Number(0L)
|
|
|
|
|
2015-07-06 00:23:34 +03:00
|
|
|
let makeVector seg = Vector(NIL, seg)
|
|
|
|
let makeList lst = List(NIL, lst)
|
|
|
|
let makeMap map = Map(NIL, map)
|
|
|
|
|
|
|
|
let EmptyLIST = [] |> makeList
|
|
|
|
let EmptyVECTOR = System.ArraySegment([| |]) |> makeVector
|
|
|
|
let EmptyMAP = Map.empty |> makeMap
|
|
|
|
|
|
|
|
let ofArray arr = System.ArraySegment(arr) |> makeVector
|
2015-04-20 06:40:45 +03:00
|
|
|
|
2016-02-13 05:01:05 +03:00
|
|
|
let ofChar chr = sprintf "%c" chr |> String
|
|
|
|
|
2015-04-20 06:40:45 +03:00
|
|
|
let toArray = function
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, lst) -> Array.ofList lst
|
|
|
|
| Vector(_, seg) -> Array.sub seg.Array seg.Offset seg.Count
|
2015-04-20 06:40:45 +03:00
|
|
|
| node -> [| node |]
|
|
|
|
|
|
|
|
let length = function
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, lst) -> List.length lst
|
|
|
|
| Vector(_, seg) -> seg.Count
|
|
|
|
| Map(_, m) -> m.Count
|
2015-04-20 06:40:45 +03:00
|
|
|
| _ -> 1
|
|
|
|
|
|
|
|
(* Active Patterns to help with pattern matching nodes *)
|
|
|
|
let inline (|Elements|_|) num node =
|
|
|
|
let rec accumList acc idx lst =
|
|
|
|
let len = Array.length acc
|
|
|
|
match lst with
|
|
|
|
| [] when idx = len -> Some(Elements acc)
|
|
|
|
| h::t when idx < len ->
|
|
|
|
acc.[idx] <- h
|
|
|
|
accumList acc (idx + 1) t
|
|
|
|
| _ -> None
|
|
|
|
match node with
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, lst) -> accumList (Array.zeroCreate num) 0 lst
|
|
|
|
| Vector(_, seg) when seg.Count = num -> Some(toArray node)
|
2015-04-20 06:40:45 +03:00
|
|
|
| _ -> None
|
|
|
|
|
|
|
|
let inline (|Cons|_|) node =
|
|
|
|
match node with
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, h::t) -> Some(Cons(h, makeList t))
|
|
|
|
| Vector(_, seg) when seg.Count > 0 ->
|
2015-04-20 06:40:45 +03:00
|
|
|
let h = seg.Array.[seg.Offset]
|
|
|
|
let t = System.ArraySegment(seg.Array, seg.Offset + 1, seg.Count - 1)
|
2015-07-06 00:23:34 +03:00
|
|
|
|> makeVector
|
2015-04-20 06:40:45 +03:00
|
|
|
Some(Cons(h, t))
|
|
|
|
| _ -> None
|
|
|
|
|
|
|
|
let inline (|Empty|_|) node =
|
|
|
|
match node with
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, []) -> Some(Empty)
|
|
|
|
| Vector(_, seg) when seg.Count = 0 -> Some(Empty)
|
2015-04-20 06:40:45 +03:00
|
|
|
| _ -> None
|
|
|
|
|
|
|
|
let inline (|Pair|_|) node =
|
|
|
|
match node with
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, a::b::t) -> Some(a, b, makeList t)
|
|
|
|
| List(_, []) -> None
|
|
|
|
| List(_, _) -> raise <| Error.expectedEvenNodeCount ()
|
|
|
|
| Vector(_, seg) ->
|
2015-04-20 06:40:45 +03:00
|
|
|
match seg.Count with
|
|
|
|
| 0 -> None
|
2015-05-02 09:50:09 +03:00
|
|
|
| 1 -> raise <| Error.expectedEvenNodeCount ()
|
2015-04-20 06:40:45 +03:00
|
|
|
| _ ->
|
|
|
|
let a = seg.Array.[seg.Offset]
|
|
|
|
let b = seg.Array.[seg.Offset + 1]
|
|
|
|
let t = System.ArraySegment(seg.Array, seg.Offset + 2, seg.Count - 2)
|
2015-07-06 00:23:34 +03:00
|
|
|
|> makeVector
|
2015-04-20 06:40:45 +03:00
|
|
|
Some(a, b, t)
|
|
|
|
| _ -> None
|
2015-07-03 20:07:57 +03:00
|
|
|
|
|
|
|
let inline (|Seq|_|) node =
|
|
|
|
match node with
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, lst) -> Some(Seq.ofList lst)
|
|
|
|
| Vector(_, seg) -> Some(seg :> Node seq)
|
2015-07-03 20:07:57 +03:00
|
|
|
| _ -> None
|