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@378 | 22 | #include <stddef.h> /* size_t */ |
paul@378 | 23 | |
paul@126 | 24 | /* Attribute tables are lists of codes confirming the presence of attributes. */ |
paul@126 | 25 | |
paul@126 | 26 | typedef struct __table |
paul@126 | 27 | { |
paul@126 | 28 | const unsigned int size, attrs[]; |
paul@126 | 29 | } __table; |
paul@126 | 30 | |
paul@126 | 31 | /* Parameter tables are lists of codes confirming the presence of parameters, as |
paul@126 | 32 | well as the positions of those parameters in the list for a given function. |
paul@126 | 33 | */ |
paul@126 | 34 | |
paul@126 | 35 | typedef struct __param |
paul@126 | 36 | { |
paul@126 | 37 | unsigned short code, pos; |
paul@126 | 38 | } __param; |
paul@126 | 39 | |
paul@126 | 40 | typedef struct __ptable |
paul@126 | 41 | { |
paul@126 | 42 | const unsigned int size; |
paul@126 | 43 | const __param params[]; |
paul@126 | 44 | } __ptable; |
paul@126 | 45 | |
paul@126 | 46 | /* Attributes are context and value pairs. |
paul@126 | 47 | Objects are collections of attributes. |
paul@126 | 48 | Object references are references to tables and collections of attributes. |
paul@126 | 49 | Attribute references are references to single attributes. */ |
paul@126 | 50 | |
paul@126 | 51 | typedef struct __obj __obj; |
paul@159 | 52 | typedef struct __fragment __fragment; |
paul@126 | 53 | |
paul@126 | 54 | typedef struct __attr |
paul@126 | 55 | { |
paul@126 | 56 | /* One of... */ |
paul@126 | 57 | union |
paul@126 | 58 | { |
paul@126 | 59 | __obj * context; /* attribute context */ |
paul@126 | 60 | unsigned int min; /* minimum number of parameters */ |
paul@360 | 61 | unsigned int code; /* parameter table code for key */ |
paul@523 | 62 | struct __attr (*inv)(); /* unbound callable details */ |
paul@378 | 63 | |
paul@378 | 64 | size_t size; /* size of value */ |
paul@126 | 65 | }; |
paul@126 | 66 | |
paul@126 | 67 | /* One of... */ |
paul@126 | 68 | union |
paul@126 | 69 | { |
paul@126 | 70 | __obj * value; /* attribute value */ |
paul@126 | 71 | const __ptable * ptable;/* parameter table */ |
paul@360 | 72 | unsigned int pos; /* parameter table position for key */ |
paul@126 | 73 | struct __attr (*fn)(); /* callable details */ |
paul@136 | 74 | |
paul@159 | 75 | int intvalue; /* integer value */ |
paul@159 | 76 | double floatvalue; /* floating point value */ |
paul@159 | 77 | char * strvalue; /* string value */ |
paul@280 | 78 | __fragment * seqvalue; /* sequence data */ |
paul@328 | 79 | void * datavalue; /* object-specific data */ |
paul@126 | 80 | }; |
paul@126 | 81 | } __attr; |
paul@126 | 82 | |
paul@126 | 83 | typedef struct __obj |
paul@126 | 84 | { |
paul@126 | 85 | const __table * table; /* attribute table */ |
paul@126 | 86 | unsigned int pos; /* position of attribute indicating class */ |
paul@126 | 87 | __attr attrs[]; /* attributes */ |
paul@126 | 88 | } __obj; |
paul@126 | 89 | |
paul@126 | 90 | typedef __obj * __ref; |
paul@126 | 91 | |
paul@280 | 92 | /* Fragments are simple collections of attributes employed by sequence types. |
paul@280 | 93 | They provide the basis of lists and tuples. */ |
paul@159 | 94 | |
paul@159 | 95 | typedef struct __fragment |
paul@159 | 96 | { |
paul@206 | 97 | unsigned int size, capacity; |
paul@159 | 98 | __attr attrs[]; |
paul@159 | 99 | } __fragment; |
paul@159 | 100 | |
paul@206 | 101 | #define __FRAGMENT_SIZE(NUMBER) (NUMBER * sizeof(__attr) + 2 * sizeof(unsigned int)) |
paul@206 | 102 | |
paul@126 | 103 | /* Special instance position value. The pos member of __obj refers to the |
paul@143 | 104 | special type attribute for classes, indicating which position holds the |
paul@143 | 105 | attribute describing the class type. For instances, it is set to zero. */ |
paul@126 | 106 | |
paul@126 | 107 | #define __INSTANCEPOS 0 |
paul@126 | 108 | |
paul@477 | 109 | /* Special null values. */ |
paul@143 | 110 | |
paul@477 | 111 | #define __NULL ((__attr) {{0}, {0}}) |
paul@143 | 112 | |
paul@126 | 113 | /* Function pointer type. */ |
paul@126 | 114 | |
paul@126 | 115 | typedef __attr (*__func)(); |
paul@126 | 116 | |
paul@126 | 117 | /* Convenience macros. */ |
paul@126 | 118 | |
paul@126 | 119 | #define __ARGS(...) ((__attr[]) {__VA_ARGS__}) |
paul@126 | 120 | #define __KWARGS(...) ((__param[]) {__VA_ARGS__}) |
paul@126 | 121 | |
paul@126 | 122 | #endif /* __TYPES_H__ */ |