summaryrefslogtreecommitdiff
path: root/modules/language/python/python.scm
blob: e8621addbe0c5738ee128a21daaeeb992869a890 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
(define-module (language python python)
  #:use-module (language python parser)
  #:use-module (language python expr)
  #:use-module (ice-9    match)
  #:export (compile-python-string compile-python-file))

;;; VARIABLES ----------------------------------------------------------------
(define (find-global-variables vars tree)
  (define (for-each* f l)
    (match l
      ((x . l)
       (f x)
       (for-each* f l))
      (x
       (f x))))

  (define (local tree)
    (match tree
      ((#:global l)
       (for-each* 
        (lambda (x) (hash-set! vars x #t)) l))
      ((x . l) 
       (for-each* local tree))
      (_       
       #t)))

  (define (collect tree)
    (match tree
      ((#:lambdef . _)
       #t)
      ((#:identifier . l)
       (hash-set! vars tree #t))
      ((_ . _)
       (for-each* collect tree))
      (_
       #t)))

  (let lp ((tree tree))
    (match tree
      ((#:def . l)
       (for-each* local l))
      ((#:lambdef . l)
       (for-each* local l))
      ((#:class  . l)
       (for-each* local l))
      ((#:expr-stmt
        a (#:assign x ... e))
       (collect a)
       (collect x))
      ((x . l)
       (for-each* lp tree))
      (_
       #t))))
;; COMPILATION

(define (expr stx out tree)
  (define (expr-lhs tree)
    (match tree
      ((#:test (#:power (#:identifier v . _)))
       (datum->syntax stx (string->symbol v)))))


  (define (expr-rhs tree)
    (define (comp-tr op)
      (match op
        ("notin" #'py-notin)
        ("isnot" #'py-isnot)
        ("=="    #'py_==)
        (">="    #'py_>=)
        ("<="    #'py_<=)
        ("<>"    #'py_<>)
        ("!="    #'py_!=)
        ("in"    #'py_in)
        ("is"    #'py_is)
        ("<"     #'py_< )
        (">"     #'py_> )))

    (let lp ((tree tree))
      (match tree
        ((#:test x #f)
         (lp x))
        ((#:test x (a b))
         #`(if #,(py-true? (lp a)) #,(lp x) #,(lp b)))
        ((#:or x . y)
         #`(py-or #,(lp x) #,@(map lp y)))
        ((#:and x y)
         #`(py-and #,(lp x) #,@(map lp y)))
        ((#:not x)
         #`(py-not #,(lp x)))
        ((#:comp x)
         (lp x))
        ((#:comp x (op . y) . l)
         #'(#,(comp-tr op) #,(lp x) #,(lp (cons* #:comp y l))))
        ((#:bor x y)
         #`(py-bor #,(lp x) #,@(map lp y)))
        ((#:bxor x y)
         #`(py-bxor #,(lp x) #,@(map lp y)))
        ((#:xand x y)
         #`(py-band #,(lp x) #,@(map lp y)))
        ((#:<< x y)
         #`(py-<< #,(lp x) #,@(map lp y)))
        ((#:>> x y)
         #`(py->> #,(lp x) #,@(map lp y)))
        ((#:+ x y)
         #`(py-+ #,(lp x) #,@(map lp y)))
        ((#:- x y)
         #`(py-- #,(lp x) #,@(map lp y)))
        ((#:* x y)
         #`(py-* #,(lp x) #,@(map lp y)))
        ((#:/ x y)
         #`(py-/ #,(lp x) #,@(map lp y)))
        ((#:// x y)
         #`(py-// #,(lp x) #,@(map lp y)))
        ((#:% x y)
         #`(py-% #,(lp x) #,@(map lp y)))
        ((#:u+ x)
         #`(py-u+ #,(lp x)))
        ((#:u- x)
         #`(py-u- #,(lp x)))
        ((#:u~ x)
         #`(py-u~ #,(lp x)))
        ((#:power x trailer . #f)
         (compile-trailer trailer (lp x)))
        ((#:power x trailer . l)
         #'(py-power ,#(compile-trailer trailer (lp x)) #,(lp l)))
        ((#:identifier x . _)
         (datum->syntax stx (string->symbol x)))
        ((not (_ . _))
         tree))))
        
        

  (lambda (tree)
    (match tree
      ((test1 (#:assign))
       (expr-rhs test1))
      ((test1 (#:assign tests ... last))
       (with-syntax (((rhs ...)       (map expr-rhs last))
                     ((lhs1 ...)      (map expr-lhs test1))
                     (((lhs ...) ...) (reverse (map (lambda (l) 
                                                      (map expr-lhs l))
                                                    tests))))
	  (with-syntax (((v ...) (generate-temporaries #'(lhs1 ...))))
	     (out #'(call-with-values (lambda () (values rhs ...))
                      (lambda (v ...)
                        (begin
                          (set! lhs v) ...)
                        ...
                        (set! lhs1 v) ...)))))))))
             

(define (compile-outer state out tree)
  (define (compile-stmt state tree)    
    (match tree
      ((#:expr-stmt l)
       (compile-expr l))

      ((#:del       l)
       (compile-del l))
      
      (#:pass 
       (out #'(if #f #f)))
      
      (#:break
       (break out))      
      
      (#:continue
       (continue out))
      
      ((#:return . l)
       (compile-return state l))
      
      ((#:raise . l)
       (compile-raise state l))
      
      ((#:import l)
       (compile-import state l))

      ((#:global . _)
       #t)
      
      ((#:nonlocal . _)
       #t)
         
      ((#:assert . l)
       (compile-assert state l))))
    
  (match tree
    ((#:stmt x)
     (for-each* compile-stmt tree))
    ((#:if . l)
     (compile-if state l))
    ((#:while . l)
     (compile-while state l))
    ((#:for  . l)
     (compile-for state l))
    ((#:try  . l)
     (compile-try state l))
    ((#:with . l)
     (compile-with state l))
    ((#:def  . l)
     (compile-def state l))
    ((#:decorated . l)
     (compile-decorated state l))))


(define (compile-python0 stx tree output)
  (define global-variables (make-hash-table))
  
  (find-global-variables global-variables tree)
  (set! all-variables 
        (hash-fold
         (lambda (k v e)
           (match k
             ((_ v . _)
              (cons (datum->syntax stx (string->symbol v)) e))))
         '() global-variables))
  (set! all-globals
        (hash-fold
         (lambda (k v e)
           (match k
	     ((_ v)
              (cons (datum->syntax stx (string->symbol v)) e))))
         '() global-variables))

  (output (with-syntax (((v ...) all-variables))
	   #'(begin (define v (if #f #f)) ...)))
  
  (output (with-syntax (((v ...) all-globals))
            #'(export v ...)))

  (output #`(begin #,@(compile-outer))))
  

(define (compile-python1 stx tree)
  (let ((out '()))
    (define (out x) (set! out (cons x out)))
    (compile-python0 stx tree out)
    (cons* #'begin (reverse out))))

(define-syntax compile-python-string
  (lambda (x)
    (syntax-case x ()
      ((_ y)
       (if (string? (syntax->datum #'y))
	   (compile-python1 x (python-parser (syntax->datum #'y))))))))

(define-syntax compile-python-file
  (lambda (x)
    (syntax-case x ()
      ((_ y)
       (if (string? (syntax->datum #'y))
	   (with-input-from-file (syntax->datum #'y)
	     (lambda () (compile-python1 x (python-parser))))
	   #f)))))