ndtypes 0.2.0dev4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +50 -0
- data/Gemfile +2 -0
- data/History.md +0 -0
- data/README.md +19 -0
- data/Rakefile +125 -0
- data/ext/ruby_ndtypes/extconf.rb +55 -0
- data/ext/ruby_ndtypes/gc_guard.c +36 -0
- data/ext/ruby_ndtypes/gc_guard.h +12 -0
- data/ext/ruby_ndtypes/ndtypes/AUTHORS.txt +5 -0
- data/ext/ruby_ndtypes/ndtypes/INSTALL.txt +101 -0
- data/ext/ruby_ndtypes/ndtypes/LICENSE.txt +29 -0
- data/ext/ruby_ndtypes/ndtypes/MANIFEST.in +3 -0
- data/ext/ruby_ndtypes/ndtypes/Makefile.in +87 -0
- data/ext/ruby_ndtypes/ndtypes/README.rst +47 -0
- data/ext/ruby_ndtypes/ndtypes/config.guess +1530 -0
- data/ext/ruby_ndtypes/ndtypes/config.h.in +67 -0
- data/ext/ruby_ndtypes/ndtypes/config.sub +1782 -0
- data/ext/ruby_ndtypes/ndtypes/configure +5260 -0
- data/ext/ruby_ndtypes/ndtypes/configure.ac +161 -0
- data/ext/ruby_ndtypes/ndtypes/doc/Makefile +14 -0
- data/ext/ruby_ndtypes/ndtypes/doc/_static/copybutton.js +66 -0
- data/ext/ruby_ndtypes/ndtypes/doc/conf.py +26 -0
- data/ext/ruby_ndtypes/ndtypes/doc/grammar/grammar.rst +27 -0
- data/ext/ruby_ndtypes/ndtypes/doc/index.rst +56 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/context.rst +131 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/encodings.rst +68 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/fields-values.rst +175 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/functions.rst +72 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/index.rst +43 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/init.rst +48 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/io.rst +100 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/memory.rst +124 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/predicates.rst +110 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/typedef.rst +31 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/types.rst +594 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/util.rst +166 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/buffer-protocol.rst +27 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/index.rst +21 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/pattern-matching.rst +330 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/quickstart.rst +144 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/types.rst +544 -0
- data/ext/ruby_ndtypes/ndtypes/doc/releases/index.rst +35 -0
- data/ext/ruby_ndtypes/ndtypes/install-sh +527 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.in +271 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.vc +269 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/alloc.c +230 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.c +268 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.h +109 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.in +73 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.vc +70 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/README.txt +16 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.c +2179 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.h +134 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.y +428 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.c +2543 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.h +735 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.l +176 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/export.c +543 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/import.c +110 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/context.c +228 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/copy.c +634 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/encodings.c +116 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/equal.c +288 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.c +3067 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.h +180 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.y +417 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/io.c +1658 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.c +2773 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.h +734 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.l +222 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/match.c +1132 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.c +2323 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.h.in +893 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/overflow.h +161 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.c +473 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.h +92 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parser.c +246 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.c +269 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.h +197 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.in +48 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.vc +46 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/deserialize.c +1007 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/serialize.c +442 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/slice.h +42 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.c +238 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.h +50 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.c +371 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.h +100 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.in +55 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.vc +45 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.c +82 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.h +49 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/runtest.c +1657 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test.h +85 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_array.c +115 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_buffer.c +137 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_indent.c +201 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_match.c +2397 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_numba.c +57 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse.c +349 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_error.c +27839 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_roundtrip.c +350 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_record.c +231 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typecheck.c +375 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typedef.c +65 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/valgrind.supp +30 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/bench.c +79 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/indent.c +94 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/print_ast.c +96 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/util.c +474 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/values.c +228 -0
- data/ext/ruby_ndtypes/ndtypes/python/bench.py +49 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndt_randtype.py +409 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndt_support.py +14 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/__init__.py +70 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/_ndtypes.c +1332 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/docstrings.h +319 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/pyndtypes.h +154 -0
- data/ext/ruby_ndtypes/ndtypes/python/test_ndtypes.py +1977 -0
- data/ext/ruby_ndtypes/ndtypes/setup.py +288 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/INSTALL.txt +41 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest32.bat +15 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest64.bat +13 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild32.bat +38 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild64.bat +38 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcclean.bat +13 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcdistclean.bat +14 -0
- data/ext/ruby_ndtypes/ruby_ndtypes.c +1003 -0
- data/ext/ruby_ndtypes/ruby_ndtypes.h +37 -0
- data/ext/ruby_ndtypes/ruby_ndtypes_internal.h +28 -0
- data/lib/ndtypes.rb +45 -0
- data/lib/ndtypes/errors.rb +2 -0
- data/lib/ndtypes/version.rb +6 -0
- data/ndtypes.gemspec +47 -0
- data/spec/gc_table_spec.rb +10 -0
- data/spec/ndtypes_spec.rb +289 -0
- data/spec/spec_helper.rb +241 -0
- metadata +242 -0
@@ -0,0 +1,176 @@
|
|
1
|
+
%{
|
2
|
+
/*
|
3
|
+
* BSD 3-Clause License
|
4
|
+
*
|
5
|
+
* Copyright (c) 2017-2018, plures
|
6
|
+
* All rights reserved.
|
7
|
+
*
|
8
|
+
* Redistribution and use in source and binary forms, with or without
|
9
|
+
* modification, are permitted provided that the following conditions are met:
|
10
|
+
*
|
11
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
12
|
+
* this list of conditions and the following disclaimer.
|
13
|
+
*
|
14
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
15
|
+
* this list of conditions and the following disclaimer in the documentation
|
16
|
+
* and/or other materials provided with the distribution.
|
17
|
+
*
|
18
|
+
* 3. Neither the name of the copyright holder nor the names of its
|
19
|
+
* contributors may be used to endorse or promote products derived from
|
20
|
+
* this software without specific prior written permission.
|
21
|
+
*
|
22
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
25
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
26
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
27
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
28
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
29
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
30
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
31
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
*/
|
33
|
+
|
34
|
+
|
35
|
+
#include <stdio.h>
|
36
|
+
#include <string.h>
|
37
|
+
#include <stdint.h>
|
38
|
+
#include <setjmp.h>
|
39
|
+
#include "ndtypes.h"
|
40
|
+
#include "bpgrammar.h"
|
41
|
+
|
42
|
+
|
43
|
+
/* From PostgreSQL: avoid exit() on fatal scanner errors. */
|
44
|
+
#undef fprintf
|
45
|
+
#define fprintf(file, fmt, msg) fprintf_to_longjmp(fmt, msg, yyscanner)
|
46
|
+
|
47
|
+
extern jmp_buf ndt_bp_lexerror;
|
48
|
+
static void
|
49
|
+
fprintf_to_longjmp(const char *fmt, const char *msg, yyscan_t yyscanner)
|
50
|
+
{
|
51
|
+
(void)fmt; (void)msg; (void)yyscanner;
|
52
|
+
|
53
|
+
/* We don't have access to the parse context here: discard the error
|
54
|
+
message, which is always either an allocation failure or an internal
|
55
|
+
flex error. */
|
56
|
+
longjmp(ndt_bp_lexerror, 1);
|
57
|
+
}
|
58
|
+
|
59
|
+
void *
|
60
|
+
yyalloc(size_t size, yyscan_t yyscanner)
|
61
|
+
{
|
62
|
+
(void)yyscanner;
|
63
|
+
|
64
|
+
return ndt_alloc(1, size);
|
65
|
+
}
|
66
|
+
|
67
|
+
void *
|
68
|
+
yyrealloc(void *ptr, size_t size, yyscan_t yyscanner)
|
69
|
+
{
|
70
|
+
(void)yyscanner;
|
71
|
+
|
72
|
+
return ndt_realloc(ptr, 1, size);
|
73
|
+
}
|
74
|
+
|
75
|
+
void
|
76
|
+
yyfree(void *ptr, yyscan_t yyscanner)
|
77
|
+
{
|
78
|
+
(void)yyscanner;
|
79
|
+
|
80
|
+
ndt_free(ptr);
|
81
|
+
}
|
82
|
+
|
83
|
+
%}
|
84
|
+
|
85
|
+
%option bison-bridge bison-locations reentrant noyywrap
|
86
|
+
%option nounput noinput noyyalloc noyyrealloc noyyfree
|
87
|
+
%option never-interactive
|
88
|
+
%option yylineno
|
89
|
+
%option 8bit
|
90
|
+
%option extra-type="ndt_context_t *"
|
91
|
+
%option warn nodefault
|
92
|
+
|
93
|
+
%x FIELDNAME
|
94
|
+
|
95
|
+
newline [\n\r]
|
96
|
+
space [ \t\f]
|
97
|
+
non_newline [^\n\r]
|
98
|
+
comment #{non_newline}*
|
99
|
+
|
100
|
+
nonzerodigit [1-9]
|
101
|
+
digit [0-9]
|
102
|
+
decimalinteger {nonzerodigit}{digit}*|0+
|
103
|
+
integer -?({decimalinteger})
|
104
|
+
|
105
|
+
name [A-Za-z][a-zA-Z0-9_]*
|
106
|
+
|
107
|
+
%%
|
108
|
+
|
109
|
+
%code {
|
110
|
+
yycolumn = 1;
|
111
|
+
|
112
|
+
#undef YY_USER_ACTION
|
113
|
+
#define YY_USER_ACTION \
|
114
|
+
yylloc->first_line = yylloc->last_line = yylineno; \
|
115
|
+
yylloc->first_column = yycolumn; \
|
116
|
+
yylloc->last_column = yycolumn+yyleng-1; \
|
117
|
+
yycolumn += yyleng;
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
<INITIAL>{
|
122
|
+
"?" { yylval->uchar = '?'; return DTYPE; }
|
123
|
+
"c" { yylval->uchar = 'c'; return DTYPE; }
|
124
|
+
"b" { yylval->uchar = 'b'; return DTYPE; }
|
125
|
+
"B" { yylval->uchar = 'B'; return DTYPE; }
|
126
|
+
"h" { yylval->uchar = 'h'; return DTYPE; }
|
127
|
+
"H" { yylval->uchar = 'H'; return DTYPE; }
|
128
|
+
"i" { yylval->uchar = 'i'; return DTYPE; }
|
129
|
+
"I" { yylval->uchar = 'I'; return DTYPE; }
|
130
|
+
"l" { yylval->uchar = 'l'; return DTYPE; }
|
131
|
+
"L" { yylval->uchar = 'L'; return DTYPE; }
|
132
|
+
"q" { yylval->uchar = 'q'; return DTYPE; }
|
133
|
+
"Q" { yylval->uchar = 'Q'; return DTYPE; }
|
134
|
+
"n" { yylval->uchar = 'n'; return DTYPE; }
|
135
|
+
"N" { yylval->uchar = 'N'; return DTYPE; }
|
136
|
+
"e" { yylval->uchar = 'e'; return DTYPE; }
|
137
|
+
"f" { yylval->uchar = 'f'; return DTYPE; }
|
138
|
+
"d" { yylval->uchar = 'd'; return DTYPE; }
|
139
|
+
"E" { yylval->uchar = 'E'; return DTYPE; }
|
140
|
+
"F" { yylval->uchar = 'F'; return DTYPE; }
|
141
|
+
"D" { yylval->uchar = 'D'; return DTYPE; }
|
142
|
+
|
143
|
+
"x" { return PAD; }
|
144
|
+
"T" { return RECORD; }
|
145
|
+
"s" { return BYTES; }
|
146
|
+
|
147
|
+
"->" { return RARROW; }
|
148
|
+
"@" { return AT; }
|
149
|
+
"=" { return EQUAL; }
|
150
|
+
"<" { return LESS; }
|
151
|
+
">" { return GREATER; }
|
152
|
+
"!" { return BANG; }
|
153
|
+
"," { return COMMA; }
|
154
|
+
":" { BEGIN(FIELDNAME); return COLON; }
|
155
|
+
"(" { return LPAREN; }
|
156
|
+
")" { return RPAREN; }
|
157
|
+
"{" { return LBRACE; }
|
158
|
+
"}" { return RBRACE; }
|
159
|
+
|
160
|
+
{integer} { yylval->string = ndt_strdup(yytext, ctx); if (yylval->string == NULL) return ERRTOKEN; return INTEGER; }
|
161
|
+
}
|
162
|
+
|
163
|
+
<FIELDNAME>{
|
164
|
+
":" { BEGIN(INITIAL); return COLON; }
|
165
|
+
{name} { yylval->string = ndt_strdup(yytext, ctx); if (yylval->string == NULL) return ERRTOKEN; return NAME; }
|
166
|
+
}
|
167
|
+
|
168
|
+
<INITIAL,FIELDNAME>{
|
169
|
+
{newline} { yycolumn = 1; }
|
170
|
+
{space} {} /* ignore */
|
171
|
+
{comment} {} /* ignore */
|
172
|
+
. { return ERRTOKEN; }
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
%%
|
@@ -0,0 +1,543 @@
|
|
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 <stdio.h>
|
35
|
+
#include <stdlib.h>
|
36
|
+
#include <stdint.h>
|
37
|
+
#include <string.h>
|
38
|
+
#include <inttypes.h>
|
39
|
+
#include <stdarg.h>
|
40
|
+
#include <errno.h>
|
41
|
+
#include <assert.h>
|
42
|
+
#include "ndtypes.h"
|
43
|
+
|
44
|
+
|
45
|
+
/******************************************************************************/
|
46
|
+
/* String buffer */
|
47
|
+
/******************************************************************************/
|
48
|
+
|
49
|
+
#undef buf_t
|
50
|
+
typedef struct {
|
51
|
+
size_t count; /* count the required size */
|
52
|
+
size_t size; /* buffer size (0 for the count phase) */
|
53
|
+
char *cur; /* buffer data (NULL for the count phase) */
|
54
|
+
} buf_t;
|
55
|
+
|
56
|
+
static int
|
57
|
+
_ndt_snprintf(ndt_context_t *ctx, buf_t *buf, const char *fmt, va_list ap)
|
58
|
+
{
|
59
|
+
int n;
|
60
|
+
|
61
|
+
errno = 0;
|
62
|
+
n = vsnprintf(buf->cur, buf->size, fmt, ap);
|
63
|
+
if (buf->cur && n < 0) {
|
64
|
+
if (errno == ENOMEM) {
|
65
|
+
ndt_err_format(ctx, NDT_MemoryError, "out of memory");
|
66
|
+
}
|
67
|
+
else {
|
68
|
+
ndt_err_format(ctx, NDT_OSError, "output error");
|
69
|
+
}
|
70
|
+
return -1;
|
71
|
+
}
|
72
|
+
|
73
|
+
if (buf->cur && (size_t)n >= buf->size) {
|
74
|
+
ndt_err_format(ctx, NDT_ValueError, "insufficient buffer size");
|
75
|
+
return -1;
|
76
|
+
}
|
77
|
+
|
78
|
+
buf->count += n;
|
79
|
+
|
80
|
+
if (buf->cur) {
|
81
|
+
buf->cur += n;
|
82
|
+
buf->size -= n;
|
83
|
+
}
|
84
|
+
|
85
|
+
return 0;
|
86
|
+
}
|
87
|
+
|
88
|
+
static int
|
89
|
+
ndt_snprintf(ndt_context_t *ctx, buf_t *buf, const char *fmt, ...)
|
90
|
+
{
|
91
|
+
va_list ap;
|
92
|
+
int n;
|
93
|
+
|
94
|
+
va_start(ap, fmt);
|
95
|
+
n = _ndt_snprintf(ctx, buf, fmt, ap);
|
96
|
+
va_end(ap);
|
97
|
+
|
98
|
+
return n;
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
/******************************************************************************/
|
103
|
+
/* PEP-3118 format string of a type */
|
104
|
+
/******************************************************************************/
|
105
|
+
|
106
|
+
static int format(buf_t *buf, const ndt_t *t, ndt_context_t *ctx);
|
107
|
+
|
108
|
+
|
109
|
+
static int
|
110
|
+
option(const ndt_t *t, ndt_context_t *ctx)
|
111
|
+
{
|
112
|
+
if (ndt_is_optional(t)) {
|
113
|
+
ndt_err_format(ctx, NDT_ValueError,
|
114
|
+
"buffer protocol does not support optional types");
|
115
|
+
return -1;
|
116
|
+
}
|
117
|
+
|
118
|
+
return 0;
|
119
|
+
}
|
120
|
+
|
121
|
+
static int
|
122
|
+
primitive(buf_t *buf, const ndt_t *t, const char *fmt, ndt_context_t *ctx)
|
123
|
+
{
|
124
|
+
int n;
|
125
|
+
|
126
|
+
if (ndt_endian_is_set(t)) {
|
127
|
+
n = ndt_snprintf(ctx, buf, ndt_is_little_endian(t) ? "<" : ">");
|
128
|
+
}
|
129
|
+
else {
|
130
|
+
n = ndt_snprintf(ctx, buf, "=");
|
131
|
+
}
|
132
|
+
if (n < 0) return -1;
|
133
|
+
|
134
|
+
return ndt_snprintf(ctx, buf, fmt);
|
135
|
+
}
|
136
|
+
|
137
|
+
static int
|
138
|
+
format_dimensions(buf_t *buf, const ndt_t *t, ndt_context_t *ctx)
|
139
|
+
{
|
140
|
+
int n;
|
141
|
+
|
142
|
+
if (!ndt_is_c_contiguous(t)) {
|
143
|
+
ndt_err_format(ctx, NDT_ValueError,
|
144
|
+
"dimensions must be C-contiguous");
|
145
|
+
return -1;
|
146
|
+
}
|
147
|
+
|
148
|
+
n = ndt_snprintf(ctx, buf, "(");
|
149
|
+
if (n < 0) return -1;
|
150
|
+
|
151
|
+
for (; t->ndim > 0; t=t->FixedDim.type) {
|
152
|
+
n = ndt_snprintf(ctx, buf, "%" PRIi64 "%s", t->FixedDim.shape,
|
153
|
+
t->ndim >= 2 ? "," : "");
|
154
|
+
if (n < 0) return -1;
|
155
|
+
}
|
156
|
+
|
157
|
+
n = ndt_snprintf(ctx, buf, ")");
|
158
|
+
if (n < 0) return -1;
|
159
|
+
|
160
|
+
return format(buf, t, ctx);
|
161
|
+
}
|
162
|
+
|
163
|
+
static int
|
164
|
+
format_tuple(buf_t *buf, const ndt_t *t, ndt_context_t *ctx)
|
165
|
+
{
|
166
|
+
int n;
|
167
|
+
|
168
|
+
if (t->Tuple.flag == Variadic) {
|
169
|
+
ndt_err_format(ctx, NDT_ValueError,
|
170
|
+
"cannot convert variadic tuple");
|
171
|
+
return -1;
|
172
|
+
}
|
173
|
+
|
174
|
+
for (int64_t i = 0; i < t->Tuple.shape; i++) {
|
175
|
+
n = format(buf, t->Tuple.types[i], ctx);
|
176
|
+
if (n < 0) return -1;
|
177
|
+
|
178
|
+
for (uint16_t k = 0; k < t->Concrete.Tuple.pad[i]; k++) {
|
179
|
+
n = ndt_snprintf(ctx, buf, "x");
|
180
|
+
if (n < 0) return -1;
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
return 0;
|
185
|
+
}
|
186
|
+
|
187
|
+
static int
|
188
|
+
format_record(buf_t *buf, const ndt_t *t, ndt_context_t *ctx)
|
189
|
+
{
|
190
|
+
int n;
|
191
|
+
|
192
|
+
if (t->Record.flag == Variadic) {
|
193
|
+
ndt_err_format(ctx, NDT_ValueError,
|
194
|
+
"cannot convert variadic record");
|
195
|
+
return -1;
|
196
|
+
}
|
197
|
+
|
198
|
+
n = ndt_snprintf(ctx, buf, "T{");
|
199
|
+
if (n < 0) return -1;
|
200
|
+
|
201
|
+
for (int64_t i = 0; i < t->Record.shape; i++) {
|
202
|
+
n = format(buf, t->Record.types[i], ctx);
|
203
|
+
if (n < 0) return -1;
|
204
|
+
|
205
|
+
for (uint16_t k = 0; k < t->Concrete.Record.pad[i]; k++) {
|
206
|
+
n = ndt_snprintf(ctx, buf, "x");
|
207
|
+
if (n < 0) return -1;
|
208
|
+
}
|
209
|
+
|
210
|
+
n = ndt_snprintf(ctx, buf, ":%s:", t->Record.names[i]);
|
211
|
+
if (n < 0) return -1;
|
212
|
+
}
|
213
|
+
|
214
|
+
n = ndt_snprintf(ctx, buf, "}");
|
215
|
+
if (n < 0) return -1;
|
216
|
+
|
217
|
+
return 0;
|
218
|
+
}
|
219
|
+
|
220
|
+
static int
|
221
|
+
format_fixed_bytes(buf_t *buf, const ndt_t *t, ndt_context_t *ctx)
|
222
|
+
{
|
223
|
+
if (t->FixedBytes.align != 1) {
|
224
|
+
ndt_err_format(ctx, NDT_ValueError,
|
225
|
+
"buffer protocol does not support alignment");
|
226
|
+
return -1;
|
227
|
+
}
|
228
|
+
|
229
|
+
return ndt_snprintf(ctx, buf, "%" PRIi64 "s", t->FixedBytes.size);
|
230
|
+
}
|
231
|
+
|
232
|
+
static int
|
233
|
+
format(buf_t *buf, const ndt_t *t, ndt_context_t *ctx)
|
234
|
+
{
|
235
|
+
int n;
|
236
|
+
|
237
|
+
n = option(t, ctx);
|
238
|
+
if (n < 0) return -1;
|
239
|
+
|
240
|
+
switch (t->tag) {
|
241
|
+
case FixedDim: return format_dimensions(buf, t, ctx);
|
242
|
+
case Tuple: return format_tuple(buf, t, ctx);
|
243
|
+
case Record: return format_record(buf, t, ctx);
|
244
|
+
|
245
|
+
case FixedBytes: return format_fixed_bytes(buf, t, ctx);
|
246
|
+
|
247
|
+
case Bool: return ndt_snprintf(ctx, buf, "?");
|
248
|
+
|
249
|
+
case Int8: return primitive(buf, t, "b", ctx);
|
250
|
+
case Int16: return primitive(buf, t, "h", ctx);
|
251
|
+
case Int32: return primitive(buf, t, "i", ctx);
|
252
|
+
case Int64: return primitive(buf, t, "q", ctx);
|
253
|
+
|
254
|
+
case Uint8: return primitive(buf, t, "B", ctx);
|
255
|
+
case Uint16: return primitive(buf, t, "H", ctx);
|
256
|
+
case Uint32: return primitive(buf, t, "I", ctx);
|
257
|
+
case Uint64: return primitive(buf, t, "Q", ctx);
|
258
|
+
|
259
|
+
case Float16: return primitive(buf, t, "e", ctx);
|
260
|
+
case Float32: return primitive(buf, t, "f", ctx);
|
261
|
+
case Float64: return primitive(buf, t, "d", ctx);
|
262
|
+
|
263
|
+
case Complex32: return primitive(buf, t, "E", ctx);
|
264
|
+
case Complex64: return primitive(buf, t, "F", ctx);
|
265
|
+
case Complex128: return primitive(buf, t, "D", ctx);
|
266
|
+
|
267
|
+
case Char:
|
268
|
+
if (t->Char.encoding == Ascii) return ndt_snprintf(ctx, buf, "c");
|
269
|
+
/* fall through */
|
270
|
+
|
271
|
+
case Module: case Function:
|
272
|
+
case VarDim: case SymbolicDim: case EllipsisDim:
|
273
|
+
case Ref: case Constr: case Nominal:
|
274
|
+
case Categorical:
|
275
|
+
case FixedString: case String: case Bytes:
|
276
|
+
case Typevar:
|
277
|
+
case AnyKind: case ScalarKind:
|
278
|
+
case SignedKind: case UnsignedKind:
|
279
|
+
case FloatKind: case ComplexKind:
|
280
|
+
case FixedStringKind: case FixedBytesKind:
|
281
|
+
ndt_err_format(ctx, NDT_ValueError,
|
282
|
+
"type is not supported by the buffer protocol");
|
283
|
+
return -1;
|
284
|
+
}
|
285
|
+
|
286
|
+
/* NOT REACHED: tags should be exhaustive. */
|
287
|
+
ndt_internal_error("invalid tag");
|
288
|
+
}
|
289
|
+
|
290
|
+
|
291
|
+
/******************************************************************************/
|
292
|
+
/* Conversion from function to NumPy signature */
|
293
|
+
/******************************************************************************/
|
294
|
+
|
295
|
+
static int
|
296
|
+
symbolic_dimensions(buf_t *buf, const ndt_t *t, ndt_context_t *ctx)
|
297
|
+
{
|
298
|
+
int n, i;
|
299
|
+
|
300
|
+
if (t->tag != EllipsisDim || t->EllipsisDim.name != NULL) {
|
301
|
+
ndt_err_format(ctx, NDT_ValueError, "expected unnamed ellipsis");
|
302
|
+
return -1;
|
303
|
+
}
|
304
|
+
|
305
|
+
n = ndt_snprintf(ctx, buf, "(");
|
306
|
+
if (n < 0) return -1;
|
307
|
+
|
308
|
+
for (i=0, t=t->EllipsisDim.type; t->ndim > 0; i++, t=t->SymbolicDim.type) {
|
309
|
+
if (t->tag != SymbolicDim) {
|
310
|
+
ndt_err_format(ctx, NDT_ValueError, "expected symbolic dimension");
|
311
|
+
return -1;
|
312
|
+
}
|
313
|
+
|
314
|
+
if (i >= 1) {
|
315
|
+
n = ndt_snprintf(ctx, buf, ", ");
|
316
|
+
if (n < 0) return -1;
|
317
|
+
}
|
318
|
+
|
319
|
+
n = ndt_snprintf(ctx, buf, "%s", t->SymbolicDim.name);
|
320
|
+
if (n < 0) return -1;
|
321
|
+
}
|
322
|
+
|
323
|
+
return ndt_snprintf(ctx, buf, ")");
|
324
|
+
}
|
325
|
+
|
326
|
+
static int
|
327
|
+
np_signature(buf_t *buf, const ndt_t *t, ndt_context_t *ctx)
|
328
|
+
{
|
329
|
+
int64_t i = 0;
|
330
|
+
int n;
|
331
|
+
|
332
|
+
assert(t->tag == Function);
|
333
|
+
|
334
|
+
if (t->Function.nin == 0) {
|
335
|
+
n = ndt_snprintf(ctx, buf, "nil");
|
336
|
+
if (n < 0) return -1;
|
337
|
+
}
|
338
|
+
else {
|
339
|
+
for (i = 0; i < t->Function.nin; i++) {
|
340
|
+
if (i >= 1) {
|
341
|
+
n = ndt_snprintf(ctx, buf, ", ");
|
342
|
+
if (n < 0) return -1;
|
343
|
+
}
|
344
|
+
|
345
|
+
n = symbolic_dimensions(buf, t->Function.types[i], ctx);
|
346
|
+
if (n < 0) return -1;
|
347
|
+
}
|
348
|
+
}
|
349
|
+
|
350
|
+
n = ndt_snprintf(ctx, buf, " -> ");
|
351
|
+
if (n < 0) return -1;
|
352
|
+
|
353
|
+
if (t->Function.nout == 0) {
|
354
|
+
n = ndt_snprintf(ctx, buf, "nil");
|
355
|
+
if (n < 0) return -1;
|
356
|
+
}
|
357
|
+
else if (t->Function.nout == 1) {
|
358
|
+
n = symbolic_dimensions(buf, t->Function.types[i], ctx);
|
359
|
+
if (n < 0) return -1;
|
360
|
+
}
|
361
|
+
else {
|
362
|
+
ndt_err_format(ctx, NDT_ValueError,
|
363
|
+
"multiple return values not supported");
|
364
|
+
return -1;
|
365
|
+
}
|
366
|
+
|
367
|
+
return 0;
|
368
|
+
}
|
369
|
+
|
370
|
+
static int
|
371
|
+
nb_dtype(buf_t *buf, const ndt_t *t, ndt_context_t *ctx)
|
372
|
+
{
|
373
|
+
switch (t->tag) {
|
374
|
+
case Bool: return ndt_snprintf(ctx, buf, "bool");
|
375
|
+
|
376
|
+
case Int8: return ndt_snprintf(ctx, buf, "int8");
|
377
|
+
case Int16: return ndt_snprintf(ctx, buf, "int16");
|
378
|
+
case Int32: return ndt_snprintf(ctx, buf, "int32");
|
379
|
+
case Int64: return ndt_snprintf(ctx, buf, "int64");
|
380
|
+
|
381
|
+
case Uint8: return ndt_snprintf(ctx, buf, "uint8");
|
382
|
+
case Uint16: return ndt_snprintf(ctx, buf, "uint16");
|
383
|
+
case Uint32: return ndt_snprintf(ctx, buf, "uint32");
|
384
|
+
case Uint64: return ndt_snprintf(ctx, buf, "uint64");
|
385
|
+
|
386
|
+
case Float16: return ndt_snprintf(ctx, buf, "float16");
|
387
|
+
case Float32: return ndt_snprintf(ctx, buf, "float32");
|
388
|
+
case Float64: return ndt_snprintf(ctx, buf, "float64");
|
389
|
+
|
390
|
+
case Complex32: return ndt_snprintf(ctx, buf, "complex32");
|
391
|
+
case Complex64: return ndt_snprintf(ctx, buf, "complex64");
|
392
|
+
case Complex128: return ndt_snprintf(ctx, buf, "complex128");
|
393
|
+
|
394
|
+
default:
|
395
|
+
ndt_err_format(ctx, NDT_ValueError, "unsupported dtype");
|
396
|
+
return -1;
|
397
|
+
}
|
398
|
+
}
|
399
|
+
|
400
|
+
static int
|
401
|
+
nb_signature(buf_t *buf, const ndt_t *t, ndt_context_t *ctx)
|
402
|
+
{
|
403
|
+
const ndt_t *dtype;
|
404
|
+
int coredims;
|
405
|
+
int n;
|
406
|
+
|
407
|
+
assert(t->tag == Function);
|
408
|
+
|
409
|
+
n = ndt_snprintf(ctx, buf, "void(");
|
410
|
+
if (n < 0) return -1;
|
411
|
+
|
412
|
+
for (int64_t i = 0; i < t->Function.nargs; i++) {
|
413
|
+
if (i >= 1) {
|
414
|
+
n = ndt_snprintf(ctx, buf, ", ");
|
415
|
+
if (n < 0) return -1;
|
416
|
+
}
|
417
|
+
|
418
|
+
dtype = ndt_hidden_dtype(t->Function.types[i]);
|
419
|
+
n = nb_dtype(buf, dtype, ctx);
|
420
|
+
if (n < 0) return -1;
|
421
|
+
|
422
|
+
coredims = t->Function.types[i]->ndim-1;
|
423
|
+
if (coredims > 0) {
|
424
|
+
n = ndt_snprintf(ctx, buf, "[");
|
425
|
+
if (n < 0) return -1;
|
426
|
+
|
427
|
+
for (int k = 0; k < coredims; k++) {
|
428
|
+
if (k >= 1) {
|
429
|
+
n = ndt_snprintf(ctx, buf, ", ");
|
430
|
+
if (n < 0) return -1;
|
431
|
+
}
|
432
|
+
n = ndt_snprintf(ctx, buf, ":");
|
433
|
+
if (n < 0) return -1;
|
434
|
+
}
|
435
|
+
|
436
|
+
n = ndt_snprintf(ctx, buf, "]");
|
437
|
+
if (n < 0) return -1;
|
438
|
+
}
|
439
|
+
}
|
440
|
+
|
441
|
+
return ndt_snprintf(ctx, buf, ")");
|
442
|
+
}
|
443
|
+
|
444
|
+
|
445
|
+
/******************************************************************************/
|
446
|
+
/* API: conversion to buffer protocol format */
|
447
|
+
/******************************************************************************/
|
448
|
+
|
449
|
+
char *
|
450
|
+
ndt_to_bpformat(const ndt_t *t, ndt_context_t *ctx)
|
451
|
+
{
|
452
|
+
buf_t buf = {0, 0, NULL};
|
453
|
+
char *s;
|
454
|
+
size_t count;
|
455
|
+
|
456
|
+
if (format(&buf, t, ctx) < 0) {
|
457
|
+
return NULL;
|
458
|
+
}
|
459
|
+
|
460
|
+
count = buf.count;
|
461
|
+
buf.count = 0;
|
462
|
+
buf.size = count+1;
|
463
|
+
|
464
|
+
buf.cur = s = ndt_alloc(1, count+1);
|
465
|
+
if (buf.cur == NULL) {
|
466
|
+
return ndt_memory_error(ctx);
|
467
|
+
}
|
468
|
+
|
469
|
+
if (format(&buf, t, ctx) < 0) {
|
470
|
+
ndt_free(s);
|
471
|
+
return NULL;
|
472
|
+
}
|
473
|
+
s[count] = '\0';
|
474
|
+
|
475
|
+
return s;
|
476
|
+
}
|
477
|
+
|
478
|
+
int
|
479
|
+
ndt_to_nbformat(char **sig, char **dtype, const ndt_t *t, ndt_context_t *ctx)
|
480
|
+
{
|
481
|
+
buf_t buf = {0, 0, NULL};
|
482
|
+
size_t count;
|
483
|
+
|
484
|
+
*sig = *dtype = NULL;
|
485
|
+
|
486
|
+
if (t->tag != Function) {
|
487
|
+
ndt_err_format(ctx, NDT_ValueError,
|
488
|
+
"conversion to numba signature requires a function type");
|
489
|
+
return -1;
|
490
|
+
}
|
491
|
+
|
492
|
+
|
493
|
+
if (np_signature(&buf, t, ctx) < 0) {
|
494
|
+
return -1;
|
495
|
+
}
|
496
|
+
|
497
|
+
count = buf.count;
|
498
|
+
buf.count = 0;
|
499
|
+
buf.size = count+1;
|
500
|
+
|
501
|
+
buf.cur = *sig = ndt_alloc(1, count+1);
|
502
|
+
if (buf.cur == NULL) {
|
503
|
+
(void)ndt_memory_error(ctx);
|
504
|
+
return -1;
|
505
|
+
}
|
506
|
+
|
507
|
+
if (np_signature(&buf, t, ctx) < 0) {
|
508
|
+
ndt_free(*sig);
|
509
|
+
return -1;
|
510
|
+
}
|
511
|
+
(*sig)[count] = '\0';
|
512
|
+
|
513
|
+
|
514
|
+
buf.count = buf.size = 0;
|
515
|
+
buf.cur = NULL;
|
516
|
+
if (nb_signature(&buf, t, ctx) < 0) {
|
517
|
+
ndt_free(*sig);
|
518
|
+
*sig = NULL;
|
519
|
+
return -1;
|
520
|
+
}
|
521
|
+
|
522
|
+
count = buf.count;
|
523
|
+
buf.count = 0;
|
524
|
+
buf.size = count+1;
|
525
|
+
|
526
|
+
buf.cur = *dtype = ndt_alloc(1, count+1);
|
527
|
+
if (buf.cur == NULL) {
|
528
|
+
ndt_free(*sig);
|
529
|
+
*sig = NULL;
|
530
|
+
(void)ndt_memory_error(ctx);
|
531
|
+
return -1;
|
532
|
+
}
|
533
|
+
|
534
|
+
if (nb_signature(&buf, t, ctx) < 0) {
|
535
|
+
ndt_free(*sig);
|
536
|
+
ndt_free(*dtype);
|
537
|
+
*sig = *dtype = NULL;
|
538
|
+
return -1;
|
539
|
+
}
|
540
|
+
(*dtype)[count] = '\0';
|
541
|
+
|
542
|
+
return 0;
|
543
|
+
}
|