1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-19 09:38:28 +03:00
mal/impls/r/printer.r

58 lines
1.8 KiB
R
Raw Normal View History

2014-11-01 23:54:48 +03:00
..printer.. <- TRUE
if(!exists("..types..")) source("types.r")
2014-11-04 06:19:13 +03:00
.pr_list <- function(lst, print_readably=TRUE, join="") {
concatl(lapply(lst,
function(e) .pr_str(e, print_readably)), sep=join)
}
2014-11-01 23:54:48 +03:00
.pr_str <- function(exp, print_readably=TRUE) {
pr <- print_readably
2014-11-01 23:54:48 +03:00
switch(class(exp),
"List"={
2014-11-04 06:19:13 +03:00
paste("(", .pr_list(exp, pr, " "), ")", sep="", collapse="")
2014-11-01 23:54:48 +03:00
},
"Vector"={
2014-11-04 06:19:13 +03:00
paste("[", .pr_list(exp, pr, " "), "]", sep="", collapse="")
},
"HashMap"={
hlst <- list()
if (length(exp) > 0) {
for(k in ls(exp)) {
hlst[[length(hlst)+1]] <- k
hlst[[length(hlst)+1]] <- exp[[k]]
}
}
paste("{", .pr_list(hlst, pr, " "), "}", sep="", collapse="")
2014-11-01 23:54:48 +03:00
},
"character"={
if (substring(exp,1,1) == "\u029e") {
concat(":", substring(exp,2))
} else if (substring(exp,1,8) == "<U+029E>") {
# terrible hack, appears in 3.1.1 on Utopic
concat(":", substring(exp,9))
} else if (print_readably) {
paste("\"",
gsub("\\n", "\\\\n",
gsub("\\\"", "\\\\\"",
gsub("\\\\", "\\\\\\\\", exp))),
"\"", sep="", collapse="")
2014-11-01 23:54:48 +03:00
} else {
exp
}
},
"Symbol"={ exp },
"nil"={ "nil" },
2014-11-01 23:54:48 +03:00
"logical"={ tolower(exp) },
"MalFunc"={
paste("(fn* ", .pr_str(exp$params,TRUE),
" ", .pr_str(exp$ast, TRUE), ")", sep="")
},
2014-11-01 23:54:48 +03:00
"function"={ "<#function>" },
"Atom"={
paste("(atom ", .pr_str(exp$val,TRUE), ")", sep="")
},
2014-11-01 23:54:48 +03:00
{ toString(exp) })
}