ndtypes 0.2.0dev4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +50 -0
  3. data/Gemfile +2 -0
  4. data/History.md +0 -0
  5. data/README.md +19 -0
  6. data/Rakefile +125 -0
  7. data/ext/ruby_ndtypes/extconf.rb +55 -0
  8. data/ext/ruby_ndtypes/gc_guard.c +36 -0
  9. data/ext/ruby_ndtypes/gc_guard.h +12 -0
  10. data/ext/ruby_ndtypes/ndtypes/AUTHORS.txt +5 -0
  11. data/ext/ruby_ndtypes/ndtypes/INSTALL.txt +101 -0
  12. data/ext/ruby_ndtypes/ndtypes/LICENSE.txt +29 -0
  13. data/ext/ruby_ndtypes/ndtypes/MANIFEST.in +3 -0
  14. data/ext/ruby_ndtypes/ndtypes/Makefile.in +87 -0
  15. data/ext/ruby_ndtypes/ndtypes/README.rst +47 -0
  16. data/ext/ruby_ndtypes/ndtypes/config.guess +1530 -0
  17. data/ext/ruby_ndtypes/ndtypes/config.h.in +67 -0
  18. data/ext/ruby_ndtypes/ndtypes/config.sub +1782 -0
  19. data/ext/ruby_ndtypes/ndtypes/configure +5260 -0
  20. data/ext/ruby_ndtypes/ndtypes/configure.ac +161 -0
  21. data/ext/ruby_ndtypes/ndtypes/doc/Makefile +14 -0
  22. data/ext/ruby_ndtypes/ndtypes/doc/_static/copybutton.js +66 -0
  23. data/ext/ruby_ndtypes/ndtypes/doc/conf.py +26 -0
  24. data/ext/ruby_ndtypes/ndtypes/doc/grammar/grammar.rst +27 -0
  25. data/ext/ruby_ndtypes/ndtypes/doc/index.rst +56 -0
  26. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/context.rst +131 -0
  27. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/encodings.rst +68 -0
  28. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/fields-values.rst +175 -0
  29. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/functions.rst +72 -0
  30. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/index.rst +43 -0
  31. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/init.rst +48 -0
  32. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/io.rst +100 -0
  33. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/memory.rst +124 -0
  34. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/predicates.rst +110 -0
  35. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/typedef.rst +31 -0
  36. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/types.rst +594 -0
  37. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/util.rst +166 -0
  38. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/buffer-protocol.rst +27 -0
  39. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/index.rst +21 -0
  40. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/pattern-matching.rst +330 -0
  41. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/quickstart.rst +144 -0
  42. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/types.rst +544 -0
  43. data/ext/ruby_ndtypes/ndtypes/doc/releases/index.rst +35 -0
  44. data/ext/ruby_ndtypes/ndtypes/install-sh +527 -0
  45. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.in +271 -0
  46. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.vc +269 -0
  47. data/ext/ruby_ndtypes/ndtypes/libndtypes/alloc.c +230 -0
  48. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.c +268 -0
  49. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.h +109 -0
  50. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.in +73 -0
  51. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.vc +70 -0
  52. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/README.txt +16 -0
  53. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.c +2179 -0
  54. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.h +134 -0
  55. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.y +428 -0
  56. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.c +2543 -0
  57. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.h +735 -0
  58. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.l +176 -0
  59. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/export.c +543 -0
  60. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/import.c +110 -0
  61. data/ext/ruby_ndtypes/ndtypes/libndtypes/context.c +228 -0
  62. data/ext/ruby_ndtypes/ndtypes/libndtypes/copy.c +634 -0
  63. data/ext/ruby_ndtypes/ndtypes/libndtypes/encodings.c +116 -0
  64. data/ext/ruby_ndtypes/ndtypes/libndtypes/equal.c +288 -0
  65. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.c +3067 -0
  66. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.h +180 -0
  67. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.y +417 -0
  68. data/ext/ruby_ndtypes/ndtypes/libndtypes/io.c +1658 -0
  69. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.c +2773 -0
  70. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.h +734 -0
  71. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.l +222 -0
  72. data/ext/ruby_ndtypes/ndtypes/libndtypes/match.c +1132 -0
  73. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.c +2323 -0
  74. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.h.in +893 -0
  75. data/ext/ruby_ndtypes/ndtypes/libndtypes/overflow.h +161 -0
  76. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.c +473 -0
  77. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.h +92 -0
  78. data/ext/ruby_ndtypes/ndtypes/libndtypes/parser.c +246 -0
  79. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.c +269 -0
  80. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.h +197 -0
  81. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.in +48 -0
  82. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.vc +46 -0
  83. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/deserialize.c +1007 -0
  84. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/serialize.c +442 -0
  85. data/ext/ruby_ndtypes/ndtypes/libndtypes/slice.h +42 -0
  86. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.c +238 -0
  87. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.h +50 -0
  88. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.c +371 -0
  89. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.h +100 -0
  90. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.in +55 -0
  91. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.vc +45 -0
  92. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.c +82 -0
  93. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.h +49 -0
  94. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/runtest.c +1657 -0
  95. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test.h +85 -0
  96. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_array.c +115 -0
  97. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_buffer.c +137 -0
  98. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_indent.c +201 -0
  99. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_match.c +2397 -0
  100. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_numba.c +57 -0
  101. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse.c +349 -0
  102. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_error.c +27839 -0
  103. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_roundtrip.c +350 -0
  104. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_record.c +231 -0
  105. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typecheck.c +375 -0
  106. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typedef.c +65 -0
  107. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/valgrind.supp +30 -0
  108. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/bench.c +79 -0
  109. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/indent.c +94 -0
  110. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/print_ast.c +96 -0
  111. data/ext/ruby_ndtypes/ndtypes/libndtypes/util.c +474 -0
  112. data/ext/ruby_ndtypes/ndtypes/libndtypes/values.c +228 -0
  113. data/ext/ruby_ndtypes/ndtypes/python/bench.py +49 -0
  114. data/ext/ruby_ndtypes/ndtypes/python/ndt_randtype.py +409 -0
  115. data/ext/ruby_ndtypes/ndtypes/python/ndt_support.py +14 -0
  116. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/__init__.py +70 -0
  117. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/_ndtypes.c +1332 -0
  118. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/docstrings.h +319 -0
  119. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/pyndtypes.h +154 -0
  120. data/ext/ruby_ndtypes/ndtypes/python/test_ndtypes.py +1977 -0
  121. data/ext/ruby_ndtypes/ndtypes/setup.py +288 -0
  122. data/ext/ruby_ndtypes/ndtypes/vcbuild/INSTALL.txt +41 -0
  123. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest32.bat +15 -0
  124. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest64.bat +13 -0
  125. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild32.bat +38 -0
  126. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild64.bat +38 -0
  127. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcclean.bat +13 -0
  128. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcdistclean.bat +14 -0
  129. data/ext/ruby_ndtypes/ruby_ndtypes.c +1003 -0
  130. data/ext/ruby_ndtypes/ruby_ndtypes.h +37 -0
  131. data/ext/ruby_ndtypes/ruby_ndtypes_internal.h +28 -0
  132. data/lib/ndtypes.rb +45 -0
  133. data/lib/ndtypes/errors.rb +2 -0
  134. data/lib/ndtypes/version.rb +6 -0
  135. data/ndtypes.gemspec +47 -0
  136. data/spec/gc_table_spec.rb +10 -0
  137. data/spec/ndtypes_spec.rb +289 -0
  138. data/spec/spec_helper.rb +241 -0
  139. metadata +242 -0
@@ -0,0 +1,161 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #include <stdint.h>
35
+
36
+
37
+ /*****************************************************************************/
38
+ /* Arithmetic with overflow checking */
39
+ /*****************************************************************************/
40
+
41
+ #if defined(__GNUC__) && __GNUC__ >= 5 && !defined(__INTEL_COMPILER)
42
+ static inline int64_t
43
+ ADDi64(int64_t a, int64_t b, bool *overflow)
44
+ {
45
+ int64_t c;
46
+ *overflow |= __builtin_add_overflow(a, b, &c);
47
+ return c;
48
+ }
49
+
50
+ static inline int64_t
51
+ SUBi64(int64_t a, int64_t b, bool *overflow)
52
+ {
53
+ int64_t c;
54
+ *overflow |= __builtin_sub_overflow(a, b, &c);
55
+ return c;
56
+ }
57
+
58
+ static inline int64_t
59
+ MULi64(int64_t a, int64_t b, bool *overflow)
60
+ {
61
+ int64_t c;
62
+ *overflow |= __builtin_mul_overflow(a, b, &c);
63
+ return c;
64
+ }
65
+
66
+ static inline int64_t
67
+ DIVi64(int64_t a, int64_t b, bool *overflow)
68
+ {
69
+ *overflow |= (b == 0 || (a == INT64_MIN && b == -1));
70
+ return *overflow ? INT64_MIN : a / b;
71
+ }
72
+
73
+ static inline size_t
74
+ MULi64_size(int64_t a, int64_t b, bool *overflow)
75
+ {
76
+ int64_t c;
77
+ *overflow |= __builtin_mul_overflow(a, b, &c);
78
+ #if SIZE_MAX < INT64_MAX
79
+ *overflow |= (c > INT32_MAX);
80
+ #endif
81
+ return (size_t)c;
82
+ }
83
+
84
+ static inline int64_t
85
+ ABSi64(int64_t a, bool *overflow)
86
+ {
87
+ if (a == INT64_MIN) {
88
+ *overflow = 1;
89
+ return INT64_MIN;
90
+ }
91
+ return a >= 0 ? a : -a;
92
+ }
93
+
94
+ static inline uint16_t
95
+ ADDu16(uint16_t a, uint16_t b, bool *overflow)
96
+ {
97
+ uint16_t c;
98
+ *overflow |= __builtin_add_overflow(a, b, &c);
99
+ return c;
100
+ }
101
+ #else
102
+ static inline int64_t
103
+ ADDi64(int64_t a, int64_t b, bool *overflow)
104
+ {
105
+ int64_t c = (uint64_t)a + (uint64_t)b;
106
+ *overflow |= ((a < 0 && b < 0 && c >= 0) || (a >= 0 && b >= 0 && c < 0));
107
+ return c;
108
+ }
109
+
110
+ static inline int64_t
111
+ SUBi64(int64_t a, int64_t b, bool *overflow)
112
+ {
113
+ int64_t c = (uint64_t)a - (uint64_t)b;
114
+ *overflow |= ((a < 0 && b >= 0 && c >= 0) || (a >= 0 && b < 0 && c < 0));
115
+ return c;
116
+ }
117
+
118
+ static inline int64_t
119
+ MULi64(int64_t a, int64_t b, bool *overflow)
120
+ {
121
+ int64_t c = (uint64_t)a * (uint64_t)b;
122
+ *overflow |= ((b < 0 && a == INT64_MIN) || (b != 0 && a != c / b));
123
+ return c;
124
+ }
125
+
126
+ static inline int64_t
127
+ DIVi64(int64_t a, int64_t b, bool *overflow)
128
+ {
129
+ *overflow |= (b == 0 || (a == INT64_MIN && b == -1));
130
+ return *overflow ? INT64_MIN : a / b;
131
+ }
132
+
133
+ static inline size_t
134
+ MULi64_size(int64_t a, int64_t b, bool *overflow)
135
+ {
136
+ int64_t c = (uint64_t)a * (uint64_t)b;
137
+ *overflow |= ((b < 0 && a == INT64_MIN) || (b != 0 && a != c / b));
138
+ #if SIZE_MAX < INT64_MAX
139
+ *overflow |= (c > INT32_MAX);
140
+ #endif
141
+ return (size_t)c;
142
+ }
143
+
144
+ static inline int64_t
145
+ ABSi64(int64_t a, bool *overflow)
146
+ {
147
+ if (a == INT64_MIN) {
148
+ *overflow = 1;
149
+ return INT64_MIN;
150
+ }
151
+ return a >= 0 ? a : -a;
152
+ }
153
+
154
+ static inline uint16_t
155
+ ADDu16(uint16_t a, uint16_t b, bool *overflow)
156
+ {
157
+ uint16_t c = a + b;
158
+ *overflow |= (c < a);
159
+ return c;
160
+ }
161
+ #endif /* OVERFLOW_H */
@@ -0,0 +1,473 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #include <stdlib.h>
35
+ #include <string.h>
36
+ #include <limits.h>
37
+ #include <assert.h>
38
+ #include "ndtypes.h"
39
+ #include "parsefuncs.h"
40
+ #include "seq.h"
41
+ #include "attr.h"
42
+
43
+
44
+ /*****************************************************************************/
45
+ /* Functions used in the lexer */
46
+ /*****************************************************************************/
47
+
48
+ char *
49
+ mk_stringlit(const char *lexeme, ndt_context_t *ctx)
50
+ {
51
+ size_t len;
52
+ char *s, *result;
53
+
54
+ len = strlen(lexeme);
55
+ if (len < 2) {
56
+ ndt_err_format(ctx, NDT_RuntimeError, "invalid string literal");
57
+ return NULL;
58
+ }
59
+
60
+ s = ndt_strdup(lexeme, ctx);
61
+ if (s == NULL) {
62
+ return NULL;
63
+ }
64
+
65
+ s[len-1] = '\0';
66
+ result = ndt_asprintf(ctx, "%s", s+1);
67
+
68
+ ndt_free(s);
69
+ return result;
70
+ }
71
+
72
+
73
+ /*****************************************************************************/
74
+ /* Parser helper functions */
75
+ /*****************************************************************************/
76
+
77
+ enum ndt_encoding
78
+ encoding_from_string(char *s, ndt_context_t *ctx)
79
+ {
80
+ enum ndt_encoding ret = ndt_encoding_from_string(s, ctx);
81
+ ndt_free(s);
82
+ return ret;
83
+ }
84
+
85
+ ndt_attr_t *
86
+ mk_attr(char *name, char *value, ndt_context_t *ctx)
87
+ {
88
+ ndt_attr_t *attr;
89
+
90
+ attr = ndt_alloc_size(sizeof *attr);
91
+ if (attr == NULL) {
92
+ ndt_free(name);
93
+ ndt_free(value);
94
+ return ndt_memory_error(ctx);
95
+ }
96
+
97
+ attr->tag = AttrValue;
98
+ attr->name = name;
99
+ attr->AttrValue = value;
100
+
101
+ return attr;
102
+ }
103
+
104
+ ndt_attr_t *
105
+ mk_attr_from_seq(char *name, ndt_string_seq_t *seq, ndt_context_t *ctx)
106
+ {
107
+ ndt_attr_t *attr;
108
+
109
+ attr = ndt_alloc_size(sizeof *attr);
110
+ if (attr == NULL) {
111
+ ndt_free(name);
112
+ ndt_string_seq_del(seq);
113
+ return ndt_memory_error(ctx);
114
+ }
115
+
116
+ attr->tag = AttrList;
117
+ attr->name = name;
118
+ attr->AttrList.len = seq->len;
119
+ attr->AttrList.items = seq->ptr;
120
+
121
+ ndt_free(seq);
122
+
123
+ return attr;
124
+ }
125
+
126
+
127
+ /*****************************************************************************/
128
+ /* Parser functions for creating types */
129
+ /*****************************************************************************/
130
+
131
+ ndt_t *
132
+ mk_function(ndt_type_seq_t *in, ndt_type_seq_t *out, ndt_context_t *ctx)
133
+ {
134
+ ndt_t *types[NDT_MAX_ARGS];
135
+ int64_t nin = in->len;
136
+ int64_t nout = out->len;
137
+ int64_t nargs, i;
138
+
139
+ nargs = nin + nout;
140
+
141
+ if (nargs > NDT_MAX_ARGS) {
142
+ ndt_err_format(ctx, NDT_ValueError,
143
+ "maximum number of function arguments is %d", NDT_MAX_ARGS);
144
+ ndt_type_seq_del(in);
145
+ ndt_type_seq_del(out);
146
+ return NULL;
147
+ }
148
+
149
+ for (i = 0; i < nin; i++) {
150
+ types[i] = in->ptr[i];
151
+ }
152
+
153
+ for (i = 0; i < nout; i++) {
154
+ types[nin+i] = out->ptr[i];
155
+ }
156
+
157
+ ndt_free(in->ptr);
158
+ ndt_free(in);
159
+ ndt_free(out->ptr);
160
+ ndt_free(out);
161
+
162
+ return ndt_function(types, nargs, nin, nout, ctx);
163
+ }
164
+
165
+ ndt_t *
166
+ mk_fortran(ndt_t *type, ndt_context_t *ctx)
167
+ {
168
+ ndt_t *t = ndt_to_fortran(type, ctx);
169
+
170
+ ndt_del(type);
171
+ return t;
172
+ }
173
+
174
+ ndt_t *
175
+ mk_contig(char *name, ndt_t *type, ndt_context_t *ctx)
176
+ {
177
+ enum ndt_contig tag = RequireNA;
178
+
179
+ if (strcmp(name, "C") == 0) {
180
+ tag = RequireC;
181
+ }
182
+ else if (strcmp(name, "F") == 0) {
183
+ tag = RequireF;
184
+ }
185
+ ndt_free(name);
186
+
187
+ if (tag == RequireNA) {
188
+ ndt_err_format(ctx, NDT_ParseError,
189
+ "valid contiguity modifiers are 'C' or 'F'");
190
+ ndt_del(type);
191
+ return NULL;
192
+ }
193
+
194
+ switch (type->tag) {
195
+ case FixedDim: {
196
+ ndt_t *t = type;
197
+
198
+ if (ndt_is_concrete(type)) {
199
+ if (!ndt_is_c_contiguous(type)) {
200
+ ndt_err_format(ctx, NDT_ParseError,
201
+ "valid contiguity modifiers are 'C' or 'F'");
202
+ ndt_del(type);
203
+ return NULL;
204
+ }
205
+
206
+ if (tag == RequireF) {
207
+ t = ndt_to_fortran(type, ctx);
208
+ ndt_del(type);
209
+ if (t == NULL) {
210
+ return NULL;
211
+ }
212
+ }
213
+ }
214
+
215
+ t->access = Abstract;
216
+ t->FixedDim.tag = tag;
217
+ return t;
218
+ }
219
+ case SymbolicDim: {
220
+ type->SymbolicDim.tag = tag;
221
+ return type;
222
+ }
223
+ case EllipsisDim: {
224
+ type->EllipsisDim.tag = tag;
225
+ return type;
226
+ }
227
+ default: {
228
+ ndt_err_format(ctx, NDT_ParseError,
229
+ "'C' or 'F' can only be applied to fixed or symbolic dimensions");
230
+ ndt_del(type);
231
+ return NULL;
232
+ }
233
+ }
234
+ }
235
+
236
+ ndt_t *
237
+ mk_fixed_dim_from_shape(char *v, ndt_t *type, ndt_context_t *ctx)
238
+ {
239
+ int64_t shape;
240
+
241
+ shape = ndt_strtoll(v, 0, INT64_MAX, ctx);
242
+ ndt_free(v);
243
+
244
+ if (ndt_err_occurred(ctx)) {
245
+ ndt_del(type);
246
+ return NULL;
247
+ }
248
+
249
+ return ndt_fixed_dim(type, shape, INT64_MAX, ctx);
250
+ }
251
+
252
+ ndt_t *
253
+ mk_fixed_dim_from_attrs(ndt_attr_seq_t *attrs, ndt_t *type, ndt_context_t *ctx)
254
+ {
255
+ static const attr_spec kwlist = {1, 2, {"shape", "step"}, {AttrInt64, AttrInt64}};
256
+ int64_t shape;
257
+ int64_t step = INT64_MAX;
258
+ int ret;
259
+
260
+ ret = ndt_parse_attr(&kwlist, ctx, attrs, &shape, &step);
261
+ ndt_attr_seq_del(attrs);
262
+ if (ret < 0) {
263
+ ndt_del(type);
264
+ return NULL;
265
+ }
266
+
267
+ return ndt_fixed_dim(type, shape, step, ctx);
268
+ }
269
+
270
+ ndt_t *
271
+ mk_var_dim(ndt_meta_t *m, ndt_attr_seq_t *attrs, ndt_t *type, ndt_context_t *ctx)
272
+ {
273
+ static const attr_spec kwlist = {1, 2, {"offsets", "_noffsets"}, {AttrInt32List, AttrInt64}};
274
+
275
+ if (attrs) {
276
+ int32_t *offsets = NULL;
277
+ int64_t noffsets = 0;
278
+ ndt_t *t;
279
+ int ret;
280
+
281
+ ret = ndt_parse_attr(&kwlist, ctx, attrs, &offsets, &noffsets);
282
+ ndt_attr_seq_del(attrs);
283
+ if (ret < 0) {
284
+ ndt_del(type);
285
+ return NULL;
286
+ }
287
+
288
+ if (noffsets > INT32_MAX) {
289
+ ndt_err_format(ctx, NDT_ValueError, "too many offsets");
290
+ ndt_del(type);
291
+ ndt_free(offsets);
292
+ return NULL;
293
+ }
294
+
295
+ if (m == NULL) {
296
+ t = ndt_var_dim(type, InternalOffsets, (int32_t)noffsets, offsets, 0, NULL, ctx);
297
+ }
298
+ else {
299
+ if (m->ndims >= NDT_MAX_DIM) {
300
+ ndt_err_format(ctx, NDT_RuntimeError, "too many dimensions");
301
+ ndt_del(type);
302
+ ndt_free(offsets);
303
+ return NULL;
304
+ }
305
+ m->noffsets[m->ndims] = (int32_t)noffsets;
306
+ m->offsets[m->ndims] = offsets;
307
+ m->ndims++;
308
+
309
+ t = ndt_var_dim(type, ExternalOffsets, (int32_t)noffsets, offsets, 0, NULL, ctx);
310
+ }
311
+
312
+ return t;
313
+ }
314
+ else {
315
+ return ndt_abstract_var_dim(type, ctx);
316
+ }
317
+ }
318
+
319
+ ndt_t *
320
+ mk_var_ellipsis(ndt_t *type, ndt_context_t *ctx)
321
+ {
322
+ char *s = ndt_strdup("var", ctx);
323
+
324
+ if (s == NULL) {
325
+ return NULL;
326
+ }
327
+
328
+ return ndt_ellipsis_dim(s, type, ctx);
329
+ }
330
+
331
+ ndt_field_t *
332
+ mk_field(char *name, ndt_t *type, ndt_attr_seq_t *attrs, ndt_context_t *ctx)
333
+ {
334
+ static const attr_spec kwlist = {0, 2, {"align", "pack"}, {AttrUint16Opt, AttrUint16Opt}};
335
+ uint16_opt_t align = {None, 0};
336
+ uint16_opt_t pack = {None, 0};
337
+ uint16_opt_t pad = {None, 0};
338
+
339
+ if (attrs) {
340
+ int ret = ndt_parse_attr(&kwlist, ctx, attrs, &align, &pack);
341
+ ndt_attr_seq_del(attrs);
342
+
343
+ if (ret < 0) {
344
+ ndt_free(name);
345
+ ndt_del(type);
346
+ return NULL;
347
+ }
348
+ }
349
+
350
+ return ndt_field(name, type, align, pack, pad, ctx);
351
+ }
352
+
353
+ ndt_t *
354
+ mk_tuple(enum ndt_variadic flag, ndt_field_seq_t *fields,
355
+ ndt_attr_seq_t *attrs, ndt_context_t *ctx)
356
+ {
357
+ static const attr_spec kwlist = {0, 2, {"align", "pack"}, {AttrUint16Opt, AttrUint16Opt}};
358
+ uint16_opt_t align = {None, 0};
359
+ uint16_opt_t pack = {None, 0};
360
+ ndt_t *t;
361
+
362
+ fields = ndt_field_seq_finalize(fields);
363
+
364
+ if (attrs) {
365
+ int ret = ndt_parse_attr(&kwlist, ctx, attrs, &align, &pack);
366
+ ndt_attr_seq_del(attrs);
367
+
368
+ if (ret < 0) {
369
+ ndt_field_seq_del(fields);
370
+ return NULL;
371
+ }
372
+ }
373
+
374
+ if (fields == NULL) {
375
+ return ndt_tuple(flag, NULL, 0, align, pack, ctx);
376
+ }
377
+
378
+ t = ndt_tuple(flag, fields->ptr, fields->len, align, pack, ctx);
379
+ ndt_free(fields);
380
+ return t;
381
+ }
382
+
383
+ ndt_t *
384
+ mk_record(enum ndt_variadic flag, ndt_field_seq_t *fields,
385
+ ndt_attr_seq_t *attrs, ndt_context_t *ctx)
386
+ {
387
+ static const attr_spec kwlist = {0, 2, {"align", "pack"}, {AttrUint16Opt, AttrUint16Opt}};
388
+ uint16_opt_t align = {None, 0};
389
+ uint16_opt_t pack = {None, 0};
390
+ ndt_t *t;
391
+
392
+ fields = ndt_field_seq_finalize(fields);
393
+
394
+ if (attrs) {
395
+ int ret = ndt_parse_attr(&kwlist, ctx, attrs, &align, &pack);
396
+ ndt_attr_seq_del(attrs);
397
+
398
+ if (ret < 0) {
399
+ ndt_field_seq_del(fields);
400
+ return NULL;
401
+ }
402
+ }
403
+
404
+ if (fields == NULL) {
405
+ return ndt_record(flag, NULL, 0, align, pack, ctx);
406
+ }
407
+
408
+ t = ndt_record(flag, fields->ptr, fields->len, align, pack, ctx);
409
+ ndt_free(fields);
410
+ return t;
411
+ }
412
+
413
+ ndt_t *
414
+ mk_categorical(ndt_value_seq_t *seq, ndt_context_t *ctx)
415
+ {
416
+ ndt_t *t;
417
+
418
+ seq = ndt_value_seq_finalize(seq);
419
+ t = ndt_categorical(seq->ptr, seq->len, ctx);
420
+
421
+ ndt_free(seq);
422
+ return t;
423
+ }
424
+
425
+ ndt_t *
426
+ mk_fixed_string(char *v, enum ndt_encoding encoding, ndt_context_t *ctx)
427
+ {
428
+ int64_t size;
429
+
430
+ size = ndt_strtoll(v, 0, INT64_MAX, ctx);
431
+ ndt_free(v);
432
+
433
+ if (ndt_err_occurred(ctx)) {
434
+ return NULL;
435
+ }
436
+
437
+ return ndt_fixed_string(size, encoding, ctx);
438
+ }
439
+
440
+ ndt_t *
441
+ mk_fixed_bytes(ndt_attr_seq_t *attrs, ndt_context_t *ctx)
442
+ {
443
+ static const attr_spec kwlist = {1, 2, {"size", "align"}, {AttrInt64, AttrUint16Opt}};
444
+ uint16_opt_t align = {None, 0};
445
+ int64_t datasize = 0;
446
+
447
+ if (attrs) {
448
+ int ret = ndt_parse_attr(&kwlist, ctx, attrs, &datasize, &align);
449
+ ndt_attr_seq_del(attrs);
450
+ if (ret < 0) {
451
+ return NULL;
452
+ }
453
+ }
454
+
455
+ return ndt_fixed_bytes(datasize, align, ctx);
456
+ }
457
+
458
+ ndt_t *
459
+ mk_bytes(ndt_attr_seq_t *attrs, ndt_context_t *ctx)
460
+ {
461
+ static const attr_spec kwlist = {0, 1, {"align"}, {AttrUint16Opt}};
462
+ uint16_opt_t target_align = {None, 0};
463
+
464
+ if (attrs) {
465
+ int ret = ndt_parse_attr(&kwlist, ctx, attrs, &target_align);
466
+ ndt_attr_seq_del(attrs);
467
+ if (ret < 0) {
468
+ return NULL;
469
+ }
470
+ }
471
+
472
+ return ndt_bytes(target_align, ctx);
473
+ }