From c55f501d2e7dec350c96968387b63d38da35d40e Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Sun, 16 Dec 2012 17:46:33 -0500 Subject: [PATCH] Frickn' awesome. --- hy/compiler/modfaker.py | 5 +++-- hy/lang/builtins.py | 12 ++++++++++++ hy/lang/expression.py | 2 +- hy/lang/hyobj.py | 16 ++++++++++++++++ hy/lang/symbol.py | 2 +- test.hy | 3 +++ 6 files changed, 36 insertions(+), 4 deletions(-) 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)