Skip to content

Commit

Permalink
add PyImportFrom in surface syntax and get-strcutured-python; havn't …
Browse files Browse the repository at this point in the history
…desugar yet [issue #3]
  • Loading branch information
lijunsong committed Dec 23, 2012
1 parent b3479d9 commit 52021df
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
16 changes: 16 additions & 0 deletions base/get-structured-python.rkt
Expand Up @@ -336,6 +336,22 @@ structure that you define in python-syntax.rkt
(string->symbol (hash-ref x 'name))
(string->symbol as))))
names))]

; largely the same with PyImport.
[(hash-table ('nodetype "ImportFrom")
('module module)
('names names)
('level level))
(PyImportFrom module
(map (lambda (x)
(hash-ref x 'name)))
(map (lambda (x)
(let ([as (hash-ref x 'asname)])
(if (equal? as #\nul)
(string->symbol (hash-ref x 'name))
(string->symbol as))))
names)
level)]

[(list (hash-table (k v) ...) ..2)
(PySeq (map get-structured-python pyjson))]
Expand Down
33 changes: 28 additions & 5 deletions base/python-desugar.rkt
Expand Up @@ -334,6 +334,29 @@
(list (PyStr (first names))))))))]))]
(PySeq (helper names asnames (list)))))

;;; desugar from module import id as id_alias
;;; __tmp_module = __import__(module, globals(), locals(), [id], 0)
;;; id_alias = __tmp_module.id
(define (desugar-importfrom-py [module : string]
[names : (listof string)]
[asnames : (listof symbol)]
[level : number]) : PyExpr
(PySeq (list)))
; (local [(define tmp-expr
; (PyAssign (list (PyId '__tmp_module 'Store))
; (PyApp (PyId '__import__ 'Load)
; (list (PyStr module)
; (PyApp (PyId '__globals 'Load) (list))
; (PyApp (PyId '__locals 'Load) (list))
; ; list type of names
; level))))
; ;(define assign-exprs






(define (rec-desugar [expr : PyExpr] [global? : boolean]
[env : IdEnv] [inclass? : boolean]) : DesugarResult
(begin ;(display expr) (display "\n\n")
Expand Down Expand Up @@ -871,13 +894,13 @@
[else (error 'desugar "We don't know how to delete identifiers yet.")]))]

[PyImport (names asnames)
(rec-desugar
(desugar-import-py names asnames)
global?
env
inclass?)]
(rec-desugar (desugar-import-py names asnames) global? env inclass?)]

[PyImportFrom (module names asnames level)
(rec-desugar (desugar-importfrom-py module names asnames level) global? env inclass?)]
)))


(define (desugar [expr : PyExpr]) : CExpr
(type-case DesugarResult (rec-desugar expr true empty false)
[DResult (expr env) expr]))
Expand Down
9 changes: 8 additions & 1 deletion base/python-syntax.rkt
Expand Up @@ -72,8 +72,15 @@
[PyNone]
[PyBreak]

; import, which is desugar to asname = __import__("name")
; import, which desugar to asname = __import__("name")
[PyImport (names : (listof string)) (asnames : (listof symbol))]
; from import, which desugar to
; 1. _temp = __import__(...)
; 2. asname = _temp.name
[PyImportFrom (module : string)
(names : (listof string))
(asnames : (listof symbol))
(level : number)]
)


0 comments on commit 52021df

Please sign in to comment.