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 typedef unsigned short __code; 26 typedef unsigned short __pos; 27 typedef unsigned short __pcode; 28 typedef unsigned short __ppos; 29 30 /* Attribute tables are lists of codes confirming the presence of attributes. */ 31 32 typedef struct __table 33 { 34 const __pos size; 35 const __code attrs[]; 36 } __table; 37 38 /* Parameter tables are lists of codes confirming the presence of parameters, as 39 well as the positions of those parameters in the list for a given function. 40 */ 41 42 typedef struct __param 43 { 44 __code code; 45 __pos pos; 46 } __param; 47 48 typedef struct __ptable 49 { 50 const __ppos size; 51 const __param params[]; 52 } __ptable; 53 54 /* Attributes are context and value pairs. 55 Objects are collections of attributes. 56 Object references are references to tables and collections of attributes. 57 Attribute references are references to single attributes. */ 58 59 typedef struct __obj __obj; 60 typedef struct __fragment __fragment; 61 62 typedef struct __attr 63 { 64 /* One of... */ 65 union 66 { 67 struct { 68 __obj * context; /* attribute context */ 69 __obj * value; /* attribute value */ 70 }; 71 struct { 72 __ppos min; /* minimum number of parameters */ 73 const __ptable * ptable; /* parameter table */ 74 }; 75 struct { 76 __pcode code; /* parameter table code for key */ 77 __ppos pos; /* parameter table position for key */ 78 }; 79 struct { 80 struct __attr (*inv)(); /* unbound callable details */ 81 struct __attr (*fn)(); /* callable details */ 82 }; 83 union 84 { 85 int intvalue; /* integer value */ 86 double floatvalue; /* floating point value */ 87 char * strvalue; /* string value */ 88 __fragment * seqvalue; /* sequence data */ 89 void * datavalue; /* object-specific data */ 90 }; 91 }; 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 typedef __obj * __ref; 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 /* Special instance position value. The pos member of __obj refers to the 115 special type attribute for classes, indicating which position holds the 116 attribute describing the class type. For instances, it is set to zero. */ 117 118 #define __INSTANCEPOS 0 119 120 /* Special null values. */ 121 122 #define __NULL ((__attr) {{.context=0, .value=0}}) 123 124 /* Function pointer type. */ 125 126 typedef __attr (*__func)(); 127 128 /* Convenience macros. */ 129 130 #define __ARGS(...) ((__attr[]) {__VA_ARGS__}) 131 #define __KWARGS(...) ((__param[]) {__VA_ARGS__}) 132 133 #endif /* __TYPES_H__ */