doing some scratch work.
This commit is contained in:
parent
c2b87efc6c
commit
154ba2dfe4
50
hy/compiler/modfaker.py
Normal file
50
hy/compiler/modfaker.py
Normal file
@ -0,0 +1,50 @@
|
||||
import imp
|
||||
from hy.lex.tokenize import tokenize
|
||||
|
||||
|
||||
def _add_builtins(mod):
|
||||
def shim():
|
||||
def _define(symbol, block):
|
||||
globals()[symbol] = block
|
||||
|
||||
def _fn(args, expr):
|
||||
def _(*args, **kwargs):
|
||||
expr(*args, **kwargs)
|
||||
return _
|
||||
|
||||
def _plus(*args):
|
||||
r = 0
|
||||
for arg in args:
|
||||
r += int(arg)
|
||||
return r
|
||||
|
||||
def _print(*args):
|
||||
print " ".join(args)
|
||||
|
||||
builtins = {
|
||||
"def": _define,
|
||||
"fn": _fn,
|
||||
"print": _print,
|
||||
"+": _plus
|
||||
}
|
||||
|
||||
for builtin in builtins:
|
||||
globals()[builtin] = builtins[builtin]
|
||||
|
||||
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)
|
||||
|
||||
def shim():
|
||||
for tree in _hy_forest:
|
||||
tree.set_namespace(globals())
|
||||
tree()
|
||||
|
||||
eval(shim.__code__, mod.__dict__)
|
||||
|
||||
return mod
|
@ -1,17 +0,0 @@
|
||||
#
|
||||
import ast
|
||||
|
||||
|
||||
def emit_node(node):
|
||||
print type(node)
|
||||
body = []
|
||||
for sn in node.get_children():
|
||||
body.append(emit_node(sn))
|
||||
print body
|
||||
|
||||
|
||||
def emit(tree):
|
||||
ret = []
|
||||
for node in tree:
|
||||
ret.append(emit_node(node))
|
||||
return ret
|
@ -1,6 +1,10 @@
|
||||
class HYExpression(list):
|
||||
from hy.lang.hyobj import HYObject
|
||||
|
||||
|
||||
class HYExpression(list, HYObject):
|
||||
def __init__(self, nodes):
|
||||
self += nodes
|
||||
self.namespace = globals()
|
||||
|
||||
def get_children(self):
|
||||
ret = []
|
||||
|
11
hy/lang/hyobj.py
Normal file
11
hy/lang/hyobj.py
Normal file
@ -0,0 +1,11 @@
|
||||
class HYObject(object):
|
||||
def set_namespace(self, ns):
|
||||
self.namespace = ns
|
||||
for c in self.get_children():
|
||||
c.set_namespace(ns)
|
||||
|
||||
def get_children(self):
|
||||
return []
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
return self
|
@ -1,6 +1,9 @@
|
||||
class HYList(list):
|
||||
from hy.lang.hyobj import HYObject
|
||||
|
||||
|
||||
class HYList(list, HYObject):
|
||||
def __init__(self, nodes):
|
||||
self += nodes
|
||||
|
||||
def get_children(self):
|
||||
return []
|
||||
return self
|
||||
|
@ -1,6 +1,6 @@
|
||||
class HYString(unicode):
|
||||
from hy.lang.hyobj import HYObject
|
||||
|
||||
|
||||
class HYString(unicode, HYObject):
|
||||
def __init__(self, string):
|
||||
self += string
|
||||
|
||||
def get_children(self):
|
||||
return []
|
||||
|
@ -56,13 +56,13 @@ class Expression(State):
|
||||
|
||||
def exit(self):
|
||||
if self.bulk:
|
||||
self.nodes.append(self.bulk)
|
||||
self.nodes.append(HYString(self.bulk))
|
||||
|
||||
self.machine.nodes.append(self.nodes)
|
||||
|
||||
def commit(self):
|
||||
if self.bulk.strip() != "":
|
||||
self.nodes.append(self.bulk)
|
||||
self.nodes.append(HYString(self.bulk))
|
||||
self.bulk = ""
|
||||
|
||||
def p(self, x):
|
||||
@ -82,12 +82,12 @@ class List(State):
|
||||
|
||||
def exit(self):
|
||||
if self.bulk:
|
||||
self.nodes.append(self.bulk)
|
||||
self.nodes.append(HYString(self.bulk))
|
||||
self.machine.nodes.append(self.nodes)
|
||||
|
||||
def commit(self):
|
||||
if self.bulk.strip() != "":
|
||||
self.nodes.append(self.bulk)
|
||||
self.nodes.append(HYString(self.bulk))
|
||||
self.bulk = ""
|
||||
|
||||
def p(self, x):
|
||||
|
Loading…
Reference in New Issue
Block a user