1 (define-module (language python python
)
2 #:use-module
(language python parser
)
3 #:use-module
(language python expr
)
4 #:use-module
(ice-9 match
)
5 #:export
(compile-python-string compile-python-file
))
7 ;;; VARIABLES ----------------------------------------------------------------
8 (define (find-global-variables vars tree
)
9 (define (for-each* f l
)
21 (lambda (x) (hash-set! vars x
#t
)) l
))
23 (for-each* local tree
))
27 (define (collect tree
)
32 (hash-set! vars tree
#t
))
34 (for-each* collect tree
))
56 (define (expr stx out tree
)
57 (define (expr-lhs tree
)
59 ((#:test
(#:power
(#:identifier v . _
)))
60 (datum->syntax stx
(string->symbol v
)))))
63 (define (expr-rhs tree
)
83 #`(if #,(py-true?
(lp a
)) #,(lp x
) #,(lp b
)))
85 #`(py-or #,(lp x
) #,@(map lp y
)))
87 #`(py-and #,(lp x
) #,@(map lp y
)))
92 ((#:comp x
(op . y
) . l
)
93 #'(#,(comp-tr op
) #,(lp x
) #,(lp (cons* #:comp y l
))))
95 #`(py-bor #,(lp x
) #,@(map lp y
)))
97 #`(py-bxor #,(lp x
) #,@(map lp y
)))
99 #`(py-band #,(lp x
) #,@(map lp y
)))
101 #`(py-<< #,(lp x
) #,@(map lp y
)))
103 #`(py->> #,(lp x
) #,@(map lp y
)))
105 #`(py-+ #,(lp x
) #,@(map lp y
)))
107 #`(py-- #,(lp x
) #,@(map lp y
)))
109 #`(py-* #,(lp x
) #,@(map lp y
)))
111 #`(py-/ #,(lp x
) #,@(map lp y
)))
113 #`(py-// #,(lp x
) #,@(map lp y
)))
115 #`(py-%
#,(lp x
) #,@(map lp y
)))
122 ((#:power x trailer .
#f
)
123 (compile-trailer trailer
(lp x
)))
124 ((#:power x trailer . l
)
125 #'(py-power ,#(compile-trailer trailer
(lp x
)) #,(lp l
)))
126 ((#:identifier x . _
)
127 (datum->syntax stx
(string->symbol x
)))
137 ((test1 (#:assign tests ... last
))
138 (with-syntax (((rhs ...
) (map expr-rhs last
))
139 ((lhs1 ...
) (map expr-lhs test1
))
140 (((lhs ...
) ...
) (reverse (map (lambda (l)
143 (with-syntax (((v ...
) (generate-temporaries #'(lhs1 ...
))))
144 (out #'(call-with-values (lambda () (values rhs ...
))
149 (set! lhs1 v
) ...
)))))))))
152 (define (compile-outer state out tree
)
153 (define (compile-stmt state tree
)
171 (compile-return state l
))
174 (compile-raise state l
))
177 (compile-import state l
))
186 (compile-assert state l
))))
190 (for-each* compile-stmt tree
))
192 (compile-if state l
))
194 (compile-while state l
))
196 (compile-for state l
))
198 (compile-try state l
))
200 (compile-with state l
))
202 (compile-def state l
))
204 (compile-decorated state l
))))
207 (define (compile-python0 stx tree output
)
208 (define global-variables
(make-hash-table))
210 (find-global-variables global-variables tree
)
216 (cons (datum->syntax stx
(string->symbol v
)) e
))))
217 '() global-variables
))
223 (cons (datum->syntax stx
(string->symbol v
)) e
))))
224 '() global-variables
))
226 (output (with-syntax (((v ...
) all-variables
))
227 #'(begin (define v
(if #f
#f
)) ...
)))
229 (output (with-syntax (((v ...
) all-globals
))
232 (output #`(begin #,@(compile-outer))))
235 (define (compile-python1 stx tree
)
237 (define (out x
) (set! out
(cons x out
)))
238 (compile-python0 stx tree out
)
239 (cons* #'begin
(reverse out
))))
241 (define-syntax compile-python-string
245 (if (string?
(syntax->datum
#'y
))
246 (compile-python1 x
(python-parser (syntax->datum
#'y
))))))))
248 (define-syntax compile-python-file
252 (if (string?
(syntax->datum
#'y
))
253 (with-input-from-file (syntax->datum
#'y
)
254 (lambda () (compile-python1 x
(python-parser))))