Uh, lolwut?

This commit is contained in:
Paul Tagliamonte 2012-12-16 16:36:44 -05:00
parent 868d1cbd6e
commit 1c6874f779
6 changed files with 51 additions and 32 deletions

View File

@ -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:

25
hy/lang/builtins.py Normal file
View File

@ -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,
}

View File

@ -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())

View File

@ -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)

View File

@ -1,6 +0,0 @@
; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp
(def square
(fn [x] (* x x)))
(print (square 4))

View File

@ -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