src/basilisp/lang/reader.py
def _read_function(ctx: ReaderContext) -> llist.List:
    """Read a function reader macro from the input stream."""
    if ctx.is_in_anon_fn:
        raise SyntaxError(f"Nested #() definitions not allowed")

    with ctx.in_anon_fn():
        form = _read_list(ctx)
    arg_set = set()

    def arg_suffix(arg_num):
        if arg_num is None:
            return "1"
        elif arg_num == "&":
            return "rest"
        else:
            return arg_num

    def sym_replacement(arg_num):
        suffix = arg_suffix(arg_num)
        return symbol.symbol(f"arg-{suffix}")

    def identify_and_replace(f):
        if isinstance(f, symbol.Symbol):
            if f.ns is None:
                match = fn_macro_args.match(f.name)
                if match is not None:
                    arg_num = match.group(2)
                    suffix = arg_suffix(arg_num)
                    arg_set.add(suffix)
                    return sym_replacement(arg_num)
        return f

    body = walk.postwalk(identify_and_replace, form) if len(form) > 0 else None

    arg_list: List[symbol.Symbol] = []
    numbered_args = sorted(map(int, filter(lambda k: k != "rest", arg_set)))
    if len(numbered_args) > 0:
        max_arg = max(numbered_args)
        arg_list = [sym_replacement(str(i)) for i in range(1, max_arg + 1)]
        if "rest" in arg_set:
            arg_list.append(_AMPERSAND)
            arg_list.append(sym_replacement("rest"))

    return llist.l(_FN, vector.vector(arg_list), body)
def _read_function(ctx: ReaderContext) -> llist.List:
    if ctx.is_in_anon_fn:
        raise SyntaxError(f"Nested #() definitions not allowed")

    with ctx.in_anon_fn():
        form = _read_list(ctx)
    arg_set = set()

    def arg_suffix(arg_num):
        if arg_num is None:
            return "1"
        elif arg_num == "&":
            return "rest"
        else:
            return arg_num

    def sym_replacement(arg_num):
        suffix = arg_suffix(arg_num)
        return symbol.symbol(f"arg-{suffix}")

    def identify_and_replace(f):
        if isinstance(f, symbol.Symbol):
            if f.ns is None:
                match = fn_macro_args.match(f.name)
                if match is not None:
                    arg_num = match.group(2)
                    suffix = arg_suffix(arg_num)
                    arg_set.add(suffix)
                    return sym_replacement(arg_num)
        return f

    body = walk.postwalk(identify_and_replace, form) if len(form) > 0 else None

    arg_list: List[symbol.Symbol] = []
    numbered_args = sorted(map(int, filter(lambda k: k != "rest", arg_set)))
    if len(numbered_args) > 0:
        max_arg = max(numbered_args)
        arg_list = [sym_replacement(str(i)) for i in range(1, max_arg + 1)]
        if "rest" in arg_set:
            arg_list.append(_AMPERSAND)
            arg_list.append(sym_replacement("rest"))

    return llist.l(_FN, vector.vector(arg_list), body)
def _read_function ( ctx : ReaderContext ) -> llist . List : if ctx . is_in_anon_fn : raise SyntaxError ( f"Nested #() definitions not allowed" ) with ctx . in_anon_fn ( ) : form = _read_list ( ctx ) arg_set = set ( ) def arg_suffix ( arg_num ) : if arg_num is None : return "1" elif arg_num == "&" : return "rest" else : return arg_num def sym_replacement ( arg_num ) : suffix = arg_suffix ( arg_num ) return symbol . symbol ( f"arg-{suffix}" ) def identify_and_replace ( f ) : if isinstance ( f , symbol . Symbol ) : if f . ns is None : match = fn_macro_args . match ( f . name ) if match is not None : arg_num = match . group ( 2 ) suffix = arg_suffix ( arg_num ) arg_set . add ( suffix ) return sym_replacement ( arg_num ) return f body = walk . postwalk ( identify_and_replace , form ) if len ( form ) > 0 else None arg_list : List [ symbol . Symbol ] = [ ] numbered_args = sorted ( map ( int , filter ( lambda k : k != "rest" , arg_set ) ) ) if len ( numbered_args ) > 0 : max_arg = max ( numbered_args ) arg_list = [ sym_replacement ( str ( i ) ) for i in range ( 1 , max_arg + 1 ) ] if "rest" in arg_set : arg_list . append ( _AMPERSAND ) arg_list . append ( sym_replacement ( "rest" ) ) return llist . l ( _FN , vector . vector ( arg_list ) , body )
Read a function reader macro from the input stream.
Read a function reader macro from the input stream .