diff --git a/hy/compiler/modfaker.py b/hy/compiler/modfaker.py index 0fe42c5..61aa552 100644 --- a/hy/compiler/modfaker.py +++ b/hy/compiler/modfaker.py @@ -2,43 +2,32 @@ import imp from hy.lex.tokenize import tokenize -def _add_builtins(mod): +def _add_native_methods(mod): def shim(): - def _define(symbol, block): - globals()[symbol] = block - - def _fn(args, expr): - def _(*args, **kwargs): - expr(*args, **kwargs) - return _ + def _print(*args, **kwargs): + print " ".join([str(x) for x in args]) def _plus(*args): - r = 0 - for arg in args: - r += int(arg) - return r + ret = 0 + for x in args: + ret += int(x) + return ret - def _print(*args): - print " ".join(args) - - builtins = { - "def": _define, - "fn": _fn, + natives = { "print": _print, "+": _plus } - for builtin in builtins: - globals()[builtin] = builtins[builtin] + for native in natives: + globals()[native] = natives[native] eval(shim.__code__, mod.__dict__) - def forge_module(name, fpath, forest): mod = imp.new_module(name) mod.__file__ = fpath mod._hy_forest = forest - _add_builtins(mod) + _add_native_methods(mod) def shim(): for tree in _hy_forest: diff --git a/hy/lang/builtins.py b/hy/lang/builtins.py new file mode 100644 index 0000000..310ff65 --- /dev/null +++ b/hy/lang/builtins.py @@ -0,0 +1,25 @@ +# + + +def _define(obj): + fd = obj.get_invocation() + args = fd['args'] + obj.namespace[args[0]] = args[1]() + + +def _fn(obj): + fd = obj.get_invocation() + args = fd['args'] + sig = args[0] + meth = args[1] + + def _(*args, **kwargs): + # meth validation + return meth(*args, **kwargs) + return _ + + +builtins = { + "def": _define, + "fn": _fn, +} diff --git a/hy/lang/expression.py b/hy/lang/expression.py index 7f61455..9181429 100644 --- a/hy/lang/expression.py +++ b/hy/lang/expression.py @@ -1,4 +1,5 @@ from hy.lang.hyobj import HYObject +from hy.lang.builtins import builtins class HYExpression(list, HYObject): @@ -21,9 +22,16 @@ class HYExpression(list, HYObject): "args": args } - def __call__(self, *args, **kwargs): + def peek(self): + return self.get_invocation()['function'] + + def eval(self, *args, **kwargs): + fn = self.peek() + if fn in builtins: + # special-case builtin handling. + return builtins[fn](self) + things = [] - fn = self.get_invocation()['function'] for child in self.get_children(): things.append(child()) diff --git a/hy/lang/hyobj.py b/hy/lang/hyobj.py index c7f4ce1..03a8cb8 100644 --- a/hy/lang/hyobj.py +++ b/hy/lang/hyobj.py @@ -7,5 +7,8 @@ class HYObject(object): def get_children(self): return [] - def __call__(self, *args, **kwargs): + def eval(self, *args, **kwargs): return self + + def __call__(self, *args, **kwargs): + return self.eval(*args, **kwargs) diff --git a/test.hy b/test.hy deleted file mode 100644 index 2ed227a..0000000 --- a/test.hy +++ /dev/null @@ -1,6 +0,0 @@ -; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp - -(def square - (fn [x] (* x x))) - -(print (square 4)) diff --git a/test.py b/test.py index 7313eac..b290cd6 100644 --- a/test.py +++ b/test.py @@ -5,7 +5,7 @@ from hy.lex.tokenize import tokenize m = forge_module( 'test', 'test.hy', - tokenize('(def two (+ 1 1))') + tokenize('(def two (fn [] (print (+ 1 1))))') ) print m.two