1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-10 02:45:44 +03:00
mal/r/printer.r
Joel Martin c9de2e82ed Tests: add testing Dockerfile. Impl fixes.
- tests/docker/Dockerfile: specifies full docker image containing all
  tools/languages (except matlab).
- tests/docker-build.sh: build above image.
- tests/docker-run.sh: launch above image.
    Example: ./tests/docker-run.sh make test^js^step2
- Various fixes across multiple languages:
    - Unicode fixes for bash and R on Ubuntu Utopic
    - readline history fixes for when ~/.mal-history is not available
      or readable/writable. No fatal errors.
    - fixes to work with perl 5.20 (and still perl 5.18)
2015-03-11 22:22:35 -05:00

58 lines
1.8 KiB
R

..printer.. <- TRUE
if(!exists("..types..")) source("types.r")
.pr_list <- function(lst, print_readably=TRUE, join="") {
concatl(lapply(lst,
function(e) .pr_str(e, print_readably)), sep=join)
}
.pr_str <- function(exp, print_readably=TRUE) {
pr <- print_readably
switch(class(exp),
"List"={
paste("(", .pr_list(exp, pr, " "), ")", sep="", collapse="")
},
"Vector"={
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="")
},
"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="")
} else {
exp
}
},
"Symbol"={ exp },
"nil"={ "nil" },
"logical"={ tolower(exp) },
"MalFunc"={
paste("(fn* ", .pr_str(exp$params,TRUE),
" ", .pr_str(exp$ast, TRUE), ")", sep="")
},
"function"={ "<#function>" },
"Atom"={
paste("(atom ", .pr_str(exp$val,TRUE), ")", sep="")
},
{ toString(exp) })
}