// Set the current namespace current_namespace = (struct namespace *)vtable_toplevel_namespace; }}}END_EMBEDDED_C EMBEDDED_C{{{ return 0; } }}}END_EMBEDDED_C """ params = globals() for key in locals().keys(): params[key] = locals()[key] grammar = OMeta.makeGrammar(strip_comments(grammar_def), params) if __name__ == '__main__': if len(sys.argv) < 2: print "Usage: iddish_compiler.py input.id [output.c]" sys.exit() in_name = sys.argv[1] if len(sys.argv) > 2: out_name = sys.argv[2] else: out_name = in_name.rsplit('.', 1)[0]+'.c' in_file = open(in_name, 'r') in_lines = ''.join([l for l in in_file.readlines()]) in_file.close()
# If we've reached a '}meta' then stop matching, otherwise keep going annotation_contents ::= <token '}meta'> => '' | <anything> <annotation_contents> => '' # A statement is a series of annotations or anything else statement ::= <annotation>+ => '' | <anything>+:a => ''.join(a) # A program is a series of statements program ::= <statement>+:a => ''.join(a) """ # Now we embed the transformations in every AST node, so that they can # apply them recursively to their children finder = OMeta.makeGrammar(strip_comments(annotation_finder), globals()) def strip_annotations(path_or_text): """This performs the translation from annotated Python to normal Python. It takes in annotated Python code (assuming the string to be a file path, falling back to treating it as raw code if it is not a valid path) and emits Python code.""" # See if the given string is a valid path if os.path.exists(path_or_text): # If so then open it and read the file contents into in_text infile = open(path_or_text, 'r') in_text = '\n'.join([line for line in infile.readlines()]) infile.close() # Otherwise take the string contents to be in_text else:
# If we've reached a '}meta' then stop matching, otherwise keep going annotation_contents ::= <token '}meta'> => '' | <anything> <annotation_contents> => '' # A statement is a series of annotations or anything else statement ::= <annotation>+ => '' | <anything>+:a => ''.join(a) # A program is a series of statements program ::= <statement>+:a => ''.join(a) """ # Now we embed the transformations in every AST node, so that they can # apply them recursively to their children finder = OMeta.makeGrammar(strip_comments(annotation_finder), globals()) def strip_annotations(path_or_text): """This performs the translation from annotated Python to normal Python. It takes in annotated Python code (assuming the string to be a file path, falling back to treating it as raw code if it is not a valid path) and emits Python code.""" # See if the given string is a valid path if os.path.exists(path_or_text): # If so then open it and read the file contents into in_text infile = open(path_or_text, 'r') in_text = '\n'.join([line for line in infile.readlines()]) infile.close() # Otherwise take the string contents to be in_text else: in_text = path_or_text