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