summaryrefslogtreecommitdiff
path: root/man/2/ubfa
blob: 46f6a7631d0fe8c6268e451a8e76f798739079f9 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
.TH UBFA 2
.SH NAME
ubfa: readubf, writeubf, UValue \- read, write and represent values in a UBF(A) data transport encoding
.SH SYNOPSIS
.EX
include "ubfa.m";
ubfa := load UBFa UBFa->PATH;

UValue: adt {
    pick{
    Atom =>
        name:  string;
    Int =>
        value: int;
    String =>
        s:     string;
    Binary =>
        a:     array of byte;
    Tuple =>
        a:     cyclic array of ref UValue;  # tree
    List =>
        l:     cyclic list of ref UValue;   # tree
    Tag =>
        name:  string;
        o:     cyclic ref UValue;
    }
    isatom:    fn(o: self ref UValue): int;
    isstring:  fn(o: self ref UValue): int;
    isint:     fn(o: self ref UValue): int;
    istuple:   fn(o: self ref UValue): int;
    isop:      fn(o: self ref UValue, op: string, arity: int): int;
    islist:    fn(o: self ref UValue): int;
    isbinary:  fn(o: self ref UValue): int;
    istag:     fn(o: self ref UValue): int;
    eq:        fn(o: self ref UValue, v: ref UValue): int;
    op:        fn(o: self ref UValue, arity: int): string;
    args:      fn(o: self ref UValue, arity: int):
                array of ref UValue;
    els:       fn(o: self ref UValue): list of ref UValue;
    val:       fn(o: self ref UValue): int;
    binary:    fn(o: self ref UValue): array of byte;
    objtag:    fn(o: self ref UValue): string;
    obj:       fn(o: self ref UValue): ref UValue;
    text:      fn(o: self ref UValue): string;
};

init:     fn(bufio: Bufio);
readubf:  fn(input: ref Iobuf): (ref UValue, string);
writeubf: fn(output: ref Iobuf, v: ref UValue): int;
uniq:     fn(s: string): string;

uvatom:   fn(s: string): ref UValue.Atom;
uvint:    fn(i: int): ref UValue.Int;
uvstring: fn(s: string): ref UValue.String;
uvbinary: fn(a: array of byte): ref UValue.Binary;
uvtuple:  fn(a: array of ref UValue): ref UValue.Tuple;
uvlist:   fn(l: list of ref UValue): ref UValue.List;
uvtag:    fn(name: string, o: ref UValue): ref UValue.Tag;
.EE
.SH DESCRIPTION
.B UBFa
provides value representations, and encoding and decoding operations for Armstrong's UBF(A) data transport
format, defined by
.IR ubfa (6).
.PP
.B Init
must be called before invoking any other operation of the module.
The
.I bufio
parameter must refer to the instance of
.IR bufio (2)
that provides the
.B Iobuf
parameters used for input and output.
.PP
.B UValue
is the internal representation of values that can be transmitted by the UBF(A) encoding.
The various sorts of values are distinguished in a pick adt:
.TP
.B UValue.Atom
Represents an
.IR atom :
a symbolic constant, for example the name of an operation or an enumeration literal.
The string
.B name
gives the spelling of the constant's name.
.TP
.B UValue.Int
Represents an integer value (eg, a Limbo
.BR int )
with the given
.BR value .
.TP
.B UValue.String
Represents a character string (eg, a Limbo
.BR string )
with the value
.BR s .
.TP
.B UValue.Binary
Represents binary data as a sequence of bytes in the array
.BR a .
.TP
.B UValue.Tuple
Represents a compound value that contains a fixed number of component values,
given by successive elements of the array
.BR a .
UBF tuples correspond to tuples or non-pick
.B adt
values in Limbo.
.TP
.B UValue.List
Represents a compound value containing a variable number of component values,
given by successive elements of the list
.BR l .
.TP
.B UValue.Tag
Associates an application-specific
.B tag
with another
.B UValue
referenced by
.BR o .
.PP
.B Readubf
reads a single value in
.IR ubfa (6)
format from the
.I input
stream and returns a tuple
.BI ( val,\ err ).
On success,
.I val
is a
.B UValue
that represents that value.
If an error occurs,
.I val
is nil and
.I err
contains a diagnostic.
.PP
.B Writeubf
writes a
.IR ubfa (6)
representation of the value
.I v
to the
.I output
stream.
It returns 0 on success and -1 on error (setting the system error string).
.PP
The easiest way to create a new
.B UValue
for subsequent output is with one of the module-level functions
.BR uvatom ,
.BR uvint ,
.BR uvstring ,
and so on.
As values of a pick adt, a
.B UValue
can be inspected using Limbo's
.B tagof
operator and the appropriate variant accessed using a
.B pick
statement.
.B UValue
also supports several groups of common operations, for smaller, tidier code.
First, the set of enquiry functions
.IB u .is X ()
return true if the value
.I u
is an instance of the UBF type
.I X
.RI ( atom ,
.IR int ,
.IR string ,
.IR binary ,
.IR tuple ,
etc).
The other operations are:
.TP
.IB u .eq( v )
Return true if the values of
.I u
and
.I v
are equal, including the values of corresponding subcomponents, recursively
.TP
.IB u .isop( op,\ n )
Return true if
.I u
is a tuple having
.I n
components, and its first component is an atom or string with the value
.IR op .
.TP
.IB u .op( n )
If
.I u
is a tuple with
.I n
components, and the first component is an atom or string, return its value.
Otherwise, return nil.
.TP
.IB u .args( n )
If
.I u
is a tuple with
.I n
components, return an array containing the values of all but the first component.
Otherwise, return  nil.
.TP
.IB u .els()
If
.I u
is a list, return a Limbo list of its elements (ie,
.IB u .l\fR)\fP.
Otherwise, return nil.
.TP
.IB u .val()
If
.I u
is an integer, return its value.
Otherwise return zero.
.TP
.IB u .binary()
If
.I u
is a binary value, return the corresponding array of bytes; if
.I u
is an atom or string, return an array of bytes containing its value;
otherwise, return nil.
.TP
.IB u .objtag()
If
.I u
is a tag, return the name of the tag.
Otherwise, return nil.
.TP
.IB u .obj()
If
.I u
is a tag, return the tagged value.
Otherwise, return
.I u
itself.
.TP
.IB u .text()
Return a printable representation of the value
.IR u ,
mainly intended for debugging and tracing.
.PP
One difference between atoms and strings is that
all atoms with identical spellings refer to the same string in the implementation's storage.
Given an atom name,
.B uniq
returns the corresponding string, stored in an internal dictionary.
It is used by
.B UBFa
to create the strings
.BR UValue.Atom.s ,
and can be put to similar use directly by applications.
It should only be applied to values that are small in number (as with symbolic constants).
.SH SOURCE
.B /appl/lib/ubfa.b
.SH SEE ALSO
.IR sexprs (2),
.IR ubfa (6)
.br
J L Armstrong, ``Getting Erlang to talk to the outside world'',
.I "ACM SIGPLAN Erlang workshop 2002" ,
Pittsburg, PA USA