diff --git a/hy/compiler/modfaker.py b/hy/compiler/modfaker.py index cdbed9f..eee05a3 100644 --- a/hy/compiler/modfaker.py +++ b/hy/compiler/modfaker.py @@ -1,5 +1,5 @@ -import imp from hy.lex.tokenize import tokenize +import imp def _add_native_methods(mod): @@ -35,12 +35,13 @@ def _add_native_methods(mod): ret /= x return ret + natives = { "print": _print, "+": _plus, "-": _subtract, "*": _mult, - "/": _divide + "/": _divide, } for native in natives: diff --git a/hy/lang/builtins.py b/hy/lang/builtins.py index 15de3d5..282ea9e 100644 --- a/hy/lang/builtins.py +++ b/hy/lang/builtins.py @@ -23,7 +23,19 @@ def _fn(obj): return _ +def _import(obj): + ns = obj.namespace + fd = obj.get_invocation() + args = fd['args'] + mods = args[0] + + for module in mods: + mod = __import__(module) + obj.namespace[module] = mod + + builtins = { "def": _define, "fn": _fn, + "import": _import } diff --git a/hy/lang/expression.py b/hy/lang/expression.py index 9181429..c503824 100644 --- a/hy/lang/expression.py +++ b/hy/lang/expression.py @@ -35,4 +35,4 @@ class HYExpression(list, HYObject): for child in self.get_children(): things.append(child()) - return self.namespace[fn](*things) + return self.lookup(fn)(*things) diff --git a/hy/lang/hyobj.py b/hy/lang/hyobj.py index 03a8cb8..c391a41 100644 --- a/hy/lang/hyobj.py +++ b/hy/lang/hyobj.py @@ -12,3 +12,19 @@ class HYObject(object): def __call__(self, *args, **kwargs): return self.eval(*args, **kwargs) + + def lookup(self, fn): + callee = None + + if fn in self.namespace: + callee = self.namespace[fn] + + if "." in fn: + lon, short = fn.rsplit(".", 1) + holder = self.lookup(lon) + callee = getattr(holder, short) + + if callee: + return callee + + raise Exception diff --git a/hy/lang/symbol.py b/hy/lang/symbol.py index ccb8d14..bc9b953 100644 --- a/hy/lang/symbol.py +++ b/hy/lang/symbol.py @@ -8,4 +8,4 @@ class HYSymbol(unicode, HYObject): def eval(self, *args, **kwargs): if self.isdigit(): return float(self) - return self.namespace[self] + return self.lookup(self) diff --git a/test.hy b/test.hy index 7a5ef01..5c53dae 100644 --- a/test.hy +++ b/test.hy @@ -1,6 +1,9 @@ ; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp +(import ["sys"]) + (def square (fn [x] (* x x))) (print (square 2)) +(print sys.argv)