Lichen

templates/types.h

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