def begin(self, interactive=False, **kwargs) -> (GenerationContext, list): from anoky.generation.default_special_forms_table import default_special_forms_table context_root_bindings = Record( default_generator=self, generator=self, domain=SDom, special_forms=default_special_forms_table(), macros=default_macro_table(), id_macros=default_id_macro_table(), interactive=interactive) context_root_bindings.update(kwargs) GC = GenerationContext(**context_root_bindings.__dict__) initialization_nodes = [] # Prepend anoky unit initialization code # Something like: # import anoky.importer as __akyimp__ # import anoky.module as __aky__ # __macros__ = {} # __id_macros__ = {} # __special_forms__ = {} from anoky.generation.stubs import akyimport_init_code as aic from anoky.generation.stubs import macrostore_init_code as mic initialization_nodes.extend(aic) initialization_nodes.extend(mic) return GC, initialization_nodes
def begin(self, interactive=False, **kwargs) -> (GenerationContext, list): from anoky.generation.default_special_forms_table import default_special_forms_table context_root_bindings = Record( default_generator = self, generator = self, domain = SDom, special_forms = default_special_forms_table(), macros = default_macro_table(), id_macros = default_id_macro_table(), interactive=interactive ) context_root_bindings.update(kwargs) GC = GenerationContext(**context_root_bindings.__dict__) initialization_nodes = [] # Prepend anoky unit initialization code # Something like: # import anoky.importer as __akyimp__ # import anoky.module as __aky__ # __macros__ = {} # __id_macros__ = {} # __special_forms__ = {} from anoky.generation.stubs import akyimport_init_code as aic from anoky.generation.stubs import macrostore_init_code as mic initialization_nodes.extend(aic) initialization_nodes.extend(mic) return GC, initialization_nodes
def parse_args(argv): options = Record({'verbose': False}) args = SysArgsParser(argv) while args.next() is not None: arg = args() if arg == '--verbose': options.verbose = True Options.PRINT_TREE_TRANSDUCER_OUTPUTS = True Options.PRINT_ARRANGEMENT_OUTPUTS = True elif arg == '--binary': options.binary = True options.encoder = msgpack.packb elif arg == '--lex': if 'encoder' not in options: options.binary = False options.encoder = unicode_encoder if args.peek() is None or args.peek().startswith("--"): options.output = sys.stdout.buffer else: output_name = args.next() if output_name == 'stdout': options.output = sys.stdout.buffer if output_name == 'stderr': options.output = sys.stderr.buffer else: options.output = open(output_name, "wb") elif arg.lower().endswith('.aky'): if 'filename' in options: print("Multiple filenames found!") exit(-1) options['filename'] = arg else: print("Unexpected option, '%s'" % arg) exit(-1) return options
def make_readtable(definition:list): """ Creates a new readtable. The parameter ``definition`` should respect the following format:: definition := [ entries*, default_entry ] entries := [ type_property_value, seq_or_seqs, other_properties? ] default_entry := ['DEFAULT', default_properties] ``type_property_value`` is the value associated with the type_property ``seq_or_seqs`` is a string or a list of strings. ``other_properties`` and ``default_properties`` are dictionaries. """ read_table = Readtable() for spec in definition: if spec[0] == 'DEFAULT': read_table.default_properties.update(spec[1]) else: seqs = spec[1] if isinstance(spec[1], list) else [spec[1]] properties = spec[2] if len(spec) >= 3 else {} for seq in seqs: existing_properties, _ = read_table.query(seq) if existing_properties is not read_table.default_properties: #assert existing_properties['type'] == spec[0] existing_properties.update(properties) else: new_seq_properties = Record({'type': spec[0]}) new_seq_properties.update(properties) read_table.add_or_upd_seq(seq, new_seq_properties) return read_table
def expand_unit(self, unit: Node, **kwargs): assert (isinstance(unit, Node)) context_root_bindings = Record( default_expander=self, expander=self, macros=default_macro_table(), id_macros=default_id_macro_table(), special_forms=default_special_forms_table()) context_root_bindings.update(kwargs) EC = ExpansionContext(**context_root_bindings.__dict__) #unit.cg_context = EC for element in unit: EC.expand(element) return EC
def main(): parser = argparse.ArgumentParser(prefix_chars='-') parser.add_argument('--print-tokens', action='store_true', help='Prints the tokenizer output.') parser.add_argument( '--print-tree-transducer-outputs', action='store_true', help='Prints the output of each transducer in the chain.') parser.add_argument( '--print-arrangement-outputs', action='store_true', help= 'Prints the output of each arrangement within each arrangement-based transducer in the chain.' ) parser.add_argument( '--print-parse', action='store_true', help= 'Prints the outcome of parsing the written code (the forms, seqs, etc).' ) parser.add_argument('--print-macro-expanded-code', action='store_true', help='Prints the code after macro-expansion.') parser.add_argument('--print-python-ast', action='store_true', help='Prints the generated Python AST.') parser.add_argument('--print-python-code', action='store_true', help='Prints the generated Python source.') parser.add_argument( '--skip-transducing', action='store_true', help='Stops the compiler immediately after tokenization.') parser.add_argument('--skip-macroexpansion', action='store_true', help='Stops the compiler immediately after parsing.') parser.add_argument( '--skip-codegen', action='store_true', help='Stops the compiler immediately after macro-expansion.') parser.add_argument('--stdout', action='store_true') parser.add_argument( '-I', '--include', help= 'A colon-separated list of source paths where to search for imported modules.' ) parser.add_argument( '-E', '--toggle-exec', action='store_true', help= 'Executes the compiled code even in non-interactive mode, and does not execute in interactive mode.' ) parser.add_argument( '-CP', '--compile-to-python', action='store_true', help= 'Compiles to Python, outputing the result into a .py file on the same path.' ) parser.add_argument('--version', action='version', version='Anoky α.1') parser.add_argument('file', nargs='*') parse = parser.parse_args() options = Record() options.print_tokens = parse.print_tokens if parse.print_tree_transducer_outputs: G.Options.PRINT_TREE_TRANSDUCER_OUTPUTS = True if parse.print_arrangement_outputs: G.Options.PRINT_ARRANGEMENT_OUTPUTS = True options.print_parse = parse.print_parse options.print_macro_expanded_code = parse.print_macro_expanded_code options.include_dirs = parse.include.split(':') if parse.include else ['.'] if len(parse.file) > 1: raise NotImplementedError() elif len(parse.file) == 0: options.interactive = True options.compile_to_python = False options.execute = not parse.toggle_exec else: options.interactive = False options.filename = parse.file[0] options.compile_to_python = parse.compile_to_python options.execute = parse.toggle_exec options.arrange_tokens = not parse.skip_transducing if not options.arrange_tokens: options.print_tokens = True options.expand_macros = not parse.skip_macroexpansion and not parse.skip_transducing if not options.expand_macros: options.print_parse = True options.generate_code = not parse.skip_macroexpansion and not parse.skip_transducing and not parse.skip_codegen if not options.generate_code: options.print_macro_expanded_code = True options.print_python_code = parse.print_python_code or not options.execute options.print_python_ast = parse.print_python_ast if parse.stdout: options.output = '<stdout>' else: options.output = '.' if options.interactive: interactive_anoky(options) else: non_interactive_anoky(options)
def set_readtable_entry(self, seq, **properties): self.readtable.add_or_upd_seq("=~", Record(**properties))
def add_reader_macro(self, macro_seq, tokenizer_class): tokenizer_name = tokenizer_class.__name__ self.tokenization_context.set(**{tokenizer_name: tokenizer_class}) self.readtable.add_or_upd_seq( macro_seq, Record(type=RT.MACRO, tokenizer=tokenizer_name))
def main(): parser = argparse.ArgumentParser(prefix_chars='-') parser.add_argument('--print-tokens', action='store_true', help='Prints the tokenizer output.') parser.add_argument('--print-tree-transducer-outputs', action='store_true', help='Prints the output of each transducer in the chain.') parser.add_argument('--print-arrangement-outputs', action='store_true', help='Prints the output of each arrangement within each arrangement-based transducer in the chain.') parser.add_argument('--print-parse', action='store_true', help='Prints the outcome of parsing the written code (the forms, seqs, etc).') parser.add_argument('--print-macro-expanded-code', action='store_true', help='Prints the code after macro-expansion.') parser.add_argument('--print-python-ast', action='store_true', help='Prints the generated Python AST.') parser.add_argument('--print-python-code', action='store_true', help='Prints the generated Python source.') parser.add_argument('--skip-transducing', action='store_true', help='Stops the compiler immediately after tokenization.') parser.add_argument('--skip-macroexpansion', action='store_true', help='Stops the compiler immediately after parsing.') parser.add_argument('--skip-codegen', action='store_true', help='Stops the compiler immediately after macro-expansion.') parser.add_argument('--stdout', action='store_true') parser.add_argument('-I', '--include', help='A colon-separated list of source paths where to search for imported modules.') parser.add_argument('-E', '--toggle-exec', action='store_true', help='Executes the compiled code even in non-interactive mode, and does not execute in interactive mode.') parser.add_argument('-CP', '--compile-to-python', action='store_true', help='Compiles to Python, outputing the result into a .py file on the same path.') parser.add_argument('--version', action='version', version='Anoky α.1') parser.add_argument('file', nargs='*') parse = parser.parse_args() options = Record() options.print_tokens = parse.print_tokens if parse.print_tree_transducer_outputs: G.Options.PRINT_TREE_TRANSDUCER_OUTPUTS = True if parse.print_arrangement_outputs: G.Options.PRINT_ARRANGEMENT_OUTPUTS = True options.print_parse = parse.print_parse options.print_macro_expanded_code = parse.print_macro_expanded_code options.include_dirs = parse.include.split(':') if parse.include else ['.'] if len(parse.file) > 1: raise NotImplementedError() elif len(parse.file) == 0: options.interactive = True options.compile_to_python = False options.execute = not parse.toggle_exec else: options.interactive = False options.filename = parse.file[0] options.compile_to_python = parse.compile_to_python options.execute = parse.toggle_exec options.arrange_tokens = not parse.skip_transducing if not options.arrange_tokens: options.print_tokens = True options.expand_macros = not parse.skip_macroexpansion and not parse.skip_transducing if not options.expand_macros: options.print_parse = True options.generate_code = not parse.skip_macroexpansion and not parse.skip_transducing and not parse.skip_codegen if not options.generate_code: options.print_macro_expanded_code = True options.print_python_code = parse.print_python_code or not options.execute options.print_python_ast = parse.print_python_ast if parse.stdout: options.output = '<stdout>' else: options.output = '.' if options.interactive: interactive_anoky(options) else: non_interactive_anoky(options)
def __init__(self): self.root_node = {} """The root of the prefix tree.""" self.default_properties = Record({}) """This is the property list of sequences of characters that are not entries in the readtable."""
def __getattr__(self, name: str): key = "GLOBALS::" + name if not hasattr(builtins, key): setattr(builtins, key, Record()) return getattr(builtins, key)