diff options
| author | Joel Martin <github@martintribe.org> | 2014-10-04 21:49:26 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-10-04 21:49:26 -0500 |
| commit | 70ea599b6e0787102f12cd543dcd65a2eb3751d5 (patch) | |
| tree | 81e43e09ed4f56f9290cc765e0f57dd35de02a5d /go/src/printer/printer.go | |
| parent | 45e1db6afbb0c63b1cd5d17e0996d7929803f37b (diff) | |
| download | mal-70ea599b6e0787102f12cd543dcd65a2eb3751d5.tar.gz mal-70ea599b6e0787102f12cd543dcd65a2eb3751d5.zip | |
go: step1_read_print print working.
Diffstat (limited to 'go/src/printer/printer.go')
| -rw-r--r-- | go/src/printer/printer.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/go/src/printer/printer.go b/go/src/printer/printer.go new file mode 100644 index 0000000..83d4c84 --- /dev/null +++ b/go/src/printer/printer.go @@ -0,0 +1,35 @@ +package printer + +import ( + "fmt" + "strings" +) + +import ( + "types" +) + +func Pr_str(obj types.MalType, print_readably bool) string { + switch tobj := obj.(type) { + case types.List: + str_list := make([]string, 0, len(tobj.Val)) + for _, e := range tobj.Val { + str_list = append(str_list, Pr_str(e, print_readably)) + } + return "(" + strings.Join(str_list, " ") + ")" + case string: + if print_readably { + // TODO: quote backslash, quote, and newline + return `"` + fmt.Sprintf("%v", obj) + `"` + } else { + return fmt.Sprintf("%v", obj) + } + case types.Symbol: + return tobj.Val + case nil: + return "nil" + default: + return fmt.Sprintf("%v", obj) + } + return "<printed>" +} |
