def ann_assign(expression): handler = handlers.get("ann_assign") var = parser(expression.target) created_variables.get(namespace).append(var) _type = parser(expression.annotation) if expression.value: val = parser(expression.value) return handler(var, _type, val=val) return handler(var, _type)
def assign(expression): value = parser(expression.value) var = parser(expression.targets[0]) if not (var in created_variables.get(namespace)): created_variables.get(namespace).append(var) handler = handlers.get("new_var") return handler(var, value) handler = handlers.get("assign") return handler(var, value)
def _for(tree): var = parser(tree.target) body = statement_block(tree.body) if type(tree.iter) == _ast.Call: if tree.iter.func.id == "range": if "c_like_for" in handlers: param = list(map(parser, tree.iter.args)) if len(param) < 3: param.append("1") if len(param) < 3: param.insert(0, "0") handler = handlers.get("c_like_for") return handler(var, body, param[0], param[1], param[2]) handler = handlers.get("for") obj = parser(tree.iter) return handler(var, obj, body)
def _while(tree): handler = handlers.get("while") condition = parser(tree.test) body = statement_block(tree.body) els = "" if tree.orelse: els = _else(tree.orelse) return handler(condition, body, els)
def define_function(tree): handler = handlers.get("def") args = list(map(parser, tree.args.args)) name = tree.name global namespace namespace += f".{name}" if not (namespace in created_variables.keys()): created_variables.update({namespace: []}) body = statement_block(tree.body) ".".join(namespace.split(".")[:-1]) if tree.returns: ret_t = parser(tree.returns) return handler(name, args, body, ret_t=ret_t) return handler(name, args, body)
def expr(expression): handler = handlers.get("expr") value = parser(expression.value) return handler(value)
def aug_assign(expression): handler = handlers.get("aug_assign") var = parser(expression.target) op = get_sign(expression.op) value = parser(expression.value) return handler(var, op, value)
def ret(expression): handler = handlers.get("return") return handler(parser(expression.value))