elixir, erlang, lua, php, r, vimscript: Fix (first nil) and (rest nil)
[jackhill/mal.git] / r / printer.r
1 ..printer.. <- TRUE
2
3 if(!exists("..types..")) source("types.r")
4
5 .pr_list <- function(lst, print_readably=TRUE, join="") {
6 concatl(lapply(lst,
7 function(e) .pr_str(e, print_readably)), sep=join)
8 }
9
10 .pr_str <- function(exp, print_readably=TRUE) {
11 pr <- print_readably
12 switch(class(exp),
13 "List"={
14 paste("(", .pr_list(exp, pr, " "), ")", sep="", collapse="")
15 },
16 "Vector"={
17 paste("[", .pr_list(exp, pr, " "), "]", sep="", collapse="")
18 },
19 "HashMap"={
20 hlst <- list()
21 if (length(exp) > 0) {
22 for(k in ls(exp)) {
23 hlst[[length(hlst)+1]] <- k
24 hlst[[length(hlst)+1]] <- exp[[k]]
25 }
26 }
27 paste("{", .pr_list(hlst, pr, " "), "}", sep="", collapse="")
28 },
29 "character"={
30 if (substring(exp,1,1) == "\u029e") {
31 concat(":", substring(exp,2))
32 } else if (substring(exp,1,8) == "<U+029E>") {
33 # terrible hack, appears in 3.1.1 on Utopic
34 concat(":", substring(exp,9))
35 } else if (print_readably) {
36 paste("\"",
37 gsub("\\n", "\\\\n",
38 gsub("\\\"", "\\\\\"",
39 gsub("\\\\", "\\\\\\\\", exp))),
40 "\"", sep="", collapse="")
41 } else {
42 exp
43 }
44 },
45 "Symbol"={ exp },
46 "nil"={ "nil" },
47 "logical"={ tolower(exp) },
48 "MalFunc"={
49 paste("(fn* ", .pr_str(exp$params,TRUE),
50 " ", .pr_str(exp$ast, TRUE), ")", sep="")
51 },
52 "function"={ "<#function>" },
53 "Atom"={
54 paste("(atom ", .pr_str(exp$val,TRUE), ")", sep="")
55 },
56 { toString(exp) })
57 }