python repo install
[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))
16
17 ;;;
18 ;;; Language definition
19 ;;;
20
21 (define (pr . x)
22 (define port (open-file "/home/stis/src/python-on-guile/log.txt" "a"))
23 (with-output-to-port port
24 (lambda ()
25 (pretty-print x) (car (reverse x))))
26 (close port)
27 (car (reverse x)))
28
29 (define (c x) (pr (comp (pr (p (pr x))))))
30 (define (cc port x)
31 (if (equal? x "") (read port) (c x)))
32
33 (define (e x) (eval (c x) (current-module)))
34
35 (catch #t
36 (lambda ()
37 (set! (@@ (ice-9 readline) *readline-completion-function*)
38 (complete-fkn e)))
39 (lambda x #f))
40
41 (define-language python
42 #:title "python"
43 #:reader (lambda (port env)
44 (if (not (fluid-ref (@@ (system base compile) %in-compile)))
45 (cc port (read-line port))
46 (cc port (read-string port))))
47
48 #:compilers `((tree-il . ,compile-tree-il))
49 #:decompilers `((tree-il . ,decompile-tree-il))
50 #:evaluator (lambda (x module) (primitive-eval x))
51 #:printer write
52 #:make-default-environment
53 (lambda ()
54 ;; Ideally we'd duplicate the whole module hierarchy so that `set!',
55 ;; `fluid-set!', etc. don't have any effect in the current environment.
56 (let ((m (make-fresh-user-module)))
57 ;; Provide a separate `current-reader' fluid so that
58 ;; compile-time changes to `current-reader' are
59 ;; limited to the current compilation unit.
60 (module-define! m 'current-reader (make-fluid))
61
62 ;; Default to `simple-format', as is the case until
63 ;; (ice-9 format) is loaded. This allows
64 ;; compile-time warnings to be emitted when using
65 ;; unsupported options.
66 (module-set! m 'format simple-format)
67
68 m)))