doing some scratch work.

This commit is contained in:
Paul Tagliamonte 2012-12-16 14:33:49 -05:00
parent c2b87efc6c
commit 154ba2dfe4
8 changed files with 79 additions and 28 deletions

50
hy/compiler/modfaker.py Normal file
View 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

View File

@ -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

View File

@ -1,6 +1,10 @@
class HYExpression(list): from hy.lang.hyobj import HYObject
class HYExpression(list, HYObject):
def __init__(self, nodes): def __init__(self, nodes):
self += nodes self += nodes
self.namespace = globals()
def get_children(self): def get_children(self):
ret = [] ret = []

11
hy/lang/hyobj.py Normal file
View 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

View File

@ -1,6 +1,9 @@
class HYList(list): from hy.lang.hyobj import HYObject
class HYList(list, HYObject):
def __init__(self, nodes): def __init__(self, nodes):
self += nodes self += nodes
def get_children(self): def get_children(self):
return [] return self

View File

@ -1,6 +1,6 @@
class HYString(unicode): from hy.lang.hyobj import HYObject
class HYString(unicode, HYObject):
def __init__(self, string): def __init__(self, string):
self += string self += string
def get_children(self):
return []

View File

@ -56,13 +56,13 @@ class Expression(State):
def exit(self): def exit(self):
if self.bulk: if self.bulk:
self.nodes.append(self.bulk) self.nodes.append(HYString(self.bulk))
self.machine.nodes.append(self.nodes) self.machine.nodes.append(self.nodes)
def commit(self): def commit(self):
if self.bulk.strip() != "": if self.bulk.strip() != "":
self.nodes.append(self.bulk) self.nodes.append(HYString(self.bulk))
self.bulk = "" self.bulk = ""
def p(self, x): def p(self, x):
@ -82,12 +82,12 @@ class List(State):
def exit(self): def exit(self):
if self.bulk: if self.bulk:
self.nodes.append(self.bulk) self.nodes.append(HYString(self.bulk))
self.machine.nodes.append(self.nodes) self.machine.nodes.append(self.nodes)
def commit(self): def commit(self):
if self.bulk.strip() != "": if self.bulk.strip() != "":
self.nodes.append(self.bulk) self.nodes.append(HYString(self.bulk))
self.bulk = "" self.bulk = ""
def p(self, x): def p(self, x):