ndtypes 0.2.0dev4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,79 @@
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 "ndtypes.h"
36
+
37
+
38
+ const char *bar = "{parent : { id: int64, count: uint16, prefix: char, length: int }, time : int, ratio : float32, size : uint16 }";
39
+
40
+ const char *s = "{battingpost: var * {yearID: ?int32, round: ?string, playerID: ?string, teamID: ?string, lgID: (?string, int64, 5 * 10 * {a: complex128, b: ?int32}), G: ?int32, AB: ?int32, R: ?int32, H: (int32, ... * int32) -> int32, B: (int32, ...) -> int32, HR: {a: 10 * float64, b: var * int32, ...}, RBI: ?int32, SB: ?int32, CS: ?int32, BB: ?int32, SO: ?int32, IBB: ?int32, HBP: ?int32, SH: ?int32, SF: ?int32, GIDP: ?int32, AString: fixed_string(100,'utf32'), BString: fixed_string(100), CBytes: bytes(align=16), DBytes: fixed_bytes(size=1600, align=16)}, awardsmanagers: var * {managerID: ?string, awardID: ?string, yearID: ?int32, lgID: ?string, tie: ?string, notes: ?string}, hofold: var * {hofID: ?string, yearid: ?int32, votedBy: ?string, ballots: ?int32, votes: ?int32, inducted: ?string, category: ?string}, salaries: var * {yearID: ?int32, teamID: ?string, lgID: ?string, playerID: ?string, salary: ?float64}, pitchingpost: var * {playerID: ?string, yearID: ?int32, round: ?string, teamID: ?string, lgID: ?string, W: ?int32, L: ?int32, G: ?int32, GS: ?int32, CG: ?int32, SHO: ?int32, SV: ?int32, IPouts: ?int32, H: ?int32, ER: ?int32, HR: ?int32, BB: ?int32, SO: ?int32, BAOpp: ?float64, ERA: ?float64, IBB: ?int32, WP: ?int32, HBP: ?int32, BK: ?int32, BFP: ?int32, GF: ?int32, R: ?int32, SH: ?int32, SF: ?int32, GIDP: ?int32}, managers: var * {managerID: ?string, yearID: ?int32, teamID: ?string, lgID: ?string, inseason: ?int32, G: ?int32, W: ?int32, L: ?int32, rank: ?int32, plyrMgr: ?string}, teams: var * {yearID: ?int32, lgID: ?string, teamID: ?string, franchID: ?string, divID: ?string, Rank: ?int32, G: ?int32, Ghome: ?int32, W: ?int32, L: ?int32, DivWin: ?string, WCWin: ?string, LgWin: ?string, WSWin: ?string, R: ?int32, AB: ?int32, H: ?int32, B: ?int32, B: ?int32, HR: ?int32, BB: ?int32, SO: ?int32, SB: ?int32, CS: ?int32, HBP: ?int32, SF: ?int32, RA: ?int32, ER: ?int32, ERA: ?float64, CG: ?int32, SHO: ?int32, SV: ?int32, IPouts: ?int32, HA: ?int32, HRA: ?int32, BBA: ?int32, SOA: ?int32, E: ?int32, DP: ?int32, FP: ?float64, name: ?string, park: ?string, attendance: ?int32, BPF: ?int32, PPF: ?int32, teamIDBR: ?string, teamIDlahman45: ?string, teamIDretro: ?string}}";
41
+
42
+
43
+ int
44
+ main(void)
45
+ {
46
+ ndt_context_t *ctx;
47
+ ndt_t *t;
48
+ int i;
49
+
50
+ ctx = ndt_context_new();
51
+ if (ctx == NULL) {
52
+ fprintf(stderr, "out of memory\n");
53
+ return 1;
54
+ }
55
+
56
+ if (ndt_init(ctx) < 0) {
57
+ ndt_err_fprint(stderr, ctx);
58
+ ndt_context_del(ctx);
59
+ return 1;
60
+ }
61
+
62
+ for (i = 0; i < 100000; i++) {
63
+ t = ndt_from_string(s, ctx);
64
+ if (t == NULL) {
65
+ ndt_err_fprint(stderr, ctx);
66
+ break;
67
+ }
68
+ ndt_del(t);
69
+ }
70
+
71
+ ndt_context_del(ctx);
72
+ ndt_finalize();
73
+
74
+ return 0;
75
+ }
76
+
77
+
78
+
79
+
@@ -0,0 +1,94 @@
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 <assert.h>
37
+ #include <setjmp.h>
38
+ #include "ndtypes.h"
39
+
40
+
41
+ #ifdef YYDEBUG
42
+ int yydebug = 1;
43
+ #endif
44
+
45
+ int
46
+ main(int argc, char **argv)
47
+ {
48
+ ndt_context_t *ctx;
49
+ ndt_t *t;
50
+ char *s;
51
+
52
+ if (argc != 2) {
53
+ fprintf(stderr, "usage: ./parser file\n");
54
+ return 1;
55
+ }
56
+
57
+ ctx = ndt_context_new();
58
+ if (ctx == NULL) {
59
+ fprintf(stderr, "out of memory\n");
60
+ return 1;
61
+ }
62
+
63
+ if (ndt_init(ctx) < 0) {
64
+ ndt_err_fprint(stderr, ctx);
65
+ ndt_context_del(ctx);
66
+ return 1;
67
+ }
68
+
69
+ t = ndt_from_file(argv[1], ctx);
70
+ if (t == NULL) {
71
+ ndt_err_fprint(stderr, ctx);
72
+ assert(ndt_err_occurred(ctx));
73
+ ndt_context_del(ctx);
74
+ return 1;
75
+ }
76
+ assert(!ndt_err_occurred(ctx));
77
+
78
+ s = ndt_indent(t, ctx);
79
+ ndt_del(t);
80
+ if (s == NULL) {
81
+ ndt_err_fprint(stderr, ctx);
82
+ assert(ndt_err_occurred(ctx));
83
+ ndt_context_del(ctx);
84
+ return 1;
85
+ }
86
+ assert(!ndt_err_occurred(ctx));
87
+
88
+ printf("%s\n", s);
89
+ ndt_free(s);
90
+ ndt_context_del(ctx);
91
+ ndt_finalize();
92
+
93
+ return 0;
94
+ }
@@ -0,0 +1,96 @@
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 <assert.h>
37
+ #include <setjmp.h>
38
+ #include "ndtypes.h"
39
+
40
+
41
+ #ifdef YYDEBUG
42
+ int yydebug = 1;
43
+ #endif
44
+
45
+ int
46
+ main(int argc, char **argv)
47
+ {
48
+ ndt_context_t *ctx;
49
+ ndt_t *t;
50
+ char *s;
51
+
52
+ if (argc != 2) {
53
+ fprintf(stderr, "usage: ./parser file\n");
54
+ return 1;
55
+ }
56
+
57
+ ctx = ndt_context_new();
58
+ if (ctx == NULL) {
59
+ fprintf(stderr, "out of memory\n");
60
+ return 1;
61
+ }
62
+
63
+ if (ndt_init(ctx) < 0) {
64
+ ndt_err_fprint(stderr, ctx);
65
+ ndt_context_del(ctx);
66
+ return 1;
67
+ }
68
+
69
+ t = ndt_from_file(argv[1], ctx);
70
+ if (t == NULL) {
71
+ ndt_err_fprint(stderr, ctx);
72
+ assert(ndt_err_occurred(ctx));
73
+ ndt_context_del(ctx);
74
+ ndt_finalize();
75
+ return 1;
76
+ }
77
+ assert(!ndt_err_occurred(ctx));
78
+
79
+ s = ndt_ast_repr(t, ctx);
80
+ ndt_del(t);
81
+ if (s == NULL) {
82
+ ndt_err_fprint(stderr, ctx);
83
+ assert(ndt_err_occurred(ctx));
84
+ ndt_context_del(ctx);
85
+ ndt_finalize();
86
+ return 1;
87
+ }
88
+ assert(!ndt_err_occurred(ctx));
89
+
90
+ printf("%s\n", s);
91
+ ndt_free(s);
92
+ ndt_context_del(ctx);
93
+ ndt_finalize();
94
+
95
+ return 0;
96
+ }
@@ -0,0 +1,474 @@
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 <string.h>
35
+ #include <stdarg.h>
36
+ #include <assert.h>
37
+ #include "ndtypes.h"
38
+
39
+
40
+ /*****************************************************************************/
41
+ /* String and formatting utilities */
42
+ /*****************************************************************************/
43
+
44
+ char *
45
+ ndt_strdup(const char *s, ndt_context_t *ctx)
46
+ {
47
+ size_t len = strlen(s);
48
+ char *cp;
49
+
50
+ cp = ndt_alloc_size(len+1);
51
+ if (cp == NULL) {
52
+ return ndt_memory_error(ctx);
53
+ }
54
+
55
+ memcpy(cp, s, len);
56
+ cp[len] = '\0';
57
+ return cp;
58
+ }
59
+
60
+ char *
61
+ ndt_asprintf(ndt_context_t *ctx, const char *fmt, ...)
62
+ {
63
+ va_list ap, aq;
64
+ char *s;
65
+ int n;
66
+
67
+ va_start(ap, fmt);
68
+ va_copy(aq, ap);
69
+
70
+ n = vsnprintf(NULL, 0, fmt, ap);
71
+ va_end(ap);
72
+ if (n < 0 || n == INT_MAX) {
73
+ ndt_err_format(ctx, NDT_RuntimeError, "vsprintf failed");
74
+ return NULL;
75
+ }
76
+
77
+ s = ndt_alloc_size(n+1);
78
+ if (s == NULL) {
79
+ va_end(aq);
80
+ return ndt_memory_error(ctx);
81
+ }
82
+
83
+ n = vsnprintf(s, n+1, fmt, aq);
84
+ va_end(aq);
85
+ if (n < 0) {
86
+ ndt_free(s);
87
+ return NULL;
88
+ }
89
+
90
+ return s;
91
+ }
92
+
93
+
94
+ /*****************************************************************************/
95
+ /* Type functions (unstable API) */
96
+ /*****************************************************************************/
97
+
98
+ /* Return the next type in a dimension chain. Undefined for non-dimensions. */
99
+ static const ndt_t *
100
+ next_dim(const ndt_t *a)
101
+ {
102
+ assert(a->ndim > 0);
103
+
104
+ switch (a->tag) {
105
+ case FixedDim:
106
+ return a->FixedDim.type;
107
+ case VarDim:
108
+ return a->VarDim.type;
109
+ case SymbolicDim:
110
+ return a->SymbolicDim.type;
111
+ case EllipsisDim:
112
+ return a->EllipsisDim.type;
113
+ default:
114
+ /* NOT REACHED: tags should be exhaustive. */
115
+ ndt_internal_error("invalid value");
116
+ }
117
+ }
118
+
119
+ const ndt_t *
120
+ ndt_dtype(const ndt_t *t)
121
+ {
122
+ while (t->ndim > 0) {
123
+ t = next_dim(t);
124
+ }
125
+
126
+ return t;
127
+ }
128
+
129
+ const ndt_t *
130
+ ndt_dim_at(const ndt_t *t, int n)
131
+ {
132
+ assert(n <= t->ndim);
133
+
134
+ for (int i = 0; i < n; i++) {
135
+ t = next_dim(t);
136
+ }
137
+
138
+ return t;
139
+ }
140
+
141
+ const ndt_t *
142
+ ndt_hidden_dtype(const ndt_t *t)
143
+ {
144
+ switch (t->tag) {
145
+ case Ref:
146
+ return ndt_hidden_dtype(t->Ref.type);
147
+ case Constr:
148
+ return ndt_hidden_dtype(t->Constr.type);
149
+ case Nominal:
150
+ return ndt_hidden_dtype(t->Nominal.type);
151
+ default:
152
+ return ndt_dtype(t);
153
+ }
154
+ }
155
+
156
+ int
157
+ ndt_dims_dtype(const ndt_t *dims[NDT_MAX_DIM], const ndt_t **dtype, const ndt_t *t)
158
+ {
159
+ int n = 0;
160
+
161
+ assert(t->ndim <= NDT_MAX_DIM);
162
+
163
+ while (t->ndim > 0) {
164
+ dims[n++] = t;
165
+ t = next_dim(t);
166
+ }
167
+
168
+ *dtype = t;
169
+
170
+ return n;
171
+ }
172
+
173
+ int
174
+ ndt_as_ndarray(ndt_ndarray_t *a, const ndt_t *t, ndt_context_t *ctx)
175
+ {
176
+ int n;
177
+
178
+ assert(t->ndim <= NDT_MAX_DIM);
179
+
180
+ if (ndt_is_abstract(t)) {
181
+ ndt_err_format(ctx, NDT_TypeError, "type is not an ndarray");
182
+ return -1;
183
+ }
184
+
185
+ if (!ndt_is_ndarray(t)) {
186
+ ndt_err_format(ctx, NDT_TypeError, "type is not an ndarray");
187
+ return -1;
188
+ }
189
+
190
+ if (t->ndim == 0) {
191
+ a->ndim = 0;
192
+ a->itemsize = t->datasize;
193
+ return 0;
194
+ }
195
+
196
+ a->ndim = t->ndim;
197
+ a->itemsize = t->Concrete.FixedDim.itemsize;
198
+
199
+ for (n=0; t->ndim > 0; n++, t=t->FixedDim.type) {
200
+ a->shape[n] = t->FixedDim.shape;
201
+ a->strides[n] = t->Concrete.FixedDim.step * a->itemsize;
202
+ a->steps[n] = t->Concrete.FixedDim.step;
203
+ }
204
+
205
+ return n;
206
+ }
207
+
208
+ /* Unoptimized hash function for experimenting. */
209
+ ndt_ssize_t
210
+ ndt_hash(ndt_t *t, ndt_context_t *ctx)
211
+ {
212
+ unsigned char *s, *cp;
213
+ size_t len;
214
+ ndt_ssize_t x;
215
+
216
+ cp = s = (unsigned char *)ndt_as_string(t, ctx);
217
+ if (s == NULL) {
218
+ return -1;
219
+ }
220
+
221
+ len = strlen((char *)s);
222
+
223
+ x = *cp << 7;
224
+ while (*cp != '\0') {
225
+ x = (1000003 * x) ^ *cp++;
226
+ }
227
+ x ^= len;
228
+
229
+ if (x == -1) {
230
+ x = -2;
231
+ }
232
+
233
+ ndt_free(s);
234
+
235
+ return x;
236
+ }
237
+
238
+
239
+ /*****************************************************************************/
240
+ /* Apply spec (unstable API) */
241
+ /*****************************************************************************/
242
+
243
+ const ndt_apply_spec_t ndt_apply_spec_empty = {
244
+ .flags = 0U,
245
+ .nout = 0,
246
+ .nbroadcast = 0,
247
+ .outer_dims = 0,
248
+ .out = {NULL},
249
+ .broadcast = {NULL}
250
+ };
251
+
252
+ ndt_apply_spec_t *
253
+ ndt_apply_spec_new(ndt_context_t *ctx)
254
+ {
255
+ ndt_apply_spec_t *spec;
256
+
257
+ spec = ndt_calloc(1, sizeof *spec);
258
+ if (spec == NULL) {
259
+ return ndt_memory_error(ctx);
260
+ }
261
+ spec->flags = 0U;
262
+ spec->nout = 0;
263
+ spec->nbroadcast = 0;
264
+ spec->outer_dims = 0;
265
+
266
+ return spec;
267
+ }
268
+
269
+ void
270
+ ndt_apply_spec_clear(ndt_apply_spec_t *spec)
271
+ {
272
+ int i;
273
+
274
+ if (spec == NULL) {
275
+ return;
276
+ }
277
+
278
+ for (i = 0; i < spec->nbroadcast; i++) {
279
+ ndt_del(spec->broadcast[i]);
280
+ }
281
+
282
+ for (i = 0; i < spec->nout; i++) {
283
+ ndt_del(spec->out[i]);
284
+ }
285
+
286
+ spec->flags = 0U;
287
+ spec->nout = 0;
288
+ spec->nbroadcast = 0;
289
+ spec->outer_dims = 0;
290
+ }
291
+
292
+ void
293
+ ndt_apply_spec_del(ndt_apply_spec_t *spec)
294
+ {
295
+ if (spec == NULL) {
296
+ return;
297
+ }
298
+
299
+ ndt_apply_spec_clear(spec);
300
+ ndt_free(spec);
301
+ }
302
+
303
+ /* This function is used in places where it is _really_ convenient not to
304
+ have to deal with deallocating an error message. */
305
+ const char *
306
+ ndt_apply_flags_as_string(const ndt_apply_spec_t *spec)
307
+ {
308
+ switch (spec->flags) {
309
+ case 0: return "None";
310
+ case NDT_C: return "C";
311
+ case NDT_FORTRAN: return "Fortran";
312
+ case NDT_FORTRAN|NDT_C: return "C|Fortran";
313
+ case NDT_STRIDED: return "Strided";
314
+ case NDT_STRIDED|NDT_C: return "C|Strided";
315
+ case NDT_STRIDED|NDT_FORTRAN: return "Fortran|Strided";
316
+ case NDT_STRIDED|NDT_FORTRAN|NDT_C: return "C|Fortran|Strided";
317
+ case NDT_XND: return "Xnd";
318
+ case NDT_XND|NDT_C: return "C|Xnd";
319
+ case NDT_XND|NDT_FORTRAN: return "Fortran|Xnd";
320
+ case NDT_XND|NDT_FORTRAN|NDT_C: return "C|Fortran|Xnd";
321
+ case NDT_XND|NDT_STRIDED: return "Strided|Xnd";
322
+ case NDT_XND|NDT_STRIDED|NDT_C: return "C|Strided|Xnd";
323
+ case NDT_XND|NDT_STRIDED|NDT_FORTRAN: return "Fortran|Strided|Xnd";
324
+ case NDT_XND|NDT_STRIDED|NDT_FORTRAN|NDT_C: return "C|Fortran|Strided|Xnd";
325
+ case NDT_ELEMWISE_1D: return "Elemwise1D";
326
+ case NDT_ELEMWISE_1D|NDT_C: return "Elemwise1D|C";
327
+ case NDT_ELEMWISE_1D|NDT_FORTRAN: return "Elemwise1D|Fortran";
328
+ case NDT_ELEMWISE_1D|NDT_FORTRAN|NDT_C: return "Elemwise1D|C|Fortran";
329
+ case NDT_ELEMWISE_1D|NDT_STRIDED: return "Elemwise1D|Strided";
330
+ case NDT_ELEMWISE_1D|NDT_STRIDED|NDT_C: return "Elemwise1D|C|Strided";
331
+ case NDT_ELEMWISE_1D|NDT_STRIDED|NDT_FORTRAN: return "Elemwise1D|Fortran|Strided";
332
+ case NDT_ELEMWISE_1D|NDT_STRIDED|NDT_FORTRAN|NDT_C: return "Elemwise1D|C|Fortran|Strided";
333
+ case NDT_ELEMWISE_1D|NDT_XND: return "Elemwise1D|Xnd";
334
+ case NDT_ELEMWISE_1D|NDT_XND|NDT_C: return "Elemwise1D|C|Xnd";
335
+ case NDT_ELEMWISE_1D|NDT_XND|NDT_FORTRAN: return "Elemwise1D|Fortran|Xnd";
336
+ case NDT_ELEMWISE_1D|NDT_XND|NDT_FORTRAN|NDT_C: return "Elemwise1D|C|Fortran|Xnd";
337
+ case NDT_ELEMWISE_1D|NDT_XND|NDT_STRIDED: return "Elemwise1D|Strided|Xnd";
338
+ case NDT_ELEMWISE_1D|NDT_XND|NDT_STRIDED|NDT_C: return "Elemwise1D|C|Strided|Xnd";
339
+ case NDT_ELEMWISE_1D|NDT_XND|NDT_STRIDED|NDT_FORTRAN: return "Elemwise1D|Fortran|Strided|Xnd";
340
+ case NDT_ELEMWISE_1D|NDT_XND|NDT_STRIDED|NDT_FORTRAN|NDT_C: return "Elemwise1D|C|Fortran|Strided|Xnd";
341
+ default: return "error: invalid combination of spec->flags";
342
+ }
343
+ }
344
+
345
+
346
+ /*****************************************************************************/
347
+ /* Metadata (unstable API) */
348
+ /*****************************************************************************/
349
+
350
+ ndt_meta_t *
351
+ ndt_meta_new(ndt_context_t *ctx)
352
+ {
353
+ ndt_meta_t *m;
354
+
355
+ m = ndt_calloc(1, sizeof *m);
356
+ if (m == NULL) {
357
+ ndt_memory_error(ctx);
358
+ }
359
+
360
+ m->ndims = 0;
361
+ return m;
362
+ }
363
+
364
+ void
365
+ ndt_meta_del(ndt_meta_t *m)
366
+ {
367
+ if (m == NULL) {
368
+ return;
369
+ }
370
+
371
+ for (int i = 0; i < m->ndims; i++) {
372
+ ndt_free(m->offsets[i]);
373
+ }
374
+
375
+ ndt_free(m);
376
+ }
377
+
378
+
379
+ /*****************************************************************************/
380
+ /* Optimized kernel strategy (unstable API) */
381
+ /*****************************************************************************/
382
+
383
+ static bool
384
+ all_inner_1D_contiguous(const ndt_t *types[], int n)
385
+ {
386
+ for (int i = 0; i < n; i++) {
387
+ if (types[i]->ndim == 0) {
388
+ return false;
389
+ }
390
+ if (!ndt_is_c_contiguous(ndt_dim_at(types[i], types[i]->ndim-1))) {
391
+ return false;
392
+ }
393
+ }
394
+
395
+ return true;
396
+ }
397
+
398
+ static bool
399
+ all_inner_c_contiguous(const ndt_t *types[], int n, int outer)
400
+ {
401
+ for (int i = 0; i < n; i++) {
402
+ if (!ndt_is_c_contiguous(ndt_dim_at(types[i], outer))) {
403
+ return false;
404
+ }
405
+ }
406
+
407
+ return true;
408
+ }
409
+
410
+ static bool
411
+ all_inner_f_contiguous(const ndt_t *types[], int n, int outer)
412
+ {
413
+ for (int i = 0; i < n; i++) {
414
+ if (!ndt_is_f_contiguous(ndt_dim_at(types[i], outer))) {
415
+ return false;
416
+ }
417
+ }
418
+
419
+ return true;
420
+ }
421
+
422
+ static bool
423
+ all_ndarray(const ndt_t *types[], int n)
424
+ {
425
+ for (int i = 0; i < n; i++) {
426
+ if (!ndt_is_ndarray(types[i])) {
427
+ return false;
428
+ }
429
+ if (ndt_subtree_is_optional(types[i])) {
430
+ return false;
431
+ }
432
+ }
433
+
434
+ return true;
435
+ }
436
+
437
+ void
438
+ ndt_select_kernel_strategy(ndt_apply_spec_t *spec, const ndt_t *sig,
439
+ const ndt_t *in[], int nin)
440
+ {
441
+ const ndt_t **out = (const ndt_t **)spec->out;
442
+ bool in_inner_c, in_inner_f, out_inner_c;
443
+
444
+ assert(sig->tag == Function);
445
+ assert(spec->flags == 0);
446
+
447
+ if (spec->nbroadcast > 0) {
448
+ in = (const ndt_t **)spec->broadcast;
449
+ nin = spec->nbroadcast;
450
+ }
451
+
452
+ in_inner_c = all_inner_c_contiguous(in, nin, spec->outer_dims);
453
+ in_inner_f = all_inner_f_contiguous(in, nin, spec->outer_dims);
454
+ out_inner_c = all_inner_c_contiguous(out, spec->nout, spec->outer_dims);
455
+
456
+ spec->flags = NDT_XND;
457
+
458
+ if (sig->Function.elemwise) {
459
+ if (all_inner_1D_contiguous(in, nin) &&
460
+ all_inner_1D_contiguous(out, spec->nout)) {
461
+ spec->flags |= NDT_ELEMWISE_1D;
462
+ }
463
+ }
464
+ if (in_inner_c && out_inner_c) {
465
+ spec->flags |= NDT_C;
466
+ }
467
+ if (in_inner_f && out_inner_c) {
468
+ spec->flags |= NDT_FORTRAN;
469
+ }
470
+
471
+ if (all_ndarray(in, nin) && all_ndarray(out, spec->nout)) {
472
+ spec->flags |= NDT_STRIDED;
473
+ }
474
+ }