elixir, erlang, lua, php, r, vimscript: Fix (first nil) and (rest nil)
[jackhill/mal.git] / r / printer.r
CommitLineData
4d1456b9
JM
1..printer.. <- TRUE
2
3if(!exists("..types..")) source("types.r")
4
36737ae5
JM
5.pr_list <- function(lst, print_readably=TRUE, join="") {
6 concatl(lapply(lst,
01feedfe
JM
7 function(e) .pr_str(e, print_readably)), sep=join)
8}
9
4d1456b9 10.pr_str <- function(exp, print_readably=TRUE) {
01feedfe 11 pr <- print_readably
4d1456b9
JM
12 switch(class(exp),
13 "List"={
36737ae5 14 paste("(", .pr_list(exp, pr, " "), ")", sep="", collapse="")
4d1456b9
JM
15 },
16 "Vector"={
36737ae5
JM
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="")
4d1456b9
JM
28 },
29 "character"={
b8ee29b2
JM
30 if (substring(exp,1,1) == "\u029e") {
31 concat(":", substring(exp,2))
c9de2e82
JM
32 } else if (substring(exp,1,8) == "<U+029E>") {
33 # terrible hack, appears in 3.1.1 on Utopic
34 concat(":", substring(exp,9))
b8ee29b2 35 } else if (print_readably) {
01feedfe
JM
36 paste("\"",
37 gsub("\\n", "\\\\n",
38 gsub("\\\"", "\\\\\"",
39 gsub("\\\\", "\\\\\\\\", exp))),
40 "\"", sep="", collapse="")
4d1456b9
JM
41 } else {
42 exp
43 }
44 },
c30efef4 45 "Symbol"={ exp },
01feedfe 46 "nil"={ "nil" },
4d1456b9 47 "logical"={ tolower(exp) },
01feedfe
JM
48 "MalFunc"={
49 paste("(fn* ", .pr_str(exp$params,TRUE),
f947d503 50 " ", .pr_str(exp$ast, TRUE), ")", sep="")
01feedfe 51 },
4d1456b9 52 "function"={ "<#function>" },
f947d503
JM
53 "Atom"={
54 paste("(atom ", .pr_str(exp$val,TRUE), ")", sep="")
55 },
4d1456b9
JM
56 { toString(exp) })
57}