smtp
[software/python-on-guile.git] / modules / language / python / spec.scm
1 (define-module (language python spec)
2 #:use-module (parser stis-parser lang python3-parser)
3 #:use-module ((language python module python) #:select ())
4 #:use-module (language python compile)
5 #:use-module (language python completer)
6 #:use-module (rnrs io ports)
7 #:use-module (ice-9 pretty-print)
8 #:use-module (ice-9 readline)
9 #:use-module (system base compile)
10 #:use-module (system base language)
11 #:use-module (language scheme compile-tree-il)
12 #:use-module (language scheme decompile-tree-il)
13 #:use-module (ice-9 rdelim)
14 #:export (python))
15
16 ;;;
17 ;;; Language definition
18 ;;;
19
20 (define (pr . x)
21 (define port (open-file "/home/stis/src/python-on-guile/log.txt" "a"))
22 (with-output-to-port port
23 (lambda ()
24 (pretty-print x) (car (reverse x))))
25 (close port)
26 (car (reverse x)))
27
28 (define (c x) (pr (comp (pr (p (pr x))))))
29 (define (cc port x)
30 (if (equal? x "") (read port) (c x)))
31
32 (define (e x) (eval (c x) (current-module)))
33
34 (set! (@@ (ice-9 readline) *readline-completion-function*) (complete-fkn e))
35
36 (define-language python
37 #:title "python"
38 #:reader (lambda (port env)
39 (if (not (fluid-ref (@@ (system base compile) %in-compile)))
40 (cc port (read-line port))
41 (cc port (read-string port))))
42
43 #:compilers `((tree-il . ,compile-tree-il))
44 #:decompilers `((tree-il . ,decompile-tree-il))
45 #:evaluator (lambda (x module) (primitive-eval x))
46 #:printer write
47 #:make-default-environment
48 (lambda ()
49 ;; Ideally we'd duplicate the whole module hierarchy so that `set!',
50 ;; `fluid-set!', etc. don't have any effect in the current environment.
51 (let ((m (make-fresh-user-module)))
52 ;; Provide a separate `current-reader' fluid so that
53 ;; compile-time changes to `current-reader' are
54 ;; limited to the current compilation unit.
55 (module-define! m 'current-reader (make-fluid))
56
57 ;; Default to `simple-format', as is the case until
58 ;; (ice-9 format) is loaded. This allows
59 ;; compile-time warnings to be emitted when using
60 ;; unsupported options.
61 (module-set! m 'format simple-format)
62
63 m)))