remove warnings, reordering
[software/python-on-guile.git] / modules / language / python / spec.scm
1 (define-module (language python spec)
2 #:use-module (language python guilemod)
3 #:use-module (parser stis-parser lang python3-parser)
4 #:use-module ((language python module python) #:select ())
5 #:use-module (language python compile)
6 #:use-module (language python completer)
7 #:use-module (rnrs io ports)
8 #:use-module (ice-9 pretty-print)
9 #:use-module (ice-9 readline)
10 #:use-module (system base compile)
11 #:use-module (system base language)
12 #:use-module (language scheme compile-tree-il)
13 #:use-module (language scheme decompile-tree-il)
14 #:use-module (ice-9 rdelim)
15 #:export (python dolog))
16
17 ;;;
18 ;;; Language definition
19 ;;;
20
21 (define dolog #f)
22 (define (pr . x)
23 (when dolog
24 (define port (open-file "log.txt" "a"))
25 (with-output-to-port port
26 (lambda ()
27 (pretty-print x) (car (reverse x))))
28 (close port))
29 (car (reverse x)))
30
31 (define (c int x) (pr (comp int (pr (p (pr x))))))
32 (define (cc int port x)
33 (if (equal? x "") (read port) (c int x)))
34
35 (define (e x) (eval (c #t x) (current-module)))
36
37
38 (define (int)
39 (catch #t
40 (lambda ()
41 (if (fluid-ref (@@ (system base compile) %in-compile))
42 #f
43 #t))
44 (lambda x #f)))
45
46 (define (in)
47 (catch #t
48 (lambda ()
49 (fluid-set! (@@ (system base compile) %in-compile) #t))
50 (lambda x #f)))
51
52 (define mapper (make-weak-key-hash-table))
53
54 (define python-reader-wrap
55 (lambda (port env)
56 (if (int)
57 (cc #t port (read-line port))
58 (let lp ((port2 (hash-ref mapper port)))
59 (if port2
60 (read port2)
61 (let ((port2
62 (open-input-string (cc #f port (read-string port)))))
63 (use-modules (language python guilemod))
64 (in)
65 (hash-set! mapper port port2)
66 (lp port2)))))))
67
68 (catch #t
69 (lambda ()
70 (set! (@@ (ice-9 readline) *readline-completion-function*)
71 (complete-fkn e)))
72 (lambda x #f))
73
74 (define-language python
75 #:title "python"
76 #:reader python-reader-wrap
77 #:compilers `((tree-il . ,compile-tree-il))
78 #:decompilers `((tree-il . ,decompile-tree-il))
79 #:evaluator (lambda (x module) (primitive-eval x))
80 #:printer write
81 #:make-default-environment
82 (lambda ()
83 ;; Ideally we'd duplicate the whole module hierarchy so that `set!',
84 ;; `fluid-set!', etc. don't have any effect in the current environment.
85 (let ((m (make-fresh-user-module)))
86 ;; Provide a separate `current-reader' fluid so that
87 ;; compile-time changes to `current-reader' are
88 ;; limited to the current compilation unit.
89 (module-define! m 'current-reader (make-fluid))
90
91 ;; Default to `simple-format', as is the case until
92 ;; (ice-9 format) is loaded. This allows
93 ;; compile-time warnings to be emitted when using
94 ;; unsupported options.
95 (module-set! m 'format simple-format)
96
97 m)))