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