aboutsummaryrefslogtreecommitdiff
path: root/racket/printer.rkt
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-01-02 23:20:00 -0600
committerJoel Martin <github@martintribe.org>2015-01-09 16:16:55 -0600
commitf522319598c701efde91a78b07110d7039a8c906 (patch)
treec903469df13f81ffb7d706680c5eaafadbb90471 /racket/printer.rkt
parent5400d4bf5e7fe7f968a4553f55101de962a39ef7 (diff)
downloadmal-f522319598c701efde91a78b07110d7039a8c906.tar.gz
mal-f522319598c701efde91a78b07110d7039a8c906.zip
Racket: add steps0-A. Self-hosting.
- Some additioanl tests. - Split step9 tests into optional but self-hosting requirements (metadata on functions) and other optional (conj, metadata on collections).
Diffstat (limited to 'racket/printer.rkt')
-rw-r--r--racket/printer.rkt44
1 files changed, 44 insertions, 0 deletions
diff --git a/racket/printer.rkt b/racket/printer.rkt
new file mode 100644
index 0000000..07a8bb8
--- /dev/null
+++ b/racket/printer.rkt
@@ -0,0 +1,44 @@
+#lang racket
+
+(provide pr_str pr_lst)
+
+(require "types.rkt")
+
+(define (pr_str obj print_readably)
+ (let ([_r print_readably])
+ (cond
+ [(list? obj)
+ (string-join (map (lambda (o) (pr_str o _r)) obj)
+ " " #:before-first "(" #:after-last ")")]
+ [(vector? obj)
+ (string-join (map (lambda (o) (pr_str o _r)) (vector->list obj))
+ " " #:before-first "[" #:after-last "]")]
+ [(hash? obj)
+ (string-join (dict-map obj (lambda (k v)
+ (format "~a ~a"
+ (pr_str k _r)
+ (pr_str v _r))))
+ " " #:before-first "{" #:after-last "}")]
+ [(string? obj)
+ (if (regexp-match #px"^\u029e" obj)
+ (format ":~a" (substring obj 1))
+ (if _r
+ (format "\"~a\""
+ (string-replace
+ (string-replace
+ (string-replace obj "\\" "\\\\")
+ "\"" "\\\"")
+ "\n" "\\n"))
+ obj))]
+ [(number? obj) (number->string obj)]
+ [(symbol? obj) (symbol->string obj)]
+ [(atom? obj) (format "(atom ~a)" (atom-val obj))]
+ [(_nil? obj) "nil"]
+ [(eq? #t obj) "true"]
+ [(eq? #f obj) "false"]
+ [else (format "~a" obj)])))
+
+(define (pr_lst lst print_readably sep)
+ (string-join
+ (map (lambda (s) (pr_str s print_readably)) lst)
+ sep))