-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.kai
348 lines (298 loc) · 9.77 KB
/
print.kai
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
346
347
348
#import kai("os")
#import kai("arrays")
#import builtin("types")
#import kai("math")
Fmt :: struct {
buf: *[]u8
width: i64
precision: i64
}
fmtU :: fn(buf: *[]u8, x: u64, base: u64) -> void {
assert(base <= 16, "only bases up to 16 are currently supported")
MAX_LEN :: 64
DIGITS :: "0123456789abcdef"
out := ([MAX_LEN]u8{})[:]
out.len = 0
i := MAX_LEN - 1
zero: u8 = "0"
a: u8 = "a"
for {
out[i] = DIGITS[x % base]
x /= base
i -= 1
if x <= 0 break
}
out = out[i+1:]
out.len = MAX_LEN - 1 - i
arrays.AppendContents(buf, out)
}
fmtPtr :: fn(buf: *[]u8, x: u64) -> void {
arrays.AppendContents(buf, "0x")
fmtU(buf, x, base: 16)
}
// Derived from the naive example at ryanjuckett.com/programming/printing-floating-point-numbers
fmtFloat :: fn(buf: *[]u8, value: f64) -> void {
using math
MAX_DIGITS :: 10
decimalDigits := [MAX_DIGITS]u8{} // buffer to put the decimal representation in
numDigits := 0 // this will be set to the number of digits in the buffer
// Compute the first digit's exponent in the equation digit*10^exponent
// (e.g. 122.5 would compute a 2 because its first digit is in the hundreds place)
firstDigitExponent := floor_64(log10_64(value))
// Scale the input value such that the first digit is in the ones place
// (e.g. 122.5 would become 1.225).
value = value / pow_64(10, firstDigitExponent)
// while there is a non-zero value to print and we have room in the buffer
for value > 0.0 && numDigits < MAX_DIGITS {
// Output the current digit.
digit := cast(u8) floor_64(value)
decimalDigits[numDigits] = "0" + digit // convert to an ASCII character
numDigits += 1
// Compute the remainder by subtracting the current digit
// (e.g. 1.225 would becom 0.225)
value -= cast(f64) digit
// Scale the next digit into the ones place.
// (e.g. 0.225 would becom 2.25)
value *= 10.0
if numDigits == firstDigitExponent + 1 {
decimalDigits[numDigits] = "."
numDigits += 1
}
}
out := decimalDigits[:]
out.len = numDigits
arrays.AppendContents(buf, out)
}
fmtType :: fn(buf: *[]u8, type: types.Type) -> void {
switch type {
case Simple:
switch type {
case Integer:
if cast(u64) type.Flags & types.FlagSigned != 0
arrays.Append(buf, "i")
else
arrays.Append(buf, "u")
fmtU(buf, cast(u64) type.Width, base: 10)
case Boolean:
arrays.Append(buf, "b")
fmtU(buf, cast(u64) type.Width, base: 10)
case Float:
arrays.Append(buf, "f")
fmtU(buf, cast(u64) type.Width, base: 10)
case Any:
arrays.AppendContents(buf, "any")
case Void:
arrays.AppendContents(buf, "void")
case:
panic("Unrecognized type")
}
case Struct:
arrays.AppendContents(buf, "struct{")
for field in type.Fields {
arrays.AppendContents(buf, field.Name)
arrays.AppendContents(buf, ": ")
fmtType(buf, field.Type)
arrays.Append(buf, ",")
}
arrays.Append(buf, "}")
case Union:
arrays.AppendContents(buf, "union{")
for c in type.Cases {
arrays.AppendContents(buf, c.Name)
arrays.AppendContents(buf, ": ")
fmtType(buf, c.Type)
arrays.Append(buf, ",")
}
arrays.Append(buf, "}")
case Enum:
arrays.AppendContents(buf, "enum{")
for c in type.Cases {
arrays.AppendContents(buf, c.Name)
arrays.Append(buf, ",")
}
arrays.Append(buf, "}")
case Function:
arrays.Append(buf, "(")
// TODO: Print param & result types
arrays.AppendContents(buf, "...")
arrays.AppendContents(buf, ") -> ")
arrays.AppendContents(buf, "...")
case Array:
arrays.Append(buf, "[")
if type.Flags & types.FlagVector != 0 arrays.AppendContents(buf, "vec ")
fmtU(buf, type.Length, base: 10)
arrays.Append(buf, "]")
fmtType(buf, type.ElementType)
case Slice:
if type.Flags & types.FlagString != 0 {
arrays.AppendContents(buf, "string")
return
}
arrays.Append(buf, "[")
arrays.Append(buf, "]")
fmtType(buf, type.ElementType)
case Pointer:
arrays.Append(buf, "*")
fmtType(buf, type.PointeeType)
case:
panic("Unrecognized type")
}
}
fmtValue :: fn(f: Fmt, buf: *[]u8, val: any) -> void {
switch val.type binding type {
case Simple:
switch type {
case Integer:
accumulator: u64 = 0
for offset := 0; offset < type.Width; offset += 8 {
byte := val.data[offset / 8]
accumulator |= cast(u64) byte << offset
}
signMask := (1 << (cast(u64) type.Width - 1))
if cast(u64) type.Flags & types.FlagSigned != 0 && accumulator & signMask != 0 {
accumulator = ~accumulator + 1
arrays.Reserve(buf, buf.len + 1)
(<buf)[buf.len] = "-"
Printf(" HERE ON LINE %\n", #line)
buf.len += 1
}
fmtU(buf, accumulator, base: 10)
case Boolean:
accumulator: u64 = 0
for offset := 0; offset < type.Width; offset += 8 {
byte := val.data[offset / 8]
accumulator |= cast(u64) byte << offset
}
if accumulator != 0 arrays.AppendContents(buf, "true")
else arrays.AppendContents(buf, "false")
case Float:
x: f64 = 0
if type.Width == 64 {
x = <(bitcast(*f64) val.data)
} else if type.Width == 32 {
x = cast(f64) <(bitcast(*f32) val.data)
} else panic("Unsupported Floating point width")
fmtFloat(buf, x)
case Any:
Any :: struct{data: rawptr; type: types.Type}
panic()
case Void: fallthrough
case:
panic()
}
case Array:
fmtType(buf, val.type)
arrays.Append(buf, "{")
BPrintf(buf, "% elements", type.Length)
/*
for n := 0; n < type.Length; n += 1 {
// TODO: Construct an any with type.ElementType and the pointer we compute
arrays.Append(buf, ",")
}
*/
arrays.Append(buf, "}")
case Slice:
if type.Flags & types.FlagString != 0 {
slice := <(bitcast(*[]u8) val.data)
arrays.AppendContents(buf, slice)
return
}
fmtType(buf, val.type)
arrays.Append(buf, "{")
Slice :: struct{raw: rawptr; len: u64; cap: u64}
slice := <(bitcast(*Slice) val.data)
BPrintf(buf, "raw: %; len: %; cap: %", slice.raw, slice.len, slice.cap)
/* for n := 0; n < slice.len; n += 1 {
// TODO: Construct an any with type.ElementType and the pointer we compute
arrays.Append(buf, ",")
} */
arrays.Append(buf, "}")
case Pointer:
fmtPtr(buf, <(bitcast(*u64) val.data))
case Function:
panic("Function printing unsupported currently")
case Struct:
arrays.AppendContents(buf, "struct{")
arrays.Append(buf, "}")
case Union:
arrays.AppendContents(buf, "union{")
arrays.Append(buf, "}")
case Enum:
arrays.AppendContents(buf, "enum{")
arrays.Append(buf, "}")
}
}
BPrintf :: fn(buf: *[]u8, format: string, args: ..any) -> void {
arrays.Reserve(buf, format.len)
argIndex := 0
for i := 0; i < format.len; argIndex += 1 {
lasti := i
// skip until we see a %
for i < format.len && format[i] != "%" {
i += 1
}
// Append everything between the last % byte and this % byte
if i > lasti {
arrays.AppendContents(buf, format[lasti:i])
}
if i >= format.len {
// finished processing format string
break
}
// Process a verb
i += 1
if format[i] == "%" {
arrays.Append(buf, "%")
continue
}
// TODO: Here is where we would parse any options & verbs (%2f)
fmt := Fmt{}
fmtValue(fmt, buf, args[argIndex])
}
}
Printf :: fn(format: string, args: ..any) -> void {
buf := []u8{}
BPrintf(&buf, format, ..args)
os.Write(os.Stdout, buf)
}
#test "BPrintf unsigned integer" {
reset :: fn(buf: *[]u8) -> void {
buf.len = 0
}
buf := []u8{}
BPrintf(&buf, "%", 0)
assert(buf.len == 1)
assert(buf[0] == "0")
reset(&buf)
BPrintf(&buf, "%", 4)
assert(buf.len == 1)
assert(buf[0] == "4")
reset(&buf)
BPrintf(&buf, "%", 18446744073709551615) // u64 max
assert(buf.len == 20)
assert(buf[0] == "1")
assert(buf[1] == "8")
assert(buf[19] == "5")
}
#test "BPrintf signed integer" {
reset :: fn(buf: *[]u8) -> void {
buf.len = 0
}
buf := []u8{}
BPrintf(&buf, "%", cast(i64) 0)
assert(buf.len == 1)
assert(buf[0] == "0")
reset(&buf)
BPrintf(&buf, "%", -4)
assert(buf.len == 2)
assert(buf[0] == "-")
assert(buf[1] == "4")
reset(&buf)
BPrintf(&buf, "%", -9223372036854775808) // i64 min
assert(buf.len == 20)
assert(buf[0] == "-")
assert(buf[1] == "0")
assert(buf[2] == "2")
assert(buf[3] == "2")
assert(buf[19] == "8")
}