trainhacking++

This commit is contained in:
Paul Tagliamonte 2013-03-05 18:16:04 -05:00
parent fecf17960d
commit 12d7fc6726
4 changed files with 46 additions and 12 deletions

View File

@ -25,4 +25,4 @@ d: clear dev
diff: diff:
git diff --color | less -r git diff --color | less -r
review: d diff r: d diff

0
bin/hy Normal file → Executable file
View File

10
bin/hy2py Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env python
from hy.compiler import hy_compile
from hy.lex import tokenize
import codegen
import sys
ast = hy_compile(tokenize(open(sys.argv[1], 'r').read()))
print codegen.to_source(ast)

View File

@ -67,12 +67,10 @@ class HyASTCompiler(object):
ret.append(ast.Return(value=el, ret.append(ast.Return(value=el,
lineno=el.lineno, lineno=el.lineno,
col_offset=el.col_offset)) col_offset=el.col_offset))
ret += [ ret += [ast.Expr(value=el,
ast.Expr(value=el, lineno=el.lineno,
lineno=el.lineno, col_offset=el.col_offset)
col_offset=el.col_offset) if not isinstance(el, ast.stmt) else el for el in tree] # NOQA
if not isinstance(el, ast.stmt) else el for el in tree # NOQA
] # for some stupid reason, flake8 thinks i'm redefining. ^^^^
ret.reverse() ret.reverse()
return ret return ret
@ -95,19 +93,40 @@ class HyASTCompiler(object):
lineno=expression.start_line, lineno=expression.start_line,
col_offset=expression.start_column) col_offset=expression.start_column)
@builds("def")
def compile_def_expression(self, expression):
expression.pop(0) # "def"
name = expression.pop(0)
what = self.compile(expression.pop(0))
if type(what) == ast.FunctionDef:
# We special case a FunctionDef, since we can define by setting
# FunctionDef's .name attribute, rather then foo == anon_fn. This
# helps keep things clean.
what.name = str(name)
return what
name = self.compile(name)
name.ctx = ast.Store()
return ast.Assign(
lineno=expression.start_line,
col_offset=expression.start_column,
targets=[name], value=what)
@builds("fn") @builds("fn")
def compile_fn_expression(self, expression): def compile_fn_expression(self, expression):
expression.pop(0) # fn
ret_status = self.returnable ret_status = self.returnable
self.returnable = True self.returnable = True
expression.pop(0) # fn
self.anon_fn_count += 1 self.anon_fn_count += 1
name = "_hy_anon_fn_%d" % (self.anon_fn_count) name = "_hy_anon_fn_%d" % (self.anon_fn_count)
sig = expression.pop(0) sig = expression.pop(0)
ret = ast.FunctionDef(name=name, vararg=None, kwarg=None, ret = ast.FunctionDef(name=name,
kwonlyargs=[], kw_defaults=[], defaults=[],
lineno=expression.start_line, lineno=expression.start_line,
col_offset=expression.start_column, col_offset=expression.start_column,
args=ast.arguments(args=[ args=ast.arguments(args=[
@ -115,7 +134,12 @@ class HyASTCompiler(object):
ctx=ast.Param(), ctx=ast.Param(),
lineno=x.start_line, lineno=x.start_line,
col_offset=x.start_column) col_offset=x.start_column)
for x in sig]), for x in sig],
vararg=None,
kwarg=None,
kwonlyargs=[],
kw_defaults=[],
defaults=[]),
body=self._mangle_branch([ body=self._mangle_branch([
self.compile(x) for x in expression]), self.compile(x) for x in expression]),
decorator_list=[]) decorator_list=[])