#line 1 "/tmp/pkgbuild/lang/ecl/work/ecl-16.1.2/src/c/mapfun.d"
/* -*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*- */
/* vim: set filetype=c tabstop=8 shiftwidth=4 expandtab: */

/*
    mapfun.c -- Mapping.
*/
/*
    Copyright (c) 1993, Giuseppe Attardi.
    Copyright (c) 2001, Juan Jose Garcia Ripoll.

    ECL is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    See file '../Copyright' for full details.
*/


#include <ecl/ecl.h>
#include <ecl/internal.h>
#include <string.h>

#define PREPARE_MAP(env, list, cdrs_frame, cars_frame, narg)            \
        struct ecl_stack_frame frames_aux[2];                           \
        const cl_object cdrs_frame = (cl_object)frames_aux;             \
        const cl_object cars_frame = (cl_object)(frames_aux+1);         \
        ECL_STACK_FRAME_FROM_VA_LIST(env,cdrs_frame,list);              \
        ECL_STACK_FRAME_COPY(cars_frame, cdrs_frame);                   \
        narg = cars_frame->frame.size;                                  \
        if (ecl_unlikely(narg == 0)) {                                  \
                FEprogram_error_noreturn("MAP*: Too few arguments", 0); \
        }

#line 35
cl_object cl_mapcar(cl_narg narg, cl_object fun, ...)
{
#line 35

        cl_object res, *val = &res;
#line 37
	const cl_env_ptr the_env = ecl_process_env();
#line 37
	ecl_va_list lists;
	ecl_va_start(lists, fun, narg, 1);
#line 37
	if (ecl_unlikely(narg < 1)) FEwrong_num_arguments(ecl_make_fixnum(545));
#line 37
{
        PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
        res = ECL_NIL;
        while (TRUE) {
                cl_index i;
                for (i = 0;  i < narg;  i++) {
                        cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
                        if (ecl_unlikely(!LISTP(cdr)))
                                FEwrong_type_nth_arg(ecl_make_fixnum(/*MAPCAR*/545), i+2, cdr, ecl_make_fixnum(/*LIST*/481));
                        if (Null(cdr)) {
                                ecl_stack_frame_close(cars_frame);
                                ecl_stack_frame_close(cdrs_frame);
                                {
#line 49
	#line 49
	cl_object __value0 = res;
#line 49
	the_env->nvalues = 1;
#line 49
	return __value0;
#line 49
}

                        }
                        ECL_STACK_FRAME_SET(cars_frame, i, ECL_CONS_CAR(cdr));
                        ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
                }
                *val = ecl_list1(ecl_apply_from_stack_frame(cars_frame, fun));
                val = &ECL_CONS_CDR(*val);
        }
} }

#line 59
cl_object cl_maplist(cl_narg narg, cl_object fun, ...)
{
#line 59

        cl_object res, *val = &res;
#line 61
	const cl_env_ptr the_env = ecl_process_env();
#line 61
	ecl_va_list lists;
	ecl_va_start(lists, fun, narg, 1);
#line 61
	if (ecl_unlikely(narg < 1)) FEwrong_num_arguments(ecl_make_fixnum(549));
#line 61
{
        PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
        res = ECL_NIL;
        while (TRUE) {
                cl_index i;
                for (i = 0;  i < narg;  i++) {
                        cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
                        if (ecl_unlikely(!LISTP(cdr)))
                                FEwrong_type_nth_arg(ecl_make_fixnum(/*MAPLIST*/549), i+2, cdr, ecl_make_fixnum(/*LIST*/481));
                        if (Null(cdr)) {
                                ecl_stack_frame_close(cars_frame);
                                ecl_stack_frame_close(cdrs_frame);
                                {
#line 73
	#line 73
	cl_object __value0 = res;
#line 73
	the_env->nvalues = 1;
#line 73
	return __value0;
#line 73
}

                        }
                        ECL_STACK_FRAME_SET(cars_frame, i, cdr);
                        ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
                }
                *val = ecl_list1(ecl_apply_from_stack_frame(cars_frame, fun));
                val = &ECL_CONS_CDR(*val);
        }
} }

#line 83
cl_object cl_mapc(cl_narg narg, cl_object fun, ...)
{
#line 83

        cl_object onelist;
#line 85
	const cl_env_ptr the_env = ecl_process_env();
#line 85
	ecl_va_list lists;
	ecl_va_start(lists, fun, narg, 1);
#line 85
	if (ecl_unlikely(narg < 1)) FEwrong_num_arguments(ecl_make_fixnum(543));
#line 85
{
        PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
        onelist = ECL_STACK_FRAME_REF(cdrs_frame, 0);
        while (TRUE) {
                cl_index i;
                for (i = 0;  i < narg;  i++) {
                        cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
                        if (ecl_unlikely(!LISTP(cdr)))
                                FEwrong_type_nth_arg(ecl_make_fixnum(/*MAPC*/543), i+2, cdr, ecl_make_fixnum(/*LIST*/481));
                        if (Null(cdr)) {
                                ecl_stack_frame_close(cars_frame);
                                ecl_stack_frame_close(cdrs_frame);
                                {
#line 97
	#line 97
	cl_object __value0 = onelist;
#line 97
	the_env->nvalues = 1;
#line 97
	return __value0;
#line 97
}

                        }
                        ECL_STACK_FRAME_SET(cars_frame, i, ECL_CONS_CAR(cdr));
                        ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
                }
                ecl_apply_from_stack_frame(cars_frame, fun);
        }
} }

#line 106
cl_object cl_mapl(cl_narg narg, cl_object fun, ...)
{
#line 106

        cl_object onelist;
#line 108
	const cl_env_ptr the_env = ecl_process_env();
#line 108
	ecl_va_list lists;
	ecl_va_start(lists, fun, narg, 1);
#line 108
	if (ecl_unlikely(narg < 1)) FEwrong_num_arguments(ecl_make_fixnum(548));
#line 108
{
        PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
        onelist = ECL_STACK_FRAME_REF(cdrs_frame, 0);
        while (TRUE) {
                cl_index i;
                for (i = 0;  i < narg;  i++) {
                        cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
                        if (ecl_unlikely(!LISTP(cdr)))
                                FEwrong_type_nth_arg(ecl_make_fixnum(/*MAPL*/548), i+2, cdr, ecl_make_fixnum(/*LIST*/481));
                        if (Null(cdr)) {
                                ecl_stack_frame_close(cars_frame);
                                ecl_stack_frame_close(cdrs_frame);
                                {
#line 120
	#line 120
	cl_object __value0 = onelist;
#line 120
	the_env->nvalues = 1;
#line 120
	return __value0;
#line 120
}

                        }
                        ECL_STACK_FRAME_SET(cars_frame, i, cdr);
                        ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
                }
                ecl_apply_from_stack_frame(cars_frame, fun);
        }
} }

#line 129
cl_object cl_mapcan(cl_narg narg, cl_object fun, ...)
{
#line 129

        cl_object res, *val = &res;
#line 131
	const cl_env_ptr the_env = ecl_process_env();
#line 131
	ecl_va_list lists;
	ecl_va_start(lists, fun, narg, 1);
#line 131
	if (ecl_unlikely(narg < 1)) FEwrong_num_arguments(ecl_make_fixnum(544));
#line 131
{
        PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
        res = ECL_NIL;
        while (TRUE) {
                cl_index i;
                for (i = 0;  i < narg;  i++) {
                        cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
                        if (ecl_unlikely(!LISTP(cdr)))
                                FEwrong_type_nth_arg(ecl_make_fixnum(/*MAPCAN*/544), i+2, cdr, ecl_make_fixnum(/*LIST*/481));
                        if (Null(cdr)) {
                                ecl_stack_frame_close(cars_frame);
                                ecl_stack_frame_close(cdrs_frame);
                                {
#line 143
	#line 143
	cl_object __value0 = res;
#line 143
	the_env->nvalues = 1;
#line 143
	return __value0;
#line 143
}

                        }
                        ECL_STACK_FRAME_SET(cars_frame, i, ECL_CONS_CAR(cdr));
                        ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
                }
                *val = ecl_apply_from_stack_frame(cars_frame, fun);
                while (CONSP(*val))
                        val = &ECL_CONS_CDR(*val);
        }
} }

#line 154
cl_object cl_mapcon(cl_narg narg, cl_object fun, ...)
{
#line 154

        cl_object res, *val = &res;
#line 156
	const cl_env_ptr the_env = ecl_process_env();
#line 156
	ecl_va_list lists;
	ecl_va_start(lists, fun, narg, 1);
#line 156
	if (ecl_unlikely(narg < 1)) FEwrong_num_arguments(ecl_make_fixnum(546));
#line 156
{
        PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
        res = ECL_NIL;
        while (TRUE) {
                cl_index i;
                for (i = 0;  i < narg;  i++) {
                        cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
                        if (ecl_unlikely(!LISTP(cdr)))
                                FEwrong_type_nth_arg(ecl_make_fixnum(/*MAPCON*/546), i+2, cdr, ecl_make_fixnum(/*LIST*/481));
                        if (Null(cdr)) {
                                ecl_stack_frame_close(cars_frame);
                                ecl_stack_frame_close(cdrs_frame);
                                {
#line 168
	#line 168
	cl_object __value0 = res;
#line 168
	the_env->nvalues = 1;
#line 168
	return __value0;
#line 168
}

                        }
                        ECL_STACK_FRAME_SET(cars_frame, i, cdr);
                        ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
                }
                *val = ecl_apply_from_stack_frame(cars_frame, fun);
                while (CONSP(*val))
                        val = &ECL_CONS_CDR(*val);
        }
} }
