2015-02-24 07:22:58 +03:00
|
|
|
module Printer
|
|
|
|
open System.Text
|
|
|
|
open Types
|
|
|
|
|
2015-03-26 22:05:02 +03:00
|
|
|
type Profile = { Pretty : bool; Separator : string }
|
|
|
|
let pr_str_profile = { Pretty = true; Separator = " " }
|
|
|
|
let str_profile = { Pretty = false; Separator = "" }
|
|
|
|
let prn_profile = { Pretty = true; Separator = " " }
|
|
|
|
let println_profile = { Pretty = false; Separator = " " }
|
|
|
|
|
|
|
|
let print profile nodes =
|
2015-02-24 07:22:58 +03:00
|
|
|
let acc = StringBuilder()
|
2015-03-05 04:14:07 +03:00
|
|
|
let appendStr (str : string) = acc.Append(str) |> ignore
|
2015-02-24 07:22:58 +03:00
|
|
|
let rec pr_node = function
|
2015-03-05 04:14:07 +03:00
|
|
|
| Nil -> appendStr "nil"
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, nodes) -> pr_list nodes
|
|
|
|
| Vector(_, nodes) -> pr_vector nodes
|
|
|
|
| Map(_, map) -> pr_map map
|
2015-03-05 04:14:07 +03:00
|
|
|
| Symbol(symbol) -> appendStr symbol
|
|
|
|
| Keyword(keyword) -> appendStr ":"; appendStr keyword
|
2015-02-24 07:22:58 +03:00
|
|
|
| Number(num) -> acc.Append(num) |> ignore
|
2015-03-26 22:05:02 +03:00
|
|
|
| String(str) when profile.Pretty -> pr_str_pretty str
|
|
|
|
| String(str) -> appendStr str
|
2015-03-05 04:14:07 +03:00
|
|
|
| Bool(true) -> appendStr "true"
|
|
|
|
| Bool(false) -> appendStr "false"
|
2015-07-06 00:23:34 +03:00
|
|
|
| BuiltInFunc(_, tag, _) | Func(_, tag, _, _, _, _) ->
|
2015-07-03 17:57:15 +03:00
|
|
|
pr_func "func" tag
|
2015-07-06 00:23:34 +03:00
|
|
|
| Macro(_, tag, _, _, _, _) -> pr_func "macro" tag
|
2015-07-04 09:02:43 +03:00
|
|
|
| Atom(tag, r) -> pr_atom tag !r
|
2015-02-24 07:22:58 +03:00
|
|
|
|
2015-03-26 22:05:02 +03:00
|
|
|
and pr separator prefix node =
|
2015-03-05 04:14:07 +03:00
|
|
|
appendStr prefix
|
|
|
|
pr_node node
|
2015-03-26 22:05:02 +03:00
|
|
|
separator
|
2015-03-01 07:07:55 +03:00
|
|
|
|
2015-03-26 22:05:02 +03:00
|
|
|
and std_pr = pr " "
|
|
|
|
|
|
|
|
and pr_str_pretty str =
|
2015-03-05 04:14:07 +03:00
|
|
|
let appendChar = function
|
|
|
|
| '\t' -> appendStr "\\t"
|
|
|
|
| '\b' -> appendStr "\\b"
|
|
|
|
| '\n' -> appendStr "\\n"
|
|
|
|
| '\r' -> appendStr "\\r"
|
|
|
|
| '\f' -> appendStr "\\f"
|
|
|
|
| '"' -> appendStr "\\\""
|
|
|
|
| '\\' -> appendStr "\\\\"
|
|
|
|
| ch -> acc.Append(ch) |> ignore
|
|
|
|
appendStr "\""
|
|
|
|
str |> Seq.iter appendChar
|
|
|
|
appendStr "\""
|
|
|
|
|
2015-07-03 17:57:15 +03:00
|
|
|
and pr_func ftype tag =
|
2015-07-04 09:02:43 +03:00
|
|
|
sprintf "#<%s %d>" ftype tag |> appendStr
|
|
|
|
|
|
|
|
and pr_atom tag node =
|
|
|
|
appendStr "(atom "
|
|
|
|
pr_node node
|
|
|
|
appendStr ")"
|
2015-03-12 05:13:31 +03:00
|
|
|
|
2015-03-05 04:14:07 +03:00
|
|
|
and pr_list nodes =
|
|
|
|
appendStr "("
|
2015-03-26 22:05:02 +03:00
|
|
|
nodes |> List.fold std_pr "" |> ignore
|
2015-03-05 04:14:07 +03:00
|
|
|
appendStr ")"
|
|
|
|
|
|
|
|
and pr_vector nodes =
|
|
|
|
appendStr "["
|
2015-03-26 22:05:02 +03:00
|
|
|
nodes |> Seq.fold std_pr "" |> ignore
|
2015-03-05 04:14:07 +03:00
|
|
|
appendStr "]"
|
2015-03-01 18:49:55 +03:00
|
|
|
|
|
|
|
and pr_map map =
|
2015-03-05 04:14:07 +03:00
|
|
|
let pr prefix key value =
|
|
|
|
appendStr prefix
|
2015-03-01 18:49:55 +03:00
|
|
|
pr_node key
|
2015-03-05 04:14:07 +03:00
|
|
|
appendStr " "
|
2015-03-01 18:49:55 +03:00
|
|
|
pr_node value
|
|
|
|
" "
|
2015-03-05 04:14:07 +03:00
|
|
|
appendStr "{"
|
|
|
|
map |> Map.fold pr "" |> ignore
|
|
|
|
appendStr "}"
|
2015-02-24 07:22:58 +03:00
|
|
|
|
2015-03-26 22:05:02 +03:00
|
|
|
nodes |> Seq.fold (pr profile.Separator) "" |> ignore
|
2015-02-24 07:22:58 +03:00
|
|
|
acc.ToString()
|
2015-03-26 22:05:02 +03:00
|
|
|
|
|
|
|
let pr_str : seq<Node> -> string = print pr_str_profile
|
|
|
|
let str : seq<Node> -> string = print str_profile
|
|
|
|
let prn : seq<Node> -> string = print prn_profile
|
|
|
|
let println : seq<Node> -> string = print println_profile
|