summaryrefslogtreecommitdiff
path: root/modules/language/python/eval.scm
blob: 9f8724f3bd641b807aa5ce65305814445789b7ab (about) (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
(define-module (language python eval)
  #:use-module (language python guilemod)
  #:use-module (parser stis-parser lang python3-parser)
  #:use-module (language python exceptions)
  #:use-module (language python module)
  #:use-module (language python try)
  #:use-module (language python list)
  #:use-module (language python for)
  #:use-module (language python dict)
  #:use-module (oop pf-objects)
  #:use-module ((ice-9 local-eval) #:select ((the-environment . locals)))
  #:re-export (locals)
  #:replace (eval)
  #:export (local-eval local-compile globals compile exec))

(define seval (@ (guile) eval))

(define-syntax-rule (L x) (@@ (ice-9 local-eval) x))

(define-syntax globals
  (lambda (x)
    (syntax-case x ()
      ((g)
       #'(M ((L env-module) (locals g)))))))

(define-syntax-rule (call- self item a ...)
  (let ((class (ref self '_module)))
    ((rawref class item) class a ...)))

(define-syntax-rule (apply- self item a ...)
  (let ((class (ref self '_module)))
    (apply (rawref class item) class a ...)))

(define-syntax-rule (ref- self item)
  (let ((class (ref self '_module)))
    (rawref class item)))

   
(define-python-class GlobalModuleWrap (dict)
  (define __init__
    (lambda (self module)
      (set self '_module module)))
  
  (define __getitem__
    (lambda (self key)
      (if (string? key) (set! key (string->symbol key)))
      (call- self '__global_getitem__ key)))

  (define get
    (lambda (self key . es)
      (if (string? key) (set! key (string->symbol key)))
      (apply- self '__global_get__ key es)))

  (define __setitem__
    (lambda (self key val)
      (if (string? key) (set! key (string->symbol key)))
      (call- self '__global_setitem__ key val)))

  (define __iter__
    (lambda (self)
      (call- self '__global_iter__)))

  (define values
    (lambda (self)
      (for ((k v : (__iter__ self))) ((l '()))
           (cons v l)
           #:final l)))

  (define keys
    (lambda (self)
      (for ((k v : (__iter__ self))) ((l '()))
           (cons k l)
           #:final l)))

  (define __contains__
    (lambda (self key)
      (if (string? key) (set! key (string->symbol key)))
      (for ((k v : (__iter__ self))) ()
           (if (eq? k key)
               (break #t))
           #:final
           #f)))
  
  (define items __iter__)

  (define __repr__
    (lambda (self)
      (format #f "globals(~a)" (ref- self '__name__)))))

  
      
(define MM (list 'error))
(define (M mod)
  (set! mod (module-name mod))
  (if (and (> (length mod) 3)
           (eq? (car   mod) 'language)
           (eq? (cadr  mod) 'python)
           (eq? (caddr mod) 'module))
      (set! mod (Module (reverse mod)
                        (reverse (cdddr mod))))
      (set! mod (Module (reverse mod) (reverse mod))))

  (GlobalModuleWrap mod))
                      

(define (local-eval x locals globals)
  "Evaluate the expression @var{x} within the local environment @var{local} and
global environment @var{global}."
  (if locals
      (apply (seval ((L local-wrap) x locals)
                    (if globals
                        globals
                        ((L env-module) locals)))
             ((L env-boxes) locals))
      (seval x (current-module))))

(define* (local-compile x locals globals #:key (opts '()))
  "Compile the expression @var{x} within the local environment @var{local} and
global environment @var{global}."
  (if locals
      (apply ((@ (system base compile) compile)
              ((L local-wrap) x locals)
              #:env (if globals
                        globals
                        ((L env-module) locals))
              #:from 'scheme #:opts opts)
             ((L env-boxes) locals))
      ((@ (system base compile) compile)
       x
       #:env (current-module)
       #:from 'scheme #:opts opts)))

(define-syntax eval 
  (lambda (x)
    (syntax-case x ()
      ((eval x)
       #'(eval0 x (locals eval)))
      ((eval x . l)
       #'(eval0 x . l)))))

(define (comp x) (error "not implemented"))

(define* (eval0 x #:optional locals globals)
  (cond
   ((string? x)
    (or (and=> (and=> (p x) comp)
               (lambda (cp)
                 (local-eval cp locals globals)))
        (raise SyntaxError)))
   ((pair? x)
    (local-eval x locals globals))))

(define* (compile x filename mode
                  #:optional (flags 0) (dont_inherit #f) (optimize -1))
  (or (and=> (p x) comp)
      (raise SyntaxError)))

(define-syntax exec
  (lambda (x)
    (syntax-case x ()
      ((exec x)
       #'(eval0 x (locals exec)))
      ((exec x . l)
       #'(exec0 x . l)))))

(define* (exec0 x #:optional locals globals)
  (local-eval x locals globals))