-
Notifications
You must be signed in to change notification settings - Fork 3
/
read.ml
74 lines (65 loc) · 1.56 KB
/
read.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
73
74
# read.ml - io read library for ml.
# Extendable and safe frontend for `scanf`/`fscanf`.
import stdlib.c.cdef
import stdlib.debug
import stdlib.string
import stdlib.c.cstdlib
# Read a string from standard input until a newline is encountered.
fun input: str
let size = 128
let length = 0
let ch: int32 = 1;
let buff: int8* = malloc(size)
defer free(buff)
if buff == null
panic("Unable to allocate buff");
end
while (ch = getchar) != '\n'
if length + 1 >= size
size = size * 2;
buff = realloc(buff, size);
if buff == null
panic("Unable to reallocate buff");
end
end
buff[length] = ch;
length = length + 1
end
buff[length] = 0;
let s = str(buff)
ret s
end
# Read helper functions
fun _read(st: c_stream, arg: int8*): void
fscanf(st, "%hhd", arg)
end
fun _read(st: c_stream, arg: int16*): void
fscanf(st, "%hd", arg)
end
fun _read(st: c_stream, arg: int32*): void
fscanf(st, "%d", arg)
end
fun _read(st: c_stream, arg: int64*): void
fscanf(st, "%Ld", arg)
end
fun _read(st: c_stream, arg: void*): void
panic("Cannot read void value")
end
fun _read(st: c_stream, arg: bool*): void
panic("Cannot read boolean value")
end
# Convenience macros
macro read(_arg)
_read(stdin, &_arg)
end
macro read(_arg, _other)
read(_arg)
read(_other)
end
macro read_from(_stream, _arg)
_read(_stream, &_arg)
end
macro read_from(_stream2, _arg, _other)
read_from(_stream2, _arg)
read_from(_stream2, _other)
end