-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-desugar.rkt
More file actions
94 lines (84 loc) · 2.96 KB
/
python-desugar.rkt
File metadata and controls
94 lines (84 loc) · 2.96 KB
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
#lang plai-typed
(require "python-syntax.rkt"
"python-core-syntax.rkt")
(print-only-errors true)
(define (uniq (l : (listof 'a))) : (listof 'a)
(hash-keys
(foldl (lambda (x h) (hash-set h x #t))
(hash empty)
l)))
(test (uniq empty) empty)
(test (uniq (list 1 2 3 4 3 5 2 3))
(list 1 2 3 4 5))
(define (find-locals exp)
(type-case PyExp exp
[PySet! (id value) (list id)]
[PySeq (es) (foldl (lambda (exp res)
(append (find-locals exp)
res))
empty
es)]
[PyIf (test t e)
(append (find-locals t)
(find-locals e))]
[else empty]))
(define (desugar-inner exp)
(type-case PyExp exp
[PyNum (n) (CNum n)]
[PyTuple (l) (CTuple (map desugar-inner l))]
[PySeq (es) (foldl (lambda (e1 e2)
(CSeq e2 (desugar-inner e1)))
(desugar-inner (first es))
(rest es))]
[PyId (x) (CId x)]
[PySet! (id value) (CSet! id (desugar-inner value))]
[PyApp (f args varargs) (CApp (desugar-inner f)
(map desugar-inner args)
(desugar-inner varargs))]
[PyFunc (args vararg body) (CFunc args vararg (desugar-body body))]
[PyReturn (value) (CReturn (desugar-inner value))]
[PyIf (test t e)
(CLet 'test-value (desugar-inner test)
(CIf (get-and-call (PyId 'test-value)
"__bool__"
empty
(PyTuple empty))
(desugar-inner t)
(desugar-inner e)))]
[PyOp (id args)
(case id
[(Add) (binop "__add__" (first args) (second args))]
[(Sub) (binop "__sub__" (first args) (second args))]
[(USub) (unop "__neg__" (first args))]
[else (CApp (CPrimF id)
(map desugar-inner args)
(CTuple empty))])]
[PyPass () (CNone)]
;;[else (error 'desugar (string-append "not implemented: "
;; (to-string exp)))]
))
(define (get-and-call inner name args vararg)
(CApp (CApp (CPrimF 'class-lookup)
(list (desugar-inner inner)
(CStr name))
(CTuple empty))
(map desugar-inner args)
(desugar-inner vararg)))
(define (binop name left right)
(get-and-call left name
(list right)
(PyTuple empty)))
(define (unop name left)
(CApp (CApp (CPrimF 'class-lookup)
(list (desugar-inner left)
(CStr name))
(CTuple empty))
empty
(CTuple empty)))
(define (desugar-body exp)
(foldl (lambda (id e)
(CLet id (CUndefined)
e))
(desugar-inner exp)
(uniq (find-locals exp))))
(define desugar desugar-body)