R: add step6_file and step7_quote
[jackhill/mal.git] / r / printer.r
... / ...
CommitLineData
1..printer.. <- TRUE
2
3if(!exists("..types..")) source("types.r")
4
5.pr_list <- function(..., print_readably=TRUE, join="") {
6 concatl(lapply(list(...),
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 data <- paste(lapply(exp, function(e) .pr_str(e, pr)),
15 sep="", collapse=" ")
16 paste("(", data, ")", sep="", collapse="")
17 },
18 "Vector"={
19 data <- paste(lapply(exp, function(e) .pr_str(e, pr)),
20 sep=" ", collapse=" ")
21 paste("[", data, "]", sep="", collapse="")
22 },
23 "character"={
24 if (print_readably) {
25 paste("\"",
26 gsub("\\n", "\\\\n",
27 gsub("\\\"", "\\\\\"",
28 gsub("\\\\", "\\\\\\\\", exp))),
29 "\"", sep="", collapse="")
30 } else {
31 exp
32 }
33 },
34 "Symbol"={ exp },
35 "nil"={ "nil" },
36 "logical"={ tolower(exp) },
37 "MalFunc"={
38 paste("(fn* ", .pr_str(exp$params,TRUE),
39 " ", .pr_str(exp$ast, FALSE), ")", sep="")
40 },
41 "function"={ "<#function>" },
42 { toString(exp) })
43}
44
45