view src/types.h @ 129:e4c55f83eef0

types: Update according to previous two commits
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 01 Sep 2019 19:22:31 +0200
parents 63650268d006
children 88fec15abdbc
line wrap: on
line source

#ifndef src_types_h
#define src_types_h

/**
 * @file types.h
 * @brief Collection of inter-dependent types used by the core implementation.
 * @addtogroup language
 * @{
 */

#include "atom.h"
#include "base/str.h"
#include "base/vec.h"

/// @brief Numeric reference to a stored value or function (see value.h). Must
/// have a MSB that is 0xff.
typedef uint64_t yvalue_id_t;

/**
 * @brief Type of a reference: Symbolic or numeric.
 *
 * Symbolic references can be resolved to a numeric one, creating a "value slot"
 * (see `value.h`).
 */
typedef enum {
    YREF_UNDEF = 0,
    YREF_ID = 1,
    YREF_SYM = 2,
    YREF_STACK = 3,
} YREF_TYPE;

/**
 * @brief A reference to a stored function or value. To be used as pointer.
 *
 * yref_t values should not be copied around freely, as the string in the
 * `ref.sym` field may only be freed once.
 */
typedef struct {
    /// The reference.
    union {
        /// A symbol, i.e. name, of a variable or function.
        ystr_t sym;
        /// An ID pointing to a slot in the values table (see value.c)
        yvalue_id_t id;
        /// An index relative to the top of the call_stack, describing a
        /// function argument.
        uint64_t cix;
    } ref;
} yref_t;

/**
 * @brief A yexpr_t can be a builtin (which looks like an ID), described by the
 * `builtin` field.
 *
 * NOTE: Don't forget updating YBUILTIN_ENUM_STR when adding types here.
 */
typedef enum {
    // Purely internal
    YBUILTIN_FAIL = -1,

    YBUILTIN_UNDEF = 0,
    YBUILTIN_FOR = 1,
    YBUILTIN_LET = 2,
    YBUILTIN_DEFN = 3,

    YBUILTIN_PLUS = 4,
    YBUILTIN_MINUS = 5,
    YBUILTIN_MULT = 6,
    YBUILTIN_DIV = 7,

    YBUILTIN_IF = 8,
    YBUILTIN_SEQ = 9,

    YBUILTIN_EQ = 10,
    YBUILTIN_LT = 11,

    YBUILTIN_CAR = 12,
    YBUILTIN_CDR = 13,
    YBUILTIN_PUSH = 14,

    YBUILTIN_RAISE = 15,
    YBUILTIN_RECOVER = 16,
} YBUILTIN_TYPE;

/**
 * @brief Type of an yexpr_t value.
 *
 * Each enum value corresponds to a union field being set.
 */
typedef enum {
    /// Undefined type (freshly initialized)
    YEXPR_UNDEF = 0,
    /// A signed integer. (also boolean: 0 is false, everything else is true)
    YEXPR_NUMBER = 1,
    /// A string.
    YEXPR_STRING = 2,
    /// An atom (a word or words that stand for themselves)
    YEXPR_ATOM = 3,
    /// A list of expressions.
    YEXPR_LIST = 4,
    /// A reference to a value or function.
    YEXPR_REF = 5,
    /// A reference to a built-in function.
    YEXPR_BUILTIN = 6,
    /// An exception described by the `str` field - when returned from a
    /// function, the interpreter "bubbles up" the call stack.
    YEXPR_EXCEPTION = 7,
} YEXPR_TYPE;

/**
 * A value. Can be either a fundamental value (number, string, atom) or a list
 * of values.
 */
struct yexpr_t {
    /// The value of the expression.
    union {
        /// A number.
        long long n;
        /// A literal string.
        ystr_t str;
        /// An atom.
        yatom_t atom;
        /// A list of yexpr_t.
        yvec_t list;
        /// A named reference (to a value or function described by yvalue_t).
        yref_t ref;
        /// A builtin such as `for`, `let`, ...
        YBUILTIN_TYPE builtin;
    } value;
    /// Type of this expression.
    YEXPR_TYPE typ;
};

typedef struct yexpr_t yexpr_t;

/**
 * @}
 */
#endif