Adding in on-the-fly-importing.
This commit is contained in:
parent
b361a230cb
commit
d25d354281
@ -32,6 +32,7 @@ from hy.models.dict import HyDict
|
|||||||
|
|
||||||
from hy.util import flatten_literal_list
|
from hy.util import flatten_literal_list
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
import codecs
|
import codecs
|
||||||
import ast
|
import ast
|
||||||
import sys
|
import sys
|
||||||
@ -117,6 +118,7 @@ class HyASTCompiler(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.returnable = False
|
self.returnable = False
|
||||||
self.anon_fn_count = 0
|
self.anon_fn_count = 0
|
||||||
|
self.imports = defaultdict(list)
|
||||||
|
|
||||||
def compile(self, tree):
|
def compile(self, tree):
|
||||||
try:
|
try:
|
||||||
@ -181,6 +183,8 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
def _render_quoted_form(self, form):
|
def _render_quoted_form(self, form):
|
||||||
name = form.__class__.__name__
|
name = form.__class__.__name__
|
||||||
|
self.imports["hy"].append((name, form))
|
||||||
|
|
||||||
if isinstance(form, HyList):
|
if isinstance(form, HyList):
|
||||||
return HyExpression(
|
return HyExpression(
|
||||||
[HySymbol(name),
|
[HySymbol(name),
|
||||||
@ -199,6 +203,8 @@ class HyASTCompiler(object):
|
|||||||
@checkargs(exact=1)
|
@checkargs(exact=1)
|
||||||
def compile_eval(self, expr):
|
def compile_eval(self, expr):
|
||||||
expr.pop(0)
|
expr.pop(0)
|
||||||
|
self.imports["hy.importer"].append(("hy_eval", expr))
|
||||||
|
|
||||||
return self.compile(HyExpression([
|
return self.compile(HyExpression([
|
||||||
HySymbol("hy_eval")] + expr + [
|
HySymbol("hy_eval")] + expr + [
|
||||||
HyExpression([HySymbol("locals")])]).replace(expr))
|
HyExpression([HySymbol("locals")])]).replace(expr))
|
||||||
@ -945,5 +951,21 @@ def hy_compile(tree, root=None):
|
|||||||
if type(_ast) == list:
|
if type(_ast) == list:
|
||||||
_ast = compiler._mangle_branch(_ast, 0, 0)
|
_ast = compiler._mangle_branch(_ast, 0, 0)
|
||||||
|
|
||||||
|
imports = []
|
||||||
|
for package in compiler.imports:
|
||||||
|
imported = set()
|
||||||
|
syms = compiler.imports[package]
|
||||||
|
for entry, form in syms:
|
||||||
|
if entry in imported:
|
||||||
|
continue
|
||||||
|
|
||||||
|
imported.add(entry)
|
||||||
|
imports.append(HyExpression([
|
||||||
|
HySymbol("import_from"),
|
||||||
|
HySymbol(package),
|
||||||
|
HySymbol(entry)
|
||||||
|
]).replace(form))
|
||||||
|
_ast = compiler.compile(imports) + _ast
|
||||||
|
|
||||||
ret = tlo(body=_ast)
|
ret = tlo(body=_ast)
|
||||||
return ret
|
return ret
|
||||||
|
@ -513,8 +513,6 @@
|
|||||||
|
|
||||||
(defn test-eval []
|
(defn test-eval []
|
||||||
"NATIVE: test eval"
|
"NATIVE: test eval"
|
||||||
(import-from hy.importer hy-eval) ; XXX: Fix this!!!!!
|
|
||||||
(import-from hy HyExpression HyInteger HySymbol HyString)
|
|
||||||
(assert (= 2 (eval (quote (+ 1 1)))))
|
(assert (= 2 (eval (quote (+ 1 1)))))
|
||||||
(setf x 2)
|
(setf x 2)
|
||||||
(assert (= 4 (eval (quote (+ x 2)))))
|
(assert (= 4 (eval (quote (+ x 2)))))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user