Lichen

Annotated templates/types.h

866:6538a119a67f
2019-01-24 Paul Boddie Added missing support for the else clause in for loops.
paul@353 1
/* Runtime types.
paul@353 2
paul@523 3
Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
paul@353 4
paul@353 5
This program is free software; you can redistribute it and/or modify it under
paul@353 6
the terms of the GNU General Public License as published by the Free Software
paul@353 7
Foundation; either version 3 of the License, or (at your option) any later
paul@353 8
version.
paul@353 9
paul@353 10
This program is distributed in the hope that it will be useful, but WITHOUT
paul@353 11
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@353 12
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@353 13
details.
paul@353 14
paul@353 15
You should have received a copy of the GNU General Public License along with
paul@353 16
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@353 17
*/
paul@126 18
paul@126 19
#ifndef __TYPES_H__
paul@126 20
#define __TYPES_H__
paul@126 21
paul@566 22
/* Define code and position types, populated by enum values defined for each
paul@566 23
   program specifically. */
paul@566 24
paul@578 25
#include <stdint.h>
paul@578 26
paul@649 27
/* Include the special instance position value. The pos member of __obj refers
paul@649 28
   to the special type attribute for classes, indicating which position holds
paul@649 29
   the attribute describing the class type. For instances, it is set to the same
paul@649 30
   attribute position as __class__ and is defined in the following file. */
paul@649 31
paul@649 32
#include "instancepos.h"
paul@649 33
paul@578 34
typedef uint16_t __code;
paul@578 35
typedef uint16_t __pos;
paul@578 36
typedef uint16_t __pcode;
paul@578 37
typedef uint16_t __ppos;
paul@566 38
paul@126 39
/* Attribute tables are lists of codes confirming the presence of attributes. */
paul@126 40
paul@126 41
typedef struct __table
paul@126 42
{
paul@566 43
    const __pos size;
paul@566 44
    const __code attrs[];
paul@126 45
} __table;
paul@126 46
paul@126 47
/* Parameter tables are lists of codes confirming the presence of parameters, as
paul@126 48
   well as the positions of those parameters in the list for a given function.
paul@126 49
*/
paul@126 50
paul@126 51
typedef struct __param
paul@126 52
{
paul@566 53
    __code code;
paul@566 54
    __pos pos;
paul@126 55
} __param;
paul@126 56
paul@126 57
typedef struct __ptable
paul@126 58
{
paul@579 59
    const __ppos min, max, size;
paul@126 60
    const __param params[];
paul@126 61
} __ptable;
paul@126 62
paul@577 63
/* Attributes are values referring to objects or encoding other information.
paul@126 64
   Objects are collections of attributes.
paul@126 65
   Object references are references to tables and collections of attributes.
paul@126 66
   Attribute references are references to single attributes. */
paul@126 67
paul@126 68
typedef struct __obj __obj;
paul@159 69
typedef struct __fragment __fragment;
paul@577 70
typedef union __attr __attr;
paul@577 71
typedef __obj * __ref;
paul@126 72
paul@577 73
typedef union __attr
paul@126 74
{
paul@758 75
    /* General attribute members. */
paul@758 76
paul@579 77
    __ref value;                /* attribute value */
paul@758 78
    int intvalue;               /* integer value data ((integer << 1) | 1) */
paul@758 79
paul@758 80
    /* Special case attribute members. */
paul@758 81
paul@579 82
    const __ptable * ptable;    /* parameter table */
paul@577 83
    struct {
paul@579 84
        __pcode code;           /* parameter table code for key */
paul@579 85
        __ppos pos;             /* parameter table position for key */
paul@577 86
    };
paul@580 87
    __attr (*fn)();             /* callable details */
paul@579 88
    float floatvalue;          	/* floating point value */
paul@579 89
    char * strvalue;            /* string value */
paul@579 90
    __fragment * seqvalue;      /* sequence data */
paul@579 91
    void * datavalue;           /* object-specific data */
paul@126 92
} __attr;
paul@126 93
paul@126 94
typedef struct __obj
paul@126 95
{
paul@126 96
    const __table * table;      /* attribute table */
paul@566 97
    __ppos pos;                 /* position of attribute indicating class */
paul@126 98
    __attr attrs[];             /* attributes */
paul@126 99
} __obj;
paul@126 100
paul@577 101
#define __INSTANCE_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + sizeof(__table *) + sizeof(__ppos))
paul@126 102
paul@280 103
/* Fragments are simple collections of attributes employed by sequence types.
paul@280 104
   They provide the basis of lists and tuples. */
paul@159 105
paul@159 106
typedef struct __fragment
paul@159 107
{
paul@206 108
    unsigned int size, capacity;
paul@159 109
    __attr attrs[];
paul@159 110
} __fragment;
paul@159 111
paul@577 112
#define __FRAGMENT_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + 2 * sizeof(unsigned int))
paul@206 113
paul@758 114
/* Attribute interpretation. */
paul@758 115
paul@758 116
#define __INTEGER(ATTR) ((ATTR).intvalue % 2)
paul@758 117
paul@626 118
/* Attribute value setting. */
paul@143 119
paul@626 120
#define __ATTRVALUE(VALUE) ((__attr) {.value=VALUE})
paul@626 121
#define __NULL __ATTRVALUE(0)
paul@758 122
#define __SETNULL(ATTR) ((ATTR).value = 0)
paul@758 123
paul@758 124
/* Attribute as instance setting. */
paul@758 125
paul@758 126
#define __INTVALUE(VALUE) ((__attr) {.intvalue=((VALUE) << 1) | 1})
paul@758 127
#define __TOINT(ATTR) ((ATTR).intvalue >> 1)
paul@758 128
#define __MAXINT ((1 << ((sizeof(__attr) * 8) - 2)) - 1)
paul@758 129
#define __MININT (-(1 << ((sizeof(__attr) * 8) - 2)))
paul@126 130
paul@623 131
/* Argument lists. */
paul@126 132
paul@126 133
#define __ARGS(...) ((__attr[]) {__VA_ARGS__})
paul@126 134
#define __KWARGS(...) ((__param[]) {__VA_ARGS__})
paul@126 135
paul@623 136
/* Attribute codes and positions for attribute names. */
paul@623 137
paul@623 138
#define __ATTRCODE(ATTRNAME) __code_##ATTRNAME
paul@623 139
#define __ATTRPOS(ATTRNAME) __pos_##ATTRNAME
paul@623 140
#define __PARAMCODE(PARAMNAME) __pcode_##PARAMNAME
paul@623 141
#define __PARAMPOS(PARAMNAME) __ppos_##PARAMNAME
paul@623 142
paul@126 143
#endif /* __TYPES_H__ */