-
Notifications
You must be signed in to change notification settings - Fork 5
/
fields.dylan
170 lines (150 loc) · 5.04 KB
/
fields.dylan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module: binary-data
author: Andreas Bogk and Hannes Mehnert
copyright: 2005-2011 Andreas Bogk and Hannes Mehnert. All rights reserved.
license: see LICENSE.txt in this distribution
define method find-field (field-name :: <string>, list :: <simple-vector>)
=> (res :: false-or(<field>))
find-field(as(<symbol>, field-name), list);
end;
define method find-field (name :: <symbol>, list :: <simple-vector>)
=> (res :: false-or(<field>))
block (ret)
for (field in list)
if (field.field-name = name)
ret(field)
end;
end;
#f;
end;
end;
define function compute-static-offset (list :: <simple-vector>)
//input is a list of <field>
//sets static-start, static-end, static-length for all fields
let start = 0;
for (field in list)
if (instance?(field, <layering-field>))
unless (field.fixup-function)
field.fixup-function := fixup-protocol-magic;
end;
end;
if (instance?(field, <variably-typed-field>))
unless (slot-initialized?(field, type-function))
field.type-function := payload-type;
end;
end;
if (start ~= $unknown-at-compile-time)
unless (field.dynamic-start)
if (field.static-start = $unknown-at-compile-time)
field.static-start := start;
else
start := field.static-start;
end;
end;
end;
let my-length = static-field-size(field);
if (my-length ~= $unknown-at-compile-time)
unless (field.dynamic-length)
if (field.static-length = $unknown-at-compile-time)
field.static-length := my-length;
else
my-length := field.static-length;
end;
end;
end;
start := start + my-length;
if (start ~= $unknown-at-compile-time)
unless (field.dynamic-end)
if (field.static-end = $unknown-at-compile-time)
field.static-end := start;
else
start := field.static-end;
end;
end;
end;
end;
end;
define open generic field-size (frame :: subclass(<frame>))
=> (length :: <number>);
define inline method field-size (frame :: subclass(<frame>))
=> (length :: <unknown-at-compile-time>)
$unknown-at-compile-time
end;
define abstract class <field> (<object>)
slot index :: <integer>, init-keyword: index:;
constant slot field-name, required-init-keyword: name:;
slot static-start :: <integer-or-unknown> = $unknown-at-compile-time,
init-keyword: static-start:;
slot static-length :: <integer-or-unknown> = $unknown-at-compile-time,
init-keyword: static-length:;
slot static-end :: <integer-or-unknown> = $unknown-at-compile-time,
init-keyword: static-end:;
slot init-value = $unsupplied, init-keyword: init-value:;
slot fixup-function :: false-or(<function>) = #f, init-keyword: fixup:;
constant slot getter, required-init-keyword: getter:;
constant slot setter, required-init-keyword: setter:;
constant slot dynamic-start :: false-or(<function>) = #f,
init-keyword: dynamic-start:;
constant slot dynamic-end :: false-or(<function>) = #f,
init-keyword: dynamic-end:;
constant slot dynamic-length :: false-or(<function>) = #f,
init-keyword: dynamic-length:;
end;
define generic static-field-size (field :: <field>)
=> (res :: <integer-or-unknown>);
define method static-field-size (field :: <field>)
=> (res :: singleton($unknown-at-compile-time));
$unknown-at-compile-time
end;
define abstract class <statically-typed-field> (<field>)
slot type, required-init-keyword: type:;
end;
define class <single-field> (<statically-typed-field>)
end;
define class <layering-field> (<single-field>)
end;
define class <enum-field> (<single-field>)
slot mappings, required-init-keyword: mappings:;
end;
define method static-field-size (field :: <single-field>)
=> (res :: <integer-or-unknown>)
if (field.static-length ~= $unknown-at-compile-time)
field.static-length
else
field.type.field-size
end
end;
define class <variably-typed-field> (<field>)
//type-function has to return a subclass of <container-frame>
slot type-function, init-keyword: type-function:;
end;
define abstract class <repeated-field> (<statically-typed-field>)
inherited slot init-value = #(), init-keyword: init-value:;
end;
define class <self-delimited-repeated-field> (<repeated-field>)
slot reached-end?, required-init-keyword: reached-end?:;
end;
define class <count-repeated-field> (<repeated-field>)
slot count, required-init-keyword: count:;
end;
/* one day I want to have this...
define class <externally-delimited-repeated-field> (<self-delimited-repeated-field>)
keyword reached-end?:, init-value: always(#f);
end;
*/
define method make (class == <repeated-field>,
#rest rest,
#key count, reached-end?,
#all-keys)
=> (instance :: <repeated-field>);
apply(make,
if (count)
<count-repeated-field>
elseif (reached-end?)
<self-delimited-repeated-field>
else
error("unknown repeated field type encountered");
end,
count: count,
reached-end?: reached-end?,
rest)
end;