summaryrefslogtreecommitdiff
path: root/tifmt.myr
blob: 29f083145570503d3d27e8129dae7a1e96d18531 (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
use std 
use "types"

pkg termdraw =
	const tifmt	: (f : tifmt#, sb : std.strbuf#, str : byte[:], params : tivar[:] -> void)
;;

const tifmt = {f, sb, str, params
	init(f, params)
	eval(f, sb, str)
}

const init = {f, params
	f.nstk = 0
	std.slfill(f.params[:], `Int 0)
	std.slcp(f.params[:params.len], params)
}

const eval = {f, sb, str
	var i, b

	i = 0
	while i < str.len
:next
		b = next(str, &i)
		if (b : char) != '%'
			std.sbputb(sb, b)
			continue
		;;

		match (next(str, &i) : char)
		| '%':	std.sbputb(sb, ('%' : byte))
		| 'c':	std.sbputb(sb, (popi(f) : byte))
		| 'p':	push(f, loadparam(f, str, &i))
		| 'P':	setvar(f, next(str, &i), pop(f))
		| 'g':	push(f, getvar(f, next(str, &i)))
		| '\'':	litchr(f, str, &i)
		| '{':	litint(f, str, &i)
		| 'l':	pushi(f, pops(f).len)
		/* arithmetic */
		| '+':	pushi(f, popi(f) + popi(f))
		| '-':	pushi(f, popi(f) - popi(f))
		| '*':	pushi(f, popi(f) * popi(f))
		| '/':	pushi(f, popi(f) / popi(f))
		| 'm':	pushi(f, popi(f) % popi(f))
		/* bitwise */
		| '&':	pushi(f, popi(f) & popi(f))
		| '|':	pushi(f, popi(f) | popi(f))
		| '^':	pushi(f, popi(f) ^ popi(f))
		| '~':	pushi(f, ~ popi(f))
		/* relational false*/
		| '=':	pushi(f, btoi(popi(f) == popi(f)))
		| '<':	pushi(f, btoi(popi(f) < popi(f)))
		| '>':	pushi(f, btoi(popi(f) > popi(f)))
		/* logical */
		| 'A':	pushi(f, btoi(popi(f) != 0 && popi(f) != 0))
		| 'O':	pushi(f, btoi(popi(f) != 0 || popi(f) != 0))
		| '!':	pushi(f, btoi(popi(f) == 0))
		| 'i':	inc(&f.params[0]); inc(&f.params[1])
		/* branching */
		| '?':	evalcond(f, sb, str, &i)
		/* special formatting */
		| ':':	i++; fmtvar(f, sb,  str[i:], &i, str[i - 1])
		| chr:	fmtvar(f, sb, str, &i, str[i - 1])
		;;
	;;
}

const evalcond ={f, sb, str, pi
	var cond, iftrue, iffalse
	var start, depth, i

	cond = ""
	iftrue = ""
	iffalse = ""
	start = pi#
	depth = 1
	for i = pi#; i < str.len - 1; i++
		if str[i] != ('%' : byte)
			continue
		;;
		if str[i + 1] == ('?' : byte)
			depth++
		elif str[i + 1] == (';' : byte)
			depth--
		elif depth == 1 && str[i + 1] == ('t' : byte)
			cond = str[start:i]
			start = i + 2
		elif depth == 1 && str[i+ 1] == ('e' : byte)
			iftrue = str[start:i]
			start = i + 2
		;;
		if depth == 0
			iffalse = str[start:i]
			break
		;;
	;;
	eval(f, sb, cond)
	if popi(f) != 0
		eval(f, sb, iftrue)
	else
		eval(f, sb, iffalse)
	;;
	pi# = i + 2
}

const loadparam = {f, str, iptr
	var p

	p = next(str, iptr)
	-> f.params[p - ('1' : byte)]
}

const pushi = {f, i
	f.stk[f.nstk++] = `Int i
}

const push = {f, v
	f.stk[f.nstk++] = v
}

const litchr = {f, str, i
	var c

	c = next(str, i)
	pushi(f, (c : int))
	if next(str, i) != ('\'' : byte)
		std.fatal("malformed format string\n")
	;;
}

const litint = {f, str, i
	var n
	n = scannum(str, i)
	pushi(f, (n : int))
	if next(str, i) != ('}' : byte)
		std.fatal("malformed format string\n")
	;;
}

const setvar = {f, name, val
	if name >= ('A' : byte) && name <= ('Z' : byte)
		f.vars[name-('A' : byte)] = val
	elif name >= ('a' : byte) && name <= ('z' : byte)
		f.vars[26+name-('a' : byte)] = val
	else
		std.fatal("setting invalid variable")
	;;
}

const getvar = {f, name
	if name >= ('A' : byte) && name <= ('Z' : byte)
		-> f.vars[name-('A' : byte)]
	elif name >= ('a' : byte) && name <= ('z' : byte)
		-> f.vars[26+name-('a' : byte)]
	else
		std.fatal("setting invalid variable")
	;;
}

const popi = {f
	match f.stk[--f.nstk]
	| `Int i:	-> i
	| `Str s:	std.fatal("popping int, but var is str\n")
	;;
}

const pops = {f
	match f.stk[--f.nstk]
	| `Int i:	std.fatal("popping str, but var is int\n")
	| `Str s:	-> s
	;;
}

const pop = {f
	-> f.stk[--f.nstk]
}

const fmtvar = {f, sb, str, i, b
	var width = 0, prec = -1
	var lpad = false
	var sign = false
	var alt = false
	var blank = false

	/* flags */
	match (b :char)
	| '-':	lpad  = true; b = next(str, i)
	| '+':	sign  = true; b = next(str, i)
	| '#':	alt   = true; b = next(str, i)
	| ' ':	blank = true; b = next(str, i)
	| _:	/* nothing */
	;;
	/* width */
	if std.isdigit((b : char))
		i#--
		width = scannum(str, i)
	;;
	/* precision */
	if b == ('.' : byte)
		prec = scannum(str, i)
	;;

	/* fmt */
	match (b : char)
	| 'd':	intfmt(f, sb, (b : char), lpad, sign, alt, blank, width, prec, popi(f), 10, false)
	| 'o':	intfmt(f, sb, (b : char), lpad, sign, alt, blank, width, prec, popi(f), 8, false)
	| 'x':	intfmt(f, sb, (b : char), lpad, sign, alt, blank, width, prec, popi(f), 16, false)
	| 'X':	intfmt(f, sb, (b : char), lpad, sign, alt, blank, width, prec, popi(f), 16, true)
	| 's':	strfmt(f, sb, lpad, width, prec, pops(f))
	| c:	std.fatal("invalid terminfo fmt {}\n", c)
	;;
}

const btoi = {b
	if b
		-> 1
	else
		-> 0
	;;
}

const inc = {pp
	match pp#
	| `Int i:	pp# = `Int (i+1)
	| `Str s:	std.fatal("cant increment str\n")
	;;
}


const next = {s, idxp -> byte
	if idxp# == s.len
		-> 0
	;;
	idxp#++
	-> s[idxp# - 1]
}

const digitchars = "0123456789ABCDEF"
const intfmt = {f, sb, chr, lpad, spad, alt, blank, width, prec, _val, base, upper
	var i, j, npad, isneg, v
	var b : byte[32]

	for i = 0; i < prec; i++
		b[i] = ('0' : byte)
	;;
	i = 0
	isneg = false
	if _val < 0 && base == 10
		v= (-_val : uint32)
		isneg = true
		/* if its negative after inverting, we have int min */
		if v < 0
			i = 11
			std.slcp(b[:11], "-2147483648")
			goto output
		;;
	else
		v = (_val : uint32)
	;;

	i = 0
	if v == 0
		b[i++] = ('0' : byte)
	;;
	while v != 0
		b[i] = digitchars[v % base]
		if !upper && b[i] >= ('A' : byte) && b[i] <= ('F' : byte)
			b[i] |= 0x60
		;;
		v /= base
		i++
	;;
	if isneg
		b[i++] = ('-' : byte)
	elif spad
		b[i++] = (' ' : byte)
	;;
	if alt
		match chr
		| 'x':	b[i++] = ('x' : byte); b[i++] = ('0' : byte)
		| 'X':	b[i++] = ('X' : byte); b[i++] = ('0' : byte)
		| 'o':	b[i++] = ('0' : byte)
		| _:	std.fatal("invalid char\n")
		;;
	;;
:output
	npad = std.max(width, i)
	if isneg
		npad--
	;;
	for j = i; j != 0; j--
		std.sbputb(sb, b[j - 1])
	;;
}

const strfmt = {f, sb, lpad, width, prec, str
	var len, nleft, nright

	len = str.len

	/* assumption: ascii, one byte per char */
	if prec > 0 && prec < str.len
		str = str[:prec]
	;;
	if width < str.len
		width = str.len
	;;
	if lpad
		nleft = width - str.len
		nright = 0
	else
		nleft = 0
		nright = width - str.len
	;;
	for var i = 0; i < nleft; i++
		std.sbputc(sb, ' ')
	;;
	std.sbputs(sb, str)
	for var i = 0; i < nright; i++
		std.sbputc(sb, ' ')
	;;
}

const scannum = {s : byte[:], start
	var i, c
	var begin = start#

	i = start
	while true
		c = std.decode(s[i#:])
		if !std.isdigit(c)
			break
		;;
		i# += std.charlen(c)
	;;
	match std.intparse(s[begin:i#])
	| `std.Some n:	-> n
	| `std.None:	-> 0
	;;
}