forked from greghendershott/pdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rkt
76 lines (65 loc) · 2.95 KB
/
common.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
;; Copyright (c) 2021-2023 by Greg Hendershott.
;; SPDX-License-Identifier: GPL-3.0-or-later
#lang racket/base
(require racket/format
racket/match
syntax/parse/define)
(provide (all-defined-out))
;;; logger/timing
(define-logger pdb)
(define (time-apply/log what proc args)
(cond [(log-level? (current-logger) 'debug 'pdb)
(define old-mem (current-memory-use))
(define-values (vs cpu real gc) (time-apply proc args))
(define new-mem (current-memory-use))
(define delta (- new-mem old-mem))
(define (fmt n) (~v #:align 'right #:min-width 4 n))
(log-pdb-debug "~a cpu ~a real ~a gc ~aMiB :: ~a"
(fmt cpu) (fmt real) (fmt gc)
(~a #:align 'right #:min-width 4
(~r #:precision 0 #:groups '(3) #:group-sep ","
(/ new-mem 1024.0 1024.0)))
what)
(apply values vs)]
[else (apply proc args)]))
(define-simple-macro (with-time/log what e ...+)
(time-apply/log what (λ () e ...) '()))
(define (exn->string e)
(define o (open-output-string))
(parameterize ([current-error-port o])
((error-display-handler) (exn-message e) e))
(get-output-string o))
;;; identifier-binding
;; This 9 field struct corresponds to the 7 item list returned by
;; identifier-binding. Each of the list's module-path-index items --
;; "from-mod" and "nominal-from-mod" -- is resolved into a pair of
;; `-path` and `-subs` fields.
(struct resolved-binding
(from-path ;resolved from identifier-binding's "from-mod"
from-subs ;/
from-sym
from-phase
nom-path ;resolved from identifier-binding's "nominal-from-modpath"
nom-subs ;/
nom-sym
nom-import-phase+space-shift
nom-export-phase+space)
#:prefab)
;; Note: `phase` cannot be a phase+space; identifier-binding doesn't
;; accept that.
(define (identifier-binding/resolved src id-stx phase)
(define (mpi->path+submods mpi)
(match (resolved-module-path-name (module-path-index-resolve mpi))
['|expanded module| (values src null)]
[(? symbol? sym) (values sym null)]
[(? path-string? path) (values path null)]
[(list (? path-string? path) (? symbol? subs) ...) (values path subs)]
[(list '|expanded module| (? symbol? subs) ...) (values src subs)]
[(list (? symbol? sym) (? symbol? subs) ...) (values src (cons sym subs))]))
(match (identifier-binding id-stx phase)
[(list from-mod from-sym nom-mod nom-sym from-phase nom-import-phase+space-shift nom-export-phase+space)
(define-values (from-path from-subs) (mpi->path+submods from-mod))
(define-values (nom-path nom-subs) (mpi->path+submods nom-mod))
(resolved-binding from-path from-subs from-sym from-phase
nom-path nom-subs nom-sym nom-import-phase+space-shift nom-export-phase+space)]
[v v]))