From 154ba2dfe4051ff8118b697ec9b8b60922565194 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Sun, 16 Dec 2012 14:33:49 -0500 Subject: [PATCH] doing some scratch work. --- hy/{emitters => compiler}/__init__.py | 0 hy/compiler/modfaker.py | 50 +++++++++++++++++++++++++++ hy/emitters/ast27.py | 17 --------- hy/lang/expression.py | 6 +++- hy/lang/hyobj.py | 11 ++++++ hy/lang/list.py | 7 ++-- hy/lang/string.py | 8 ++--- hy/lex/states.py | 8 ++--- 8 files changed, 79 insertions(+), 28 deletions(-) rename hy/{emitters => compiler}/__init__.py (100%) create mode 100644 hy/compiler/modfaker.py delete mode 100644 hy/emitters/ast27.py create mode 100644 hy/lang/hyobj.py diff --git a/hy/emitters/__init__.py b/hy/compiler/__init__.py similarity index 100% rename from hy/emitters/__init__.py rename to hy/compiler/__init__.py diff --git a/hy/compiler/modfaker.py b/hy/compiler/modfaker.py new file mode 100644 index 0000000..0fe42c5 --- /dev/null +++ b/hy/compiler/modfaker.py @@ -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 diff --git a/hy/emitters/ast27.py b/hy/emitters/ast27.py deleted file mode 100644 index b156c6e..0000000 --- a/hy/emitters/ast27.py +++ /dev/null @@ -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 diff --git a/hy/lang/expression.py b/hy/lang/expression.py index 73b64a1..65fe734 100644 --- a/hy/lang/expression.py +++ b/hy/lang/expression.py @@ -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 = [] diff --git a/hy/lang/hyobj.py b/hy/lang/hyobj.py new file mode 100644 index 0000000..c7f4ce1 --- /dev/null +++ b/hy/lang/hyobj.py @@ -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 diff --git a/hy/lang/list.py b/hy/lang/list.py index befeaee..90775ed 100644 --- a/hy/lang/list.py +++ b/hy/lang/list.py @@ -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 diff --git a/hy/lang/string.py b/hy/lang/string.py index 2ec66f5..44a9144 100644 --- a/hy/lang/string.py +++ b/hy/lang/string.py @@ -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 [] diff --git a/hy/lex/states.py b/hy/lex/states.py index 23dde2e..bc3b576 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -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):