-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpython-lib.rkt
More file actions
48 lines (34 loc) · 1.22 KB
/
python-lib.rkt
File metadata and controls
48 lines (34 loc) · 1.22 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
#lang plai-typed
(require "python-core-syntax.rkt")
#|
Here is a suggestion for how to implement shared runtime functionality -
write it as core expression forms and use python-lib to wrap your
desugared expressions in an environment that will contain useful
bindings. For example, this sample library binds `print` to a function
that calls the primitive `print`.
|#
(define-type-alias Lib (CExp -> CExp))
(define print-lambda
(CFunc (list 'to-print)
(CPrim1 'print (CId 'to-print))))
(define assert-true-lambda
(CFunc (list 'check-true)
(CIf (CId 'check-true) (CTrue) (CError (CStr "Assert failed")))))
(define true-val
(CTrue))
(define-type LibBinding
[bind (left : symbol) (right : CExp)])
(define lib-functions
(list (bind 'print print-lambda)
(bind 'True true-val)
(bind '___assertTrue assert-true-lambda)
))
(define (python-lib expr)
(local [(define (python-lib/recur libs)
(cond [(empty? libs) expr]
[(cons? libs)
(type-case LibBinding (first libs)
(bind (name value)
(CLet name value
(python-lib/recur (rest libs)))))]))]
(python-lib/recur lib-functions)))