adding comparitors

This commit is contained in:
Paul Tagliamonte 2012-12-22 17:08:02 -05:00
parent c60d410426
commit 412e42a43e

View File

@ -38,6 +38,28 @@ def _ast_binop(node, children, obj):
return calc return calc
def _ast_cmp(node, children, obj):
# Compare(left=Num(n=1), ops=[LtE()], comparators=[Num(n=2)])
# Compare(left=Num(n=1), ops=[Gt(), Gt()],
# comparators=[Num(n=2), Num(n=3)])
# opscmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn
inv = node.get_invocation()
ops = {
"==": ast.Eq,
"<=": ast.LtE,
">=": ast.GtE,
">": ast.Gt,
"<": ast.Lt,
"!=": ast.NotEq
}
op = ops[inv['function']]
left = children.pop(0)
cop = [op()] * len(children)
return ast.Compare(left=left, ops=cop, comparators=children)
def _ast_if(node, children, obj): def _ast_if(node, children, obj):
cond = children.pop(0) cond = children.pop(0)
true = children.pop(0) true = children.pop(0)
@ -51,13 +73,27 @@ def _ast_if(node, children, obj):
return ret return ret
def _ast_return(node, children, obj):
return ast.Return(value=children[-1])
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,
"if": _ast_if
"==": _ast_cmp,
"<=": _ast_cmp,
">=": _ast_cmp,
"<": _ast_cmp,
">": _ast_cmp,
"!=": _ast_cmp,
"if": _ast_if,
"return": _ast_return,
} }
@ -112,10 +148,7 @@ class AST27Converter(object):
body = [c[-1]] body = [c[-1]]
if doc: if doc:
# Shim in docstrings # Shim in docstrings
body.insert( body.insert(0, ast.Expr(value=ast.Str(s=str(doc))))
0,
ast.Expr(value=ast.Str(s=str(doc)))
)
ret = ast.FunctionDef( ret = ast.FunctionDef(
name=str(name), name=str(name),