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