Lichen

templates/types.h

660:fc5943513f3a
2017-03-05 Paul Boddie Removed superfluous __TEST macro.
     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 /* Include the special instance position value. The pos member of __obj refers    28    to the special type attribute for classes, indicating which position holds    29    the attribute describing the class type. For instances, it is set to the same    30    attribute position as __class__ and is defined in the following file. */    31     32 #include "instancepos.h"    33     34 typedef uint16_t __code;    35 typedef uint16_t __pos;    36 typedef uint16_t __pcode;    37 typedef uint16_t __ppos;    38     39 /* Attribute tables are lists of codes confirming the presence of attributes. */    40     41 typedef struct __table    42 {    43     const __pos size;    44     const __code attrs[];    45 } __table;    46     47 /* Parameter tables are lists of codes confirming the presence of parameters, as    48    well as the positions of those parameters in the list for a given function.    49 */    50     51 typedef struct __param    52 {    53     __code code;    54     __pos pos;    55 } __param;    56     57 typedef struct __ptable    58 {    59     const __ppos min, max, size;    60     const __param params[];    61 } __ptable;    62     63 /* Attributes are values referring to objects or encoding other information.    64    Objects are collections of attributes.    65    Object references are references to tables and collections of attributes.    66    Attribute references are references to single attributes. */    67     68 typedef struct __obj __obj;    69 typedef struct __fragment __fragment;    70 typedef union __attr __attr;    71 typedef __obj * __ref;    72     73 typedef union __attr    74 {    75     __ref value;                /* attribute value */    76     const __ptable * ptable;    /* parameter table */    77     struct {    78         __pcode code;           /* parameter table code for key */    79         __ppos pos;             /* parameter table position for key */    80     };    81     __attr (*fn)();             /* callable details */    82     int intvalue;               /* integer value */    83     float floatvalue;          	/* floating point value */    84     char * strvalue;            /* string value */    85     __fragment * seqvalue;      /* sequence data */    86     void * datavalue;           /* object-specific data */    87 } __attr;    88     89 typedef struct __obj    90 {    91     const __table * table;      /* attribute table */    92     __ppos pos;                 /* position of attribute indicating class */    93     __attr attrs[];             /* attributes */    94 } __obj;    95     96 #define __INSTANCE_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + sizeof(__table *) + sizeof(__ppos))    97     98 /* Fragments are simple collections of attributes employed by sequence types.    99    They provide the basis of lists and tuples. */   100    101 typedef struct __fragment   102 {   103     unsigned int size, capacity;   104     __attr attrs[];   105 } __fragment;   106    107 #define __FRAGMENT_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + 2 * sizeof(unsigned int))   108    109 /* Attribute value setting. */   110    111 #define __ATTRVALUE(VALUE) ((__attr) {.value=VALUE})   112 #define __NULL __ATTRVALUE(0)   113    114 /* Argument lists. */   115    116 #define __ARGS(...) ((__attr[]) {__VA_ARGS__})   117 #define __KWARGS(...) ((__param[]) {__VA_ARGS__})   118    119 /* Attribute codes and positions for attribute names. */   120    121 #define __ATTRCODE(ATTRNAME) __code_##ATTRNAME   122 #define __ATTRPOS(ATTRNAME) __pos_##ATTRNAME   123 #define __PARAMCODE(PARAMNAME) __pcode_##PARAMNAME   124 #define __PARAMPOS(PARAMNAME) __ppos_##PARAMNAME   125    126 #endif /* __TYPES_H__ */