Frickn' awesome.
This commit is contained in:
parent
c8937f8f07
commit
c55f501d2e
@ -1,5 +1,5 @@
|
|||||||
import imp
|
|
||||||
from hy.lex.tokenize import tokenize
|
from hy.lex.tokenize import tokenize
|
||||||
|
import imp
|
||||||
|
|
||||||
|
|
||||||
def _add_native_methods(mod):
|
def _add_native_methods(mod):
|
||||||
@ -35,12 +35,13 @@ def _add_native_methods(mod):
|
|||||||
ret /= x
|
ret /= x
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
natives = {
|
natives = {
|
||||||
"print": _print,
|
"print": _print,
|
||||||
"+": _plus,
|
"+": _plus,
|
||||||
"-": _subtract,
|
"-": _subtract,
|
||||||
"*": _mult,
|
"*": _mult,
|
||||||
"/": _divide
|
"/": _divide,
|
||||||
}
|
}
|
||||||
|
|
||||||
for native in natives:
|
for native in natives:
|
||||||
|
@ -23,7 +23,19 @@ def _fn(obj):
|
|||||||
return _
|
return _
|
||||||
|
|
||||||
|
|
||||||
|
def _import(obj):
|
||||||
|
ns = obj.namespace
|
||||||
|
fd = obj.get_invocation()
|
||||||
|
args = fd['args']
|
||||||
|
mods = args[0]
|
||||||
|
|
||||||
|
for module in mods:
|
||||||
|
mod = __import__(module)
|
||||||
|
obj.namespace[module] = mod
|
||||||
|
|
||||||
|
|
||||||
builtins = {
|
builtins = {
|
||||||
"def": _define,
|
"def": _define,
|
||||||
"fn": _fn,
|
"fn": _fn,
|
||||||
|
"import": _import
|
||||||
}
|
}
|
||||||
|
@ -35,4 +35,4 @@ class HYExpression(list, HYObject):
|
|||||||
for child in self.get_children():
|
for child in self.get_children():
|
||||||
things.append(child())
|
things.append(child())
|
||||||
|
|
||||||
return self.namespace[fn](*things)
|
return self.lookup(fn)(*things)
|
||||||
|
@ -12,3 +12,19 @@ class HYObject(object):
|
|||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
return self.eval(*args, **kwargs)
|
return self.eval(*args, **kwargs)
|
||||||
|
|
||||||
|
def lookup(self, fn):
|
||||||
|
callee = None
|
||||||
|
|
||||||
|
if fn in self.namespace:
|
||||||
|
callee = self.namespace[fn]
|
||||||
|
|
||||||
|
if "." in fn:
|
||||||
|
lon, short = fn.rsplit(".", 1)
|
||||||
|
holder = self.lookup(lon)
|
||||||
|
callee = getattr(holder, short)
|
||||||
|
|
||||||
|
if callee:
|
||||||
|
return callee
|
||||||
|
|
||||||
|
raise Exception
|
||||||
|
@ -8,4 +8,4 @@ class HYSymbol(unicode, HYObject):
|
|||||||
def eval(self, *args, **kwargs):
|
def eval(self, *args, **kwargs):
|
||||||
if self.isdigit():
|
if self.isdigit():
|
||||||
return float(self)
|
return float(self)
|
||||||
return self.namespace[self]
|
return self.lookup(self)
|
||||||
|
3
test.hy
3
test.hy
@ -1,6 +1,9 @@
|
|||||||
; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp
|
; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp
|
||||||
|
|
||||||
|
(import ["sys"])
|
||||||
|
|
||||||
(def square (fn [x]
|
(def square (fn [x]
|
||||||
(* x x)))
|
(* x x)))
|
||||||
|
|
||||||
(print (square 2))
|
(print (square 2))
|
||||||
|
(print sys.argv)
|
||||||
|
Loading…
Reference in New Issue
Block a user