forked from prozion/odysseus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.rkt
108 lines (89 loc) · 3.24 KB
/
debug.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#lang racket
(require compatibility/defmacro)
(require (for-syntax "strings.rkt" racket/match))
(provide (all-defined-out))
(define debug-output (make-parameter #f))
(define debug
(lambda args (apply string-append
(map
(lambda (el)
(cond
((number? el) (number->string el))
((list? el) (list->string el)) ; list of chars to string
(else el)))
args))))
; (define-macro (benchmark . args)
; (let ((f (list-ref args 0))
; (descr (if (> (length args) 1) (list-ref args 1) #f)))
; `(let* ((start-time (current-inexact-milliseconds))
; (res ,f)
; (post-time (- (current-inexact-milliseconds) start-time)))
; (if ,descr
; (begin
; (printf "~a ~a ms~n" ,descr post-time)
; res)
; post-time))))
(define-macro (benchmark . args)
(let-values (((descr args)
(match args
(`((d ,description-string) ,expr-args ...) (values description-string expr-args))
(`(,expr-args ...) (values #f expr-args)))))
`(let* ((start-time (current-inexact-milliseconds))
(res (begin ,@args))
(post-time (- (current-inexact-milliseconds) start-time)))
(printf "~a~a ms~n" (if ,descr (format "~a: " ,descr) "") post-time)
res)))
(define-syntax (print-benchmark stx)
(syntax-case stx ()
((_ args ...) #'(void (benchmark args ...)))))
(define-macro (show-status status-var text)
`(when (,status-var)
(display ,text)
(flush-output)))
(define-macro (show-status-in-let status-var text)
`(when (,status-var) (display ,text) (flush-output)))
(define-macro (_t text)
`(show-status debug-output ,text))
(define-macro (__t text)
`(show-status-in-let debug-output ,text))
(define-macro (errorf frmt . args)
`(error (format ,frmt ,@args)))
(define (print-with-delay p)
(if p (begin (sleep (first p)) (flush-output) (display (second p))) (void)))
(define-syntax (--- stx)
(syntax-case stx ()
((_ parameters ...)
(with-syntax ((frmt #'(for/fold
((s "~n"))
((i (reverse (list parameters ...))))
(string-append "~a " s))))
#'(printf frmt parameters ...)))))
; #'(if (debug-output)
; (append-file (debug-output) (format frmt parameters ...))
; (printf frmt parameters ...)
; )))))
;;; shorthands
(define (print-list lst)
(for ((i lst))
(if (debug-output)
(error "print-list now doesn't work with (debug-output) turned-on")
; (append-file (debug-output) i)
(println i))))
(define (---- obj)
(cond
((list? obj) (print-list obj))
((hash? obj) (print-list (for/list (((k v) obj)) (cons k v))))
(else obj)))
(define-macro (terpri)
`(--- ""))
(define (----- n)
(for ((i (range n))) (displayln ""))
(flush-output))
(define-macro (let*-print . let-exprs)
(let* ((let-forms (car let-exprs))
(let-forms (for/fold
((res '((_ (--- 1)))))
((let-form let-forms) (i (in-naturals 2)))
(append res (list let-form) `((_ (--- ,i))))))
(body-forms (cdr let-exprs)))
`(let* ,let-forms ,@body-forms)))