def sent_if(sentence: ast.If, f=""): elif_list = [] #Make Elif Block list tmp = sentence.orelse while len(tmp) != 0 and type(tmp[0]) == ast.If and len(tmp[0].orelse) != 0: elif_list.append(tmp) tmp = tmp[0].orelse blocks = [a[0] for a in elif_list] elseblock = tmp tmp = "" #If Block tmp += "if(" + util.conv(sentence.test, mode=util.modes.EXPR) + "){\n" tmp += util.walk_shallow(sentence.body, f=f + " ") tmp += "}" #Elif Blocks for block in blocks: tmp += f + "else if(" + util.conv(block.test, mode=util.modes.EXPR) + "){\n" tmp += util.walk_shallow(block.body, f + " ") tmp += f + "}" #Else Block if len(elseblock) != 0: tmp += f + "else{\n" tmp += util.walk_shallow(elseblock, f + " ") tmp += f + "}" return tmp + "\n"
def sent_classdef(sentence: ast.ClassDef, f=""): tmp = "" tmp += f + "class _" + sentence.name if len(sentence.bases) != 0: tmp += ": " tmp += ", ".join([ "public " + util.conv(base, mode=util.modes.EXPR) for base in sentence.bases ]) tmp += "\n" tmp += "{\n" tmp += util.walk_shallow(sentence.body, f=f + " ") tmp += "}\n" decor_proc = "_" + sentence.name decors = [ util.conv(a, mode=util.modes.EXPR) for a in sentence.decorator_list ] decors.reverse() for decor in decors: decor_proc = f"{decor}({decor_proc})" tmp += f"#define {sentence.name} {decor_proc}\n" return tmp
def sent_try(sentence: ast.Try, f=""): s = "" s += f + "try{\n" s += util.walk_shallow(sentence.body, f + " ") s += util.walk_shallow(sentence.finalbody, f + " ") s += f + "}" for handler in sentence.handlers: name = handler.name if not name: name = "ex" s += f + "catch(" + util.conv( handler.type, mode=util.modes.EXPR) + " " + name + "){\n" s += util.walk_shallow(handler.body, f + " ") s += util.walk_shallow(sentence.finalbody, f + " ") s += f + "}" if len(sentence.orelse) != 0: s += util.walk_shallow(sentence.orelse, f) return s
def sent_funcdef(sentence: ast.FunctionDef, f=""): if type(sentence.body[0]) == ast.Expr and type( sentence.body[0].value) == ast.Constant: body = sentence.body[1:] else: body = sentence.body return \ f+"Any "+sentence.name+"("+expr_args(sentence.args)+")"+"{\n"+\ util.walk_shallow(body,f+" ")+\ f+"}\n"
def sent_with(sentence: ast.With, f=""): tmp = "" tmp += f + "\n" for item in sentence.items: tmp += f + util.conv( item.optional_vars, mode=util.modes.EXPR) + " = " + util.conv( item.context_expr, mode=util.modes.EXPR) + ".__enter__();\n" tmp += util.walk_shallow(sentence.body, f) for item in sentence.items: tmp += f + util.conv( item.optional_vars, mode=util.modes.EXPR) + " = " + util.conv( item.context_expr, mode=util.modes.EXPR) + ".__exit__(nullptr,nullptr,nullptr);\n" tmp += f + "\n" return tmp
def sent_while(sentence: ast.While, f=""): return \ f+"while ("+util.conv(sentence.test,mode=util.modes.EXPR)+"){\n"+\ util.walk_shallow(sentence.body,f+" ")+\ f+"}\n"
def sent_for(sentence: ast.For, f=""): return \ f+"for ("+"auto "+util.conv(sentence.target,mode=util.modes.EXPR)+" : "+util.conv(sentence.iter,mode=util.modes.EXPR)+"){\n"+\ util.walk_shallow(sentence.body,f+" ")+\ f+"}\n"