gogyou 0.1.240911.prototype → 0.2

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.
@@ -0,0 +1,317 @@
1
+ #--
2
+ # this code is generated by mkprims.rb
3
+ #++
4
+
5
+ require_relative "typespec"
6
+
7
+ module Gogyou
8
+ module Primitives
9
+ class Primitive < ::Struct.new(:name, :bytesize, :bytealign)
10
+ BasicStruct = superclass
11
+
12
+ undef :name=, :bytesize=, :bytealign=
13
+
14
+ def initialize(name, bytesize, bytealign, aset, aref)
15
+ super(name.intern, bytesize.to_i, bytealign.to_i)
16
+ define_singleton_method(:aset, aset)
17
+ define_singleton_method(:aref, aref)
18
+ end
19
+
20
+ def extensible?
21
+ false
22
+ end
23
+
24
+ def to_s
25
+ "#{self.class}[name=#{name.inspect}, bytesize=#{bytesize.inspect}, bytealign=#{bytealign.inspect}]"
26
+ end
27
+
28
+ alias inspect to_s
29
+
30
+ def pretty_print(q)
31
+ #name, bytesize, bytealign
32
+ q.group(1, "#{self.class}[") do
33
+ q.text "name="
34
+ q.pp name
35
+ q.text ", "
36
+ #q.breakable
37
+ q.text "bytesize="
38
+ q.pp bytesize
39
+ q.text ", "
40
+ #q.breakable
41
+ q.text "bytealign="
42
+ q.pp bytealign
43
+ end
44
+ q.text "]"
45
+ end
46
+ end
47
+
48
+ SIZE_T = Primitive[:size_t, TypeSpec::SIZEOF_SIZE_T, TypeSpec::SIZEOF_SIZE_T,
49
+ ->(buf, offset, num) { buf.store_sizet(offset, num) },
50
+ ->(buf, offset) { buf.load_sizet(offset) }].freeze
51
+ SSIZE_T = Primitive[:ssize_t, TypeSpec::SIZEOF_SIZE_T, TypeSpec::SIZEOF_SIZE_T,
52
+ ->(buf, offset, num) { buf.store_sizet(offset, num) },
53
+ ->(buf, offset) { buf.load_ssizet(offset) }].freeze
54
+ INTPTR_T = Primitive[:intptr_t, TypeSpec::SIZEOF_SIZE_T, TypeSpec::SIZEOF_SIZE_T,
55
+ ->(buf, offset, num) { buf.store_sizet(offset, num) },
56
+ ->(buf, offset) { buf.load_ssizet(offset) }].freeze
57
+ UINTPTR_T = Primitive[:uintptr_t, TypeSpec::SIZEOF_SIZE_T, TypeSpec::SIZEOF_SIZE_T,
58
+ ->(buf, offset, num) { buf.store_sizet(offset, num) },
59
+ ->(buf, offset) { buf.load_sizet(offset) }].freeze
60
+ INT8_T = Primitive[:int8_t, 1, 1,
61
+ ->(buf, offset, num) { buf.store8(offset, num) },
62
+ ->(buf, offset) { buf.loadi8(offset) }].freeze
63
+ UINT8_T = Primitive[:uint8_t, 1, 1,
64
+ ->(buf, offset, num) { buf.store8(offset, num) },
65
+ ->(buf, offset) { buf.loadu8(offset) }].freeze
66
+ INT16_T = Primitive[:int16_t, 2, 2,
67
+ ->(buf, offset, num) { buf.store16(offset, num) },
68
+ ->(buf, offset) { buf.loadi16(offset) }].freeze
69
+ UINT16_T = Primitive[:uint16_t, 2, 2,
70
+ ->(buf, offset, num) { buf.store16(offset, num) },
71
+ ->(buf, offset) { buf.loadu16(offset) }].freeze
72
+ INT24_T = Primitive[:int24_t, 3, 1,
73
+ ->(buf, offset, num) { buf.store24(offset, num) },
74
+ ->(buf, offset) { buf.loadi24(offset) }].freeze
75
+ UINT24_T = Primitive[:uint24_t, 3, 1,
76
+ ->(buf, offset, num) { buf.store24(offset, num) },
77
+ ->(buf, offset) { buf.loadu24(offset) }].freeze
78
+ INT32_T = Primitive[:int32_t, 4, 4,
79
+ ->(buf, offset, num) { buf.store32(offset, num) },
80
+ ->(buf, offset) { buf.loadi32(offset) }].freeze
81
+ UINT32_T = Primitive[:uint32_t, 4, 4,
82
+ ->(buf, offset, num) { buf.store32(offset, num) },
83
+ ->(buf, offset) { buf.loadu32(offset) }].freeze
84
+ INT48_T = Primitive[:int48_t, 6, 2,
85
+ ->(buf, offset, num) { buf.store48(offset, num) },
86
+ ->(buf, offset) { buf.loadi48(offset) }].freeze
87
+ UINT48_T = Primitive[:uint48_t, 6, 2,
88
+ ->(buf, offset, num) { buf.store48(offset, num) },
89
+ ->(buf, offset) { buf.loadu48(offset) }].freeze
90
+ INT64_T = Primitive[:int64_t, 8, 8,
91
+ ->(buf, offset, num) { buf.store64(offset, num) },
92
+ ->(buf, offset) { buf.loadi64(offset) }].freeze
93
+ UINT64_T = Primitive[:uint64_t, 8, 8,
94
+ ->(buf, offset, num) { buf.store64(offset, num) },
95
+ ->(buf, offset) { buf.loadu64(offset) }].freeze
96
+ INT16_BE = Primitive[:int16_be, 2, 2,
97
+ ->(buf, offset, num) { buf.store16be(offset, num) },
98
+ ->(buf, offset) { buf.loadi16be(offset) }].freeze
99
+ UINT16_BE = Primitive[:uint16_be, 2, 2,
100
+ ->(buf, offset, num) { buf.store16be(offset, num) },
101
+ ->(buf, offset) { buf.loadu16be(offset) }].freeze
102
+ INT24_BE = Primitive[:int24_be, 3, 1,
103
+ ->(buf, offset, num) { buf.store24be(offset, num) },
104
+ ->(buf, offset) { buf.loadi24be(offset) }].freeze
105
+ UINT24_BE = Primitive[:uint24_be, 3, 1,
106
+ ->(buf, offset, num) { buf.store24be(offset, num) },
107
+ ->(buf, offset) { buf.loadu24be(offset) }].freeze
108
+ INT32_BE = Primitive[:int32_be, 4, 4,
109
+ ->(buf, offset, num) { buf.store32be(offset, num) },
110
+ ->(buf, offset) { buf.loadi32be(offset) }].freeze
111
+ UINT32_BE = Primitive[:uint32_be, 4, 4,
112
+ ->(buf, offset, num) { buf.store32be(offset, num) },
113
+ ->(buf, offset) { buf.loadu32be(offset) }].freeze
114
+ INT48_BE = Primitive[:int48_be, 6, 2,
115
+ ->(buf, offset, num) { buf.store48be(offset, num) },
116
+ ->(buf, offset) { buf.loadi48be(offset) }].freeze
117
+ UINT48_BE = Primitive[:uint48_be, 6, 2,
118
+ ->(buf, offset, num) { buf.store48be(offset, num) },
119
+ ->(buf, offset) { buf.loadu48be(offset) }].freeze
120
+ INT64_BE = Primitive[:int64_be, 8, 8,
121
+ ->(buf, offset, num) { buf.store64be(offset, num) },
122
+ ->(buf, offset) { buf.loadi64be(offset) }].freeze
123
+ UINT64_BE = Primitive[:uint64_be, 8, 8,
124
+ ->(buf, offset, num) { buf.store64be(offset, num) },
125
+ ->(buf, offset) { buf.loadu64be(offset) }].freeze
126
+ INT16_LE = Primitive[:int16_le, 2, 2,
127
+ ->(buf, offset, num) { buf.store16le(offset, num) },
128
+ ->(buf, offset) { buf.loadi16le(offset) }].freeze
129
+ UINT16_LE = Primitive[:uint16_le, 2, 2,
130
+ ->(buf, offset, num) { buf.store16le(offset, num) },
131
+ ->(buf, offset) { buf.loadu16le(offset) }].freeze
132
+ INT24_LE = Primitive[:int24_le, 3, 1,
133
+ ->(buf, offset, num) { buf.store24le(offset, num) },
134
+ ->(buf, offset) { buf.loadi24le(offset) }].freeze
135
+ UINT24_LE = Primitive[:uint24_le, 3, 1,
136
+ ->(buf, offset, num) { buf.store24le(offset, num) },
137
+ ->(buf, offset) { buf.loadu24le(offset) }].freeze
138
+ INT32_LE = Primitive[:int32_le, 4, 4,
139
+ ->(buf, offset, num) { buf.store32le(offset, num) },
140
+ ->(buf, offset) { buf.loadi32le(offset) }].freeze
141
+ UINT32_LE = Primitive[:uint32_le, 4, 4,
142
+ ->(buf, offset, num) { buf.store32le(offset, num) },
143
+ ->(buf, offset) { buf.loadu32le(offset) }].freeze
144
+ INT48_LE = Primitive[:int48_le, 6, 2,
145
+ ->(buf, offset, num) { buf.store48le(offset, num) },
146
+ ->(buf, offset) { buf.loadi48le(offset) }].freeze
147
+ UINT48_LE = Primitive[:uint48_le, 6, 2,
148
+ ->(buf, offset, num) { buf.store48le(offset, num) },
149
+ ->(buf, offset) { buf.loadu48le(offset) }].freeze
150
+ INT64_LE = Primitive[:int64_le, 8, 8,
151
+ ->(buf, offset, num) { buf.store64le(offset, num) },
152
+ ->(buf, offset) { buf.loadi64le(offset) }].freeze
153
+ UINT64_LE = Primitive[:uint64_le, 8, 8,
154
+ ->(buf, offset, num) { buf.store64le(offset, num) },
155
+ ->(buf, offset) { buf.loadu64le(offset) }].freeze
156
+ INT16_SWAP = Primitive[:int16_swap, 2, 2,
157
+ ->(buf, offset, num) { buf.store16swap(offset, num) },
158
+ ->(buf, offset) { buf.loadi16swap(offset) }].freeze
159
+ UINT16_SWAP = Primitive[:uint16_swap, 2, 2,
160
+ ->(buf, offset, num) { buf.store16swap(offset, num) },
161
+ ->(buf, offset) { buf.loadu16swap(offset) }].freeze
162
+ INT24_SWAP = Primitive[:int24_swap, 3, 1,
163
+ ->(buf, offset, num) { buf.store24swap(offset, num) },
164
+ ->(buf, offset) { buf.loadi24swap(offset) }].freeze
165
+ UINT24_SWAP = Primitive[:uint24_swap, 3, 1,
166
+ ->(buf, offset, num) { buf.store24swap(offset, num) },
167
+ ->(buf, offset) { buf.loadu24swap(offset) }].freeze
168
+ INT32_SWAP = Primitive[:int32_swap, 4, 4,
169
+ ->(buf, offset, num) { buf.store32swap(offset, num) },
170
+ ->(buf, offset) { buf.loadi32swap(offset) }].freeze
171
+ UINT32_SWAP = Primitive[:uint32_swap, 4, 4,
172
+ ->(buf, offset, num) { buf.store32swap(offset, num) },
173
+ ->(buf, offset) { buf.loadu32swap(offset) }].freeze
174
+ INT48_SWAP = Primitive[:int48_swap, 6, 2,
175
+ ->(buf, offset, num) { buf.store48swap(offset, num) },
176
+ ->(buf, offset) { buf.loadi48swap(offset) }].freeze
177
+ UINT48_SWAP = Primitive[:uint48_swap, 6, 2,
178
+ ->(buf, offset, num) { buf.store48swap(offset, num) },
179
+ ->(buf, offset) { buf.loadu48swap(offset) }].freeze
180
+ INT64_SWAP = Primitive[:int64_swap, 8, 8,
181
+ ->(buf, offset, num) { buf.store64swap(offset, num) },
182
+ ->(buf, offset) { buf.loadi64swap(offset) }].freeze
183
+ UINT64_SWAP = Primitive[:uint64_swap, 8, 8,
184
+ ->(buf, offset, num) { buf.store64swap(offset, num) },
185
+ ->(buf, offset) { buf.loadu64swap(offset) }].freeze
186
+ CHAR = Primitive[:char, 1, 1,
187
+ ->(buf, offset, num) { buf.store8(offset, num) },
188
+ ->(buf, offset) { buf.loadi8(offset) }].freeze
189
+ UCHAR = Primitive[:uchar, 1, 1,
190
+ ->(buf, offset, num) { buf.store8(offset, num) },
191
+ ->(buf, offset) { buf.loadu8(offset) }].freeze
192
+ SHORT = Primitive[:short, 2, 2,
193
+ ->(buf, offset, num) { buf.store16(offset, num) },
194
+ ->(buf, offset) { buf.loadi16(offset) }].freeze
195
+ USHORT = Primitive[:ushort, 2, 2,
196
+ ->(buf, offset, num) { buf.store16(offset, num) },
197
+ ->(buf, offset) { buf.loadu16(offset) }].freeze
198
+ INT = Primitive[:int, 4, 4,
199
+ ->(buf, offset, num) { buf.store32(offset, num) },
200
+ ->(buf, offset) { buf.loadi32(offset) }].freeze
201
+ UINT = Primitive[:uint, 4, 4,
202
+ ->(buf, offset, num) { buf.store32(offset, num) },
203
+ ->(buf, offset) { buf.loadu32(offset) }].freeze
204
+ LONG = Primitive[:long, TypeSpec::SIZEOF_LONG, TypeSpec::SIZEOF_LONG,
205
+ ->(buf, offset, num) { buf.store_long(offset, num) },
206
+ ->(buf, offset) { buf.load_long(offset) }].freeze
207
+ ULONG = Primitive[:ulong, TypeSpec::SIZEOF_LONG, TypeSpec::SIZEOF_LONG,
208
+ ->(buf, offset, num) { buf.store_long(offset, num) },
209
+ ->(buf, offset) { buf.load_ulong(offset) }].freeze
210
+ LONGLONG = Primitive[:longlong, 8, 8,
211
+ ->(buf, offset, num) { buf.store64(offset, num) },
212
+ ->(buf, offset) { buf.loadi64(offset) }].freeze
213
+ ULONGLONG = Primitive[:ulonglong, 8, 8,
214
+ ->(buf, offset, num) { buf.store64(offset, num) },
215
+ ->(buf, offset) { buf.loadu64(offset) }].freeze
216
+ FLOAT = Primitive[:float, 4, 4,
217
+ ->(buf, offset, num) { buf.storef32(offset, num) },
218
+ ->(buf, offset) { buf.loadf32(offset) }].freeze
219
+ DOUBLE = Primitive[:double, 8, 8,
220
+ ->(buf, offset, num) { buf.storef64(offset, num) },
221
+ ->(buf, offset) { buf.loadf64(offset) }].freeze
222
+ FLOAT_BE = Primitive[:float_be, 4, 4,
223
+ ->(buf, offset, num) { buf.storef32be(offset, num) },
224
+ ->(buf, offset) { buf.loadf32be(offset) }].freeze
225
+ DOUBLE_BE = Primitive[:double_be, 8, 8,
226
+ ->(buf, offset, num) { buf.storef64be(offset, num) },
227
+ ->(buf, offset) { buf.loadf64be(offset) }].freeze
228
+ FLOAT_LE = Primitive[:float_le, 4, 4,
229
+ ->(buf, offset, num) { buf.storef32le(offset, num) },
230
+ ->(buf, offset) { buf.loadf32le(offset) }].freeze
231
+ DOUBLE_LE = Primitive[:double_le, 8, 8,
232
+ ->(buf, offset, num) { buf.storef64le(offset, num) },
233
+ ->(buf, offset) { buf.loadf64le(offset) }].freeze
234
+ FLOAT_SWAP = Primitive[:float_swap, 4, 4,
235
+ ->(buf, offset, num) { buf.storef32swap(offset, num) },
236
+ ->(buf, offset) { buf.loadf32swap(offset) }].freeze
237
+ DOUBLE_SWAP = Primitive[:double_swap, 8, 8,
238
+ ->(buf, offset, num) { buf.storef64swap(offset, num) },
239
+ ->(buf, offset) { buf.loadf64swap(offset) }].freeze
240
+ end
241
+
242
+ class Model
243
+ TYPEMAP = {
244
+ size_t: Primitives::SIZE_T,
245
+ ssize_t: Primitives::SSIZE_T,
246
+ intptr_t: Primitives::INTPTR_T,
247
+ uintptr_t: Primitives::UINTPTR_T,
248
+ int8_t: Primitives::INT8_T,
249
+ uint8_t: Primitives::UINT8_T,
250
+ int16_t: Primitives::INT16_T,
251
+ uint16_t: Primitives::UINT16_T,
252
+ int24_t: Primitives::INT24_T,
253
+ uint24_t: Primitives::UINT24_T,
254
+ int32_t: Primitives::INT32_T,
255
+ uint32_t: Primitives::UINT32_T,
256
+ int48_t: Primitives::INT48_T,
257
+ uint48_t: Primitives::UINT48_T,
258
+ int64_t: Primitives::INT64_T,
259
+ uint64_t: Primitives::UINT64_T,
260
+ int16_be: Primitives::INT16_BE,
261
+ uint16_be: Primitives::UINT16_BE,
262
+ int24_be: Primitives::INT24_BE,
263
+ uint24_be: Primitives::UINT24_BE,
264
+ int32_be: Primitives::INT32_BE,
265
+ uint32_be: Primitives::UINT32_BE,
266
+ int48_be: Primitives::INT48_BE,
267
+ uint48_be: Primitives::UINT48_BE,
268
+ int64_be: Primitives::INT64_BE,
269
+ uint64_be: Primitives::UINT64_BE,
270
+ int16_le: Primitives::INT16_LE,
271
+ uint16_le: Primitives::UINT16_LE,
272
+ int24_le: Primitives::INT24_LE,
273
+ uint24_le: Primitives::UINT24_LE,
274
+ int32_le: Primitives::INT32_LE,
275
+ uint32_le: Primitives::UINT32_LE,
276
+ int48_le: Primitives::INT48_LE,
277
+ uint48_le: Primitives::UINT48_LE,
278
+ int64_le: Primitives::INT64_LE,
279
+ uint64_le: Primitives::UINT64_LE,
280
+ int16_swap: Primitives::INT16_SWAP,
281
+ uint16_swap: Primitives::UINT16_SWAP,
282
+ int24_swap: Primitives::INT24_SWAP,
283
+ uint24_swap: Primitives::UINT24_SWAP,
284
+ int32_swap: Primitives::INT32_SWAP,
285
+ uint32_swap: Primitives::UINT32_SWAP,
286
+ int48_swap: Primitives::INT48_SWAP,
287
+ uint48_swap: Primitives::UINT48_SWAP,
288
+ int64_swap: Primitives::INT64_SWAP,
289
+ uint64_swap: Primitives::UINT64_SWAP,
290
+ char: Primitives::CHAR,
291
+ uchar: Primitives::UCHAR,
292
+ short: Primitives::SHORT,
293
+ ushort: Primitives::USHORT,
294
+ int: Primitives::INT,
295
+ uint: Primitives::UINT,
296
+ long: Primitives::LONG,
297
+ ulong: Primitives::ULONG,
298
+ longlong: Primitives::LONGLONG,
299
+ ulonglong: Primitives::ULONGLONG,
300
+ float: Primitives::FLOAT,
301
+ double: Primitives::DOUBLE,
302
+ float_be: Primitives::FLOAT_BE,
303
+ double_be: Primitives::DOUBLE_BE,
304
+ float_le: Primitives::FLOAT_LE,
305
+ double_le: Primitives::DOUBLE_LE,
306
+ float_swap: Primitives::FLOAT_SWAP,
307
+ double_swap: Primitives::DOUBLE_SWAP,
308
+ }
309
+
310
+ TYPEMAP[:unsigned_char] = TYPEMAP[:uchar]
311
+ TYPEMAP[:unsigned_short] = TYPEMAP[:ushort]
312
+ TYPEMAP[:unsigned_int] = TYPEMAP[:uint]
313
+ TYPEMAP[:unsigned_long] = TYPEMAP[:ulong]
314
+ TYPEMAP[:unsigned_long_long] = TYPEMAP[:ulonglong]
315
+ TYPEMAP[:long_long] = TYPEMAP[:longlong]
316
+ end
317
+ end
@@ -0,0 +1,15 @@
1
+ module Gogyou
2
+ module TypeSpec
3
+ SIZEOF_CHAR = [0].pack("C").bytesize
4
+ SIZEOF_SHORT = [0].pack("S").bytesize
5
+ SIZEOF_INT = [0].pack("I").bytesize
6
+ SIZEOF_LONG = [0].pack("L").bytesize
7
+ SIZEOF_LONGLONG = [0].pack("Q").bytesize
8
+ SIZEOF_SIZE_T = [nil].pack("P").bytesize
9
+ SIZEOF_FLOAT = [0].pack("F").bytesize
10
+ SIZEOF_DOUBLE = [0].pack("D").bytesize
11
+ SIZEOF_INTPTR_T = SIZEOF_UINTPTR_T = SIZEOF_SIZE_T
12
+ end
13
+
14
+ include TypeSpec
15
+ end
data/mkprims.rb ADDED
@@ -0,0 +1,172 @@
1
+ #!/bin/env ruby
2
+
3
+ path = File.join(File.dirname(__FILE__), "lib", "gogyou", "primitives.rb")
4
+
5
+ # type name, byte bytesize, alignment bytesize, store method, load method
6
+ records = <<EOS
7
+ size_t TypeSpec::SIZEOF_SIZE_T TypeSpec::SIZEOF_SIZE_T store_sizet load_sizet
8
+ ssize_t TypeSpec::SIZEOF_SIZE_T TypeSpec::SIZEOF_SIZE_T store_sizet load_ssizet
9
+ intptr_t TypeSpec::SIZEOF_SIZE_T TypeSpec::SIZEOF_SIZE_T store_sizet load_ssizet
10
+ uintptr_t TypeSpec::SIZEOF_SIZE_T TypeSpec::SIZEOF_SIZE_T store_sizet load_sizet
11
+ int8_t 1 1 store8 loadi8
12
+ uint8_t 1 1 store8 loadu8
13
+ int16_t 2 2 store16 loadi16
14
+ uint16_t 2 2 store16 loadu16
15
+ int24_t 3 1 store24 loadi24
16
+ uint24_t 3 1 store24 loadu24
17
+ int32_t 4 4 store32 loadi32
18
+ uint32_t 4 4 store32 loadu32
19
+ int48_t 6 2 store48 loadi48
20
+ uint48_t 6 2 store48 loadu48
21
+ int64_t 8 8 store64 loadi64
22
+ uint64_t 8 8 store64 loadu64
23
+ int16_be 2 2 store16be loadi16be
24
+ uint16_be 2 2 store16be loadu16be
25
+ int24_be 3 1 store24be loadi24be
26
+ uint24_be 3 1 store24be loadu24be
27
+ int32_be 4 4 store32be loadi32be
28
+ uint32_be 4 4 store32be loadu32be
29
+ int48_be 6 2 store48be loadi48be
30
+ uint48_be 6 2 store48be loadu48be
31
+ int64_be 8 8 store64be loadi64be
32
+ uint64_be 8 8 store64be loadu64be
33
+ int16_le 2 2 store16le loadi16le
34
+ uint16_le 2 2 store16le loadu16le
35
+ int24_le 3 1 store24le loadi24le
36
+ uint24_le 3 1 store24le loadu24le
37
+ int32_le 4 4 store32le loadi32le
38
+ uint32_le 4 4 store32le loadu32le
39
+ int48_le 6 2 store48le loadi48le
40
+ uint48_le 6 2 store48le loadu48le
41
+ int64_le 8 8 store64le loadi64le
42
+ uint64_le 8 8 store64le loadu64le
43
+ int16_swap 2 2 store16swap loadi16swap
44
+ uint16_swap 2 2 store16swap loadu16swap
45
+ int24_swap 3 1 store24swap loadi24swap
46
+ uint24_swap 3 1 store24swap loadu24swap
47
+ int32_swap 4 4 store32swap loadi32swap
48
+ uint32_swap 4 4 store32swap loadu32swap
49
+ int48_swap 6 2 store48swap loadi48swap
50
+ uint48_swap 6 2 store48swap loadu48swap
51
+ int64_swap 8 8 store64swap loadi64swap
52
+ uint64_swap 8 8 store64swap loadu64swap
53
+ char 1 1 store8 loadi8
54
+ uchar 1 1 store8 loadu8
55
+ short 2 2 store16 loadi16
56
+ ushort 2 2 store16 loadu16
57
+ int 4 4 store32 loadi32
58
+ uint 4 4 store32 loadu32
59
+ long TypeSpec::SIZEOF_LONG TypeSpec::SIZEOF_LONG store_long load_long
60
+ ulong TypeSpec::SIZEOF_LONG TypeSpec::SIZEOF_LONG store_long load_ulong
61
+ longlong 8 8 store64 loadi64
62
+ ulonglong 8 8 store64 loadu64
63
+ float 4 4 storef32 loadf32
64
+ double 8 8 storef64 loadf64
65
+ float_be 4 4 storef32be loadf32be
66
+ double_be 8 8 storef64be loadf64be
67
+ float_le 4 4 storef32le loadf32le
68
+ double_le 8 8 storef64le loadf64le
69
+ float_swap 4 4 storef32swap loadf32swap
70
+ double_swap 8 8 storef64swap loadf64swap
71
+ EOS
72
+
73
+ File.open(path, "wb") do |f|
74
+ f.puts <<-EOS
75
+ #--
76
+ # this code is generated by #{File.basename __FILE__}
77
+ #++
78
+
79
+ require_relative "typespec"
80
+
81
+ module Gogyou
82
+ module Primitives
83
+ class Primitive < ::Struct.new(:name, :bytesize, :bytealign)
84
+ BasicStruct = superclass
85
+
86
+ undef :name=, :bytesize=, :bytealign=
87
+
88
+ def initialize(name, bytesize, bytealign, aset, aref)
89
+ super(name.intern, bytesize.to_i, bytealign.to_i)
90
+ define_singleton_method(:aset, aset)
91
+ define_singleton_method(:aref, aref)
92
+ end
93
+
94
+ def extensible?
95
+ false
96
+ end
97
+
98
+ def to_s
99
+ "\#{self.class}[name=\#{name.inspect}, bytesize=\#{bytesize.inspect}, bytealign=\#{bytealign.inspect}]"
100
+ end
101
+
102
+ alias inspect to_s
103
+
104
+ def pretty_print(q)
105
+ #name, bytesize, bytealign
106
+ q.group(1, "\#{self.class}[") do
107
+ q.text "name="
108
+ q.pp name
109
+ q.text ", "
110
+ #q.breakable
111
+ q.text "bytesize="
112
+ q.pp bytesize
113
+ q.text ", "
114
+ #q.breakable
115
+ q.text "bytealign="
116
+ q.pp bytealign
117
+ end
118
+ q.text "]"
119
+ end
120
+ end
121
+
122
+ EOS
123
+
124
+ records = records.split(/\n/)
125
+ records.map! { |r| r.split(/\s+/) }
126
+
127
+ records.each do |typename, bytesize, bytealign, aset, aref|
128
+ name = typename.upcase
129
+ f.puts <<-EOS
130
+ #{name.ljust(9)} = Primitive[#{typename.intern.inspect}, #{bytesize}, #{bytealign},
131
+ ->(buf, offset, num) { buf.#{aset}(offset, num) },
132
+ ->(buf, offset) { buf.#{aref}(offset) }].freeze
133
+ EOS
134
+ end
135
+
136
+ f.puts <<-EOS
137
+ end
138
+
139
+ class Model
140
+ TYPEMAP = {
141
+ EOS
142
+
143
+ records.each do |typename, bytesize, bytealign, aset, aref|
144
+ sym = typename.to_sym.inspect
145
+ f.puts <<-EOS % ["#{typename}:".ljust(17, " "), typename.upcase]
146
+ %s Primitives::%s,
147
+ EOS
148
+ end
149
+
150
+ f.puts <<-EOS
151
+ }
152
+
153
+ EOS
154
+
155
+ [
156
+ %w(unsigned_char uchar),
157
+ %w(unsigned_short ushort),
158
+ %w(unsigned_int uint),
159
+ %w(unsigned_long ulong),
160
+ %w(unsigned_long_long ulonglong),
161
+ %w(long_long longlong),
162
+ ].each do |link, name|
163
+ f.puts <<-EOS
164
+ TYPEMAP[:#{link}] = TYPEMAP[:#{name}]
165
+ EOS
166
+ end
167
+
168
+ f.puts <<-EOS
169
+ end
170
+ end
171
+ EOS
172
+ end