def expand_inner_escapes(element: Element, EC: ExpansionContext): aquote = element.code # If we have a quote escape, expand sub-elements if is_form(aquote, "~"): assert len(aquote) == 2 EC.expand(aquote[1]) # otherwise, recurse on nodes, ignore the rest elif isinstance(aquote, Node): for e in aquote: Quote.expand_inner_escapes(e, EC)
def expand_inner_escapes(element:Element, EC:ExpansionContext): aquote = element.code # If we have a quote escape, expand sub-elements if is_form(aquote, "~"): assert len(aquote) == 2 EC.expand(aquote[1]) # otherwise, recurse on nodes, ignore the rest elif isinstance(aquote, Node): for e in aquote: Quote.expand_inner_escapes(e, EC)
def expand(self, element: Element, EC: ExpansionContext): code = element.code # expand downwards by cases if isinstance(code, Form): head = code.first headcode = head.code # check if any macros apply if is_identifier(headcode): if headcode.full_name in EC.macros: EC.macros[headcode.full_name].expand(element, EC) elif headcode.full_name in EC.special_forms: EC.special_forms[headcode.full_name].expand(element, EC) # otherwise expand recursively else: for item in code: EC.expand(item) elif isinstance(code, Seq): # expand all elements in the seq for item in code: EC.expand(item) elif isinstance(code, Literal): pass elif isinstance(code, Identifier): # IDENTIFIER MACROS if code.full_name in EC.id_macros: idmac = EC.id_macros[code.full_name] idmac.expand(element, EC) # element.parent.replace(element, idmac.expand(element, context)) elif code.full_name in EC.macros: raise MacroExpansionError( code.range, "Refering to macro `%s` by name requires the use of `the`." % code.full_name)
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 expand(self, element:Element, EC:ExpansionContext): acode = element.code sig = acode[1].code mac_name = sig[0].code.full_name param_names = [p.code.full_name for p in sig[1:]] body = [s.code for s in acode[2:]] # TODO: implement multistatement definitions assert len(body) == 1 # we are supposed to RUN this statement and take the result # as the replacement, but for now this just uses the code itself rcode = body[0] mac = Macro(specs=(param_names, rcode)) EC.macro_table[mac_name] = mac
def expand(self, element:Element, EC:ExpansionContext): for child in element.code[1:]: EC.expand(child)
def expand(self, element: Element, EC: ExpansionContext): for item in element.code: EC.expand(item)
def expand_macros(code: Node): default_expander = DefaultExpander() c = ExpansionContext(default_expander=default_expander) for item in code: c.expand(item)
def expand(self, id_element, EC: ExpansionContext): id_element.expand(self.substitution.copy()) EC.expand(id_element)
def expand(self, id_element, EC:ExpansionContext): id_element.expand(self.substitution.copy()) EC.expand(id_element)