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