def parse_string(calculation_string):
    """
    Wandelt mit Hilfe der ast2json-Bibliothek einen String in einen abstrakten Syntaxbaum. Kann auch zur Überprüfung der Syntax einer Formel genutzt werden.

    :param calculation_string: Stringrepräsentation einer Formel
    :type calculation_string: str

    :return: Formel als abstrakter Syntaxbaum (Dictionary) repräsentiert
    """
    try:
        return str2json(calculation_string)
    except Exception:
        return None
Exemplo n.º 2
0
def get_value(attr_value):
    if attr_value is None:
        return attr_value
    if isinstance(attr_value, (int, str, float, complex, bool)):
        return attr_value
    if isinstance(attr_value, list) or isinstance(attr_value, tuple):
        return [get_value(x) for x in attr_value]
    if isinstance(attr_value, dict):
        return attr_value
    if isinstance(attr_value, renpy.ast.Node):
        return node2json(attr_value)
    if isinstance(attr_value, renpy.ast.PyCode):
        return {
            "source": attr_value.source,
            "ast": ast2json.str2json(attr_value.source)
        }
    if isinstance(attr_value, renpy.ast.ArgumentInfo):
        return list(map(lambda x: getattr(attr_value, x), ["arguments", "extrapos", "extrakw"]))
    if isinstance(attr_value, renpy.atl.RawBlock):
        return 'ATL not implemented'

    raise Exception("I don't know how to decompile '%s' of type '%s'!" % (attr_value, type(attr_value)))
Exemplo n.º 3
0
        
    def get_node_text(self, node):
        l1 = node['lineno']
        l2 = node['end_lineno']
        c1 = node['col_offset']
        c2 = node['end_col_offset']
        return self.subset_program_text(l1, c1, l2, c2)
        
    def format_lineno(self, node):
        return "At line " + str(node['lineno']) + " :"

if __name__ == '__main__':
    file_name = sys.argv[1]
    with open(file_name, 'r') as f:
        program = f.read()
        program_ast = ast2json.str2json(program)
        syntax_checker = Syntax_Checker()
        syntax_checker.visit_Module(program_ast, program)
        grapher = GraphGenerator()
        grapher.visit_Module(program_ast, program)
        fva = FaintVariableAnalyzer(grapher.graph)
        fva.run_optimizer()
        print("Completed faint variable analysis.")
        printer = OptimizedPrinter()
        printer.visit_Module(program_ast, program)

        if len(sys.argv) == 2:
            output_filename, _ = os.path.splitext(file_name)
            output_filename += "_optimized.py"
        elif len(sys.argv) == 3:
            output_filename = sys.argv[2]