Builtin#

Builtin functions and builtin syntaxes are represented with lsp_builtin_entry_t structures. Each entry is defined with unique name and function pointer:

typedef lsp_status_t (*lsp_builtin_cb_t)(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args);

Associated function pointer is called during function/syntax application (described in following chapters).

Source code#

builtin.h#

#ifndef LISP16_BUILTIN_H
#define LISP16_BUILTIN_H

#include "env.h"


typedef lsp_status_t (*lsp_builtin_cb_t)(lsp_env_t *e, lsp_addr_t ctx,
                                         lsp_addr_t args);

typedef struct {
    char *name;
    lsp_builtin_cb_t cb;
} lsp_builtin_entry_t;


lsp_status_t lsp_builtin_get_args_1(lsp_mem_t *m, lsp_addr_t args,
                                    lsp_addr_t *arg1);
lsp_status_t lsp_builtin_get_args_2(lsp_mem_t *m, lsp_addr_t args,
                                    lsp_addr_t *arg1, lsp_addr_t *arg2);
lsp_status_t lsp_builtin_get_args_3(lsp_mem_t *m, lsp_addr_t args,
                                    lsp_addr_t *arg1, lsp_addr_t *arg2,
                                    lsp_addr_t *arg3);

#endif

builtin.c#

#include "builtin.h"


lsp_status_t lsp_builtin_get_args_1(lsp_mem_t *m, lsp_addr_t args,
                                    lsp_addr_t *arg1) {
    if (args == m->nil)
        return LSP_ERR_ARG_COUNT;

    *arg1 = lsp_mem_get_pair_first(m, args);

    args = lsp_mem_get_pair_second(m, args);
    if (args != m->nil)
        return LSP_ERR_ARG_COUNT;

    return LSP_SUCCESS;
}


lsp_status_t lsp_builtin_get_args_2(lsp_mem_t *m, lsp_addr_t args,
                                    lsp_addr_t *arg1, lsp_addr_t *arg2) {
    if (args == m->nil)
        return LSP_ERR_ARG_COUNT;

    *arg1 = lsp_mem_get_pair_first(m, args);

    args = lsp_mem_get_pair_second(m, args);
    if (args == m->nil)
        return LSP_ERR_ARG_COUNT;

    *arg2 = lsp_mem_get_pair_first(m, args);

    args = lsp_mem_get_pair_second(m, args);
    if (args != m->nil)
        return LSP_ERR_ARG_COUNT;

    return LSP_SUCCESS;
}


lsp_status_t lsp_builtin_get_args_3(lsp_mem_t *m, lsp_addr_t args,
                                    lsp_addr_t *arg1, lsp_addr_t *arg2,
                                    lsp_addr_t *arg3) {
    if (args == m->nil)
        return LSP_ERR_ARG_COUNT;

    *arg1 = lsp_mem_get_pair_first(m, args);

    args = lsp_mem_get_pair_second(m, args);
    if (args == m->nil)
        return LSP_ERR_ARG_COUNT;

    *arg2 = lsp_mem_get_pair_first(m, args);

    args = lsp_mem_get_pair_second(m, args);
    if (args == m->nil)
        return LSP_ERR_ARG_COUNT;

    *arg3 = lsp_mem_get_pair_first(m, args);

    args = lsp_mem_get_pair_second(m, args);
    if (args != m->nil)
        return LSP_ERR_ARG_COUNT;

    return LSP_SUCCESS;
}