diff --git a/hy/compiler.py b/hy/compiler.py index 4c23515..5754fcd 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -138,6 +138,24 @@ class HyASTCompiler(object): kw_defaults=[]), body=self.compile(body)) + @builds("import") + def compile_import_expression(self, expr): + expr.pop(0) # index + return ast.Import( + lineno=expr.start_line, + col_offset=expr.start_column, + names=[ast.alias(name=str(x), asname=None) for x in expr]) + + @builds("import_from") + def compile_import_from_expression(self, expr): + expr.pop(0) # index + return ast.ImportFrom( + lineno=expr.start_line, + col_offset=expr.start_column, + module=str(expr.pop(0)), + names=[ast.alias(name=str(x), asname=None) for x in expr], + level=0) + @builds("get") def compile_index_expression(self, expr): expr.pop(0) # index @@ -321,6 +339,19 @@ class HyASTCompiler(object): @builds(HySymbol) def compile_symbol(self, symbol): + if "." in symbol: + glob, local = symbol.rsplit(".", 1) + glob = HySymbol(glob) + glob.replace(symbol) + + return ast.Attribute( + lineno=symbol.start_line, + col_offset=symbol.start_column, + value=self.compile_symbol(glob), + attr=str(local), + ctx=ast.Load() + ) + return ast.Name(id=str(symbol), ctx=ast.Load(), lineno=symbol.start_line, col_offset=symbol.start_column) diff --git a/hy/lex/states.py b/hy/lex/states.py index 8af5b23..3d1bf93 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -57,6 +57,7 @@ def _resolve_atom(obj): "false": "False", "null": "None", } + if obj in table: return HySymbol(table[obj]) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 130fea4..b349f4e 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -1,5 +1,12 @@ ; +(import sys) +(import-from os.path exists isdir isfile) + + +(defn test-sys-argv [] + "NATIVE: test sys.argv" + (assert (isinstance sys.argv list))) (defn test-lists [] "NATIVE: test lists work right" @@ -74,3 +81,10 @@ "NATIVE: test lambda operator" (def square (lambda [x] (* x x))) (assert (= 4 (square 2)))) + + +(defn test-imported-bits [] + "NATIVE: test the imports work" + (assert (is (exists ".") true)) + (assert (is (isdir ".") true)) + (assert (is (isfile ".") false)))