Lichen

templates/types.h

934:2989aab1b4f7
13 months ago Paul Boddie Renamed the utf8string class to unicode, eliminating the unicode function. This means that the simple case of merely returning an object if it is already a Unicode object no longer occurs when using the unicode callable, but such behaviour might be better supported with more general customised instantiation functionality.
     1 /* Runtime types.     2      3 Copyright (C) 2015, 2016, 2017, 2018, 2019 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     /* General attribute members. */    76     77     __ref value;                /* attribute value */    78     int intvalue;               /* integer value data (shifted value, tagged) */    79     80     /* Special case attribute members. */    81     82     const __ptable * ptable;    /* parameter table */    83     struct {    84         __pcode code;           /* parameter table code for key */    85         __ppos pos;             /* parameter table position for key */    86     };    87     __attr (*fn)();             /* callable details */    88     float floatvalue;          	/* floating point value */    89     char * strvalue;            /* string value */    90     __fragment * seqvalue;      /* sequence data */    91     void * datavalue;           /* object-specific data */    92 } __attr;    93     94 typedef struct __obj    95 {    96     const __table * table;      /* attribute table */    97     __ppos pos;                 /* position of attribute indicating class */    98     __attr attrs[];             /* attributes */    99 } __obj;   100    101 #define __INSTANCE_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + sizeof(__table *) + sizeof(__ppos))   102    103 /* Fragments are simple collections of attributes employed by sequence types.   104    They provide the basis of lists and tuples. */   105    106 typedef struct __fragment   107 {   108     unsigned int size, capacity;   109     __attr attrs[];   110 } __fragment;   111    112 #define __FRAGMENT_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + 2 * sizeof(unsigned int))   113    114 /* Attribute interpretation. */   115    116 #define __NUM_TAG_BITS      1   117 #define __TAG_INT           0b1   118 #define __INTEGER(ATTR)     ((ATTR).intvalue & __TAG_INT)   119    120 /* Attribute value setting. */   121    122 #define __ATTRVALUE(VALUE)  ((__attr) {.value=(__ref) VALUE})   123 #define __NULL              __ATTRVALUE(0)   124 #define __SETNULL(ATTR)     ((ATTR).value = 0)   125    126 /* Attribute as instance setting. */   127    128 #define __INTVALUE(VALUE)   ((__attr) {.intvalue=((VALUE) << __NUM_TAG_BITS) | __TAG_INT})   129 #define __TOINT(ATTR)       ((ATTR).intvalue >> __NUM_TAG_BITS)   130 #define __MAXINT            ((1 << ((sizeof(int) * 8) - 1 - __NUM_TAG_BITS)) - 1)   131 #define __MININT            (-(1 << ((sizeof(int) * 8) - 1 - __NUM_TAG_BITS)))   132    133 /* Argument lists. */   134    135 #define __ARGS(...)         ((__attr[]) {__VA_ARGS__})   136 #define __KWARGS(...)       ((__param[]) {__VA_ARGS__})   137    138 /* Attribute codes and positions for attribute names. */   139    140 #define __ATTRCODE(ATTRNAME)    __code_##ATTRNAME   141 #define __ATTRPOS(ATTRNAME)     __pos_##ATTRNAME   142 #define __PARAMCODE(PARAMNAME)  __pcode_##PARAMNAME   143 #define __PARAMPOS(PARAMNAME)   __ppos_##PARAMNAME   144    145 #endif /* __TYPES_H__ */