forked from mighty-gerbils/gerbil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.ss
66 lines (56 loc) · 1.75 KB
/
interactive.ss
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
;;; -*- Gerbil -*-
;;; © vyzo
;;; interactive development utilities
(export #t (for-syntax #t))
(module <util>
(import :gerbil/core/expander)
(export #t)
;; Module reloading
(def (reload-module! mod)
(cond
((string? mod) ; file path, resource it
(import-module mod #t #t))
((symbol? mod)
(let (str (symbol->string mod))
(cond
((string-empty? str)
(error "Invalid module path" mod))
((eq? (string-ref str 0) #\:) ; library module
(reload-module! (substring str 1 (string-length str)))
(import-module mod #t #t))
(else ; top module
(void)))))
(else
(error "Invalid module path" mod)))))
(import (for-syntax <util>))
(defsyntax (reload! stx)
(syntax-case stx ()
((_ mod)
(begin
(reload-module! (stx-e #'mod))
#'(import mod)))))
(defrules reload ()
((_ mod ...)
(begin (reload! mod) ...)))
;; Enter a nested repl with the syntactic context of a module
(def (enter-module! mod)
(parameterize ((gx#current-expander-context (gx#import-module mod #f #t)))
(##repl)))
(defrules enter! ()
((_ mod)
(enter-module! 'mod)))
;; Macro expansion
;; These two macros expand a form, pretty print the expansion, and
;; return the result of the expansion for debugging purposes.
;; @expand uses core-expand* while @expand1 performs a single step
;; expansion with core-expand1
(defrules @expand ()
((_ expr) (macro-expand 'expr)))
(defrules @expand1 ()
((_ expr) (macro-expand1 'expr)))
(def (macro-expand expr (expand-e gx#core-expand*))
(let (expr* (expand-e expr))
(pretty-print (gx#syntax->datum expr*))
expr*))
(def (macro-expand1 expr)
(macro-expand expr gx#core-expand1))