some hacking

This commit is contained in:
Paul Tagliamonte 2012-12-22 00:18:37 -05:00
parent 9c58c196c8
commit 395943ca53
7 changed files with 166 additions and 142 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@
*hy*egg* *hy*egg*
.tox .tox
*pycache* *pycache*
dist
.coverage

View File

@ -2,6 +2,5 @@
; Copyright (c) Paul Tagliamonte, in sofar as any of this is at all ; Copyright (c) Paul Tagliamonte, in sofar as any of this is at all
; copyrightable. ; copyrightable.
(loop (print (eval (read)))) (loop (print (eval (read))))
; Yes folks, this is where REPL comes from. ; Yes folks, this is where REPL comes from.

View File

@ -5,156 +5,27 @@ import imp
def _add_native_methods(mod): def _add_native_methods(mod):
def shim(): def shim():
from hy.lang.bool import HYBool from hy.lang.natives import natives, _lex
from hy.lex.tokenize import tokenize as _hy_tok
import sys
def _print(*args, **kwargs):
sys.stdout.write(" ".join([str(x) for x in args]) + "\n")
sys.stdout.flush()
def _read(*args):
return sys.stdin.readline()
def _lex(*args):
ret = []
for thing in args:
ret.append(_hy_tok(thing))
return ret
def _foreach(*args):
a = args[0]
for arg in a:
args[1](arg)
def _get(*args):
m = args[0]
k = args[1]
if k in m:
return m[k]
else:
if len(args) > 2:
return args[2]
raise KeyError("No such key `%s` in map." % (k))
def _eval(*args): def _eval(*args):
"""
This needs to be in here, since we need to assign the
global namespace of evaluated nodes in the same namespace
as the caller.
"""
ret = [] ret = []
for node in _lex(*args): for node in _lex(*args):
for tree in node: for tree in node:
tree.set_namespace(globals()) tree.set_namespace(globals())
ret.append(tree()) ret.append(tree())
return ret return ret
globals()['eval'] = _eval
def _plus(*args):
ret = args[0]
args = args[1:]
for x in args:
ret += x
return ret
def _subtract(*args):
ret = args[0]
args = args[1:]
for x in args:
ret -= x
return ret
def _mult(*args):
ret = args[0]
args = args[1:]
for x in args:
ret *= x
return ret
def _divide(*args):
ret = args[0]
args = args[1:]
for x in args:
ret /= x
return ret
def _eq(*args):
car, cdr = args[0], args[1:]
for arg in cdr:
if arg != car:
return False
return True
def _ne(*args):
seen = set()
for arg in args:
if arg in seen:
return False
seen.add(arg)
return True
def _gt(*args):
arg = args[0]
for i in range(1, len(args)):
if not (args[i - 1] > args[i]):
return False
return True
def _ge(*args):
arg = args[0]
for i in range(1, len(args)):
if not (args[i - 1] >= args[i]):
return False
return True
def _le(*args):
arg = args[0]
for i in range(1, len(args)):
if not (args[i - 1] <= args[i]):
return False
return True
def _lt(*args):
arg = args[0]
for i in range(1, len(args)):
if not (args[i - 1] < args[i]):
return False
return True
natives = {
"print": _print,
"puts": _print,
"+": _plus,
"-": _subtract,
"*": _mult,
"/": _divide,
"==": _eq,
">": _gt,
">=": _ge,
"<": _lt,
"<=": _le,
"!=": _ne,
"eval": _eval,
"lex": _lex,
"read": _read,
"foreach": _foreach,
"get": _get
}
for native in natives: for native in natives:
globals()[native] = natives[native] globals()[native] = natives[native]
del(natives)
eval(shim.__code__, mod.__dict__) eval(shim.__code__, mod.__dict__)

View File

@ -25,13 +25,12 @@ def _fn(obj, lns):
def _(*args, **kwargs): def _(*args, **kwargs):
l = lns.clone() l = lns.clone()
m = meth.copy()
for i in range(0, len(sig)): for i in range(0, len(sig)):
name = sig[i] name = sig[i]
value = args[i] value = args[i]
l[name] = value l[name] = value
ret = m.eval(l, *args, **kwargs) ret = meth.eval(l, *args, **kwargs)
return ret return ret
return _ return _
@ -80,5 +79,5 @@ builtins = {
"kwapply": _kwapply, "kwapply": _kwapply,
"if": _if, "if": _if,
"loop": _loop, "loop": _loop,
"progn": _progn "progn": _progn,
} }

View File

@ -1,5 +1,6 @@
from hy.lang.internals import HYNamespaceCOW from hy.lang.internals import HYNamespaceCOW
class HYObject(object): class HYObject(object):
def set_namespace(self, ns): def set_namespace(self, ns):
self.namespace = ns self.namespace = ns
@ -20,6 +21,10 @@ class HYObject(object):
if fn in self.namespace: if fn in self.namespace:
return self.namespace[fn] return self.namespace[fn]
if fn in self.namespace['__builtins__']:
return self.namespace['__builtins__'][fn]
# builtin lookup
if "." in fn: if "." in fn:
lon, short = fn.rsplit(".", 1) lon, short = fn.rsplit(".", 1)
holder = self.lookup(lns, lon) holder = self.lookup(lns, lon)
@ -32,6 +37,12 @@ class HYObject(object):
node.eval(lns, *args, **kwargs) node.eval(lns, *args, **kwargs)
return self return self
def _issue_job(self, job, *args, **kwargs):
pass
def _join(self):
pass
def copy(self): def copy(self):
new = type(self)(self) new = type(self)(self)
new.set_namespace(self.namespace) new.set_namespace(self.namespace)

141
hy/lang/natives.py Normal file
View File

@ -0,0 +1,141 @@
from hy.lang.bool import HYBool
from hy.lex.tokenize import tokenize as _hy_tok
import sys
def _print(*args, **kwargs):
sys.stdout.write(" ".join([str(x) for x in args]) + "\n")
sys.stdout.flush()
def _read(*args):
return sys.stdin.readline()
def _lex(*args):
ret = []
for thing in args:
ret.append(_hy_tok(thing))
return ret
def _foreach(*args):
a = args[0]
for arg in a:
args[1](arg)
def _get(*args):
m = args[0]
k = args[1]
if k in m:
return m[k]
else:
if len(args) > 2:
return args[2]
raise KeyError("No such key `%s` in map." % (k))
def _plus(*args):
ret = args[0]
args = args[1:]
for x in args:
ret += x
return ret
def _subtract(*args):
ret = args[0]
args = args[1:]
for x in args:
ret -= x
return ret
def _mult(*args):
ret = args[0]
args = args[1:]
for x in args:
ret *= x
return ret
def _divide(*args):
ret = args[0]
args = args[1:]
for x in args:
ret /= x
return ret
def _eq(*args):
car, cdr = args[0], args[1:]
for arg in cdr:
if arg != car:
return False
return True
def _ne(*args):
seen = set()
for arg in args:
if arg in seen:
return False
seen.add(arg)
return True
def _gt(*args):
arg = args[0]
for i in range(1, len(args)):
if not (args[i - 1] > args[i]):
return False
return True
def _ge(*args):
arg = args[0]
for i in range(1, len(args)):
if not (args[i - 1] >= args[i]):
return False
return True
def _le(*args):
arg = args[0]
for i in range(1, len(args)):
if not (args[i - 1] <= args[i]):
return False
return True
def _lt(*args):
arg = args[0]
for i in range(1, len(args)):
if not (args[i - 1] < args[i]):
return False
return True
def _throw(*args):
raise args[0]
natives = {
"print": _print,
"puts": _print,
"+": _plus,
"-": _subtract,
"*": _mult,
"/": _divide,
"==": _eq,
">": _gt,
">=": _ge,
"<": _lt,
"<=": _le,
"!=": _ne,
"lex": _lex,
"read": _read,
"foreach": _foreach,
"get": _get,
"throw": _throw
}

View File

@ -1,2 +1,3 @@
flake8 flake8
nose nose
coverage