pyinterop
This commit is contained in:
parent
f7b85cb655
commit
58e13d40ae
@ -14,11 +14,7 @@ from hy.lang.natives import natives
|
|||||||
|
|
||||||
def _ast_print(node, children, obj):
|
def _ast_print(node, children, obj):
|
||||||
""" Handle `print' statements """
|
""" Handle `print' statements """
|
||||||
return ast.Print(
|
return ast.Print(dest=None, values=children, nl=True)
|
||||||
dest=None,
|
|
||||||
values=children,
|
|
||||||
nl=True
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _ast_binop(node, children, obj):
|
def _ast_binop(node, children, obj):
|
||||||
@ -46,15 +42,8 @@ def _ast_cmp(node, children, obj):
|
|||||||
# opscmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn
|
# opscmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn
|
||||||
inv = node.get_invocation()
|
inv = node.get_invocation()
|
||||||
ops = {
|
ops = {
|
||||||
"==": ast.Eq,
|
"==": ast.Eq, "<=": ast.LtE, ">=": ast.GtE, ">": ast.Gt, "<": ast.Lt,
|
||||||
"<=": ast.LtE,
|
"!=": ast.NotEq, "in": ast.In, "not-in": ast.NotIn, "is": ast.Is,
|
||||||
">=": ast.GtE,
|
|
||||||
">": ast.Gt,
|
|
||||||
"<": ast.Lt,
|
|
||||||
"!=": ast.NotEq,
|
|
||||||
"in": ast.In,
|
|
||||||
"not-in": ast.NotIn,
|
|
||||||
"is": ast.Is,
|
|
||||||
"is-not": ast.IsNot
|
"is-not": ast.IsNot
|
||||||
}
|
}
|
||||||
op = ops[inv['function']]
|
op = ops[inv['function']]
|
||||||
@ -67,9 +56,7 @@ def _ast_cmp(node, children, obj):
|
|||||||
def _ast_import(tree):
|
def _ast_import(tree):
|
||||||
i = tree.get_invocation()
|
i = tree.get_invocation()
|
||||||
c = i['args']
|
c = i['args']
|
||||||
return ast.Import(
|
return ast.Import(names=[ast.alias(name=str(x), asname=None) for x in c])
|
||||||
names=[ast.alias(name=str(x), asname=None) for x in c]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _ast_if(node, children, obj):
|
def _ast_if(node, children, obj):
|
||||||
@ -80,11 +67,7 @@ def _ast_if(node, children, obj):
|
|||||||
true = true if isinstance(true, list) else [true]
|
true = true if isinstance(true, list) else [true]
|
||||||
flse = flse if isinstance(flse, list) else [flse]
|
flse = flse if isinstance(flse, list) else [flse]
|
||||||
|
|
||||||
ret = ast.If(
|
ret = ast.If(test=cond, body=true, orelse=flse)
|
||||||
test=cond,
|
|
||||||
body=true,
|
|
||||||
orelse=flse,
|
|
||||||
)
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
@ -99,21 +82,14 @@ def _ast_return(node, children, obj):
|
|||||||
special_cases = {
|
special_cases = {
|
||||||
"print": _ast_print,
|
"print": _ast_print,
|
||||||
|
|
||||||
"+": _ast_binop,
|
"+": _ast_binop, "/": _ast_binop,
|
||||||
"/": _ast_binop,
|
"-": _ast_binop, "*": _ast_binop,
|
||||||
"-": _ast_binop,
|
|
||||||
"*": _ast_binop,
|
|
||||||
|
|
||||||
"==": _ast_cmp,
|
"==": _ast_cmp, "<=": _ast_cmp,
|
||||||
"<=": _ast_cmp,
|
">=": _ast_cmp, "<": _ast_cmp,
|
||||||
">=": _ast_cmp,
|
">": _ast_cmp, "!=": _ast_cmp,
|
||||||
"<": _ast_cmp,
|
"in": _ast_cmp, "not-in": _ast_cmp,
|
||||||
">": _ast_cmp,
|
"is": _ast_cmp, "is-not": _ast_cmp,
|
||||||
"!=": _ast_cmp,
|
|
||||||
"in": _ast_cmp,
|
|
||||||
"not-in": _ast_cmp,
|
|
||||||
"is": _ast_cmp,
|
|
||||||
"is-not": _ast_cmp,
|
|
||||||
|
|
||||||
"if": _ast_if,
|
"if": _ast_if,
|
||||||
"return": _ast_return,
|
"return": _ast_return,
|
||||||
@ -147,9 +123,7 @@ class AST27Converter(object):
|
|||||||
blob = self.render(args[0])
|
blob = self.render(args[0])
|
||||||
|
|
||||||
ret = ast.Assign(
|
ret = ast.Assign(
|
||||||
targets=[
|
targets=[ast.Name(id=str(name), ctx=ast.Store())],
|
||||||
ast.Name(id=str(name), ctx=ast.Store())
|
|
||||||
],
|
|
||||||
value=blob
|
value=blob
|
||||||
)
|
)
|
||||||
return ret
|
return ret
|
||||||
@ -234,7 +208,7 @@ class AST27Converter(object):
|
|||||||
return special_cases[inv['function']](node, c, self)
|
return special_cases[inv['function']](node, c, self)
|
||||||
|
|
||||||
ret = value=ast.Call(
|
ret = value=ast.Call(
|
||||||
func=ast.Name(id=str(inv['function']), ctx=ast.Load()),
|
func=self.render_symbol(inv['function']),
|
||||||
args=c,
|
args=c,
|
||||||
keywords=[],
|
keywords=[],
|
||||||
starargs=None,
|
starargs=None,
|
||||||
@ -256,6 +230,9 @@ def forge_ast(name, forest):
|
|||||||
|
|
||||||
statements = []
|
statements = []
|
||||||
for tree in forest:
|
for tree in forest:
|
||||||
statements.append(conv.render(tree))
|
ret = conv.render(tree)
|
||||||
|
if not isinstance(ret, ast.stmt):
|
||||||
|
ret = ast.Expr(value=ret)
|
||||||
|
statements.append(ret)
|
||||||
|
|
||||||
return ast.fix_missing_locations(ast.Module(body=statements))
|
return ast.fix_missing_locations(ast.Module(body=statements))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user