Getting fib working :>
This commit is contained in:
parent
c02900412c
commit
7f4b4f8bf8
11
AUTHORS
11
AUTHORS
@ -1 +1,10 @@
|
||||
Paul R. Tagliamonte <tag@pault.ag>
|
||||
Code contributors:
|
||||
|
||||
- Paul R. Tagliamonte <tag@pault.ag>
|
||||
|
||||
I'd also like to thank the following people:
|
||||
|
||||
- Kragen Sitaker:
|
||||
Major thanks goes to Kragen on helping me work through some of the
|
||||
namespacing crap. I think the majority of the approach taken now
|
||||
with expressions' eval bits is his doing in one way or another.
|
||||
|
6
bin/hython
Executable file
6
bin/hython
Executable file
@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from hy.lang.importer import _hy_import_file
|
||||
import sys
|
||||
|
||||
mod = _hy_import_file(sys.argv[1], '<stdin>')
|
@ -120,7 +120,7 @@ def forge_module(name, fpath, forest):
|
||||
def shim():
|
||||
ns = globals()
|
||||
for tree in _hy_forest:
|
||||
tree.set_namespace(ns, {})
|
||||
tree.set_namespace(ns)
|
||||
|
||||
for tree in _hy_forest:
|
||||
tree()
|
||||
|
@ -5,5 +5,5 @@ class HYBool(HYObject):
|
||||
def __init__(self, val):
|
||||
self._val = val
|
||||
|
||||
def eval(self, *args, **kwargs):
|
||||
def eval(self, lns, *args, **kwargs):
|
||||
return self._val == True
|
||||
|
@ -1,37 +1,40 @@
|
||||
#
|
||||
import sys
|
||||
from hy.lang.internals import HYNamespaceCOW
|
||||
|
||||
|
||||
def _define(obj):
|
||||
def _define(obj, lns):
|
||||
fd = obj.get_invocation()
|
||||
args = fd['args']
|
||||
obj.namespace[args[0]] = args[1]()
|
||||
|
||||
|
||||
def _fn(obj):
|
||||
def _fn(obj, lns):
|
||||
fd = obj.get_invocation()
|
||||
args = fd['args']
|
||||
sig = args[0]
|
||||
meth = args[1]
|
||||
|
||||
def _(*args, **kwargs):
|
||||
l = lns.clone()
|
||||
m = meth.copy()
|
||||
for i in range(0, len(sig)):
|
||||
name = sig[i]
|
||||
value = args[i]
|
||||
meth.local_namespace[name] = value
|
||||
l[name] = value
|
||||
|
||||
ret = meth(*args, **kwargs)
|
||||
ret = m.eval(l, *args, **kwargs)
|
||||
return ret
|
||||
return _
|
||||
|
||||
|
||||
def _kwapply(obj):
|
||||
def _kwapply(obj, lns):
|
||||
fd = obj.get_invocation()
|
||||
subshell, kwargs = fd['args']
|
||||
return subshell.eval(**kwargs)
|
||||
return subshell.eval(lns.clone(), **kwargs)
|
||||
|
||||
|
||||
def _import(obj):
|
||||
def _import(obj, lns):
|
||||
ns = obj.namespace
|
||||
fd = obj.get_invocation()
|
||||
args = fd['args']
|
||||
@ -44,13 +47,13 @@ def _import(obj):
|
||||
ns[basename] = mod
|
||||
|
||||
|
||||
def _if(obj):
|
||||
def _if(obj, lns):
|
||||
fd = obj.get_invocation()
|
||||
args = fd['args']
|
||||
if args[0].eval():
|
||||
return args[1].eval()
|
||||
if args[0].eval(lns.clone()):
|
||||
return args[1].eval(lns.clone())
|
||||
else:
|
||||
return args[2].eval()
|
||||
return args[2].eval(lns.clone())
|
||||
|
||||
|
||||
builtins = {
|
||||
|
@ -2,7 +2,7 @@ from hy.lang.hyobj import HYObject
|
||||
from hy.lang.builtins import builtins
|
||||
|
||||
|
||||
class HYExpression(list, HYObject):
|
||||
class HYExpression(HYObject, list):
|
||||
def __init__(self, nodes):
|
||||
self += nodes
|
||||
|
||||
@ -24,17 +24,17 @@ class HYExpression(list, HYObject):
|
||||
def peek(self):
|
||||
return self.get_invocation()['function']
|
||||
|
||||
def eval(self, *args, **kwargs):
|
||||
def eval(self, lns, *args, **kwargs):
|
||||
fn = self.peek()
|
||||
|
||||
if fn in builtins:
|
||||
# special-case builtin handling.
|
||||
return builtins[fn](self)
|
||||
return builtins[fn](self, lns)
|
||||
|
||||
things = []
|
||||
for child in self.get_children():
|
||||
c = child.copy()
|
||||
things.append(c())
|
||||
things.append(c.eval(lns.clone()))
|
||||
|
||||
ret = self.lookup(fn)(*things, **kwargs)
|
||||
ret = self.lookup(lns, fn)(*things, **kwargs)
|
||||
return ret
|
||||
|
@ -1,44 +1,38 @@
|
||||
from hy.lang.internals import HYNamespaceCOW
|
||||
|
||||
class HYObject(object):
|
||||
def set_namespace(self, ns, ls):
|
||||
def set_namespace(self, ns):
|
||||
self.namespace = ns
|
||||
nns = HYNamespaceCOW(ls)
|
||||
self.local_namespace = nns
|
||||
|
||||
for c in self.get_children():
|
||||
c.set_namespace(ns, nns)
|
||||
c.set_namespace(ns)
|
||||
|
||||
def get_children(self):
|
||||
return []
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
return self.eval(*args, **kwargs)
|
||||
ns = HYNamespaceCOW({}) # Each invocation needs it's own ns
|
||||
return self.eval(ns, *args, **kwargs)
|
||||
|
||||
def lookup(self, fn):
|
||||
callee = None
|
||||
if fn in self.local_namespace:
|
||||
callee = self.local_namespace[fn]
|
||||
def lookup(self, lns, fn):
|
||||
if fn in lns:
|
||||
return lns[fn]
|
||||
|
||||
elif callee is None and fn in self.namespace:
|
||||
callee = self.namespace[fn]
|
||||
if fn in self.namespace:
|
||||
return self.namespace[fn]
|
||||
|
||||
elif callee is None and "." in fn:
|
||||
if "." in fn:
|
||||
lon, short = fn.rsplit(".", 1)
|
||||
holder = self.lookup(lon)
|
||||
callee = getattr(holder, short)
|
||||
|
||||
if callee is not None:
|
||||
return callee
|
||||
holder = self.lookup(lns, lon)
|
||||
return getattr(holder, short)
|
||||
|
||||
raise Exception("No such symbol: `%s`" % (fn))
|
||||
|
||||
def eval(self, *args, **kwargs):
|
||||
def eval(self, lns, *args, **kwargs):
|
||||
for node in self.get_children():
|
||||
node.eval(*args, **kwargs)
|
||||
node.eval(lns, *args, **kwargs)
|
||||
return self
|
||||
|
||||
def copy(self):
|
||||
new = type(self)(self)
|
||||
new.set_namespace(self.namespace, self.local_namespace)
|
||||
new.set_namespace(self.namespace)
|
||||
return new
|
||||
|
@ -18,3 +18,6 @@ class HYNamespaceCOW(object):
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self._mute[key] = value
|
||||
|
||||
def clone(self):
|
||||
return HYNamespaceCOW(self)
|
||||
|
@ -1,7 +1,7 @@
|
||||
from hy.lang.hyobj import HYObject
|
||||
|
||||
|
||||
class HYList(list, HYObject):
|
||||
class HYList(HYObject, list):
|
||||
def __init__(self, nodes):
|
||||
self += nodes
|
||||
|
||||
|
@ -3,10 +3,8 @@ from hy.lang.hyobj import HYObject
|
||||
|
||||
class HYNumber(HYObject, int):
|
||||
def __new__(cls, number, *args, **kwargs):
|
||||
if isinstance(number, HYObject):
|
||||
number = number.eval()
|
||||
number = int(number)
|
||||
return super(HYNumber, cls).__new__(cls, number)
|
||||
|
||||
def eval(self, *args, **kwargs):
|
||||
def eval(self, lns, *args, **kwargs):
|
||||
return int(self)
|
||||
|
@ -5,6 +5,6 @@ class HYSymbol(HYString):
|
||||
def __init__(self, string):
|
||||
self += string
|
||||
|
||||
def eval(self, *args, **kwargs):
|
||||
obj = self.lookup(self)
|
||||
def eval(self, lns, *args, **kwargs):
|
||||
obj = self.lookup(lns, self)
|
||||
return obj
|
||||
|
Loading…
x
Reference in New Issue
Block a user