## Data and control flow

### C Reference

#### `ecl_bds_bind`

Bind a special variable

##### Functions

    	  ecl_bds_bind
    	  cl_env_ptr cl_env
    	  cl_object var
    	  cl_object value

    	  ecl_bds_push
    	  cl_env_ptr cl_env
    	  cl_object var

##### Description

Establishes a variable binding for the symbol var in the Common Lisp environment env, assigning it value.

This macro or function is the equivalent of LET* and LET.

`ecl_bds_push` does a similar thing, but reuses the old value of the same variable. It is thus the equivalent of `(LET ((VAR VAR)) ...)`

Every variable binding must undone when no longer needed. It is best practice to match each call to `ecl_bds_bind` by another call to `ecl_bds_unwind` in the same function.

#### `ecl_bds_unwind`

Undo one variable binding

##### Function

    	  ecl_bds_unwind1
    	  cl_env_ptr cl_env

    	  ecl_bds_unwind_n
    	  cl_env_ptr cl_env
    	  int n

##### Description

`ecl_bds_unwind1` undoes the outermost variable binding, restoring the original value of the symbol in the process.

`ecl_bds_unwind_n` does the same, but for the n last variables.

Every variable binding must undone when no longer needed. It is best practice to match each call to `ecl_bds_bind` by another call to `ecl_bds_unwind` in the same function.

#### `ecl_setq`

C equivalent of setq

##### Macro

    	  ecl_setq
    	  cl_env_ptr cl_env
    	  cl_object var
    	  cl_object value

##### Description

Assigns value to the special variable denoted by the symbol var, in the Common Lisp environment cl_env.

This function implements a variable assignment, not a variable binding. It is thus the equivalent of setq.

#### `ecl_symbol_value`

C equivalent of symbol-value

##### Funciton

    	  ecl_symbol_value
    	  cl_env_ptr cl_env
    	  cl_object var

##### Description

Retrieves the value of the special variable or constant denoted by the symbol var, in the Common Lisp environment cl_env.

This function implements the equivalent of symbol-value and works both on special variables and constants.

If the symbol is not bound, an error is signaled.

#### `ecl_va_arg`

Accepting a variable number of arguments

##### Macros

    typedef struct { ... } ecl_va_list[1];

    	  ecl_va_start
    	  ecl_va_list arglist
    	  last_argument
    	  narg
    	  n_ordinary

    	  cl_object ecl_va_arg
    	  ecl_va_list arglist

    	  cl_object ecl_va_end
    	  ecl_va_list arglist

##### Description

The macros above are used to code a function that accepts an arbitrary number of arguments. We will describe them in a practical example

    cl_object my_plus(cl_narg narg, cl_object required1, ...)
    {
        cl_env_ptr env = ecl_process_env();
        cl_object other_value;
        ecl_va_list varargs;
        ecl_va_start(varargs, required1, narg, 1);
        while (narg > 1) {
            cl_object other_value = ecl_va_arg(varargs);
    	required1 = ecl_plus(required1, other_value);
        }
        ecl_va_end(varargs);
        ecl_return1(env, required1);
    }

The first thing to do is to declare the variable that will hold the arguments. This is varargs in our example and it has the type ecl_va_list.

This arguments list is initialized with the `ecl_va_start` macro, based on the supplied number of arguments, narg, the number of required arguments which are passed as ordinary C arguments (1 in this case), the last such ordinary arguments, required, and the buffer for the argument list, varargs.

Once varargs has been initialized, we can retrieve these values one by one using `ecl_va_arg`. Note that the returned value always has the type cl_object, for it is always a Common Lisp object.

The last statement before returning the output of the function is `ecl_va_end`. This macro performs any required cleanup and should never be omitted.

#### `ecl_nth_value`, `ecl_nvalues`

Accessing output values

##### Functions and macros

    	  cl_object ecl_nvalues
    	  cl_env_ptr env

    	  cl_object ecl_nth_value
    	  cl_env_ptr env
    	  int n

##### Description

Common Lisp functions may return zero, one or more values. In &ECL;, the first two cases do not require any special manipulation, as the C function returns either NIL or the first (zeroth) value directly. However, if one wishes to access additional values from a function, one needs to use these two macros or functions

- `ecl_nvalues(env)` returns the number of values that the function actually outputs. The single argument is the lisp environment. This value is larger or equal to 0 and smaller than `ECL_MULTIPLE_VALUES_LIMIT`.

- Once we know the number of return values, they can be directly accessed using the function `ecl_nth_value(env,n)`, where n is a number larger than or equal to 1, and smaller than `ECL_MULTIPLE_VALUES_LIMIT`, which must correspond to a valid output value. No checking is done.

Note that in both cases these macros and functions have to be used right after the Lisp function was called. This is so because other Lisp functions might destroy the content of the return stack.

##### Example

A C/C++ exceprt

    cl_env_ptr env = ecl_process_env();
    cl_object a = ecl_make_fixnum(13);
    cl_object b = ecl_make_fixnum(6);
    cl_object modulus = cl_floor(2, a, b);
    cl_object remainder = ecl_nth_value(env, 1);

The somewhat equivalent Common Lisp code

    (multiple-value-bind (modulus equivalent)
        (floor 13 6))

#### `ecl_return0`, `ecl_return1`, ...

Returning multiple values

    	  ecl_return0
    	  cl_env_ptr cl_env

    	  ecl_return1
    	  cl_env_ptr cl_env
    	  cl_object value1

    	  ecl_return2
    	  cl_env_ptr cl_env
    	  cl_object value1
    	  cl_object value2

    	  ecl_return3
    	  cl_env_ptr cl_env
    	  cl_object value1
    	  cl_object value2
    	  cl_object value3

##### Description

Returns N values from a C/C++ function in a way that a Common Lisp function can recognize and use them. The 0-th value is returned directly, while values 1 to N are stored in the Common Lisp environment cl_env. This macro has to be used from a function which returns an object of type cl_object.

#### `ECL_BLOCK_BEGIN`

C macro for block

    ECL_BLOCK_BEGIN(env,code) {

    } ECL_BLOCK_END;

##### Description

`ECL_BLOCK_BEGIN` establishes a block named code that becomes visible for the Common Lisp code. This block can be used then as a target for `cl_return`.

env must be the value of the current Common Lisp environment, obtained with `ecl_process_env`.

The C/C++ program has to ensure that the code in `ECL_BLOCK_END` gets executed, avoiding a direct exit of the block via `goto` or a C/C++ `return`.

#### `ECL_CATCH_BEGIN`

C macro for catch

    ECL_CATCH_BEGIN(env,tag) {

    } ECL_CATCH_END;

##### Description

`ECL_CATCH_BEGIN` establishes a destination for throw with the code given by tag.

env must be the value of the current Common Lisp environment, obtained with `ecl_process_env`.

The C/C++ program has to ensure that the code in `ECL_CATCH_END` gets executed, avoiding a direct exit of the catch block via `goto` or a C/C++ `return`.

#### `ECL_UNWIND_PROTECT_BEGIN`

C macro for unwind-protect

    ECL_UNWIND_PROTECT_BEGIN(env) {

    } ECL_UNWIND_PROTECT_EXIT {

    } ECL_UNWIND_PROTECT_END;

##### Description

`ECL_UNWIND_PROTECT_BEGIN` establishes two blocks of C code that work like the equivalent ones in Common Lisp: a protected block, contained between the "BEGIN" and the "EXIT" statement, and the exit block, appearing immediately afterwards. The form guarantees that the exit block is always executed, even if the protected block attempts to exit via som nonlocal jump construct (throw,return, etc).

env must be the value of the current Common Lisp environment, obtained with `ecl_process_env`.

The utility of this construct is limited, for it only protects against nonlocal exits caused by Common Lisp constructs: it does not interfere with C `goto`, `return` or with C++ exceptions.

#### ANSI Dictionary

&ANSI-C-Dict;

- Lisp symbol
- C function or constant
- apply
- `cl_object cl_apply(cl_narg narg, cl_object function, ...)`
- call-arguments-limit
- `ECL_CALL_ARGUMENTS_LIMIT`
- compiled-function-p
- `cl_object cl_compiled_function_p(cl_object object)`
- complement
- `cl_object cl_complement(cl_object function)`
- constantly
- `cl_object cl_constantly(cl_object value)`
- every
- `cl_object cl_every(cl_narg narg, cl_object predicate, ...)`
- eq
- `cl_object cl_eq(cl_object x, cl_object y)`
- eql
- `cl_object cl_eql(cl_object x, cl_object y)`
- equal
- `cl_object cl_equal(cl_object x, cl_object y)`
- equalp
- `cl_object cl_equalp(cl_object x, cl_object y)`
- fboundp
- `cl_object cl_fboundp(cl_object function_name)`
- fdefinition
- `cl_object cl_fdefinition(cl_object function_name)`
- fmakunbound
- `cl_object cl_fmakunbound(cl_object function_name)`
- funcall
- `cl_object cl_funcall(cl_narg narg, cl_object function, ...)`
- function-lambda-expression
- `cl_object cl_function_lambda_expression(cl_object function)`
- functionp
- `cl_object cl_functionp(cl_object object)`
- get-setf-expansion
- `cl_object cl_get_setf_expansion(cl_narg narg, cl_object place, ...)`
- identity
- `cl_object cl_identity(cl_object x)`
- LET, LET*
- `cl_object ecl_bds_bind(cl_env_ptr env, cl_object symbol, cl_object value)`
- lambda-parameters-limit
- `ECL_LAMBDA_PARAMETERS_LIMIT`
- multiple-values-limit
- `ECL_MULTIPLE_VALUES_LIMIT`
- not
- `cl_object cl_not(cl_object object)`
- notevery
- `cl_object cl_notevery(cl_narg narg, cl_object predicate, ...)`
- notany
- `cl_object cl_notany(cl_narg narg, cl_object predicate, ...)`
- set
- `cl_object cl_set(cl_object symbol, cl_object value)`
- setq
- `cl_object ecl_setq(cl_env_ptr env, cl_object symbol, cl_object value)`
- symbol-value
- `cl_object ecl_symbol_value(cl_env_ptr env, cl_object symbol)`
- some
- `cl_object cl_some(cl_narg narg, cl_object predicate, ...)`
- values-list
- `cl_object cl_values_list(cl_object list)`

##### Description