summaryrefslogtreecommitdiff
path: root/libstd/fmt2.myr
blob: b97052ed7d649bfff198b43bf50074af8df8c841 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use "alloc.use"
use "chartype.use"
use "die.use"
use "extremum.use"
use "fltfmt.use"
use "introspect.use"
use "strbuf.use"
use "syswrap-ss.use"
use "syswrap.use"
use "types.use"
use "utf.use"
use "varargs.use"

use "fmt.use" 	/* FOR DEBUGGING */

pkg std =
	/* write to fd */
	const f2put	: (fmt : byte[:], args : ... -> size)
	const f2fput	: (fd : fd, fmt : byte[:], args : ... -> size)
	const f2putv	: (fmt : byte[:], ap : valist -> size)
	const f2fputv	: (fd : fd, fmt : byte[:], ap : valist -> size)

	/* write to buffer */
	const f2fmt	: (fmt : byte[:], args : ... -> byte[:])
	const f2fmtv	: (fmt : byte[:], ap : valist -> byte[:])
	const f2bfmt	: (buf : byte[:], fmt : byte[:], args : ... -> byte[:])
	const f2bfmtv	: (buf : byte[:], fmt : byte[:], ap : valist -> byte[:])

	/* write to strbuf */
	const f2sbfmt	: (buf : strbuf#, fmt : byte[:], args : ... -> size)
	const f2sbfmtv	: (buf : strbuf#, fmt : byte[:], ap : valist -> size)

	$noret const f2fatal	: (fmt : byte[:], args : ... -> void)
	$noret const f2fatalv	: (fmt : byte[:], ap : valist -> void)
;;

const f2put = {fmt, args
	-> f2fputv(1, fmt, vastart(&args))
}

const f2putv = {fmt, ap
	-> f2fputv(1, fmt, ap)
}

const f2fput = {fd, fmt, args
	-> f2fputv(fd, fmt, vastart(&args))
}

const f2fputv = {fd, fmt, ap
	var sb, s

	sb = mksb()
	f2sbfmtv(sb, fmt, ap)
	s = sbfin(sb)
	-> writeall(fd, s)
}

const f2fmt = {fmt, args
	-> f2fmtv(fmt, vastart(&args))
}

const f2fmtv = {fmt, ap
	var sb

	sb = mksb()
	f2sbfmtv(sb, fmt, ap)
	-> sbfin(sb)
}

const f2bfmt = {buf, fmt, args
	-> f2bfmtv(buf, fmt, vastart(&args))
}

const f2bfmtv = {buf, fmt, ap
	var sb

	sb = mkbufsb(buf)
	f2sbfmtv(sb, fmt, ap)
	-> sbfin(sb)
}

const f2sbfmt = {sb, fmt, args
	-> f2sbfmtv(sb, fmt, vastart(&args))
}

const f2sbfmtv = {sb, fmt, ap -> size
	var c, params
	var nfmt, nparams

	nparams = ap.tc.nelt
	nfmt = 0
	while fmt.len != 0
		(c, fmt) = striter(fmt)
		match c
		| '%':
			nfmt++
			if nfmt > nparams
				die("too few params for fmt\n")
			;;

			if fmt.len > 1 && fmt[0] == '{' castto(byte)
				(params, fmt) = f2getparams(fmt[1:])
			;;
			ap = fallbackfmt(sb, params, vatype(ap), ap)

		| chr:
			sbputc(sb, chr)
		;;
	;;
	if nfmt != nparams
		die("too many params for fmt\n")
	;;
	-> sb.len
}

const fallbackfmt = {sb, params, tyenc, ap
	/* value types */
	var t_val : bool
	var b_val : int8, ub_val : uint8
	var w_val : int16, uw_val : uint16
	var i_val : int32, ui_val : uint32
	var l_val : int64, ul_val : uint64
	var z_val : size
	var p_val : byte#
        var c_val : char
	var s_val : byte[:]
	var f32_val : flt32, f64_val : flt64
	var i8 : int8, i16: int16, i32 : int32
	var by : byte
	var i : int, i64 : int64, l : long
	var ui8 : int8, ui16: int16, ui32 : int32
	var ui : int, ui64 : int64, ul : long

	match typedesc(tyenc)
	| `Tynone:	/* nothing */
	/* atomic types */
	| `Tyvoid:
		sbputs(sb, "void")
	| `Tybool:
		(t_val, ap) = vanext(ap)
		if t_val
			sbputs(sb ,"true")
		else
			sbputs(sb, "false")
		;;
	| `Tychar:
		(c_val, ap) = vanext(ap)
		sbputc(sb, c_val)
	| `Tyint8:
		(b_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), true, b_val)
	| `Tyint16:
		(w_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), true, w_val)
	| `Tyint:
		(i_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), true, i_val)
	| `Tyint32:
		(i_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), true, i_val)
	| `Tyint64:
		(l_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), true, l_val)
	| `Tylong:
		(l_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), true, l_val)

	| `Tybyte:
		(ub_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), false, ub_val)
	| `Tyuint8:
		(ub_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), false, ub_val)
	| `Tyuint16:
		(w_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), false, uw_val)
	| `Tyuint:
		(i_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), false, ui_val)
	| `Tyuint32:
		(i_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), false, ui_val)
	| `Tyuint64:
		(l_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), false, ul_val)
	| `Tyulong:
		(l_val, ap) = vanext(ap)
		intfmt(sb, intparams(params), false, ul_val)
	| `Tyflt32:
		(f32_val, ap) = vanext(ap)
		flt32bfmt(sb, f32_val, MNormal, 0)
	| `Tyflt64:
		(f64_val, ap) = vanext(ap)
		flt64bfmt(sb, f64_val, MNormal, 0)
	| `Tyvalist:
		sbputs(sb, "...")

	/* compound types */
	| `Typtr desc:
		(p_val, ap) = vanext(ap)
		sbputs(sb, "0x")
		intfmt(sb, \
			[.base=16, .padto=2*sizeof(void#), .padfill='0'], \
			false, p_val castto(intptr))
	| `Tyslice desc:
		match typedesc(desc)
		| `Tybyte:
			(s_val, ap) = vanext(ap)
			sbputs(sb, s_val)
		| _:
			sbputs(sb, "slice[:]")
		;;
	| `Tyfunc tc:
		(p_val, ap) = vanext(ap)
		sbputs(sb, "func{")
		intfmt(sb, \
			[.base=16, .padto=2*sizeof(void#), .padfill='0'], \
			false, p_val castto(intptr))
		sbputs(sb, "}")
	| `Tyarray (sz, data):
		sbputs(sb, "array")
	/* aggregate types */
	| `Tytuple typecursor:
		sbputs(sb, "tuple")
	| `Tystruct namecursor:
		sbputs(sb, "struct")
	| `Tyunion namecursor:
		sbputs(sb, "struct")
	| `Tyname (name, desc):
		ap = fallbackfmt(sb, params, desc, ap)
	;;
	-> ap
}

type intparams = struct
	base	: size
	padto	: size
	padfill	: char
;;

const f2getparams = {fmt
	var i

	for i = 0; i < fmt.len; i++
		if fmt[i] == '}' castto(byte)
			goto foundparams
		;;
	;;
	die("invalid format string")
:foundparams
	-> (fmt[:i], fmt[i+1:])
}

const intparams = {params
	var ip : intparams
	var c

	ip = [
		.base = 10,
		.padfill = ' ',
		.padto = 0
	]

	while params.len > 0
		(c, params) = striter(params)
		match c
		| 'x':	ip.base = 16
		| '0':	ip.padfill = '0'
		| chr:
			while isdigit(c)
				ip.padto = ip.padto*10 + charval(c, 10)
				(c, params) = striter(params)
			;;
		;;
	;;
	-> ip

}

const digitchars = [
	'0','1','2','3','4',
	'5','6','7','8','9',
	'a','b','c','d','e','f'
]
generic intfmt = {sb, opts, signed, bits : @a::(integral,numeric)
	var isneg
	var val
	var b : char[32]
	var i, j, npad
	var base

	base = opts.base castto(uint64)
	std.put("base = %l\n", base)
	if signed && bits < 0
		val = -bits castto(uint64)
		isneg = true
	else
		val = bits castto(uint64)
		val &= ~0 >> (8*(sizeof(uint64)-sizeof(@a)))
		isneg = false
	;;

	i = 0
	if val == 0
		b[0] = '0'
		i++
	;;
	while val != 0
		b[i] = digitchars[val % base]
		val /= base
		i++
	;;

	npad = clamp(opts.padto - i, 0, opts.padto)
	if isneg
		npad--
	;;
	if opts.padfill == '0' && isneg
		sbputc(sb, '-')
	;;
	for j = 0; j < npad; j++
		sbputc(sb, opts.padfill)
	;;
	if opts.padfill != '0' && isneg
		sbputc(sb, '-')
	;;
	for j = i; j != 0; j--
		sbputc(sb, b[j - 1])
	;;
}

const writeall = {fd, buf
	var n, len

	len = 0
	while true
		n = write(fd, buf)
		if n <= 0 || n >= len
			break
		;;
		len += n
	;;
	-> len
}