Uh, lolwut?
This commit is contained in:
parent
868d1cbd6e
commit
1c6874f779
@ -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
25
hy/lang/builtins.py
Normal 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,
|
||||
}
|
@ -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())
|
||||
|
||||
|
@ -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)
|
||||
|
6
test.hy
6
test.hy
@ -1,6 +0,0 @@
|
||||
; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp
|
||||
|
||||
(def square
|
||||
(fn [x] (* x x)))
|
||||
|
||||
(print (square 4))
|
Loading…
Reference in New Issue
Block a user