-
Notifications
You must be signed in to change notification settings - Fork 3
/
print.ml
72 lines (69 loc) · 1.56 KB
/
print.ml
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
# print.ml - io print library for ml.
# Extendable and safe frontend for `printf`/`fprintf`.
import stdlib.c.cdef
import stdlib.c.cstdlib
import stdlib.debug
import stdlib.string
# Print helper functions
fun _print(st: c_stream, arg: float64): void
fprintf(st, "%f", arg)
end
fun _print(st: c_stream, arg: float32): void
fprintf(st, "%lf", arg)
end
fun _print(st: c_stream, arg: int64): void
fprintf(st, "%lld", arg)
end
fun _print(st: c_stream, arg: int32): void
fprintf(st, "%d", arg)
end
fun _print(st: c_stream, arg: int16): void
fprintf(st, "%hd", arg)
end
fun _print(st: c_stream, arg: int8): void
fprintf(st, "%hhd", arg)
end
fun _print(st: c_stream, arg: str): void
fprintf(st, "%s", c_str(arg))
end
fun _print(st: c_stream, arg: str&): void
fprintf(st, "%s", c_str(arg))
end
fun _print(st: c_stream, arg: void*): void
fprintf(st, "%p", arg)
end
fun _print(st: c_stream, arg: int8*): void
fprintf(st, "%s", arg)
end
fun _print(st: c_stream, arg: bool): void
if arg == true
fprintf(st, "true")
elif arg == false
fprintf(st, "false")
else
panic("Logic error")
end
end
# Convenience macros
macro print(_arg)
_print(stdout, _arg)
end
macro print(_arg, _other)
print(_arg)
print(_other)
end
macro println(_args)
print(_args)
puts ""
end
macro print_to(_stream, _arg)
_print(_stream, _arg)
end
macro print_to(_stream2, _arg, _other)
print_to(_stream2, _arg)
print_to(_stream2, _other)
end
macro println_to(_stream3, _args)
print_to(_stream3, _args)
fprintf(_stream3, "\n")
end