Lichen

templates/types.h

611:d1d907801d42
2017-02-23 Paul Boddie Replaced list comprehension usage. method-wrapper-for-context
     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 /* Special null values. */   109    110 #define __NULL ((__attr) {.value=0})   111    112 /* Function pointer type. */   113    114 typedef __attr (*__func)();   115    116 /* Convenience macros. */   117    118 #define __ARGS(...) ((__attr[]) {__VA_ARGS__})   119 #define __KWARGS(...) ((__param[]) {__VA_ARGS__})   120    121 #endif /* __TYPES_H__ */